Skip to content

Commit

Permalink
[ts] Parse import ... = and export = in scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Mar 15, 2023
1 parent fbfbd1d commit a04c976
Show file tree
Hide file tree
Showing 19 changed files with 230 additions and 0 deletions.
12 changes: 12 additions & 0 deletions packages/babel-parser/src/plugins/typescript/index.ts
Expand Up @@ -2711,6 +2711,18 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
*/
checkDuplicateExports() {}

assertModuleNodeAllowed(node: N.Node): void {
if (
node.type === "TSImportEqualsDeclaration" ||
node.type === "TSExportAssignment"
) {
// `import ... =` and `export =` are allowed in scripts,
// since they are used for CommonJS.
return;
}
super.assertModuleNodeAllowed(node);
}

parseImport(
node: Undone<N.ImportDeclaration | N.TsImportEqualsDeclaration>,
): N.AnyImport {
Expand Down
@@ -0,0 +1 @@
export = f;
@@ -0,0 +1,3 @@
{
"sourceType": "script"
}
@@ -0,0 +1,22 @@
{
"type": "File",
"start":0,"end":11,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":11,"index":11}},
"program": {
"type": "Program",
"start":0,"end":11,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":11,"index":11}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "TSExportAssignment",
"start":0,"end":11,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":11,"index":11}},
"expression": {
"type": "Identifier",
"start":9,"end":10,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":10,"index":10},"identifierName":"f"},
"name": "f"
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
export = f;
@@ -0,0 +1,3 @@
{
"sourceType": "unambiguous"
}
@@ -0,0 +1,22 @@
{
"type": "File",
"start":0,"end":11,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":11,"index":11}},
"program": {
"type": "Program",
"start":0,"end":11,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":11,"index":11}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "TSExportAssignment",
"start":0,"end":11,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":11,"index":11}},
"expression": {
"type": "Identifier",
"start":9,"end":10,"loc":{"start":{"line":1,"column":9,"index":9},"end":{"line":1,"column":10,"index":10},"identifierName":"f"},
"name": "f"
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
import A = B.C;
@@ -0,0 +1,3 @@
{
"sourceType": "script"
}
@@ -0,0 +1,38 @@
{
"type": "File",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":15,"index":15}},
"program": {
"type": "Program",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":15,"index":15}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "TSImportEqualsDeclaration",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":15,"index":15}},
"importKind": "value",
"isExport": false,
"id": {
"type": "Identifier",
"start":7,"end":8,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":8,"index":8},"identifierName":"A"},
"name": "A"
},
"moduleReference": {
"type": "TSQualifiedName",
"start":11,"end":14,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":14,"index":14}},
"left": {
"type": "Identifier",
"start":11,"end":12,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":12,"index":12},"identifierName":"B"},
"name": "B"
},
"right": {
"type": "Identifier",
"start":13,"end":14,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":14,"index":14},"identifierName":"C"},
"name": "C"
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
import A = B.C;
@@ -0,0 +1,3 @@
{
"sourceType": "unambiguous"
}
@@ -0,0 +1,38 @@
{
"type": "File",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":15,"index":15}},
"program": {
"type": "Program",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":15,"index":15}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "TSImportEqualsDeclaration",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":15,"index":15}},
"importKind": "value",
"isExport": false,
"id": {
"type": "Identifier",
"start":7,"end":8,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":8,"index":8},"identifierName":"A"},
"name": "A"
},
"moduleReference": {
"type": "TSQualifiedName",
"start":11,"end":14,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":14,"index":14}},
"left": {
"type": "Identifier",
"start":11,"end":12,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":12,"index":12},"identifierName":"B"},
"name": "B"
},
"right": {
"type": "Identifier",
"start":13,"end":14,"loc":{"start":{"line":1,"column":13,"index":13},"end":{"line":1,"column":14,"index":14},"identifierName":"C"},
"name": "C"
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
import a = require("a");
@@ -0,0 +1,3 @@
{
"sourceType": "script"
}
@@ -0,0 +1,37 @@
{
"type": "File",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}},
"program": {
"type": "Program",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "TSImportEqualsDeclaration",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}},
"importKind": "value",
"isExport": false,
"id": {
"type": "Identifier",
"start":7,"end":8,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":8,"index":8},"identifierName":"a"},
"name": "a"
},
"moduleReference": {
"type": "TSExternalModuleReference",
"start":11,"end":23,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":23,"index":23}},
"expression": {
"type": "StringLiteral",
"start":19,"end":22,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":22,"index":22}},
"extra": {
"rawValue": "a",
"raw": "\"a\""
},
"value": "a"
}
}
}
],
"directives": []
}
}
@@ -0,0 +1 @@
import a = require("a");
@@ -0,0 +1,3 @@
{
"sourceType": "unambiguous"
}
@@ -0,0 +1,37 @@
{
"type": "File",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}},
"program": {
"type": "Program",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}},
"sourceType": "script",
"interpreter": null,
"body": [
{
"type": "TSImportEqualsDeclaration",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}},
"importKind": "value",
"isExport": false,
"id": {
"type": "Identifier",
"start":7,"end":8,"loc":{"start":{"line":1,"column":7,"index":7},"end":{"line":1,"column":8,"index":8},"identifierName":"a"},
"name": "a"
},
"moduleReference": {
"type": "TSExternalModuleReference",
"start":11,"end":23,"loc":{"start":{"line":1,"column":11,"index":11},"end":{"line":1,"column":23,"index":23}},
"expression": {
"type": "StringLiteral",
"start":19,"end":22,"loc":{"start":{"line":1,"column":19,"index":19},"end":{"line":1,"column":22,"index":22}},
"extra": {
"rawValue": "a",
"raw": "\"a\""
},
"value": "a"
}
}
}
],
"directives": []
}
}

0 comments on commit a04c976

Please sign in to comment.