Skip to content

Commit

Permalink
feat: Add excludeVariableMatch option to require-variable-type
Browse files Browse the repository at this point in the history
Enable ignoring variables that match a certain regex
  • Loading branch information
ahstro committed Nov 2, 2016
1 parent 3013025 commit a087fe6
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
21 changes: 21 additions & 0 deletions .README/rules/require-variable-type.md
Expand Up @@ -2,4 +2,25 @@

Requires that all variable declarators have type annotations.

#### Options

You can exclude variables that match a certain regex by using `excludeVariableMatch`.

This excludes all parameters that start with an underscore (`_`).
The default pattern is `a^`, which doesn't match anything, i.e., all parameters are checked.

```js
{
"rules": {
"flowtype/require-variable-type": [
2,
{
"excludeVariableMatch": "^_"
}
]
}
}
```


<!-- assertions requireVariableType -->
9 changes: 8 additions & 1 deletion src/rules/requireVariableType.js
Expand Up @@ -11,13 +11,20 @@ export default (context) => {
return () => {};
}

const excludeVariableMatch = new RegExp(_.get(context, 'options[0].excludeVariableMatch', 'a^'));

return {
VariableDeclaration: (variableDeclaration) => {
_.forEach(variableDeclaration.declarations, (variableDeclarator) => {
const identifierNode = _.get(variableDeclarator, 'id');
const typeAnnotation = _.get(identifierNode, 'typeAnnotation');
const identifierName = _.get(identifierNode, 'name');

if (excludeVariableMatch.test(identifierName)) {
return;
}

const typeAnnotation = _.get(identifierNode, 'typeAnnotation');

if (!typeAnnotation) {
context.report({
data: {
Expand Down
22 changes: 21 additions & 1 deletion tests/rules/assertions/requireVariableType.js
Expand Up @@ -15,15 +15,35 @@ export default {
message: 'Missing "bar" variable type annotation.'
}
]
},
{
code: 'var _foo = "bar", bar = 1',
errors: [
{
message: 'Missing "bar" variable type annotation.'
}
],
options: [
{
excludeVariableMatch: '^_'
}
]
}

],
valid: [
{
code: 'var foo : string = "bar"'
},
{
code: 'var foo : string = "bar", bar : number = 1'
},
{
code: 'var _foo = "bar", bar : number = 1',
options: [
{
excludeVariableMatch: '^_'
}
]
}
]
};

0 comments on commit a087fe6

Please sign in to comment.