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

WIP: feat: new ast for typescript 3.8 #1465

Closed
wants to merge 11 commits into from
6 changes: 3 additions & 3 deletions package.json
Expand Up @@ -72,11 +72,11 @@
"lint-staged": "^9.4.3",
"prettier": "^1.19.1",
"ts-jest": "^24.0.0",
"ts-node": "^8.5.0",
"ts-node": "^8.6.2",
"tslint": "^5.20.1",
"typescript": ">=3.2.1 <3.8.0"
"typescript": "3.8.0-dev.20200124"
},
"resolutions": {
"typescript": "^3.7.2"
"typescript": "3.8.0-dev.20200124"
}
}
2 changes: 1 addition & 1 deletion packages/eslint-plugin-tslint/package.json
Expand Up @@ -37,7 +37,7 @@
"peerDependencies": {
"eslint": "^5.0.0 || ^6.0.0",
"tslint": "^5.0.0",
"typescript": "*"
"typescript": "3.8.0-dev.20200124"
},
"devDependencies": {
"@types/lodash": "^4.14.149",
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin/package.json
Expand Up @@ -53,7 +53,7 @@
"chalk": "^3.0.0",
"marked": "^0.7.0",
"prettier": "*",
"typescript": "*"
"typescript": "3.8.0-dev.20200124"
},
"peerDependencies": {
"@typescript-eslint/parser": "^2.0.0",
Expand Down
2 changes: 1 addition & 1 deletion packages/experimental-utils/package.json
Expand Up @@ -44,7 +44,7 @@
"eslint": "*"
},
"devDependencies": {
"typescript": "*"
"typescript": "3.8.0-dev.20200124"
},
"funding": {
"type": "opencollective",
Expand Down
@@ -0,0 +1 @@
const foo = <this.#foo<string>/>
@@ -0,0 +1 @@
const foo = <this.#foo/>
@@ -0,0 +1,5 @@
class Foo {
private #bar: string
public #bar: string
static #bar: string
}
@@ -0,0 +1,11 @@
class Foo {
#bar: string

constructor(bar: string) {
this.#bar = name;
}

get bar () {
return this.#bar
}
}
@@ -0,0 +1,3 @@
class Foo {
get #foo() { }
}
@@ -0,0 +1,3 @@
class Foo {
#foo() { }
}
@@ -0,0 +1,3 @@
class Foo {
readonly #bar: string
}
@@ -0,0 +1 @@
export * as foo from 'bar'
@@ -0,0 +1 @@
export type { A as B };
@@ -0,0 +1,2 @@
// TODO: validate this
export type T;
@@ -0,0 +1 @@
export type { B as C } from './a';
@@ -0,0 +1 @@
export type { foo } from 'bar'
@@ -0,0 +1 @@
export type { foo };
@@ -0,0 +1 @@
import type foo from 'bar';
@@ -0,0 +1 @@
import type from './foo';
@@ -0,0 +1 @@
import type foo, { bar } from 'bar';
@@ -0,0 +1 @@
import type { foo as bar } from 'baz';
@@ -0,0 +1 @@
import type { foo, bar } from 'baz';
@@ -0,0 +1 @@
import type * as foo from './bar';
8 changes: 4 additions & 4 deletions packages/typescript-estree/package.json
Expand Up @@ -48,9 +48,9 @@
"tsutils": "^3.17.1"
},
"devDependencies": {
"@babel/code-frame": "7.5.5",
"@babel/parser": "7.7.7",
"@babel/types": "^7.7.4",
"@babel/code-frame": "7.8.3",
"@babel/parser": "7.8.3",
"@babel/types": "^7.8.3",
armano2 marked this conversation as resolved.
Show resolved Hide resolved
"@types/babel__code-frame": "^7.0.1",
"@types/debug": "^4.1.5",
"@types/glob": "^7.1.1",
Expand All @@ -60,7 +60,7 @@
"@types/tmp": "^0.1.0",
"@typescript-eslint/shared-fixtures": "2.17.0",
"tmp": "^0.1.0",
"typescript": "*"
"typescript": "3.8.0-dev.20200124"
},
"peerDependenciesMeta": {
"typescript": {
Expand Down
43 changes: 35 additions & 8 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -18,14 +18,14 @@ import {
isComputedProperty,
isESTreeClassMember,
isOptional,
unescapeStringLiteralText,
TSError,
unescapeStringLiteralText,
} from './node-utils';
import {
AST_NODE_TYPES,
TSESTree,
TSNode,
TSESTreeToTSNode,
TSNode,
} from './ts-estree';
import { ParserWeakMap, ParserWeakMapESTreeToTSNode } from './parser-options';

Expand Down Expand Up @@ -177,6 +177,7 @@ export class Converter {
declaration: result,
specifiers: [],
source: null,
exportKind: 'value',
range: [exportKeyword.getStart(this.ast), result.range[1]],
});
}
Expand Down Expand Up @@ -442,10 +443,10 @@ export class Converter {
* @returns the converted ESTree name object
*/
private convertJSXTagName(
node: ts.JsxTagNameExpression,
node: ts.JsxTagNameExpression | ts.PrivateIdentifier,
parent: ts.Node,
): TSESTree.JSXMemberExpression | TSESTree.JSXIdentifier {
let result: TSESTree.JSXMemberExpression | TSESTree.JSXIdentifier;
): TSESTree.JSXTagNameExpression {
let result: TSESTree.JSXTagNameExpression;
switch (node.kind) {
case SyntaxKind.PropertyAccessExpression:
result = this.createNode<TSESTree.JSXMemberExpression>(node, {
Expand All @@ -463,6 +464,12 @@ export class Converter {
name: 'this',
});
break;
case SyntaxKind.PrivateIdentifier:
result = this.createNode<TSESTree.JSXPrivateIdentifier>(node, {
type: AST_NODE_TYPES.JSXPrivateIdentifier,
name: node.text,
});
break;
case SyntaxKind.Identifier:
default:
result = this.createNode<TSESTree.JSXIdentifier>(node, {
Expand Down Expand Up @@ -588,6 +595,16 @@ export class Converter {
});
}

/**
* TODO: This structure should be valdiated before merge
* @see https://github.com/estree/estree/pull/180
*/
case SyntaxKind.PrivateIdentifier:
return this.createNode<TSESTree.PrivateName>(node, {
type: AST_NODE_TYPES.PrivateName,
name: node.text,
});

case SyntaxKind.WithStatement:
return this.createNode<TSESTree.WithStatement>(node, {
type: AST_NODE_TYPES.WithStatement,
Expand Down Expand Up @@ -1555,6 +1572,7 @@ export class Converter {
return this.createNode<TSESTree.ImportDefaultSpecifier>(node, {
type: AST_NODE_TYPES.ImportDefaultSpecifier,
local: this.convertChild(node.name),
importKind: node.isTypeOnly ? 'type' : 'value',
bradzacher marked this conversation as resolved.
Show resolved Hide resolved
range: [node.getStart(this.ast), node.name!.end],
});

Expand All @@ -1563,14 +1581,17 @@ export class Converter {
return this.createNode<TSESTree.ExportNamedDeclaration>(node, {
type: AST_NODE_TYPES.ExportNamedDeclaration,
source: this.convertChild(node.moduleSpecifier),
specifiers: node.exportClause.elements.map(el =>
this.convertChild(el),
),
specifiers:
node.exportClause.kind === SyntaxKind.NamedExports
? node.exportClause.elements.map(el => this.convertChild(el))
: [this.convertChild(node.exportClause)],
exportKind: node.isTypeOnly ? 'type' : 'value',
declaration: null,
});
} else {
return this.createNode<TSESTree.ExportAllDeclaration>(node, {
type: AST_NODE_TYPES.ExportAllDeclaration,
exportKind: node.isTypeOnly ? 'type' : 'value',
source: this.convertChild(node.moduleSpecifier),
});
}
Expand All @@ -1595,6 +1616,12 @@ export class Converter {
});
}

case SyntaxKind.NamespaceExport:
return this.createNode<TSESTree.ExportNamespaceSpecifier>(node, {
type: AST_NODE_TYPES.ExportNamespaceSpecifier,
exported: this.convertChild(node.name),
});

// Unary Operations

case SyntaxKind.PrefixUnaryExpression:
Expand Down
3 changes: 3 additions & 0 deletions packages/typescript-estree/src/ts-estree/ast-node-types.ts
Expand Up @@ -24,6 +24,7 @@ export enum AST_NODE_TYPES {
ExportAllDeclaration = 'ExportAllDeclaration',
ExportDefaultDeclaration = 'ExportDefaultDeclaration',
ExportNamedDeclaration = 'ExportNamedDeclaration',
ExportNamespaceSpecifier = 'ExportNamespaceSpecifier',
ExportSpecifier = 'ExportSpecifier',
ExpressionStatement = 'ExpressionStatement',
ForInStatement = 'ForInStatement',
Expand All @@ -49,6 +50,7 @@ export enum AST_NODE_TYPES {
JSXMemberExpression = 'JSXMemberExpression',
JSXOpeningElement = 'JSXOpeningElement',
JSXOpeningFragment = 'JSXOpeningFragment',
JSXPrivateIdentifier = 'JSXPrivateIdentifier',
JSXSpreadAttribute = 'JSXSpreadAttribute',
JSXSpreadChild = 'JSXSpreadChild',
JSXText = 'JSXText',
Expand All @@ -63,6 +65,7 @@ export enum AST_NODE_TYPES {
ObjectPattern = 'ObjectPattern',
OptionalCallExpression = 'OptionalCallExpression',
OptionalMemberExpression = 'OptionalMemberExpression',
PrivateName = 'PrivateName',
Program = 'Program',
Property = 'Property',
RestElement = 'RestElement',
Expand Down
Expand Up @@ -53,6 +53,7 @@ export interface EstreeToTsNodeTypes {
| ts.InterfaceDeclaration
| ts.EnumDeclaration
| ts.ModuleDeclaration;
[AST_NODE_TYPES.ExportNamespaceSpecifier]: ts.NamespaceExport;
[AST_NODE_TYPES.ExportSpecifier]: ts.ExportSpecifier;
[AST_NODE_TYPES.ExpressionStatement]: ts.ExpressionStatement;
[AST_NODE_TYPES.ForInStatement]: ts.ForInStatement;
Expand Down Expand Up @@ -87,6 +88,7 @@ export interface EstreeToTsNodeTypes {
| ts.JsxOpeningElement
| ts.JsxSelfClosingElement;
[AST_NODE_TYPES.JSXOpeningFragment]: ts.JsxOpeningFragment;
[AST_NODE_TYPES.JSXPrivateIdentifier]: ts.PrivateIdentifier;
[AST_NODE_TYPES.JSXSpreadAttribute]: ts.JsxSpreadAttribute;
[AST_NODE_TYPES.JSXSpreadChild]: ts.JsxExpression;
[AST_NODE_TYPES.JSXMemberExpression]: ts.PropertyAccessExpression;
Expand Down Expand Up @@ -118,6 +120,7 @@ export interface EstreeToTsNodeTypes {
[AST_NODE_TYPES.OptionalMemberExpression]:
| ts.PropertyAccessExpression
| ts.ElementAccessExpression;
[AST_NODE_TYPES.PrivateName]: ts.PrivateIdentifier;
[AST_NODE_TYPES.Program]: ts.SourceFile;
[AST_NODE_TYPES.Property]:
| ts.PropertyAssignment
Expand Down
30 changes: 29 additions & 1 deletion packages/typescript-estree/src/ts-estree/ts-estree.ts
Expand Up @@ -162,6 +162,7 @@ export type Node =
| ExportAllDeclaration
| ExportDefaultDeclaration
| ExportNamedDeclaration
| ExportNamespaceSpecifier
| ExportSpecifier
| ExpressionStatement
| ForInStatement
Expand All @@ -186,6 +187,7 @@ export type Node =
| JSXIdentifier
| JSXOpeningElement
| JSXOpeningFragment
| JSXPrivateIdentifier
| JSXSpreadAttribute
| JSXSpreadChild
| JSXMemberExpression
Expand All @@ -201,6 +203,7 @@ export type Node =
| ObjectPattern
| OptionalCallExpression
| OptionalMemberExpression
| PrivateName
| Program
| Property
| RestElement
Expand Down Expand Up @@ -380,7 +383,10 @@ export type JSXExpression =
| JSXEmptyExpression
| JSXSpreadChild
| JSXExpressionContainer;
export type JSXTagNameExpression = JSXIdentifier | JSXMemberExpression;
export type JSXTagNameExpression =
| JSXIdentifier
| JSXMemberExpression
| JSXPrivateIdentifier;
export type LeftHandSideExpression =
| CallExpression
| ClassExpression
Expand Down Expand Up @@ -868,6 +874,7 @@ export interface EmptyStatement extends BaseNode {
export interface ExportAllDeclaration extends BaseNode {
type: AST_NODE_TYPES.ExportAllDeclaration;
source: Expression | null;
exportKind: 'type' | 'value';
}

export interface ExportDefaultDeclaration extends BaseNode {
Expand All @@ -880,6 +887,12 @@ export interface ExportNamedDeclaration extends BaseNode {
declaration: ExportDeclaration | null;
specifiers: ExportSpecifier[];
source: Expression | null;
exportKind: 'type' | 'value';
}

export interface ExportNamespaceSpecifier extends BaseNode {
type: AST_NODE_TYPES.ExportNamespaceSpecifier;
exported: Identifier;
}

export interface ExportSpecifier extends BaseNode {
Expand Down Expand Up @@ -954,6 +967,7 @@ export interface ImportDeclaration extends BaseNode {
export interface ImportDefaultSpecifier extends BaseNode {
type: AST_NODE_TYPES.ImportDefaultSpecifier;
local: Identifier;
importKind: 'type' | 'value';
}

export interface ImportNamespaceSpecifier extends BaseNode {
Expand Down Expand Up @@ -1010,6 +1024,11 @@ export interface JSXIdentifier extends BaseNode {
name: string;
}

export interface JSXPrivateIdentifier extends BaseNode {
type: AST_NODE_TYPES.JSXPrivateIdentifier;
name: string;
}

export interface JSXMemberExpression extends BaseNode {
type: AST_NODE_TYPES.JSXMemberExpression;
object: JSXTagNameExpression;
Expand Down Expand Up @@ -1129,6 +1148,15 @@ export interface OptionalMemberExpressionNonComputedName
optional: boolean;
}

/**
* TODO: This structure should be valdiated before merge
* @see https://github.com/estree/estree/pull/180
*/
export interface PrivateName extends BaseNode {
type: AST_NODE_TYPES.PrivateName;
name: string;
}

export interface Program extends BaseNode {
type: AST_NODE_TYPES.Program;
body: Statement[];
Expand Down
2 changes: 2 additions & 0 deletions packages/typescript-estree/src/ts-estree/ts-nodes.ts
Expand Up @@ -84,6 +84,7 @@ export type TSNode = ts.Node &
| ts.TemplateTail
| ts.TemplateExpression
| ts.TemplateSpan
| ts.PrivateIdentifier
| ts.ParenthesizedExpression
| ts.ArrayLiteralExpression
| ts.SpreadElement
Expand Down Expand Up @@ -157,6 +158,7 @@ export type TSNode = ts.Node &
| ts.ImportSpecifier
| ts.ExportSpecifier
| ts.ExportAssignment
| ts.NamespaceExport
| ts.CommentRange
| ts.SourceFile
| ts.Bundle
Expand Down