From cd0884731914302acb3f3f03a88a4603c71d65bb Mon Sep 17 00:00:00 2001 From: Evan Wallace Date: Sun, 7 Feb 2021 23:41:53 -0800 Subject: [PATCH] ts "import id =" syntax is not esm syntax --- internal/bundler/snapshots/snapshots_default.txt | 13 ++++++++----- internal/js_parser/js_parser.go | 7 ++++--- internal/js_parser/ts_parser_test.go | 8 ++++++-- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/internal/bundler/snapshots/snapshots_default.txt b/internal/bundler/snapshots/snapshots_default.txt index daa8074860a..9d0bc3b9286 100644 --- a/internal/bundler/snapshots/snapshots_default.txt +++ b/internal/bundler/snapshots/snapshots_default.txt @@ -2179,6 +2179,12 @@ var require_dummy = __commonJS((exports) => { var dummy = 123; }); +// es6-import-assign.ts +var require_es6_import_assign = __commonJS((exports) => { + var x2 = require_dummy(); + console.log(exports); +}); + // es6-import-dynamic.js var require_es6_import_dynamic = __commonJS((exports) => { Promise.resolve().then(() => __toModule(require_dummy())); @@ -2295,11 +2301,8 @@ var import_cjs = __toModule(require_cjs()); var import_dummy = __toModule(require_dummy()); console.log(void 0); -// es6-import-assign.ts -var x = require_dummy(); -console.log(void 0); - // entry.js +var import_es6_import_assign = __toModule(require_es6_import_assign()); var import_es6_import_dynamic = __toModule(require_es6_import_dynamic()); // es6-import-meta.js @@ -2365,7 +2368,7 @@ console.log(void 0); var import_es6_export_assign = __toModule(require_es6_export_assign()); // es6-export-import-assign.ts -var x2 = require_dummy(); +var x = require_dummy(); console.log(void 0); // entry.js diff --git a/internal/js_parser/js_parser.go b/internal/js_parser/js_parser.go index ec67a6f4df2..82033fe1c20 100644 --- a/internal/js_parser/js_parser.go +++ b/internal/js_parser/js_parser.go @@ -4909,7 +4909,7 @@ func (p *parser) parseStmt(opts parseStmtOpts) js_ast.Stmt { return js_ast.Stmt{Loc: loc, Data: &js_ast.SEmpty{}} case js_lexer.TExport: - oldExportKeyword := p.es6ExportKeyword + previousExportKeyword := p.es6ExportKeyword if opts.isModuleScope { p.es6ExportKeyword = p.lexer.Range() } else if !opts.isNamespaceScope { @@ -5190,7 +5190,7 @@ func (p *parser) parseStmt(opts parseStmtOpts) js_ast.Stmt { case js_lexer.TEquals: // "export = value;" - p.es6ExportKeyword = oldExportKeyword // Never mind it's CommonJS syntax instead of ECMAScript module syntax + p.es6ExportKeyword = previousExportKeyword // This wasn't an ESM export statement after all if p.options.ts.Parse { p.lexer.Next() value := p.parseExpr(js_ast.LLowest) @@ -5600,7 +5600,7 @@ func (p *parser) parseStmt(opts parseStmtOpts) js_ast.Stmt { case js_lexer.TOpenParen, js_lexer.TDot: // "import('path')" // "import.meta" - p.es6ImportKeyword = previousImportKeyword // This wasn't an import statement after all + p.es6ImportKeyword = previousImportKeyword // This wasn't an ESM import statement after all expr := p.parseSuffix(p.parseImportExpr(loc, js_ast.LLowest), js_ast.LLowest, nil, 0) p.lexer.ExpectOrInsertSemicolon() return js_ast.Stmt{Loc: loc, Data: &js_ast.SExpr{Value: expr}} @@ -5689,6 +5689,7 @@ func (p *parser) parseStmt(opts parseStmtOpts) js_ast.Stmt { // Parse TypeScript import assignment statements if p.lexer.Token == js_lexer.TEquals || opts.isExport || (opts.isNamespaceScope && !opts.isTypeScriptDeclare) { + p.es6ImportKeyword = previousImportKeyword // This wasn't an ESM import statement after all return p.parseTypeScriptImportEqualsStmt(loc, opts, stmt.DefaultName.Loc, defaultName) } } diff --git a/internal/js_parser/ts_parser_test.go b/internal/js_parser/ts_parser_test.go index c1ece68ad92..52a0ab20e4c 100644 --- a/internal/js_parser/ts_parser_test.go +++ b/internal/js_parser/ts_parser_test.go @@ -1312,15 +1312,19 @@ func TestTSImport(t *testing.T) { // This is TypeScript-specific export syntax func TestTSExportEquals(t *testing.T) { - expectPrintedTS(t, "export = []", "module.exports = [];\n") - // This use of the "export" keyword should not trigger strict mode because // this syntax works in CommonJS modules, not in ECMAScript modules + expectPrintedTS(t, "export = []", "module.exports = [];\n") expectPrintedTS(t, "export = []; with ({}) ;", "with ({})\n ;\nmodule.exports = [];\n") } // This is TypeScript-specific import syntax func TestTSImportEquals(t *testing.T) { + // This use of the "export" keyword should not trigger strict mode because + // this syntax works in CommonJS modules, not in ECMAScript modules + expectPrintedTS(t, "import x = require('y')", "const x = require(\"y\");\n") + expectPrintedTS(t, "import x = require('y'); with ({}) ;", "const x = require(\"y\");\nwith ({})\n ;\n") + expectPrintedTS(t, "import x = require('foo'); x()", "const x = require(\"foo\");\nx();\n") expectPrintedTS(t, "import x = require('foo')\nx()", "const x = require(\"foo\");\nx();\n") expectPrintedTS(t, "import x = require\nx()", "const x = require;\nx();\n")