Skip to content

Commit

Permalink
ts "import id =" syntax is not esm syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 8, 2021
1 parent 6da51d4 commit cd08847
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 10 deletions.
13 changes: 8 additions & 5 deletions internal/bundler/snapshots/snapshots_default.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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}}
Expand Down Expand Up @@ -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)
}
}
Expand Down
8 changes: 6 additions & 2 deletions internal/js_parser/ts_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit cd08847

Please sign in to comment.