Skip to content

Commit

Permalink
fix(typescript-estree): change source of ExportNamedDeclaration to …
Browse files Browse the repository at this point in the history
…Literal from Expression (#3763)

* fix(typescript-estree): change  of ExportNamedDeclaration to Literal from Expression

* fix(typescript-estree): add assertion for module specifier
  • Loading branch information
sosukesuzuki committed Aug 22, 2021
1 parent 0983d3e commit 99bb19d
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 416 deletions.
Expand Up @@ -2,13 +2,13 @@ import type { AST_NODE_TYPES } from '../../ast-node-types';
import type { BaseNode } from '../../base/BaseNode';
import type { ExportSpecifier } from '../../special/ExportSpecifier/spec';
import type { ExportDeclaration } from '../../unions/ExportDeclaration';
import type { Expression } from '../../unions/Expression';
import type { Literal } from '../../unions/Literal';
import type { ExportKind } from '../ExportAndImportKind';

export interface ExportNamedDeclaration extends BaseNode {
type: AST_NODE_TYPES.ExportNamedDeclaration;
declaration: ExportDeclaration | null;
specifiers: ExportSpecifier[];
source: Expression | null;
source: Literal | null;
exportKind: ExportKind;
}
@@ -0,0 +1 @@
export { foo } from bar;
21 changes: 20 additions & 1 deletion packages/typescript-estree/src/convert.ts
Expand Up @@ -724,6 +724,21 @@ export class Converter {
}
}

private assertModuleSpecifier(
node: ts.ExportDeclaration | ts.ImportDeclaration,
): void {
if (
node.moduleSpecifier &&
node.moduleSpecifier.kind !== SyntaxKind.StringLiteral
) {
throw createError(
this.ast,
node.moduleSpecifier.pos,
'Module specifier must be a string literal.',
);
}
}

/**
* Converts a TypeScript node into an ESTree node.
* The core of the conversion logic:
Expand Down Expand Up @@ -1690,6 +1705,8 @@ export class Converter {
});

case SyntaxKind.ImportDeclaration: {
this.assertModuleSpecifier(node);

const result = this.createNode<TSESTree.ImportDeclaration>(node, {
type: AST_NODE_TYPES.ImportDeclaration,
source: this.convertChild(node.moduleSpecifier),
Expand Down Expand Up @@ -1748,7 +1765,8 @@ export class Converter {
});
}

case SyntaxKind.ExportDeclaration:
case SyntaxKind.ExportDeclaration: {
this.assertModuleSpecifier(node);
if (node.exportClause?.kind === SyntaxKind.NamedExports) {
return this.createNode<TSESTree.ExportNamedDeclaration>(node, {
type: AST_NODE_TYPES.ExportNamedDeclaration,
Expand All @@ -1775,6 +1793,7 @@ export class Converter {
: null,
});
}
}

case SyntaxKind.ExportSpecifier:
return this.createNode<TSESTree.ExportSpecifier>(node, {
Expand Down
Expand Up @@ -908,6 +908,15 @@ TSError {
}
`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/modules/invalid-export-module-specifier.src 1`] = `
TSError {
"column": 19,
"index": 19,
"lineNumber": 1,
"message": "Module specifier must be a string literal.",
}
`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/modules/invalid-export-named-default.src 1`] = `"TEST OUTPUT: No semantic or syntactic issues found"`;

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/modules/invalid-export-named-extra-comma.src 1`] = `
Expand Down Expand Up @@ -966,10 +975,10 @@ TSError {

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/modules/invalid-import-default-module-specifier.src 1`] = `
TSError {
"column": 16,
"index": 16,
"column": 15,
"index": 15,
"lineNumber": 1,
"message": "String literal expected.",
"message": "Module specifier must be a string literal.",
}
`;

Expand All @@ -984,10 +993,10 @@ TSError {

exports[`Parse all fixtures with "errorOnTypeScriptSyntacticAndSemanticIssues" enabled fixtures/javascript/modules/invalid-import-module-specifier.src 1`] = `
TSError {
"column": 18,
"index": 18,
"column": 17,
"index": 17,
"lineNumber": 1,
"message": "String literal expected.",
"message": "Module specifier must be a string literal.",
}
`;

Expand Down
@@ -0,0 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`javascript modules invalid-export-module-specifier.src 1`] = `
TSError {
"column": 19,
"index": 19,
"lineNumber": 1,
"message": "Module specifier must be a string literal.",
}
`;
@@ -1,190 +1,10 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`javascript modules invalid-import-default-module-specifier.src 1`] = `
Object {
"body": Array [
Object {
"importKind": "value",
"loc": Object {
"end": Object {
"column": 20,
"line": 1,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
20,
],
"source": Object {
"loc": Object {
"end": Object {
"column": 19,
"line": 1,
},
"start": Object {
"column": 16,
"line": 1,
},
},
"name": "bar",
"range": Array [
16,
19,
],
"type": "Identifier",
},
"specifiers": Array [
Object {
"loc": Object {
"end": Object {
"column": 10,
"line": 1,
},
"start": Object {
"column": 7,
"line": 1,
},
},
"local": Object {
"loc": Object {
"end": Object {
"column": 10,
"line": 1,
},
"start": Object {
"column": 7,
"line": 1,
},
},
"name": "foo",
"range": Array [
7,
10,
],
"type": "Identifier",
},
"range": Array [
7,
10,
],
"type": "ImportDefaultSpecifier",
},
],
"type": "ImportDeclaration",
},
],
"comments": Array [],
"loc": Object {
"end": Object {
"column": 0,
"line": 2,
},
"start": Object {
"column": 0,
"line": 1,
},
},
"range": Array [
0,
21,
],
"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": "import",
},
Object {
"loc": Object {
"end": Object {
"column": 10,
"line": 1,
},
"start": Object {
"column": 7,
"line": 1,
},
},
"range": Array [
7,
10,
],
"type": "Identifier",
"value": "foo",
},
Object {
"loc": Object {
"end": Object {
"column": 15,
"line": 1,
},
"start": Object {
"column": 11,
"line": 1,
},
},
"range": Array [
11,
15,
],
"type": "Identifier",
"value": "from",
},
Object {
"loc": Object {
"end": Object {
"column": 19,
"line": 1,
},
"start": Object {
"column": 16,
"line": 1,
},
},
"range": Array [
16,
19,
],
"type": "Identifier",
"value": "bar",
},
Object {
"loc": Object {
"end": Object {
"column": 20,
"line": 1,
},
"start": Object {
"column": 19,
"line": 1,
},
},
"range": Array [
19,
20,
],
"type": "Punctuator",
"value": ";",
},
],
"type": "Program",
TSError {
"column": 15,
"index": 15,
"lineNumber": 1,
"message": "Module specifier must be a string literal.",
}
`;

0 comments on commit 99bb19d

Please sign in to comment.