Skip to content

Commit

Permalink
Merge branch 'typeCastColonSpacing'
Browse files Browse the repository at this point in the history
  • Loading branch information
danharper committed Sep 20, 2016
2 parents aa76272 + 0e0081f commit 891ae9d
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 4 deletions.
33 changes: 32 additions & 1 deletion src/rules/spaceAfterTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,10 +178,41 @@ const objectTypePropertyEvaluator = (context) => {
};
};

const typeCastEvaluator = (context) => {
const sourceCode = context.getSourceCode();
const {always} = parseOptions(context);

return (typeCastExpression) => {
const [, firstTokenOfType] = sourceCode.getFirstTokens(typeCastExpression.typeAnnotation, 2);
const spaces = firstTokenOfType.start - typeCastExpression.typeAnnotation.start - 1;

if (always && spaces > 1) {
context.report({
fix: spacingFixers.stripSpacesBefore(firstTokenOfType, spaces - 1),
message: 'There must be 1 space after type cast colon.',
node: typeCastExpression
});
} else if (always && spaces === 0) {
context.report({
fix: spacingFixers.addSpaceBefore(firstTokenOfType),
message: 'There must be a space after type cast colon.',
node: typeCastExpression
});
} else if (!always && spaces > 0) {
context.report({
fix: spacingFixers.stripSpacesBefore(firstTokenOfType, spaces),
message: 'There must be no space after type cast colon.',
node: typeCastExpression
});
}
};
};

export default (context) => {
return {
...functionEvaluators(context),
ClassProperty: propertyEvaluator(context, 'class property'),
ObjectTypeProperty: objectTypePropertyEvaluator(context)
ObjectTypeProperty: objectTypePropertyEvaluator(context),
TypeCastExpression: typeCastEvaluator(context)
};
};
33 changes: 32 additions & 1 deletion src/rules/spaceBeforeTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,41 @@ const objectTypePropertyEvaluator = (context) => {
};
};

const typeCastEvaluator = (context) => {
const sourceCode = context.getSourceCode();
const {always} = parseOptions(context);

return (typeCastExpression) => {
const lastTokenOfIdentifier = sourceCode.getTokenBefore(typeCastExpression.typeAnnotation);
const spaces = typeCastExpression.typeAnnotation.start - lastTokenOfIdentifier.end;

if (always && spaces > 1) {
context.report({
fix: spacingFixers.stripSpacesAfter(lastTokenOfIdentifier, spaces - 1),
message: 'There must be 1 space before type cast colon.',
node: typeCastExpression
});
} else if (always && spaces === 0) {
context.report({
fix: spacingFixers.addSpaceAfter(lastTokenOfIdentifier),
message: 'There must be a space before type cast colon.',
node: typeCastExpression
});
} else if (!always && spaces > 0) {
context.report({
fix: spacingFixers.stripSpacesAfter(lastTokenOfIdentifier, spaces),
message: 'There must be no space before type cast colon.',
node: typeCastExpression
});
}
};
};

export default (context) => {
return {
...functionEvaluators(context),
ClassProperty: propertyEvaluator(context, 'class property'),
ObjectTypeProperty: objectTypePropertyEvaluator(context)
ObjectTypeProperty: objectTypePropertyEvaluator(context),
TypeCastExpression: typeCastEvaluator(context)
};
};
12 changes: 12 additions & 0 deletions src/utilities/spacingFixers.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
export const stripSpacesBefore = (node, spaces) => {
return (fixer) => {
return fixer.removeRange([node.start - spaces, node.start]);
};
};

export const stripSpacesAfter = (node, spaces) => {
return (fixer) => {
return fixer.removeRange([node.end, node.end + spaces]);
};
};

export const addSpaceBefore = (node) => {
return (fixer) => {
return fixer.insertTextBefore(node, ' ');
};
};

export const addSpaceAfter = (node) => {
return (fixer) => {
return fixer.insertTextAfter(node, ' ');
Expand Down
63 changes: 62 additions & 1 deletion tests/rules/assertions/spaceAfterTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -683,14 +683,75 @@ const OBJECT_TYPE_PROPERTIES = {
]
};


const TYPE_CAST_EXPRESSIONS = {
invalid: [
{
code: 'const x = ({}: {})',
errors: [{message: 'There must be no space after type cast colon.'}],
options: ['never'],
output: 'const x = ({}:{})'
},
{
code: 'const x = ({}:{})',
errors: [{message: 'There must be a space after type cast colon.'}],
options: ['always'],
output: 'const x = ({}: {})'
},
{
code: 'const x = ({}: {})',
errors: [{message: 'There must be 1 space after type cast colon.'}],
options: ['always'],
output: 'const x = ({}: {})'
},
{
code: '((x): (string))',
errors: [{message: 'There must be no space after type cast colon.'}],
options: ['never'],
output: '((x):(string))'
},
{
code: '((x):(string))',
errors: [{message: 'There must be a space after type cast colon.'}],
options: ['always'],
output: '((x): (string))'
},
{
code: '((x): (string))',
errors: [{message: 'There must be 1 space after type cast colon.'}],
options: ['always'],
output: '((x): (string))'
}
],
valid: [
{
code: 'const x = ({}:{})',
options: ['never']
},
{
code: 'const x = ({}: {})',
options: ['always']
},
{
code: '((x):(string))',
options: ['never']
},
{
code: '((x): (string))',
options: ['always']
}
]
};

const ALL = [
ARROW_FUNCTION_PARAMS,
ARROW_FUNCTION_RETURN,
FUNCTION_PARAMS,
FUNCTION_RETURN,
FUNCTION_TYPE_PARAMS,
CLASS_PROPERTIES,
OBJECT_TYPE_PROPERTIES
OBJECT_TYPE_PROPERTIES,
TYPE_CAST_EXPRESSIONS
];

export default {
Expand Down
62 changes: 61 additions & 1 deletion tests/rules/assertions/spaceBeforeTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -470,14 +470,74 @@ const OBJECT_TYPE_PROPERTIES = {
]
};

const TYPE_CAST_EXPRESSIONS = {
invalid: [
{
code: 'const x = ({} :{})',
errors: [{message: 'There must be no space before type cast colon.'}],
options: ['never'],
output: 'const x = ({}:{})'
},
{
code: 'const x = ({}:{})',
errors: [{message: 'There must be a space before type cast colon.'}],
options: ['always'],
output: 'const x = ({} :{})'
},
{
code: 'const x = ({} :{})',
errors: [{message: 'There must be 1 space before type cast colon.'}],
options: ['always'],
output: 'const x = ({} :{})'
},
{
code: '((x) : string)',
errors: [{message: 'There must be no space before type cast colon.'}],
options: ['never'],
output: '((x): string)'
},
{
code: '((x): string)',
errors: [{message: 'There must be a space before type cast colon.'}],
options: ['always'],
output: '((x) : string)'
},
{
code: '((x) : string)',
errors: [{message: 'There must be 1 space before type cast colon.'}],
options: ['always'],
output: '((x) : string)'
}
],
valid: [
{
code: 'const x = ({}:{})',
options: ['never']
},
{
code: 'const x = ({} :{})',
options: ['always']
},
{
code: '((x): string)',
options: ['never']
},
{
code: '((x) : string)',
options: ['always']
}
]
};

const ALL = [
ARROW_FUNCTION_PARAMS,
ARROW_FUNCTION_RETURN,
FUNCTION_PARAMS,
FUNCTION_RETURN,
FUNCTION_TYPE_PARAMS,
CLASS_PROPERTIES,
OBJECT_TYPE_PROPERTIES
OBJECT_TYPE_PROPERTIES,
TYPE_CAST_EXPRESSIONS
];

export default {
Expand Down

0 comments on commit 891ae9d

Please sign in to comment.