Skip to content

Commit

Permalink
Align behavior with TS
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Mar 30, 2023
1 parent fb30f90 commit 72a5861
Show file tree
Hide file tree
Showing 15 changed files with 31 additions and 38 deletions.
14 changes: 2 additions & 12 deletions packages/babel-parser/src/plugins/typescript/index.ts
Expand Up @@ -2017,6 +2017,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
// For compatibility to estree we cannot call parseLiteral directly here
node.expression = super.parseExprAtom() as N.StringLiteral;
this.expect(tt.parenR);
this.sawUnambiguousESM = true;
return this.finishNode(node, "TSExternalModuleReference");
}

Expand Down Expand Up @@ -2711,18 +2712,6 @@ 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 Expand Up @@ -2800,6 +2789,7 @@ export default (superClass: ClassWithMixin<typeof Parser, IJSXParserMixin>) =>
const assign = node as Undone<N.TsExportAssignment>;
assign.expression = super.parseExpression();
this.semicolon();
this.sawUnambiguousESM = true;
return this.finishNode(assign, "TSExportAssignment");
} else if (this.eatContextual(tt._as)) {
// `export as namespace A;`
Expand Down
@@ -1,6 +1,9 @@
{
"type": "File",
"start":0,"end":11,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":11,"index":11}},
"errors": [
"SyntaxError: 'import' and 'export' may appear only with 'sourceType: \"module\"' (1:0)"
],
"program": {
"type": "Program",
"start":0,"end":11,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":11,"index":11}},
Expand Down
Expand Up @@ -4,7 +4,7 @@
"program": {
"type": "Program",
"start":0,"end":11,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":11,"index":11}},
"sourceType": "script",
"sourceType": "module",
"interpreter": null,
"body": [
{
Expand Down
@@ -1,6 +1,9 @@
{
"type": "File",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":15,"index":15}},
"errors": [
"SyntaxError: 'import' and 'export' may appear only with 'sourceType: \"module\"' (1:0)"
],
"program": {
"type": "Program",
"start":0,"end":15,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":15,"index":15}},
Expand Down
@@ -1,6 +1,9 @@
{
"type": "File",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}},
"errors": [
"SyntaxError: 'import' and 'export' may appear only with 'sourceType: \"module\"' (1:0)"
],
"program": {
"type": "Program",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}},
Expand Down
Expand Up @@ -4,7 +4,7 @@
"program": {
"type": "Program",
"start":0,"end":24,"loc":{"start":{"line":1,"column":0,"index":0},"end":{"line":1,"column":24,"index":24}},
"sourceType": "script",
"sourceType": "module",
"interpreter": null,
"body": [
{
Expand Down
26 changes: 16 additions & 10 deletions packages/babel-plugin-transform-typescript/src/index.ts
@@ -1,6 +1,6 @@
import { declare } from "@babel/helper-plugin-utils";
import syntaxTypeScript from "@babel/plugin-syntax-typescript";
import type { types as t } from "@babel/core";
import type { PluginPass, types as t } from "@babel/core";
import { injectInitialization } from "@babel/helper-create-class-features-plugin";
import type { Binding, NodePath, Scope } from "@babel/traverse";
import type { Options as SyntaxOptions } from "@babel/plugin-syntax-typescript";
Expand Down Expand Up @@ -78,17 +78,18 @@ function safeRemove(path: NodePath) {
path.opts.noScope = false;
}

function assertCjsModuleIsScript(
function assertCjsTransformEnabled(
path: NodePath,
pass: PluginPass,
wrong: string,
suggestion: string,
extra: string = "",
): void {
const programParent = path.find(p => p.isProgram()) as NodePath<t.Program>;
if (programParent.node.sourceType !== "script") {
if (pass.get("@babel/plugin-transform-modules-*") !== "commonjs") {
throw path.buildCodeFrameError(
`\`${wrong}\` is only supported when "sourceType" is "script".\n` +
`Please consider using \`${suggestion}\`${extra}.`,
`\`${wrong}\` is only supported when compiling modules to CommonJS.\n` +
`Please consider using \`${suggestion}\`${extra}, or add ` +
`@babel/plugin-transform-modules-commonjs to your Babel config.`,
);
}
}
Expand Down Expand Up @@ -589,15 +590,19 @@ export default declare((api, opts: Options) => {
}
},

TSImportEqualsDeclaration(path: NodePath<t.TSImportEqualsDeclaration>) {
TSImportEqualsDeclaration(
path: NodePath<t.TSImportEqualsDeclaration>,
pass,
) {
const { id, moduleReference } = path.node;

let init: t.Expression;
let varKind: "var" | "const";
if (t.isTSExternalModuleReference(moduleReference)) {
// import alias = require('foo');
assertCjsModuleIsScript(
assertCjsTransformEnabled(
path,
pass,
`import ${id.name} = require(...);`,
`import ${id.name} from '...';`,
" alongside Typescript's --allowSyntheticDefaultImports option",
Expand All @@ -618,9 +623,10 @@ export default declare((api, opts: Options) => {
path.scope.registerDeclaration(path);
},

TSExportAssignment(path) {
assertCjsModuleIsScript(
TSExportAssignment(path, pass) {
assertCjsTransformEnabled(
path,
pass,
`export = <value>;`,
`export default <value>;`,
);
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

@@ -1,3 +1,3 @@
{
"throws": "`export = <value>;` is only supported when \"sourceType\" is \"script\".\nPlease consider using `export default <value>;`"
"throws": "`export = <value>;` is only supported when compiling modules to CommonJS.\nPlease consider using `export default <value>;`, or add @babel/plugin-transform-modules-commonjs to your Babel config."
}
@@ -1,4 +1,4 @@
{
"sourceType": "module",
"throws": "`import lib = require(...);` is only supported when \"sourceType\" is \"script\".\nPlease consider using `import lib from '...';` alongside Typescript's --allowSyntheticDefaultImports option."
"throws": "`import lib = require(...);` is only supported when compiling modules to CommonJS.\nPlease consider using `import lib from '...';` alongside Typescript's --allowSyntheticDefaultImports option, or add @babel/plugin-transform-modules-commonjs to your Babel config."
}

This file was deleted.

This file was deleted.

This file was deleted.

0 comments on commit 72a5861

Please sign in to comment.