Skip to content

Commit

Permalink
feat(typescript-estree): support 3.8 export * as ns (#1698)
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Mar 11, 2020
1 parent b5b3be0 commit 133f622
Show file tree
Hide file tree
Showing 9 changed files with 281 additions and 7 deletions.
50 changes: 50 additions & 0 deletions packages/parser/tests/lib/__snapshots__/typescript.ts.snap
Expand Up @@ -16384,6 +16384,56 @@ Object {
}
`;

exports[`typescript fixtures/basics/export-star-as-ns-from.src 1`] = `
Object {
"$id": 1,
"block": Object {
"range": Array [
0,
28,
],
"type": "Program",
},
"childScopes": Array [
Object {
"$id": 0,
"block": Object {
"range": Array [
0,
28,
],
"type": "Program",
},
"childScopes": Array [],
"functionExpressionScope": false,
"isStrict": true,
"references": Array [],
"throughReferences": Array [],
"type": "module",
"upperScope": Object {
"$ref": 1,
},
"variableMap": Object {},
"variableScope": Object {
"$ref": 0,
},
"variables": Array [],
},
],
"functionExpressionScope": false,
"isStrict": false,
"references": Array [],
"throughReferences": Array [],
"type": "global",
"upperScope": null,
"variableMap": Object {},
"variableScope": Object {
"$ref": 1,
},
"variables": Array [],
}
`;

exports[`typescript fixtures/basics/export-type.src 1`] = `
Object {
"$id": 2,
Expand Down
@@ -0,0 +1 @@
export * as foo from 'bar';
9 changes: 5 additions & 4 deletions packages/typescript-estree/src/convert.ts
Expand Up @@ -1586,10 +1586,7 @@ export class Converter {
});

case SyntaxKind.ExportDeclaration:
if (node.exportClause) {
if (node.exportClause.kind !== SyntaxKind.NamedExports) {
throw new Error('`export * as ns` is not yet supported.');
}
if (node.exportClause?.kind === SyntaxKind.NamedExports) {
return this.createNode<TSESTree.ExportNamedDeclaration>(node, {
type: AST_NODE_TYPES.ExportNamedDeclaration,
source: this.convertChild(node.moduleSpecifier),
Expand All @@ -1604,6 +1601,10 @@ export class Converter {
type: AST_NODE_TYPES.ExportAllDeclaration,
source: this.convertChild(node.moduleSpecifier),
exportKind: node.isTypeOnly ? 'type' : 'value',
exported:
node.exportClause?.kind === SyntaxKind.NamespaceExport
? this.convertChild(node.exportClause.name)
: null,
});
}

Expand Down
1 change: 1 addition & 0 deletions packages/typescript-estree/src/ts-estree/ts-estree.ts
Expand Up @@ -869,6 +869,7 @@ export interface ExportAllDeclaration extends BaseNode {
type: AST_NODE_TYPES.ExportAllDeclaration;
source: Expression | null;
exportKind: 'type' | 'value';
exported: Identifier | null;
}

export interface ExportDefaultDeclaration extends BaseNode {
Expand Down
Expand Up @@ -436,6 +436,11 @@ tester.addFixturePatternConfig('typescript/basics', {
'import-type-named-as',
'import-type-named',
'import-type-star-as-ns',
/**
* TS 3.8 export * as namespace
* babel uses a representation that does not match the ESTree spec: https://github.com/estree/estree/pull/205
*/
'export-star-as-ns-from',
],
ignoreSourceType: [
/**
Expand Down
12 changes: 9 additions & 3 deletions packages/typescript-estree/tests/ast-alignment/utils.ts
Expand Up @@ -249,8 +249,7 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any {
}
},
/**
* TS 3.8 import/export type
* babel coming soon https://github.com/babel/babel/pull/11171
* TS 3.8 features
*/
ExportNamedDeclaration(node: any) {
/**
Expand Down Expand Up @@ -281,10 +280,17 @@ export function preprocessBabylonAST(ast: BabelTypes.File): any {
node.exportKind = 'value';
}
}
/**
* TS 3.8 export * as namespace
* babel uses a representation that does not match the ESTree spec: https://github.com/estree/estree/pull/205
*/
if (!node.exported) {
node.exported = null;
}
},
ImportDeclaration(node) {
/**
* TS 3.8: export type
* TS 3.8: import type
*/
if (!node.importKind) {
node.importKind = 'value';
Expand Down
Expand Up @@ -111896,6 +111896,7 @@ Object {
"body": Array [
Object {
"exportKind": "value",
"exported": null,
"loc": Object {
"end": Object {
"column": 20,
Expand Down
Expand Up @@ -1878,6 +1878,8 @@ Object {
}
`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/export-star-as-ns-from.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

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

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/typescript/basics/export-type-as.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;
Expand Down
207 changes: 207 additions & 0 deletions packages/typescript-estree/tests/lib/__snapshots__/typescript.ts.snap
Expand Up @@ -46829,6 +46829,212 @@ Object {
}
`;

exports[`typescript fixtures/basics/export-star-as-ns-from.src 1`] = `
Object {
"body": Array [
Object {
"exportKind": "value",
"exported": Object {
"loc": Object {
"end": Object {
"column": 15,
"line": 1,
},
"start": Object {
"column": 12,
"line": 1,
},
},
"name": "foo",
"range": Array [
12,
15,
],
"type": "Identifier",
},
"loc": Object {
"end": Object {
"column": 27,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
27,
],
"source": Object {
"loc": Object {
"end": Object {
"column": 26,
"line": 1,
},
"start": Object {
"column": 21,
"line": 1,
},
},
"range": Array [
21,
26,
],
"raw": "'bar'",
"type": "Literal",
"value": "bar",
},
"type": "ExportAllDeclaration",
},
],
"loc": Object {
"end": Object {
"column": 0,
"line": 2,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
28,
],
"sourceType": "module",
"tokens": Array [
Object {
"loc": Object {
"end": Object {
"column": 6,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
6,
],
"type": "Keyword",
"value": "export",
},
Object {
"loc": Object {
"end": Object {
"column": 8,
"line": 1,
},
"start": Object {
"column": 7,
"line": 1,
},
},
"range": Array [
7,
8,
],
"type": "Punctuator",
"value": "*",
},
Object {
"loc": Object {
"end": Object {
"column": 11,
"line": 1,
},
"start": Object {
"column": 9,
"line": 1,
},
},
"range": Array [
9,
11,
],
"type": "Identifier",
"value": "as",
},
Object {
"loc": Object {
"end": Object {
"column": 15,
"line": 1,
},
"start": Object {
"column": 12,
"line": 1,
},
},
"range": Array [
12,
15,
],
"type": "Identifier",
"value": "foo",
},
Object {
"loc": Object {
"end": Object {
"column": 20,
"line": 1,
},
"start": Object {
"column": 16,
"line": 1,
},
},
"range": Array [
16,
20,
],
"type": "Identifier",
"value": "from",
},
Object {
"loc": Object {
"end": Object {
"column": 26,
"line": 1,
},
"start": Object {
"column": 21,
"line": 1,
},
},
"range": Array [
21,
26,
],
"type": "String",
"value": "'bar'",
},
Object {
"loc": Object {
"end": Object {
"column": 27,
"line": 1,
},
"start": Object {
"column": 26,
"line": 1,
},
},
"range": Array [
26,
27,
],
"type": "Punctuator",
"value": ";",
},
],
"type": "Program",
}
`;

exports[`typescript fixtures/basics/export-type.src 1`] = `
Object {
"body": Array [
Expand Down Expand Up @@ -47846,6 +48052,7 @@ Object {
"body": Array [
Object {
"exportKind": "type",
"exported": null,
"loc": Object {
"end": Object {
"column": 25,
Expand Down

0 comments on commit 133f622

Please sign in to comment.