Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(typescript-estree): align optional fields #1429

Merged
merged 8 commits into from May 10, 2020
731 changes: 731 additions & 0 deletions packages/parser/tests/lib/__snapshots__/typescript.ts.snap

Large diffs are not rendered by default.

@@ -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
Expand Up @@ -1080,11 +1080,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
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
Expand Up @@ -360,14 +360,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 @@ -397,7 +389,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
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
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