Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'ahstro-feature/require-variable-type'
- Loading branch information
Showing
6 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
### `require-variable-type` | ||
|
||
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": "^_" | ||
} | ||
] | ||
} | ||
} | ||
``` | ||
|
||
|
||
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 --> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import _ from 'lodash'; | ||
import { | ||
isFlowFile, | ||
quoteName | ||
} from './../utilities'; | ||
|
||
export default (context) => { | ||
const checkThisFile = !_.get(context, 'settings.flowtype.onlyFilesWithFlowAnnotation') || isFlowFile(context); | ||
|
||
if (!checkThisFile) { | ||
return () => {}; | ||
} | ||
|
||
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'); | ||
|
||
if (excludeVariableMatch.test(identifierName)) { | ||
return; | ||
} | ||
|
||
const typeAnnotation = _.get(identifierNode, 'typeAnnotation'); | ||
|
||
if (!typeAnnotation) { | ||
context.report({ | ||
data: { | ||
name: quoteName(identifierName) | ||
}, | ||
message: 'Missing {{name}}variable type annotation.', | ||
node: identifierNode | ||
}); | ||
} | ||
}); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
export default { | ||
invalid: [ | ||
{ | ||
code: 'var foo = "bar"', | ||
errors: [ | ||
{ | ||
message: 'Missing "foo" variable type annotation.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'var foo : string = "bar", bar = 1', | ||
errors: [ | ||
{ | ||
message: 'Missing "bar" variable type annotation.' | ||
} | ||
] | ||
}, | ||
{ | ||
code: 'var _foo = "bar", bar = 1', | ||
errors: [ | ||
{ | ||
message: 'Missing "bar" variable type annotation.' | ||
} | ||
], | ||
options: [ | ||
{ | ||
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: [ | ||
{ | ||
code: 'var foo : string = "bar"' | ||
}, | ||
{ | ||
code: 'var foo : string = "bar", bar : number = 1' | ||
}, | ||
{ | ||
code: 'var _foo = "bar", bar : number = 1', | ||
options: [ | ||
{ | ||
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 | ||
} | ||
} | ||
] | ||
} | ||
] | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters