Skip to content

Commit

Permalink
feat(typescript-estree): align optional fields (#1429)
Browse files Browse the repository at this point in the history
  • Loading branch information
armano2 authored and bradzacher committed May 21, 2020
1 parent b6c3b7b commit 0e0010f
Show file tree
Hide file tree
Showing 8 changed files with 4,239 additions and 410 deletions.
731 changes: 731 additions & 0 deletions packages/parser/tests/lib/__snapshots__/typescript.ts.snap

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const computed1 = "buzz";
const computed2 = "bazz";
const obj = {
member: "member",
member2: "member2",
};
class X {
[computed1]?();
[computed2]?() {};
[1]?();
[2]?() {};
["literal1"]?();
["literal2"]?() {};
[obj.member]?() {};
[obj.member2]?();
[f()]?() {}
}
7 changes: 2 additions & 5 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1084,11 +1084,8 @@ export class Converter {
}
}

if (
result.key.type === AST_NODE_TYPES.Identifier &&
node.questionToken
) {
result.key.optional = true;
if (node.questionToken) {
result.optional = true;
}

if (node.kind === SyntaxKind.GetAccessor) {
Expand Down
2 changes: 2 additions & 0 deletions packages/typescript-estree/src/ts-estree/ts-estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ interface MethodDefinitionBase extends BaseNode {
computed: boolean;
static: boolean;
kind: 'method' | 'get' | 'set' | 'constructor';
optional?: boolean;
decorators?: Decorator[];
accessibility?: Accessibility;
typeParameters?: TSTypeParameterDeclaration;
Expand All @@ -682,6 +683,7 @@ interface PropertyBase extends BaseNode {
computed: boolean;
method: boolean;
shorthand: boolean;
optional?: boolean;
kind: 'init' | 'get' | 'set';
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -362,14 +362,6 @@ tester.addFixturePatternConfig('typescript/babylon-convergence', {
tester.addFixturePatternConfig('typescript/basics', {
fileType: 'ts',
ignore: [
/**
* Babel and ts-estree reports optional field on different nodes
* TODO: investigate
*/
'class-with-optional-methods',
'abstract-class-with-abstract-method',
'abstract-class-with-optional-method',
'declare-class-with-optional-method',
/**
* Babel parses it as TSQualifiedName
* ts parses it as MemberExpression
Expand Down Expand Up @@ -399,7 +391,11 @@ tester.addFixturePatternConfig('typescript/basics', {
* SyntaxError: 'abstract' modifier can only appear on a class, method, or property declaration.
*/
'abstract-class-with-abstract-constructor',
// babel hard fails on computed string enum members, but TS doesn't
/**
* [BABEL ERRORED, BUT TS-ESTREE DID NOT]
* babel hard fails on computed string enum members, but TS doesn't
* TODO: report this to babel
*/
'export-named-enum-computed-string',
/**
* Babel: TSTypePredicate includes `:` statement in range
Expand Down
18 changes: 18 additions & 0 deletions packages/typescript-estree/tests/ast-alignment/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any {
};
}
},
MethodDefinition(node) {
/**
* Babel: MethodDefinition + abstract: true
* ts-estree: TSAbstractClassProperty
*/
if (node.abstract) {
node.type = AST_NODE_TYPES.TSAbstractMethodDefinition;
delete node.abstract;
}
},
ClassProperty(node) {
/**
* Babel: ClassProperty + abstract: true
Expand Down Expand Up @@ -198,6 +208,14 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any {
node.range[0] = node.typeParameters.range[0];
node.loc.start = Object.assign({}, node.typeParameters.loc.start);
}

/**
* ts-estree: if there's no body, it becomes a TSEmptyBodyFunctionExpression
*/
if (!node.body) {
node.type = AST_NODE_TYPES.TSEmptyBodyFunctionExpression;
node.body = null;
}
},
/**
* Template strings seem to also be affected by the difference in opinion between different parsers in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1781,6 +1781,8 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e

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

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"`;

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"`;

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

0 comments on commit 0e0010f

Please sign in to comment.