Skip to content

Commit

Permalink
consistent-function-scoping: Check anonymous functions (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker authored Sep 30, 2020
1 parent 6c342c0 commit 29ecbf6
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
4 changes: 2 additions & 2 deletions rules/consistent-function-scoping.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ const create = context => {
const functions = [];

return {
'ArrowFunctionExpression, FunctionDeclaration': () => {
':function': () => {
functions.push(false);
},
JSXElement: () => {
Expand All @@ -168,7 +168,7 @@ const create = context => {
functions[functions.length - 1] = true;
}
},
':matches(ArrowFunctionExpression, FunctionDeclaration):exit': node => {
':function:exit': node => {
const currentFunctionHasJsx = functions.pop();
if (!currentFunctionHasJsx && !checkNode(node, scopeManager)) {
context.report({
Expand Down
82 changes: 82 additions & 0 deletions test/consistent-function-scoping.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ ruleTester.run('consistent-function-scoping', rule, {
return bar;
}
`,
outdent`
const doFoo = function() {};
`,
outdent`
const doFoo = foo => foo;
`,
Expand All @@ -54,6 +57,30 @@ ruleTester.run('consistent-function-scoping', rule, {
return foo;
}
`,
outdent`
const doFoo = function(foo) {
function doBar(bar) {
return foo + bar;
}
return foo;
};
`,
outdent`
const doFoo = function(foo) {
const doBar = function(bar) {
return foo + bar;
};
return foo;
};
`,
outdent`
function doFoo(foo) {
const doBar = function(bar) {
return foo + bar;
};
return foo;
}
`,
outdent`
function doFoo(foo) {
function doBar(bar) {
Expand Down Expand Up @@ -281,6 +308,13 @@ ruleTester.run('consistent-function-scoping', rule, {
function bar() {}
})();
`,
outdent`
function doFoo() {
const doBar = (function(bar) {
return bar;
})();
}
`,
// #391
outdent`
const enrichErrors = (packageName, cliArgs, f) => async (...args) => {
Expand Down Expand Up @@ -382,6 +416,47 @@ ruleTester.run('consistent-function-scoping', rule, {
`,
errors: [createError('function \'doBar\'')]
},
{
code: outdent`
const doFoo = function() {
function doBar(bar) {
return bar;
}
};
`,
errors: [createError('function \'doBar\'')]
},
{
code: outdent`
const doFoo = function() {
const doBar = function(bar) {
return bar;
};
};
`,
errors: [createError('function')]
},
{
code: outdent`
function doFoo() {
const doBar = function(bar) {
return bar;
};
}
`,
errors: [createError('function')]
},
{
code: outdent`
function doFoo() {
const doBar = function(bar) {
return bar;
};
doBar();
}
`,
errors: [createError('function')]
},
{
code: outdent`
const doFoo = () => {
Expand Down Expand Up @@ -758,5 +833,12 @@ visualizeTester.run('consistent-function-scoping', rule, [
function foo() {
const bar = async () => {}
}
`,
outdent`
function doFoo() {
const doBar = function(bar) {
return bar;
};
}
`
]);
14 changes: 14 additions & 0 deletions test/snapshots/consistent-function-scoping.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,17 @@ Generated by [AVA](https://avajs.dev).
| ^^ Move async arrow function 'bar' to the outer scope.␊
3 | }␊
`

## consistent-function-scoping - #8

> Snapshot 1
`␊
Error 1/1:␊
1 | function doFoo() {␊
> 2 | const doBar = function(bar) {␊
| ^^^^^^^^ Move function to the outer scope.␊
3 | return bar;␊
4 | };␊
5 | }␊
`
Binary file modified test/snapshots/consistent-function-scoping.js.snap
Binary file not shown.

0 comments on commit 29ecbf6

Please sign in to comment.