Skip to content

Commit

Permalink
consistent-function-scoping: Display function name in error me… (#589)
Browse files Browse the repository at this point in the history
* `consistent-function-scoping`: Display function name in error message
  • Loading branch information
fisker committed Mar 11, 2020
1 parent 4c48c02 commit e7e49ec
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
27 changes: 22 additions & 5 deletions rules/consistent-function-scoping.js
@@ -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 @@ -125,9 +125,26 @@ const create = context => {
},
':matches(ArrowFunctionExpression, FunctionDeclaration):exit': node => {
if (!hasJsx && !checkNode(node, scopeManager)) {
const functionType = node.type === 'ArrowFunctionExpression' ? 'arrow function' : 'function';
let functionName = '';
if (node.id) {
functionName = node.id.name;
} else if (
node.parent &&
node.parent.type === 'VariableDeclarator' &&
node.parent.id &&
node.parent.id.type === 'Identifier'
) {
functionName = node.parent.id.name;
}

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

Expand All @@ -147,8 +164,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
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 @@ -268,7 +271,7 @@ ruleTester.run('consistent-function-scoping', rule, {
return foo;
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
Expand All @@ -280,7 +283,7 @@ ruleTester.run('consistent-function-scoping', rule, {
return foo;
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
Expand All @@ -290,7 +293,7 @@ ruleTester.run('consistent-function-scoping', rule, {
}
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
Expand All @@ -300,13 +303,13 @@ ruleTester.run('consistent-function-scoping', rule, {
}
}
`,
errors: [arrowError]
errors: [createError({name: 'doBar', arrow: true})]
},
{
code: outdent`
const doFoo = () => bar => bar;
`,
errors: [arrowError]
errors: [createError({arrow: true})]
},
{
code: outdent`
Expand All @@ -317,7 +320,7 @@ ruleTester.run('consistent-function-scoping', rule, {
return foo;
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
Expand All @@ -328,15 +331,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 @@ -351,7 +354,7 @@ ruleTester.run('consistent-function-scoping', rule, {
return foo;
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
Expand All @@ -363,7 +366,7 @@ ruleTester.run('consistent-function-scoping', rule, {
}
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
Expand All @@ -373,7 +376,7 @@ ruleTester.run('consistent-function-scoping', rule, {
}
}
`,
errors: [functionError]
errors: [createError({name: 'doBar'})]
}
]
});
Expand Down

0 comments on commit e7e49ec

Please sign in to comment.