Skip to content

Commit 46776aa

Browse files
weirdpatternJamesHenry
authored andcommitted
Fix: Handles type spacing on TSParenthesizedType expressions (fixes typescript-eslint#79) (typescript-eslint#80)
1 parent 2b8b2b9 commit 46776aa

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

packages/eslint-plugin-typescript/lib/rules/type-annotation-spacing.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ module.exports = {
4545
},
4646

4747
create(context) {
48+
const punctuators = [":", "=>"];
4849
const sourceCode = context.getSourceCode();
4950
const options = context.options[0] || {};
5051

@@ -78,12 +79,16 @@ module.exports = {
7879
const nextToken = typeAnnotation;
7980
const punctuatorToken = sourceCode.getTokenBefore(nextToken);
8081
const previousToken = sourceCode.getTokenBefore(punctuatorToken);
82+
const type = punctuatorToken.value;
83+
84+
if (punctuators.indexOf(type) === -1) {
85+
return;
86+
}
8187

8288
const previousDelta =
8389
punctuatorToken.range[0] - previousToken.range[1];
8490
const nextDelta = nextToken.range[0] - punctuatorToken.range[1];
8591

86-
const type = punctuatorToken.value;
8792
const before =
8893
type === ":" ? colonOptions.before : arrowOptions.before;
8994
const after =
@@ -159,9 +164,7 @@ module.exports = {
159164
}
160165
},
161166
TypeAnnotation(node) {
162-
if (node.parent.type !== "TSAsExpression") {
163-
checkTypeAnnotationSpacing(node.typeAnnotation);
164-
}
167+
checkTypeAnnotationSpacing(node.typeAnnotation);
165168
},
166169
FunctionDeclaration: checkFunctionReturnTypeSpacing,
167170
FunctionExpression: checkFunctionReturnTypeSpacing,

packages/eslint-plugin-typescript/tests/lib/rules/type-annotation-spacing.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ const ruleTester = new RuleTester();
1919

2020
ruleTester.run("type-annotation-spacing", rule, {
2121
valid: [
22+
{
23+
code: `
24+
interface resolve {
25+
resolver: (() => PromiseLike<T>) | PromiseLike<T>;
26+
}
27+
`,
28+
parser: "typescript-eslint-parser"
29+
},
2230
{
2331
code: "const foo = {} as Foo;",
2432
parser: "typescript-eslint-parser"
@@ -1175,6 +1183,18 @@ class Foo {
11751183
`,
11761184
options: [{ before: true }],
11771185
parser: "typescript-eslint-parser"
1186+
},
1187+
{
1188+
code: "let resolver: (() => PromiseLike<T>) | PromiseLike<T>;",
1189+
parser: "typescript-eslint-parser"
1190+
},
1191+
{
1192+
code: `
1193+
interface resolve {
1194+
resolver: (() => PromiseLike<T>) | PromiseLike<T>;
1195+
}
1196+
`,
1197+
parser: "typescript-eslint-parser"
11781198
}
11791199
],
11801200
invalid: [

0 commit comments

Comments
 (0)