Skip to content

Commit

Permalink
Allow excluding arrow functions from require-param
Browse files Browse the repository at this point in the history
  • Loading branch information
danharper committed Aug 9, 2016
1 parent caebfe0 commit 25c5212
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,13 @@ The following patterns are considered problems:
(foo) => {}
// Message: Missing "foo" parameter type annotation.

function x(foo) {}
// Message: Missing "foo" parameter type annotation.

// Options: [{"excludeArrowFunctions":true}]
function x(foo) {}
// Message: Missing "foo" parameter type annotation.

(foo = 'FOO') => {}
// Message: Missing "foo" parameter type annotation.

Expand Down Expand Up @@ -263,6 +270,9 @@ The following patterns are not considered problems:

([foo]: Array) => {}

(foo) => {}

// Options: [{"excludeArrowFunctions":true}]
(foo) => {}
```

Expand Down
5 changes: 4 additions & 1 deletion src/rules/requireParameterType.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@ export default iterateFunctionNodes((context) => {
return () => {};
}

const skipArrows = _.get(context, 'options[0].excludeArrowFunctions');

return (functionNode) => {
_.forEach(functionNode.params, (identifierNode) => {
const parameterName = getParameterName(identifierNode, context);
const typeAnnotation = _.get(identifierNode, 'typeAnnotation') || _.get(identifierNode, 'left.typeAnnotation');
const shouldSkip = functionNode.type === 'ArrowFunctionExpression' && skipArrows;

if (!typeAnnotation) {
if (!typeAnnotation && !shouldSkip) {
context.report(identifierNode, 'Missing "' + parameterName + '" parameter type annotation.');
}
});
Expand Down
29 changes: 29 additions & 0 deletions tests/rules/assertions/requireParameterType.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,27 @@ export default {
}
]
},
{
code: 'function x(foo) {}',
errors: [
{
message: 'Missing "foo" parameter type annotation.'
}
]
},
{
code: 'function x(foo) {}',
errors: [
{
message: 'Missing "foo" parameter type annotation.'
}
],
options: [
{
excludeArrowFunctions: true
}
]
},
{
code: '(foo = \'FOO\') => {}',
errors: [
Expand Down Expand Up @@ -85,6 +106,14 @@ export default {
onlyFilesWithFlowAnnotation: true
}
}
},
{
code: '(foo) => {}',
options: [
{
excludeArrowFunctions: true
}
]
}
]
};

0 comments on commit 25c5212

Please sign in to comment.