Skip to content

Commit

Permalink
feat: Add excludeVariableTypes option to require-variable-type
Browse files Browse the repository at this point in the history
Enable ignoring all var, let, or const independently
  • Loading branch information
ahstro committed Nov 2, 2016
1 parent a087fe6 commit 35f601f
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
24 changes: 24 additions & 0 deletions .README/rules/require-variable-type.md
Expand Up @@ -23,4 +23,28 @@ The default pattern is `a^`, which doesn't match anything, i.e., all parameters
```


You can choose specific variable types (`var`, `let`, and `const`) to ignore using `excludeVariableTypes`.

This excludes `var` and `let` declarations from needing type annotations, but forces `const` declarations to have it.
By default, all declarations are checked.

```js
{
"rules": {
"flowtype/require-variable-type": [
2,
{
"excludeVariableTypes": {
"var": true,
"let": true,
"const": false,
}
}
]
}
}
```



<!-- assertions requireVariableType -->
7 changes: 7 additions & 0 deletions src/rules/requireVariableType.js
Expand Up @@ -12,9 +12,16 @@ export default (context) => {
}

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

return {
VariableDeclaration: (variableDeclaration) => {
const variableType = _.get(variableDeclaration, 'kind');

if (_.get(excludeVariableTypes, variableType)) {
return;
}

_.forEach(variableDeclaration.declarations, (variableDeclarator) => {
const identifierNode = _.get(variableDeclarator, 'id');
const identifierName = _.get(identifierNode, 'name');
Expand Down
37 changes: 37 additions & 0 deletions tests/rules/assertions/requireVariableType.js
Expand Up @@ -28,6 +28,22 @@ export default {
excludeVariableMatch: '^_'
}
]
},
{
code: 'var foo = "bar", bar = 1; const oob : string = "oob"; let hey = "yah"',
errors: [
{
message: 'Missing "hey" variable type annotation.'
}
],
options: [
{
excludeVariableTypes: {
let: false,
var: true
}
}
]
}
],
valid: [
Expand All @@ -44,6 +60,27 @@ export default {
excludeVariableMatch: '^_'
}
]
},
{
code: 'var foo = "bar", bar = 1',
options: [
{
excludeVariableTypes: {
var: true
}
}
]
},
{
code: 'var foo = "bar", bar = 1; const oob : string = "oob"; let hey = "yah"',
options: [
{
excludeVariableTypes: {
let: true,
var: true
}
}
]
}
]
};

0 comments on commit 35f601f

Please sign in to comment.