Skip to content

Commit

Permalink
consistent-function-scoping: Allow ignoring arrow functions (#932)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Dec 12, 2020
1 parent 898fcb4 commit 17bf27c
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
8 changes: 8 additions & 0 deletions docs/rules/consistent-function-scoping.md
Expand Up @@ -43,6 +43,14 @@ export function doFoo(foo) {
}
```

## Options

### checkArrowFunctions

Type: `boolean`\
Default: `true`

Pass `"checkArrowFunctions": false` to disable linting of arrow functions.

## Limitations

Expand Down
42 changes: 33 additions & 9 deletions rules/consistent-function-scoping.js
Expand Up @@ -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;

Expand All @@ -170,27 +171,50 @@ 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: {
type: 'suggestion',
docs: {
url: getDocumentationUrl(__filename)
},
schema,
messages
}
};
21 changes: 20 additions & 1 deletion test/consistent-function-scoping.js
Expand Up @@ -372,7 +372,16 @@ test({
inner();
}
`
`,
// Should ignore functions inside arrow functions
{
code: outdent`
function outer () {
const inner = () => {}
}
`,
options: [{checkArrowFunctions: false}]
}
],
invalid: [
{
Expand Down Expand Up @@ -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}]
}
]
});
Expand Down

0 comments on commit 17bf27c

Please sign in to comment.