Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consistent-function-scoping: Check anonymous functions #835

Merged
merged 6 commits into from
Sep 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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')]
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function should has name doBar, mysticatea/eslint-utils#14

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this PR blocked by that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we can improve message later.

},
{
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.