Skip to content

Commit 625d603

Browse files
authored
feat(typescript-estree): support 3.8 import/export type (typescript-eslint#1697)
1 parent 1543117 commit 625d603

26 files changed

+5193
-1212
lines changed

packages/parser/tests/lib/__snapshots__/typescript.ts.snap

Lines changed: 1037 additions & 31 deletions
Large diffs are not rendered by default.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type { A as B };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type { B as C } from './a';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type { foo } from 'bar';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type * from 'bar';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type { foo };
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import type foo from 'bar';
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// this should be treated as a normal import statement
2+
import type from './foo';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import type foo, { bar } from 'bar';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import type { foo as bar } from 'baz';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import type { foo, bar } from 'baz';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import type * as foo from './bar';

packages/typescript-estree/src/convert.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,11 +172,15 @@ export class Converter {
172172
range: [exportKeyword.getStart(this.ast), result.range[1]],
173173
});
174174
} else {
175+
const isType =
176+
result.type === AST_NODE_TYPES.TSInterfaceDeclaration ||
177+
result.type === AST_NODE_TYPES.TSTypeAliasDeclaration;
175178
return this.createNode<TSESTree.ExportNamedDeclaration>(node, {
176179
type: AST_NODE_TYPES.ExportNamedDeclaration,
177180
declaration: result,
178181
specifiers: [],
179182
source: null,
183+
exportKind: isType ? 'type' : 'value',
180184
range: [exportKeyword.getStart(this.ast), result.range[1]],
181185
});
182186
}
@@ -1529,9 +1533,14 @@ export class Converter {
15291533
type: AST_NODE_TYPES.ImportDeclaration,
15301534
source: this.convertChild(node.moduleSpecifier),
15311535
specifiers: [],
1536+
importKind: 'value',
15321537
});
15331538

15341539
if (node.importClause) {
1540+
if (node.importClause.isTypeOnly) {
1541+
result.importKind = 'type';
1542+
}
1543+
15351544
if (node.importClause.name) {
15361545
result.specifiers.push(this.convertChild(node.importClause));
15371546
}
@@ -1587,12 +1596,14 @@ export class Converter {
15871596
specifiers: node.exportClause.elements.map(el =>
15881597
this.convertChild(el),
15891598
),
1599+
exportKind: node.isTypeOnly ? 'type' : 'value',
15901600
declaration: null,
15911601
});
15921602
} else {
15931603
return this.createNode<TSESTree.ExportAllDeclaration>(node, {
15941604
type: AST_NODE_TYPES.ExportAllDeclaration,
15951605
source: this.convertChild(node.moduleSpecifier),
1606+
exportKind: node.isTypeOnly ? 'type' : 'value',
15961607
});
15971608
}
15981609

packages/typescript-estree/src/ts-estree/ts-estree.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -868,6 +868,7 @@ export interface EmptyStatement extends BaseNode {
868868
export interface ExportAllDeclaration extends BaseNode {
869869
type: AST_NODE_TYPES.ExportAllDeclaration;
870870
source: Expression | null;
871+
exportKind: 'type' | 'value';
871872
}
872873

873874
export interface ExportDefaultDeclaration extends BaseNode {
@@ -880,6 +881,7 @@ export interface ExportNamedDeclaration extends BaseNode {
880881
declaration: ExportDeclaration | null;
881882
specifiers: ExportSpecifier[];
882883
source: Expression | null;
884+
exportKind: 'type' | 'value';
883885
}
884886

885887
export interface ExportSpecifier extends BaseNode {
@@ -949,6 +951,7 @@ export interface ImportDeclaration extends BaseNode {
949951
type: AST_NODE_TYPES.ImportDeclaration;
950952
source: Literal;
951953
specifiers: ImportClause[];
954+
importKind: 'type' | 'value';
952955
}
953956

954957
export interface ImportDefaultSpecifier extends BaseNode {

packages/typescript-estree/tests/ast-alignment/fixtures-to-test.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,8 +379,8 @@ tester.addFixturePatternConfig('typescript/basics', {
379379
* But not fixed in Babel 7.3
380380
* TODO: Investigate differences
381381
*/
382-
'import-type',
383-
'import-type-with-type-parameters-in-type-reference',
382+
'type-import-type',
383+
'type-import-type-with-type-parameters-in-type-reference',
384384
/**
385385
* Not yet supported in Babel https://github.com/babel/babel/issues/9228
386386
* Directive field is not added to module and namespace
@@ -422,6 +422,20 @@ tester.addFixturePatternConfig('typescript/basics', {
422422
*/
423423
'abstract-class-with-declare-properties',
424424
'class-with-declare-properties',
425+
/**
426+
* TS 3.8 import/export type
427+
* babel coming soon https://github.com/babel/babel/pull/11171
428+
*/
429+
'export-type-as',
430+
'export-type-from-as',
431+
'export-type-from',
432+
'export-type-star-from',
433+
'export-type',
434+
'import-type-default',
435+
'import-type-error',
436+
'import-type-named-as',
437+
'import-type-named',
438+
'import-type-star-as-ns',
425439
],
426440
ignoreSourceType: [
427441
/**

packages/typescript-estree/tests/ast-alignment/utils.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,48 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any {
248248
node.asserts = false;
249249
}
250250
},
251+
/**
252+
* TS 3.8 import/export type
253+
* babel coming soon https://github.com/babel/babel/pull/11171
254+
*/
255+
ExportNamedDeclaration(node: any) {
256+
/**
257+
* TS 3.8: export type
258+
*/
259+
if (!node.exportKind) {
260+
if (
261+
node.declaration?.type === AST_NODE_TYPES.TSTypeAliasDeclaration ||
262+
node.declaration?.type === AST_NODE_TYPES.TSInterfaceDeclaration
263+
) {
264+
node.exportKind = 'type';
265+
} else {
266+
node.exportKind = 'value';
267+
}
268+
}
269+
},
270+
ExportAllDeclaration(node: any) {
271+
/**
272+
* TS 3.8: export type
273+
*/
274+
if (!node.exportKind) {
275+
if (
276+
node.declaration?.type === AST_NODE_TYPES.TSTypeAliasDeclaration ||
277+
node.declaration?.type === AST_NODE_TYPES.TSInterfaceDeclaration
278+
) {
279+
node.exportKind = 'type';
280+
} else {
281+
node.exportKind = 'value';
282+
}
283+
}
284+
},
285+
ImportDeclaration(node) {
286+
/**
287+
* TS 3.8: export type
288+
*/
289+
if (!node.importKind) {
290+
node.importKind = 'value';
291+
}
292+
},
251293
},
252294
);
253295
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy