Skip to content

Commit

Permalink
feat: add variable declarations to colon spacing checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Harvey committed Jan 18, 2017
1 parent 3b1c895 commit c4b87ca
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 3 deletions.
31 changes: 31 additions & 0 deletions src/rules/typeColonSpacing/evaluateVariables.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import _ from 'lodash';
import {getParameterName, quoteName} from '../../utilities';

export default (context, report) => {
const sourceCode = context.getSourceCode();

const getColon = (node, typeAnnotation) => {
if (node.type === 'FunctionTypeParam') {
return sourceCode.getFirstToken(node, node.optional ? 2 : 1);
} else {
return sourceCode.getFirstToken(typeAnnotation);
}
};

return (node) => {
const declarations = _.get(node, 'declarations', []);

_.forEach(declarations, (leaf) => {
const typeAnnotation = _.get(leaf, 'id.typeAnnotation');

if (typeAnnotation) {
report({
colon: getColon(leaf, typeAnnotation),
name: quoteName(getParameterName(leaf, context)),
node: leaf,
type: node.kind + ' type annotation'
});
}
});
};
};
4 changes: 3 additions & 1 deletion src/rules/typeColonSpacing/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import evaluateObjectTypeProperty from './evaluateObjectTypeProperty';
import evaluateTypeCastExpression from './evaluateTypeCastExpression';
import evaluateTypical from './evaluateTypical';
import evaluateFunctions from './evaluateFunctions';
import evaluateVariables from './evaluateVariables';

export default (direction, context, options) => {
const report = reporter(direction, context, options);
Expand All @@ -13,6 +14,7 @@ export default (direction, context, options) => {
ClassProperty: evaluateTypical(context, report, 'class property'),
ObjectTypeIndexer: evaluateObjectTypeIndexer(context, report),
ObjectTypeProperty: evaluateObjectTypeProperty(context, report),
TypeCastExpression: evaluateTypeCastExpression(context, report)
TypeCastExpression: evaluateTypeCastExpression(context, report),
VariableDeclaration: evaluateVariables(context, report)
};
};
30 changes: 29 additions & 1 deletion tests/rules/assertions/spaceAfterTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -1055,6 +1055,33 @@ const TYPE_CAST_EXPRESSIONS = {
]
};

const VARIABLE_EXPRESSIONS = {
invalid: [
{
code: 'const x:number = 7;',
errors: [{message: 'There must be a space after const type annotation colon.'}],
options: ['always'],
output: 'const x: number = 7;'
},
{
code: 'let x:number = 42;',
errors: [{message: 'There must be a space after let type annotation colon.'}],
options: ['always'],
output: 'let x: number = 42;'
}
],
valid: [
{
code: 'const x: number = 7;',
options: ['always']
},
{
code: 'let x: number = 42;',
options: ['always']
}
]
};


const ALL = [
ARROW_FUNCTION_PARAMS,
Expand All @@ -1065,7 +1092,8 @@ const ALL = [
CLASS_PROPERTIES,
OBJECT_TYPE_PROPERTIES,
OBJECT_TYPE_INDEXERS,
TYPE_CAST_EXPRESSIONS
TYPE_CAST_EXPRESSIONS,
VARIABLE_EXPRESSIONS
];

export default {
Expand Down
30 changes: 29 additions & 1 deletion tests/rules/assertions/spaceBeforeTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,33 @@ const TYPE_CAST_EXPRESSIONS = {
]
};

const VARIABLE_EXPRESSIONS = {
invalid: [
{
code: 'const x:number = 7;',
errors: [{message: 'There must be a space before const type annotation colon.'}],
options: ['always'],
output: 'const x :number = 7;'
},
{
code: 'let x:number = 42;',
errors: [{message: 'There must be a space before let type annotation colon.'}],
options: ['always'],
output: 'let x :number = 42;'
}
],
valid: [
{
code: 'const x :number = 7;',
options: ['always']
},
{
code: 'let x :number = 42;',
options: ['always']
}
]
};

const ALL = [
ARROW_FUNCTION_PARAMS,
ARROW_FUNCTION_RETURN,
Expand All @@ -879,7 +906,8 @@ const ALL = [
CLASS_PROPERTIES,
OBJECT_TYPE_PROPERTIES,
OBJECT_TYPE_INDEXERS,
TYPE_CAST_EXPRESSIONS
TYPE_CAST_EXPRESSIONS,
VARIABLE_EXPRESSIONS
];

export default {
Expand Down

0 comments on commit c4b87ca

Please sign in to comment.