Skip to content

Commit

Permalink
Merge 66e6653 into c8bf850
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 11, 2020
2 parents c8bf850 + 66e6653 commit e5e2d67
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 24 deletions.
17 changes: 12 additions & 5 deletions rules/consistent-function-scoping.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';
const getDocumentationUrl = require('./utils/get-documentation-url');

const MESSAGE_ID_ARROW = 'ArrowFunctionExpression';
const MESSAGE_ID_FUNCTION = 'FunctionDeclaration';
const MESSAGE_ID_NAMED = 'named';
const MESSAGE_ID_ANONYMOUS = 'anonymous';

const getReferences = scope => scope.references.concat(
...scope.childScopes.map(scope => getReferences(scope))
Expand Down Expand Up @@ -110,9 +110,16 @@ const create = context => {
},
':matches(ArrowFunctionExpression, FunctionDeclaration):exit': node => {
if (!hasJsx && !checkNode(node, scopeManager)) {
const functionType = node.type === 'ArrowFunctionExpression' ? 'arrow function' : 'function';
const functionName = node.id && node.id.name;

context.report({
node,
messageId: node.type
messageId: functionName ? MESSAGE_ID_NAMED : MESSAGE_ID_ANONYMOUS,
data: {
functionType,
functionName
}
});
}

Expand All @@ -132,8 +139,8 @@ module.exports = {
url: getDocumentationUrl(__filename)
},
messages: {
[MESSAGE_ID_ARROW]: 'Move arrow function to the outer scope.',
[MESSAGE_ID_FUNCTION]: 'Move function to the outer scope.'
[MESSAGE_ID_NAMED]: 'Move {{functionType}} `{{functionName}}` to the outer scope.',
[MESSAGE_ID_ANONYMOUS]: 'Move {{functionType}} to the outer scope.'
}
}
};
41 changes: 22 additions & 19 deletions test/consistent-function-scoping.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@ const typescriptRuleTester = avaRuleTester(test, {
parser: require.resolve('@typescript-eslint/parser')
});

const arrowError = {
ruleId: 'consistent-function-scoping',
messageId: 'ArrowFunctionExpression'
};
const ruleId = 'consistent-function-scoping';
const MESSAGE_ID_NAMED = 'named';
const MESSAGE_ID_ANONYMOUS = 'anonymous';

const functionError = {
ruleId: 'consistent-function-scoping',
messageId: 'FunctionDeclaration'
};
const createError = ({name, arrow}) => ({
ruleId,
messageId: name ? MESSAGE_ID_NAMED : MESSAGE_ID_ANONYMOUS,
data: {
functionType: arrow ? 'arrow function' : 'function',
functionName: name
}
});

ruleTester.run('consistent-function-scoping', rule, {
valid: [
Expand Down Expand Up @@ -256,7 +259,7 @@ ruleTester.run('consistent-function-scoping', rule, {
return foo;
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
Expand All @@ -268,7 +271,7 @@ ruleTester.run('consistent-function-scoping', rule, {
return foo;
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
Expand All @@ -278,7 +281,7 @@ ruleTester.run('consistent-function-scoping', rule, {
}
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
Expand All @@ -288,13 +291,13 @@ ruleTester.run('consistent-function-scoping', rule, {
}
}
`,
errors: [arrowError]
errors: [createError({arrow: true})]
},
{
code: outdent`
const doFoo = () => bar => bar;
`,
errors: [arrowError]
errors: [createError({arrow: true})]
},
{
code: outdent`
Expand All @@ -305,7 +308,7 @@ ruleTester.run('consistent-function-scoping', rule, {
return foo;
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
Expand All @@ -316,15 +319,15 @@ ruleTester.run('consistent-function-scoping', rule, {
return doBar;
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
function doFoo() {
function doBar() {}
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
Expand All @@ -339,7 +342,7 @@ ruleTester.run('consistent-function-scoping', rule, {
return foo;
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
Expand All @@ -351,7 +354,7 @@ ruleTester.run('consistent-function-scoping', rule, {
}
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
Expand All @@ -361,7 +364,7 @@ ruleTester.run('consistent-function-scoping', rule, {
}
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
}
]
});
Expand Down

0 comments on commit e5e2d67

Please sign in to comment.