Skip to content

Commit 0e0010f

Browse files
armano2bradzacher
authored andcommitted
feat(typescript-estree): align optional fields (#1429)
1 parent b6c3b7b commit 0e0010f

File tree

8 files changed

+4239
-410
lines changed

8 files changed

+4239
-410
lines changed

packages/parser/tests/lib/__snapshots__/typescript.ts.snap

Lines changed: 731 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const computed1 = "buzz";
2+
const computed2 = "bazz";
3+
const obj = {
4+
member: "member",
5+
member2: "member2",
6+
};
7+
class X {
8+
[computed1]?();
9+
[computed2]?() {};
10+
[1]?();
11+
[2]?() {};
12+
["literal1"]?();
13+
["literal2"]?() {};
14+
[obj.member]?() {};
15+
[obj.member2]?();
16+
[f()]?() {}
17+
}

packages/typescript-estree/src/convert.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,11 +1084,8 @@ export class Converter {
10841084
}
10851085
}
10861086

1087-
if (
1088-
result.key.type === AST_NODE_TYPES.Identifier &&
1089-
node.questionToken
1090-
) {
1091-
result.key.optional = true;
1087+
if (node.questionToken) {
1088+
result.optional = true;
10921089
}
10931090

10941091
if (node.kind === SyntaxKind.GetAccessor) {

packages/typescript-estree/src/ts-estree/ts-estree.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,7 @@ interface MethodDefinitionBase extends BaseNode {
656656
computed: boolean;
657657
static: boolean;
658658
kind: 'method' | 'get' | 'set' | 'constructor';
659+
optional?: boolean;
659660
decorators?: Decorator[];
660661
accessibility?: Accessibility;
661662
typeParameters?: TSTypeParameterDeclaration;
@@ -682,6 +683,7 @@ interface PropertyBase extends BaseNode {
682683
computed: boolean;
683684
method: boolean;
684685
shorthand: boolean;
686+
optional?: boolean;
685687
kind: 'init' | 'get' | 'set';
686688
}
687689

packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -362,14 +362,6 @@ tester.addFixturePatternConfig('typescript/babylon-convergence', {
362362
tester.addFixturePatternConfig('typescript/basics', {
363363
fileType: 'ts',
364364
ignore: [
365-
/**
366-
* Babel and ts-estree reports optional field on different nodes
367-
* TODO: investigate
368-
*/
369-
'class-with-optional-methods',
370-
'abstract-class-with-abstract-method',
371-
'abstract-class-with-optional-method',
372-
'declare-class-with-optional-method',
373365
/**
374366
* Babel parses it as TSQualifiedName
375367
* ts parses it as MemberExpression
@@ -399,7 +391,11 @@ tester.addFixturePatternConfig('typescript/basics', {
399391
* SyntaxError: 'abstract' modifier can only appear on a class, method, or property declaration.
400392
*/
401393
'abstract-class-with-abstract-constructor',
402-
// babel hard fails on computed string enum members, but TS doesn't
394+
/**
395+
* [BABEL ERRORED, BUT TS-ESTREE DID NOT]
396+
* babel hard fails on computed string enum members, but TS doesn't
397+
* TODO: report this to babel
398+
*/
403399
'export-named-enum-computed-string',
404400
/**
405401
* Babel: TSTypePredicate includes `:` statement in range

packages/typescript-estree/tests/ast-alignment/utils.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,16 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any {
151151
};
152152
}
153153
},
154+
MethodDefinition(node) {
155+
/**
156+
* Babel: MethodDefinition + abstract: true
157+
* ts-estree: TSAbstractClassProperty
158+
*/
159+
if (node.abstract) {
160+
node.type = AST_NODE_TYPES.TSAbstractMethodDefinition;
161+
delete node.abstract;
162+
}
163+
},
154164
ClassProperty(node) {
155165
/**
156166
* Babel: ClassProperty + abstract: true
@@ -198,6 +208,14 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any {
198208
node.range[0] = node.typeParameters.range[0];
199209
node.loc.start = Object.assign({}, node.typeParameters.loc.start);
200210
}
211+
212+
/**
213+
* ts-estree: if there's no body, it becomes a TSEmptyBodyFunctionExpression
214+
*/
215+
if (!node.body) {
216+
node.type = AST_NODE_TYPES.TSEmptyBodyFunctionExpression;
217+
node.body = null;
218+
}
201219
},
202220
/**
203221
* Template strings seem to also be affected by the difference in opinion between different parsers in

packages/typescript-estree/tests/lib/__snapshots__/semantic-diagnostics-enabled.ts.snap

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1781,6 +1781,8 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e
17811781

17821782
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/class-with-mixin-reference.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
17831783

1784+
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/class-with-optional-computed-method.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
1785+
17841786
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/class-with-optional-computed-property.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
17851787

17861788
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/class-with-optional-methods.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

0 commit comments

Comments
 (0)