Skip to content

feat: [4.7] support instantiation expressions #4938

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/ast-spec/src/ast-node-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ export enum AST_NODE_TYPES {
TSExportKeyword = 'TSExportKeyword',
TSExternalModuleReference = 'TSExternalModuleReference',
TSFunctionType = 'TSFunctionType',
TSInstantiationExpression = 'TSInstantiationExpression',
TSImportEqualsDeclaration = 'TSImportEqualsDeclaration',
TSImportType = 'TSImportType',
TSIndexedAccessType = 'TSIndexedAccessType',
Expand Down
10 changes: 10 additions & 0 deletions packages/ast-spec/src/expression/TSInstantiationExpression/spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { AST_NODE_TYPES } from '../../ast-node-types';
import type { BaseNode } from '../../base/BaseNode';
import type { TSTypeParameterInstantiation } from '../../special/spec';
import type { Expression } from '../../unions/Expression';

export interface TSInstantiationExpression extends BaseNode {
type: AST_NODE_TYPES.TSInstantiationExpression;
expression: Expression;
typeParameters: TSTypeParameterInstantiation;
}
1 change: 1 addition & 0 deletions packages/ast-spec/src/expression/spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export * from './SequenceExpression/spec';
export * from './Super/spec';
export * from './TSAsExpression/spec';
export * from './TSEmptyBodyFunctionExpression/spec';
export * from './TSInstantiationExpression/spec';
export * from './TSNonNullExpression/spec';
export * from './TSTypeAssertion/spec';
export * from './TaggedTemplateExpression/spec';
Expand Down
2 changes: 2 additions & 0 deletions packages/ast-spec/src/unions/Expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import type { TaggedTemplateExpression } from '../expression/TaggedTemplateExpre
import type { TemplateLiteral } from '../expression/TemplateLiteral/spec';
import type { ThisExpression } from '../expression/ThisExpression/spec';
import type { TSAsExpression } from '../expression/TSAsExpression/spec';
import type { TSInstantiationExpression } from '../expression/TSInstantiationExpression/spec';
import type { TSNonNullExpression } from '../expression/TSNonNullExpression/spec';
import type { TSTypeAssertion } from '../expression/TSTypeAssertion/spec';
import type { UnaryExpression } from '../expression/UnaryExpression/spec';
Expand Down Expand Up @@ -69,6 +70,7 @@ export type Expression =
| TemplateLiteral
| ThisExpression
| TSAsExpression
| TSInstantiationExpression
| TSNonNullExpression
| TSTypeAssertion
| UnaryExpression
Expand Down
2 changes: 2 additions & 0 deletions packages/ast-spec/src/unions/Node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import type { TemplateLiteral } from '../expression/TemplateLiteral/spec';
import type { ThisExpression } from '../expression/ThisExpression/spec';
import type { TSAsExpression } from '../expression/TSAsExpression/spec';
import type { TSEmptyBodyFunctionExpression } from '../expression/TSEmptyBodyFunctionExpression/spec';
import type { TSInstantiationExpression } from '../expression/TSInstantiationExpression/spec';
import type { TSNonNullExpression } from '../expression/TSNonNullExpression/spec';
import type { TSTypeAssertion } from '../expression/TSTypeAssertion/spec';
import type { UnaryExpression } from '../expression/UnaryExpression/spec';
Expand Down Expand Up @@ -277,6 +278,7 @@ export type Node =
| TSIndexedAccessType
| TSIndexSignature
| TSInferType
| TSInstantiationExpression
| TSInterfaceBody
| TSInterfaceDeclaration
| TSInterfaceHeritage
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
a<b>;

(a<b>)<c>;
(a<b>)<c>();
(a<b>)<c>?.();
(a?.b<c>)<d>();
new (a<b>)<c>();
16 changes: 11 additions & 5 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2574,13 +2574,19 @@ export class Converter {
}

case SyntaxKind.ExpressionWithTypeArguments: {
const parentKind = parent.kind;
const type =
parentKind === SyntaxKind.InterfaceDeclaration
? AST_NODE_TYPES.TSInterfaceHeritage
: parentKind === SyntaxKind.HeritageClause
? AST_NODE_TYPES.TSClassImplements
: AST_NODE_TYPES.TSInstantiationExpression;
const result = this.createNode<
TSESTree.TSInterfaceHeritage | TSESTree.TSClassImplements
| TSESTree.TSInterfaceHeritage
| TSESTree.TSClassImplements
| TSESTree.TSInstantiationExpression
>(node, {
type:
parent && parent.kind === SyntaxKind.InterfaceDeclaration
? AST_NODE_TYPES.TSInterfaceHeritage
: AST_NODE_TYPES.TSClassImplements,
type,
expression: this.convertChild(node.expression),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ export interface EstreeToTsNodeTypes {
[AST_NODE_TYPES.TSInterfaceBody]: ts.InterfaceDeclaration;
[AST_NODE_TYPES.TSInterfaceHeritage]: ts.ExpressionWithTypeArguments;
[AST_NODE_TYPES.TSIntersectionType]: ts.IntersectionTypeNode;
[AST_NODE_TYPES.TSInstantiationExpression]: ts.ExpressionWithTypeArguments;
[AST_NODE_TYPES.TSLiteralType]: ts.LiteralTypeNode;
[AST_NODE_TYPES.TSMappedType]: ts.MappedTypeNode;
[AST_NODE_TYPES.TSMethodSignature]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,13 @@ tester.addFixturePatternConfig('typescript/decorators/property-decorators', {

tester.addFixturePatternConfig('typescript/expressions', {
fileType: 'ts',
ignore: [
/**
* [BABEL ERRORED, BUT TS-ESTREE DID NOT]
* Babel doesn't support TS 4.7 new features yet.
*/
'instantiation-expression',
],
});

tester.addFixturePatternConfig('typescript/errorRecovery', {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2640,6 +2640,8 @@ TSError {

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/expressions/call-expression-type-arguments.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

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

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/expressions/new-expression-type-arguments.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

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