Skip to content

Commit

Permalink
Merge branch 'aaron-harvey-fix/declare-variable-space-check'
Browse files Browse the repository at this point in the history
  • Loading branch information
gajus committed Jul 9, 2018
2 parents f37b1fc + 3eb85a8 commit 0e8a579
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 3 deletions.
23 changes: 23 additions & 0 deletions src/rules/typeColonSpacing/evaluateVariables.js
Original file line number Diff line number Diff line change
@@ -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'
});
}
});
};
};
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)
};
};
40 changes: 39 additions & 1 deletion tests/rules/assertions/spaceAfterTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -1074,7 +1111,8 @@ const ALL = [
CLASS_PROPERTIES,
OBJECT_TYPE_PROPERTIES,
OBJECT_TYPE_INDEXERS,
TYPE_CAST_EXPRESSIONS
TYPE_CAST_EXPRESSIONS,
VARIABLE_EXPRESSIONS
];

const MISCONFIGURED = [
Expand Down
40 changes: 39 additions & 1 deletion tests/rules/assertions/spaceBeforeTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -881,7 +918,8 @@ const ALL = [
CLASS_PROPERTIES,
OBJECT_TYPE_PROPERTIES,
OBJECT_TYPE_INDEXERS,
TYPE_CAST_EXPRESSIONS
TYPE_CAST_EXPRESSIONS,
VARIABLE_EXPRESSIONS
];

const MISCONFIGURED = [
Expand Down

0 comments on commit 0e8a579

Please sign in to comment.