Skip to content

Commit

Permalink
fix: parens in TypeCastExpression
Browse files Browse the repository at this point in the history
  • Loading branch information
danharper committed Sep 19, 2016
1 parent 98839f0 commit 0e0081f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/rules/spaceAfterTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,26 +179,28 @@ const objectTypePropertyEvaluator = (context) => {
};

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

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

if (always && spaces > 1) {
context.report({
fix: spacingFixers.stripSpacesBefore(typeCastExpression.typeAnnotation.typeAnnotation, spaces - 1),
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(typeCastExpression.typeAnnotation.typeAnnotation),
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(typeCastExpression.typeAnnotation.typeAnnotation, spaces),
fix: spacingFixers.stripSpacesBefore(firstTokenOfType, spaces),
message: 'There must be no space after type cast colon.',
node: typeCastExpression
});
Expand Down
10 changes: 6 additions & 4 deletions src/rules/spaceBeforeTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,26 +134,28 @@ const objectTypePropertyEvaluator = (context) => {
};

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

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

if (always && spaces > 1) {
context.report({
fix: spacingFixers.stripSpacesAfter(typeCastExpression.expression, spaces - 1),
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(typeCastExpression.expression),
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(typeCastExpression.expression, spaces),
fix: spacingFixers.stripSpacesAfter(lastTokenOfIdentifier, spaces),
message: 'There must be no space before type cast colon.',
node: typeCastExpression
});
Expand Down
26 changes: 26 additions & 0 deletions tests/rules/assertions/spaceAfterTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,24 @@ const TYPE_CAST_EXPRESSIONS = {
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: [
Expand All @@ -713,6 +731,14 @@ const TYPE_CAST_EXPRESSIONS = {
{
code: 'const x = ({}: {})',
options: ['always']
},
{
code: '((x):(string))',
options: ['never']
},
{
code: '((x): (string))',
options: ['always']
}
]
};
Expand Down
26 changes: 26 additions & 0 deletions tests/rules/assertions/spaceBeforeTypeColon.js
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,24 @@ const TYPE_CAST_EXPRESSIONS = {
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: [
Expand All @@ -499,6 +517,14 @@ const TYPE_CAST_EXPRESSIONS = {
{
code: 'const x = ({} :{})',
options: ['always']
},
{
code: '((x): string)',
options: ['never']
},
{
code: '((x) : string)',
options: ['always']
}
]
};
Expand Down

0 comments on commit 0e0081f

Please sign in to comment.