diff --git a/docs/rules/consistent-function-scoping.md b/docs/rules/consistent-function-scoping.md index 3981a04317..68c7419699 100644 --- a/docs/rules/consistent-function-scoping.md +++ b/docs/rules/consistent-function-scoping.md @@ -43,6 +43,14 @@ export function doFoo(foo) { } ``` +## Options + +### checkArrowFunctions + +Type: `boolean`\ +Default: `true` + +Pass `"checkArrowFunctions": false` to disable linting of arrow functions. ## Limitations diff --git a/rules/consistent-function-scoping.js b/rules/consistent-function-scoping.js index cb4e6819e3..8dc34a9bfa 100644 --- a/rules/consistent-function-scoping.js +++ b/rules/consistent-function-scoping.js @@ -152,6 +152,7 @@ function checkNode(node, scopeManager) { } const create = context => { + const {checkArrowFunctions} = {checkArrowFunctions: true, ...context.options[0]}; const sourceCode = context.getSourceCode(); const {scopeManager} = sourceCode; @@ -170,20 +171,42 @@ const create = context => { }, ':function:exit': node => { const currentFunctionHasJsx = functions.pop(); - if (!currentFunctionHasJsx && !checkNode(node, scopeManager)) { - context.report({ - node, - loc: getFunctionHeadLocation(node, sourceCode), - messageId: MESSAGE_ID, - data: { - functionNameWithKind: getFunctionNameWithKind(node) - } - }); + if (currentFunctionHasJsx) { + return; } + + if (node.type === 'ArrowFunctionExpression' && !checkArrowFunctions) { + return; + } + + if (checkNode(node, scopeManager)) { + return; + } + + context.report({ + node, + loc: getFunctionHeadLocation(node, sourceCode), + messageId: MESSAGE_ID, + data: { + functionNameWithKind: getFunctionNameWithKind(node) + } + }); } }; }; +const schema = [ + { + type: 'object', + properties: { + checkArrowFunctions: { + type: 'boolean', + default: true + } + } + } +]; + module.exports = { create, meta: { @@ -191,6 +214,7 @@ module.exports = { docs: { url: getDocumentationUrl(__filename) }, + schema, messages } }; diff --git a/test/consistent-function-scoping.js b/test/consistent-function-scoping.js index 642ff640e0..5ed6163f55 100644 --- a/test/consistent-function-scoping.js +++ b/test/consistent-function-scoping.js @@ -372,7 +372,16 @@ test({ inner(); } - ` + `, + // Should ignore functions inside arrow functions + { + code: outdent` + function outer () { + const inner = () => {} + } + `, + options: [{checkArrowFunctions: false}] + } ], invalid: [ { @@ -729,6 +738,16 @@ test({ } `, errors: ['b', 'c', 'foo'].map(functionName => createError(`function '${functionName}'`)) + }, + // Should check functions inside arrow functions + { + code: outdent` + const outer = () => { + function inner() {} + } + `, + errors: [createError('function \'inner\'')], + options: [{checkArrowFunctions: false}] } ] });