diff --git a/src/rules/typeColonSpacing/evaluateVariables.js b/src/rules/typeColonSpacing/evaluateVariables.js new file mode 100644 index 00000000..7325818b --- /dev/null +++ b/src/rules/typeColonSpacing/evaluateVariables.js @@ -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' + }); + } + }); + }; +}; diff --git a/src/rules/typeColonSpacing/index.js b/src/rules/typeColonSpacing/index.js index 2bfa3c2d..53d0d556 100644 --- a/src/rules/typeColonSpacing/index.js +++ b/src/rules/typeColonSpacing/index.js @@ -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); @@ -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) }; }; diff --git a/tests/rules/assertions/spaceAfterTypeColon.js b/tests/rules/assertions/spaceAfterTypeColon.js index 4733a60c..0015d0e8 100644 --- a/tests/rules/assertions/spaceAfterTypeColon.js +++ b/tests/rules/assertions/spaceAfterTypeColon.js @@ -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, @@ -1065,7 +1092,8 @@ const ALL = [ CLASS_PROPERTIES, OBJECT_TYPE_PROPERTIES, OBJECT_TYPE_INDEXERS, - TYPE_CAST_EXPRESSIONS + TYPE_CAST_EXPRESSIONS, + VARIABLE_EXPRESSIONS ]; export default { diff --git a/tests/rules/assertions/spaceBeforeTypeColon.js b/tests/rules/assertions/spaceBeforeTypeColon.js index 80fe4460..3827cd71 100644 --- a/tests/rules/assertions/spaceBeforeTypeColon.js +++ b/tests/rules/assertions/spaceBeforeTypeColon.js @@ -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, @@ -879,7 +906,8 @@ const ALL = [ CLASS_PROPERTIES, OBJECT_TYPE_PROPERTIES, OBJECT_TYPE_INDEXERS, - TYPE_CAST_EXPRESSIONS + TYPE_CAST_EXPRESSIONS, + VARIABLE_EXPRESSIONS ]; export default {