Skip to content

Commit 3eb85a8

Browse files
committed
Merge branch 'fix/declare-variable-space-check' of https://github.com/aaron-harvey/eslint-plugin-flowtype into aaron-harvey-fix/declare-variable-space-check
2 parents f37b1fc + 11860dc commit 3eb85a8

File tree

4 files changed

+104
-3
lines changed

4 files changed

+104
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import _ from 'lodash';
2+
import {getParameterName, quoteName} from '../../utilities';
3+
4+
export default (context, report) => {
5+
const sourceCode = context.getSourceCode();
6+
7+
return (node) => {
8+
const declarations = _.get(node, 'declarations', []);
9+
10+
_.forEach(declarations, (leaf) => {
11+
const typeAnnotation = _.get(leaf, 'id.typeAnnotation');
12+
13+
if (typeAnnotation) {
14+
report({
15+
colon: sourceCode.getFirstToken(typeAnnotation),
16+
name: quoteName(getParameterName(leaf, context)),
17+
node: leaf,
18+
type: node.kind + ' type annotation'
19+
});
20+
}
21+
});
22+
};
23+
};

src/rules/typeColonSpacing/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import evaluateObjectTypeProperty from './evaluateObjectTypeProperty';
44
import evaluateTypeCastExpression from './evaluateTypeCastExpression';
55
import evaluateTypical from './evaluateTypical';
66
import evaluateFunctions from './evaluateFunctions';
7+
import evaluateVariables from './evaluateVariables';
78

89
export default (direction, context, options) => {
910
const report = reporter(direction, context, options);
@@ -13,6 +14,7 @@ export default (direction, context, options) => {
1314
ClassProperty: evaluateTypical(context, report, 'class property'),
1415
ObjectTypeIndexer: evaluateObjectTypeIndexer(context, report),
1516
ObjectTypeProperty: evaluateObjectTypeProperty(context, report),
16-
TypeCastExpression: evaluateTypeCastExpression(context, report)
17+
TypeCastExpression: evaluateTypeCastExpression(context, report),
18+
VariableDeclaration: evaluateVariables(context, report)
1719
};
1820
};

tests/rules/assertions/spaceAfterTypeColon.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -1065,6 +1065,43 @@ const TYPE_CAST_EXPRESSIONS = {
10651065
]
10661066
};
10671067

1068+
const VARIABLE_EXPRESSIONS = {
1069+
invalid: [
1070+
{
1071+
code: 'const x:number = 7;',
1072+
errors: [{message: 'There must be a space after const type annotation colon.'}],
1073+
options: ['always'],
1074+
output: 'const x: number = 7;'
1075+
},
1076+
{
1077+
code: 'let x:number = 42;',
1078+
errors: [{message: 'There must be a space after let type annotation colon.'}],
1079+
options: ['always'],
1080+
output: 'let x: number = 42;'
1081+
},
1082+
{
1083+
code: 'var x:number = 42;',
1084+
errors: [{message: 'There must be a space after var type annotation colon.'}],
1085+
options: ['always'],
1086+
output: 'var x: number = 42;'
1087+
}
1088+
],
1089+
valid: [
1090+
{
1091+
code: 'const x: number = 7;',
1092+
options: ['always']
1093+
},
1094+
{
1095+
code: 'let x: number = 42;',
1096+
options: ['always']
1097+
},
1098+
{
1099+
code: 'var x: number = 42;',
1100+
options: ['always']
1101+
}
1102+
]
1103+
};
1104+
10681105
const ALL = [
10691106
ARROW_FUNCTION_PARAMS,
10701107
ARROW_FUNCTION_RETURN,
@@ -1074,7 +1111,8 @@ const ALL = [
10741111
CLASS_PROPERTIES,
10751112
OBJECT_TYPE_PROPERTIES,
10761113
OBJECT_TYPE_INDEXERS,
1077-
TYPE_CAST_EXPRESSIONS
1114+
TYPE_CAST_EXPRESSIONS,
1115+
VARIABLE_EXPRESSIONS
10781116
];
10791117

10801118
const MISCONFIGURED = [

tests/rules/assertions/spaceBeforeTypeColon.js

+39-1
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,43 @@ const TYPE_CAST_EXPRESSIONS = {
872872
]
873873
};
874874

875+
const VARIABLE_EXPRESSIONS = {
876+
invalid: [
877+
{
878+
code: 'const x:number = 7;',
879+
errors: [{message: 'There must be a space before const type annotation colon.'}],
880+
options: ['always'],
881+
output: 'const x :number = 7;'
882+
},
883+
{
884+
code: 'let x:number = 42;',
885+
errors: [{message: 'There must be a space before let type annotation colon.'}],
886+
options: ['always'],
887+
output: 'let x :number = 42;'
888+
},
889+
{
890+
code: 'var x:number = 42;',
891+
errors: [{message: 'There must be a space before var type annotation colon.'}],
892+
options: ['always'],
893+
output: 'var x :number = 42;'
894+
}
895+
],
896+
valid: [
897+
{
898+
code: 'const x :number = 7;',
899+
options: ['always']
900+
},
901+
{
902+
code: 'let x :number = 42;',
903+
options: ['always']
904+
},
905+
{
906+
code: 'var x :number = 42;',
907+
options: ['always']
908+
}
909+
]
910+
};
911+
875912
const ALL = [
876913
ARROW_FUNCTION_PARAMS,
877914
ARROW_FUNCTION_RETURN,
@@ -881,7 +918,8 @@ const ALL = [
881918
CLASS_PROPERTIES,
882919
OBJECT_TYPE_PROPERTIES,
883920
OBJECT_TYPE_INDEXERS,
884-
TYPE_CAST_EXPRESSIONS
921+
TYPE_CAST_EXPRESSIONS,
922+
VARIABLE_EXPRESSIONS
885923
];
886924

887925
const MISCONFIGURED = [

0 commit comments

Comments
 (0)