Skip to content
This repository has been archived by the owner on Jan 19, 2019. It is now read-only.

Commit

Permalink
Fix: Handles type spacing on TSParenthesizedType expressions (fixes #79
Browse files Browse the repository at this point in the history
…) (#80)
  • Loading branch information
weirdpattern authored and JamesHenry committed Nov 23, 2017
1 parent 45cd23e commit 2b197fd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
11 changes: 7 additions & 4 deletions lib/rules/type-annotation-spacing.js
Expand Up @@ -45,6 +45,7 @@ module.exports = {
},

create(context) {
const punctuators = [":", "=>"];
const sourceCode = context.getSourceCode();
const options = context.options[0] || {};

Expand Down Expand Up @@ -78,12 +79,16 @@ module.exports = {
const nextToken = typeAnnotation;
const punctuatorToken = sourceCode.getTokenBefore(nextToken);
const previousToken = sourceCode.getTokenBefore(punctuatorToken);
const type = punctuatorToken.value;

if (punctuators.indexOf(type) === -1) {
return;
}

const previousDelta =
punctuatorToken.range[0] - previousToken.range[1];
const nextDelta = nextToken.range[0] - punctuatorToken.range[1];

const type = punctuatorToken.value;
const before =
type === ":" ? colonOptions.before : arrowOptions.before;
const after =
Expand Down Expand Up @@ -159,9 +164,7 @@ module.exports = {
}
},
TypeAnnotation(node) {
if (node.parent.type !== "TSAsExpression") {
checkTypeAnnotationSpacing(node.typeAnnotation);
}
checkTypeAnnotationSpacing(node.typeAnnotation);
},
FunctionDeclaration: checkFunctionReturnTypeSpacing,
FunctionExpression: checkFunctionReturnTypeSpacing,
Expand Down
20 changes: 20 additions & 0 deletions tests/lib/rules/type-annotation-spacing.js
Expand Up @@ -19,6 +19,14 @@ const ruleTester = new RuleTester();

ruleTester.run("type-annotation-spacing", rule, {
valid: [
{
code: `
interface resolve {
resolver: (() => PromiseLike<T>) | PromiseLike<T>;
}
`,
parser: "typescript-eslint-parser"
},
{
code: "const foo = {} as Foo;",
parser: "typescript-eslint-parser"
Expand Down Expand Up @@ -1175,6 +1183,18 @@ class Foo {
`,
options: [{ before: true }],
parser: "typescript-eslint-parser"
},
{
code: "let resolver: (() => PromiseLike<T>) | PromiseLike<T>;",
parser: "typescript-eslint-parser"
},
{
code: `
interface resolve {
resolver: (() => PromiseLike<T>) | PromiseLike<T>;
}
`,
parser: "typescript-eslint-parser"
}
],
invalid: [
Expand Down

0 comments on commit 2b197fd

Please sign in to comment.