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 4.2 syntax support #3112

Merged
merged 10 commits into from Mar 1, 2021
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -112,9 +112,9 @@
"ts-jest": "^26.5.1",
"ts-node": "^9.0.0",
"tslint": "^6.1.3",
"typescript": ">=3.3.1 <4.2.0"
"typescript": ">=3.3.1 <4.3.0"
},
"resolutions": {
"typescript": "4.1.5"
"typescript": "4.2.2"
}
}
@@ -0,0 +1 @@
const x: abstract new () => void;
@@ -0,0 +1 @@
const x: new () => void;
2 changes: 2 additions & 0 deletions packages/types/src/ts-estree.ts
Expand Up @@ -374,6 +374,7 @@ export type Expression =
| SequenceExpression
| SpreadElement
| TSAsExpression
| TSTypeAssertion
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change is unrelated to upgrade of typescript

| TSUnaryExpression
| YieldExpression;
export type ForInitialiser = Expression | VariableDeclaration;
Expand Down Expand Up @@ -1340,6 +1341,7 @@ export interface TSConditionalType extends BaseNode {

export interface TSConstructorType extends FunctionSignatureBase {
type: AST_NODE_TYPES.TSConstructorType;
abstract: boolean;
}

export interface TSConstructSignatureDeclaration extends FunctionSignatureBase {
Expand Down
4 changes: 2 additions & 2 deletions packages/typescript-estree/package.json
Expand Up @@ -51,8 +51,8 @@
},
"devDependencies": {
"@babel/code-frame": "^7.12.13",
"@babel/parser": "^7.12.16",
"@babel/types": "^7.12.13",
"@babel/parser": "^7.13.4",
"@babel/types": "^7.13.0",
"@types/babel__code-frame": "*",
"@types/debug": "*",
"@types/glob": "*",
Expand Down
47 changes: 25 additions & 22 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -2407,36 +2407,40 @@ export class Converter {
}
return result;
}
case SyntaxKind.ConstructorType:
case SyntaxKind.ConstructorType: {
const result = this.createNode<TSESTree.TSConstructorType>(node, {
type: AST_NODE_TYPES.TSConstructorType,
params: this.convertParameters(node.parameters),
abstract: hasModifier(SyntaxKind.AbstractKeyword, node),
});
if (node.type) {
result.returnType = this.convertTypeAnnotation(node.type, node);
}
if (node.typeParameters) {
result.typeParameters = this.convertTSTypeParametersToTypeParametersDeclaration(
node.typeParameters,
);
}
return result;
}

case SyntaxKind.FunctionType:
case SyntaxKind.ConstructSignature:
case SyntaxKind.CallSignature: {
let type: AST_NODE_TYPES;
switch (node.kind) {
case SyntaxKind.ConstructSignature:
type = AST_NODE_TYPES.TSConstructSignatureDeclaration;
break;
case SyntaxKind.CallSignature:
type = AST_NODE_TYPES.TSCallSignatureDeclaration;
break;
case SyntaxKind.FunctionType:
type = AST_NODE_TYPES.TSFunctionType;
break;
case SyntaxKind.ConstructorType:
default:
type = AST_NODE_TYPES.TSConstructorType;
break;
}
let type =
node.kind === SyntaxKind.ConstructSignature
? AST_NODE_TYPES.TSConstructSignatureDeclaration
: node.kind === SyntaxKind.CallSignature
? AST_NODE_TYPES.TSCallSignatureDeclaration
: AST_NODE_TYPES.TSFunctionType;
const result = this.createNode<
| TSESTree.TSConstructSignatureDeclaration
| TSESTree.TSCallSignatureDeclaration
| TSESTree.TSFunctionType
| TSESTree.TSConstructorType
| TSESTree.TSCallSignatureDeclaration
| TSESTree.TSConstructSignatureDeclaration
>(node, {
type: type,
params: this.convertParameters(node.parameters),
});

if (node.type) {
result.returnType = this.convertTypeAnnotation(node.type, node);
}
Expand All @@ -2446,7 +2450,6 @@ export class Converter {
node.typeParameters,
);
}

return result;
}

Expand Down
Expand Up @@ -157,15 +157,11 @@ export interface EstreeToTsNodeTypes {
| ts.ConstructorDeclaration;
[AST_NODE_TYPES.TSArrayType]: ts.ArrayTypeNode;
[AST_NODE_TYPES.TSAsExpression]: ts.AsExpression;
[AST_NODE_TYPES.TSCallSignatureDeclaration]: ts.PropertySignature;
[AST_NODE_TYPES.TSCallSignatureDeclaration]: ts.CallSignatureDeclaration;
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
[AST_NODE_TYPES.TSClassImplements]: ts.ExpressionWithTypeArguments;
[AST_NODE_TYPES.TSConditionalType]: ts.ConditionalTypeNode;
[AST_NODE_TYPES.TSConstructorType]: ts.ConstructorTypeNode;
[AST_NODE_TYPES.TSConstructSignatureDeclaration]:
| ts.ConstructorTypeNode
| ts.FunctionTypeNode
| ts.ConstructSignatureDeclaration
| ts.CallSignatureDeclaration;
[AST_NODE_TYPES.TSConstructSignatureDeclaration]: ts.ConstructSignatureDeclaration;
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
[AST_NODE_TYPES.TSDeclareFunction]: ts.FunctionDeclaration;
[AST_NODE_TYPES.TSEnumDeclaration]: ts.EnumDeclaration;
[AST_NODE_TYPES.TSEnumMember]: ts.EnumMember;
Expand Down
20 changes: 8 additions & 12 deletions packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts
Expand Up @@ -299,23 +299,19 @@ tester.addFixturePatternConfig('jsx', {
* https://github.com/Microsoft/TypeScript/issues/7410
*/
'embedded-tags',
/**
* JSX fixtures which have known issues for typescript-estree
* @see https://github.com/Microsoft/TypeScript/issues/7411
*/
'namespaced-attribute-and-value-inserted',
/**
* JSX fixtures which have known issues for typescript-estree
* @see https://github.com/Microsoft/TypeScript/issues/7411
*/
'namespaced-name-and-attribute',
/**
* Current random error difference on jsx/invalid-no-tag-name.src.js
* ts-estree - SyntaxError
* Babel - RangeError
* @see https://github.com/babel/babel/issues/6680
*/
'invalid-no-tag-name',
/**
* [BABEL ERRORED, BUT TS-ESTREE DID NOT]
* SyntaxError: Unexpected token
* TODO: investigate if this code is valid as there is no typescript error
*/
'invalid-namespace-value-with-dots',
],
});
tester.addFixturePatternConfig('jsx-useJSXTextNode');
Expand Down Expand Up @@ -346,7 +342,7 @@ tester.addFixturePatternConfig('typescript/basics', {
/**
* Babel parses it as TSQualifiedName
* ts parses it as MemberExpression
* TODO: report it to babel
* @see https://github.com/babel/babel/issues/12884
*/
'interface-with-extends-member-expression',
/**
Expand Down Expand Up @@ -387,7 +383,7 @@ tester.addFixturePatternConfig('typescript/basics', {
'import-type-error',
/**
* [TS-ESTREE ERRORED, BUT BABEL DID NOT]
* TODO: report this to babel
* This is intentional; babel is not checking types
*/
'catch-clause-with-invalid-annotation',
],
Expand Down
Expand Up @@ -1435,10 +1435,10 @@ Object {

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/jsx/invalid-mismatched-namespace-tag.src 1`] = `
Object {
"column": 2,
"index": 2,
"column": 5,
"index": 5,
"lineNumber": 1,
"message": "Identifier expected.",
"message": "Expected corresponding JSX closing tag for 'a:b'.",
}
`;

Expand Down Expand Up @@ -1496,14 +1496,7 @@ Object {
}
`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/jsx/invalid-namespace-value-with-dots.src 1`] = `
Object {
"column": 2,
"index": 2,
"lineNumber": 1,
"message": "Identifier expected.",
}
`;
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/jsx/invalid-namespace-value-with-dots.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/jsx/invalid-no-common-parent.src 1`] = `
Object {
Expand Down Expand Up @@ -1587,23 +1580,9 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/jsx/multiple-blank-spaces.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/jsx/namespaced-attribute-and-value-inserted.src 1`] = `
Object {
"column": 4,
"index": 4,
"lineNumber": 1,
"message": "Identifier expected.",
}
`;
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/jsx/namespaced-attribute-and-value-inserted.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/jsx/namespaced-name-and-attribute.src 1`] = `
Object {
"column": 2,
"index": 2,
"lineNumber": 1,
"message": "Identifier expected.",
}
`;
exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/jsx/namespaced-name-and-attribute.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/jsx/newslines-and-entities.src 1`] = `
Object {
Expand Down Expand Up @@ -2622,6 +2601,10 @@ exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" e

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

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

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

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

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/types/constructor-in-generic.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
Expand Down
Expand Up @@ -2,9 +2,9 @@

exports[`jsx invalid-mismatched-namespace-tag.src 1`] = `
Object {
"column": 2,
"index": 2,
"column": 5,
"index": 5,
"lineNumber": 1,
"message": "Identifier expected.",
"message": "Expected corresponding JSX closing tag for 'a:b'.",
}
`;