diff --git a/src/rules/typeColonSpacing/evaluateVariables.js b/src/rules/typeColonSpacing/evaluateVariables.js new file mode 100644 index 00000000..2ed99ec0 --- /dev/null +++ b/src/rules/typeColonSpacing/evaluateVariables.js @@ -0,0 +1,23 @@ +import _ from 'lodash'; +import {getParameterName, quoteName} from '../../utilities'; + +export default (context, report) => { + const sourceCode = context.getSourceCode(); + + return (node) => { + const declarations = _.get(node, 'declarations', []); + + _.forEach(declarations, (leaf) => { + const typeAnnotation = _.get(leaf, 'id.typeAnnotation'); + + if (typeAnnotation) { + report({ + colon: sourceCode.getFirstToken(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 6b5be901..0ee9ed92 100644 --- a/tests/rules/assertions/spaceAfterTypeColon.js +++ b/tests/rules/assertions/spaceAfterTypeColon.js @@ -1065,6 +1065,43 @@ 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;' + }, + { + code: 'var x:number = 42;', + errors: [{message: 'There must be a space after var type annotation colon.'}], + options: ['always'], + output: 'var x: number = 42;' + } + ], + valid: [ + { + code: 'const x: number = 7;', + options: ['always'] + }, + { + code: 'let x: number = 42;', + options: ['always'] + }, + { + code: 'var x: number = 42;', + options: ['always'] + } + ] +}; + const ALL = [ ARROW_FUNCTION_PARAMS, ARROW_FUNCTION_RETURN, @@ -1074,7 +1111,8 @@ const ALL = [ CLASS_PROPERTIES, OBJECT_TYPE_PROPERTIES, OBJECT_TYPE_INDEXERS, - TYPE_CAST_EXPRESSIONS + TYPE_CAST_EXPRESSIONS, + VARIABLE_EXPRESSIONS ]; const MISCONFIGURED = [ diff --git a/tests/rules/assertions/spaceBeforeTypeColon.js b/tests/rules/assertions/spaceBeforeTypeColon.js index b2deb798..82967c32 100644 --- a/tests/rules/assertions/spaceBeforeTypeColon.js +++ b/tests/rules/assertions/spaceBeforeTypeColon.js @@ -872,6 +872,43 @@ 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;' + }, + { + code: 'var x:number = 42;', + errors: [{message: 'There must be a space before var type annotation colon.'}], + options: ['always'], + output: 'var x :number = 42;' + } + ], + valid: [ + { + code: 'const x :number = 7;', + options: ['always'] + }, + { + code: 'let x :number = 42;', + options: ['always'] + }, + { + code: 'var x :number = 42;', + options: ['always'] + } + ] +}; + const ALL = [ ARROW_FUNCTION_PARAMS, ARROW_FUNCTION_RETURN, @@ -881,7 +918,8 @@ const ALL = [ CLASS_PROPERTIES, OBJECT_TYPE_PROPERTIES, OBJECT_TYPE_INDEXERS, - TYPE_CAST_EXPRESSIONS + TYPE_CAST_EXPRESSIONS, + VARIABLE_EXPRESSIONS ]; const MISCONFIGURED = [