Skip to content

Commit

Permalink
feat: Add require-variable-type rule
Browse files Browse the repository at this point in the history
  • Loading branch information
ahstro committed Nov 2, 2016
1 parent 8f355c2 commit 3013025
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .README/rules/require-variable-type.md
@@ -0,0 +1,5 @@
### `require-variable-type`

Requires that all variable declarators have type annotations.

<!-- assertions requireVariableType -->
3 changes: 3 additions & 0 deletions src/index.js
Expand Up @@ -4,6 +4,7 @@ import noWeakTypes from './rules/noWeakTypes';
import requireParameterType from './rules/requireParameterType';
import requireReturnType from './rules/requireReturnType';
import requireValidFileAnnotation from './rules/requireValidFileAnnotation';
import requireVariableType from './rules/requireVariableType';
import semi from './rules/semi';
import spaceAfterTypeColon from './rules/spaceAfterTypeColon';
import spaceBeforeGenericBracket from './rules/spaceBeforeGenericBracket';
Expand Down Expand Up @@ -34,6 +35,7 @@ export default {
'require-parameter-type': requireParameterType,
'require-return-type': requireReturnType,
'require-valid-file-annotation': requireValidFileAnnotation,
'require-variable-type': requireVariableType,
semi,
'sort-keys': sortKeys,
'space-after-type-colon': spaceAfterTypeColon,
Expand All @@ -54,6 +56,7 @@ export default {
'object-type-delimiter': 0,
'require-parameter-type': 0,
'require-return-type': 0,
'require-variable-type': 0,
semi: 0,
'sort-keys': 0,
'space-after-type-colon': 0,
Expand Down
33 changes: 33 additions & 0 deletions src/rules/requireVariableType.js
@@ -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
});
}
});
}
};
};
29 changes: 29 additions & 0 deletions tests/rules/assertions/requireVariableType.js
@@ -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'
}
]
};
1 change: 1 addition & 0 deletions tests/rules/index.js
Expand Up @@ -17,6 +17,7 @@ const reportingRules = [
'require-parameter-type',
'require-return-type',
'require-valid-file-annotation',
'require-variable-type',
'semi',
'sort-keys',
'space-after-type-colon',
Expand Down

0 comments on commit 3013025

Please sign in to comment.