Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add require-variable-type rule
- Loading branch information
Showing
5 changed files
with
71 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
### `require-variable-type` | ||
|
||
Requires that all variable declarators have type annotations. | ||
|
||
<!-- 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,33 @@ | ||
import _ from 'lodash'; | ||
import { | ||
isFlowFile, | ||
quoteName | ||
} from './../utilities'; | ||
|
||
export default (context) => { | ||
const checkThisFile = !_.get(context, 'settings.flowtype.onlyFilesWithFlowAnnotation') || isFlowFile(context); | ||
|
||
if (!checkThisFile) { | ||
return () => {}; | ||
} | ||
|
||
return { | ||
VariableDeclaration: (variableDeclaration) => { | ||
_.forEach(variableDeclaration.declarations, (variableDeclarator) => { | ||
const identifierNode = _.get(variableDeclarator, 'id'); | ||
const typeAnnotation = _.get(identifierNode, 'typeAnnotation'); | ||
const identifierName = _.get(identifierNode, 'name'); | ||
|
||
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,29 @@ | ||
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.' | ||
} | ||
] | ||
} | ||
|
||
], | ||
valid: [ | ||
{ | ||
code: 'var foo : string = "bar"' | ||
}, | ||
{ | ||
code: 'var foo : string = "bar", bar : number = 1' | ||
} | ||
] | ||
}; |
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