Skip to content

Commit c4b87ca

Browse files
author
Aaron Harvey
committed
feat: add variable declarations to colon spacing checks
1 parent 3b1c895 commit c4b87ca

File tree

4 files changed

+92
-3
lines changed

4 files changed

+92
-3
lines changed

Diff for: src/rules/typeColonSpacing/evaluateVariables.js

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

Diff for: 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
};

Diff for: tests/rules/assertions/spaceAfterTypeColon.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -1055,6 +1055,33 @@ const TYPE_CAST_EXPRESSIONS = {
10551055
]
10561056
};
10571057

1058+
const VARIABLE_EXPRESSIONS = {
1059+
invalid: [
1060+
{
1061+
code: 'const x:number = 7;',
1062+
errors: [{message: 'There must be a space after const type annotation colon.'}],
1063+
options: ['always'],
1064+
output: 'const x: number = 7;'
1065+
},
1066+
{
1067+
code: 'let x:number = 42;',
1068+
errors: [{message: 'There must be a space after let type annotation colon.'}],
1069+
options: ['always'],
1070+
output: 'let x: number = 42;'
1071+
}
1072+
],
1073+
valid: [
1074+
{
1075+
code: 'const x: number = 7;',
1076+
options: ['always']
1077+
},
1078+
{
1079+
code: 'let x: number = 42;',
1080+
options: ['always']
1081+
}
1082+
]
1083+
};
1084+
10581085

10591086
const ALL = [
10601087
ARROW_FUNCTION_PARAMS,
@@ -1065,7 +1092,8 @@ const ALL = [
10651092
CLASS_PROPERTIES,
10661093
OBJECT_TYPE_PROPERTIES,
10671094
OBJECT_TYPE_INDEXERS,
1068-
TYPE_CAST_EXPRESSIONS
1095+
TYPE_CAST_EXPRESSIONS,
1096+
VARIABLE_EXPRESSIONS
10691097
];
10701098

10711099
export default {

Diff for: tests/rules/assertions/spaceBeforeTypeColon.js

+29-1
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,33 @@ const TYPE_CAST_EXPRESSIONS = {
870870
]
871871
};
872872

873+
const VARIABLE_EXPRESSIONS = {
874+
invalid: [
875+
{
876+
code: 'const x:number = 7;',
877+
errors: [{message: 'There must be a space before const type annotation colon.'}],
878+
options: ['always'],
879+
output: 'const x :number = 7;'
880+
},
881+
{
882+
code: 'let x:number = 42;',
883+
errors: [{message: 'There must be a space before let type annotation colon.'}],
884+
options: ['always'],
885+
output: 'let x :number = 42;'
886+
}
887+
],
888+
valid: [
889+
{
890+
code: 'const x :number = 7;',
891+
options: ['always']
892+
},
893+
{
894+
code: 'let x :number = 42;',
895+
options: ['always']
896+
}
897+
]
898+
};
899+
873900
const ALL = [
874901
ARROW_FUNCTION_PARAMS,
875902
ARROW_FUNCTION_RETURN,
@@ -879,7 +906,8 @@ const ALL = [
879906
CLASS_PROPERTIES,
880907
OBJECT_TYPE_PROPERTIES,
881908
OBJECT_TYPE_INDEXERS,
882-
TYPE_CAST_EXPRESSIONS
909+
TYPE_CAST_EXPRESSIONS,
910+
VARIABLE_EXPRESSIONS
883911
];
884912

885913
export default {

0 commit comments

Comments
 (0)