Skip to content

feat(typescript-estree): support 3.8 import/export type #1697

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 3 commits into from
Mar 9, 2020
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,068 changes: 1,037 additions & 31 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 @@
export type { A as B };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { B as C } from './a';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { foo } from 'bar';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type * from 'bar';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type { foo };
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import type foo from 'bar';
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// this should be treated as a normal import statement
import type from './foo';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import type foo, { bar } from 'bar';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import type { foo as bar } from 'baz';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import type { foo, bar } from 'baz';
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import type * as foo from './bar';
11 changes: 11 additions & 0 deletions packages/typescript-estree/src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,15 @@ export class Converter {
range: [exportKeyword.getStart(this.ast), result.range[1]],
});
} else {
const isType =
result.type === AST_NODE_TYPES.TSInterfaceDeclaration ||
result.type === AST_NODE_TYPES.TSTypeAliasDeclaration;
return this.createNode<TSESTree.ExportNamedDeclaration>(node, {
type: AST_NODE_TYPES.ExportNamedDeclaration,
declaration: result,
specifiers: [],
source: null,
exportKind: isType ? 'type' : 'value',
range: [exportKeyword.getStart(this.ast), result.range[1]],
});
}
Expand Down Expand Up @@ -1529,9 +1533,14 @@ export class Converter {
type: AST_NODE_TYPES.ImportDeclaration,
source: this.convertChild(node.moduleSpecifier),
specifiers: [],
importKind: 'value',
});

if (node.importClause) {
if (node.importClause.isTypeOnly) {
result.importKind = 'type';
}

if (node.importClause.name) {
result.specifiers.push(this.convertChild(node.importClause));
}
Expand Down Expand Up @@ -1587,12 +1596,14 @@ export class Converter {
specifiers: node.exportClause.elements.map(el =>
this.convertChild(el),
),
exportKind: node.isTypeOnly ? 'type' : 'value',
declaration: null,
});
} else {
return this.createNode<TSESTree.ExportAllDeclaration>(node, {
type: AST_NODE_TYPES.ExportAllDeclaration,
source: this.convertChild(node.moduleSpecifier),
exportKind: node.isTypeOnly ? 'type' : 'value',
});
}

Expand Down
3 changes: 3 additions & 0 deletions packages/typescript-estree/src/ts-estree/ts-estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -868,6 +868,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 +881,7 @@ export interface ExportNamedDeclaration extends BaseNode {
declaration: ExportDeclaration | null;
specifiers: ExportSpecifier[];
source: Expression | null;
exportKind: 'type' | 'value';
}

export interface ExportSpecifier extends BaseNode {
Expand Down Expand Up @@ -949,6 +951,7 @@ export interface ImportDeclaration extends BaseNode {
type: AST_NODE_TYPES.ImportDeclaration;
source: Literal;
specifiers: ImportClause[];
importKind: 'type' | 'value';
}

export interface ImportDefaultSpecifier extends BaseNode {
Expand Down
18 changes: 16 additions & 2 deletions packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,8 +379,8 @@ tester.addFixturePatternConfig('typescript/basics', {
* But not fixed in Babel 7.3
* TODO: Investigate differences
*/
'import-type',
'import-type-with-type-parameters-in-type-reference',
'type-import-type',
'type-import-type-with-type-parameters-in-type-reference',
/**
* Not yet supported in Babel https://github.com/babel/babel/issues/9228
* Directive field is not added to module and namespace
Expand Down Expand Up @@ -422,6 +422,20 @@ tester.addFixturePatternConfig('typescript/basics', {
*/
'abstract-class-with-declare-properties',
'class-with-declare-properties',
/**
* TS 3.8 import/export type
* babel coming soon https://github.com/babel/babel/pull/11171
*/
'export-type-as',
'export-type-from-as',
'export-type-from',
'export-type-star-from',
'export-type',
'import-type-default',
'import-type-error',
'import-type-named-as',
'import-type-named',
'import-type-star-as-ns',
],
ignoreSourceType: [
/**
Expand Down
42 changes: 42 additions & 0 deletions packages/typescript-estree/tests/ast-alignment/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,48 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any {
node.asserts = false;
}
},
/**
* TS 3.8 import/export type
* babel coming soon https://github.com/babel/babel/pull/11171
*/
ExportNamedDeclaration(node: any) {
/**
* TS 3.8: export type
*/
if (!node.exportKind) {
if (
node.declaration?.type === AST_NODE_TYPES.TSTypeAliasDeclaration ||
node.declaration?.type === AST_NODE_TYPES.TSInterfaceDeclaration
) {
node.exportKind = 'type';
} else {
node.exportKind = 'value';
}
}
},
ExportAllDeclaration(node: any) {
/**
* TS 3.8: export type
*/
if (!node.exportKind) {
if (
node.declaration?.type === AST_NODE_TYPES.TSTypeAliasDeclaration ||
node.declaration?.type === AST_NODE_TYPES.TSInterfaceDeclaration
) {
node.exportKind = 'type';
} else {
node.exportKind = 'value';
}
}
},
ImportDeclaration(node) {
/**
* TS 3.8: export type
*/
if (!node.importKind) {
node.importKind = 'value';
}
},
},
);
}
Expand Down
Loading