diff --git a/internal/js_parser/js_parser.go b/internal/js_parser/js_parser.go index 2caa0277156..ff73463ae7a 100644 --- a/internal/js_parser/js_parser.go +++ b/internal/js_parser/js_parser.go @@ -7546,6 +7546,7 @@ func (p *parser) parseStmt(opts parseStmtOpts) js_ast.Stmt { defaultName := p.lexer.Identifier stmt.DefaultName = &ast.LocRef{Loc: p.lexer.Loc(), Ref: p.storeNameInRef(defaultName)} + oldLexer := p.lexer p.lexer.Next() if p.options.ts.Parse { @@ -7553,22 +7554,25 @@ func (p *parser) parseStmt(opts parseStmtOpts) js_ast.Stmt { if defaultName.String == "type" { switch p.lexer.Token { case js_lexer.TIdentifier: - if p.lexer.Identifier.String != "from" { - defaultName = p.lexer.Identifier - stmt.DefaultName.Loc = p.lexer.Loc() + defaultName = p.lexer.Identifier + stmt.DefaultName.Loc = p.lexer.Loc() + p.lexer.Next() + if p.lexer.Token == js_lexer.TEquals { + // "import type foo = require('bar');" + // "import type foo = bar.baz;" + opts.isTypeScriptDeclare = true + return p.parseTypeScriptImportEqualsStmt(loc, opts, stmt.DefaultName.Loc, defaultName.String) + } else if defaultName.String != "from" || p.lexer.IsContextualKeyword("from") { + // "import type foo from 'bar';" + p.lexer.ExpectContextualKeyword("from") + p.parsePath() + p.lexer.ExpectOrInsertSemicolon() + return js_ast.Stmt{Loc: loc, Data: js_ast.STypeScriptShared} + } else { + defaultName = oldLexer.Identifier + stmt.DefaultName.Loc = oldLexer.Loc() + p.lexer = oldLexer p.lexer.Next() - if p.lexer.Token == js_lexer.TEquals { - // "import type foo = require('bar');" - // "import type foo = bar.baz;" - opts.isTypeScriptDeclare = true - return p.parseTypeScriptImportEqualsStmt(loc, opts, stmt.DefaultName.Loc, defaultName.String) - } else { - // "import type foo from 'bar';" - p.lexer.ExpectContextualKeyword("from") - p.parsePath() - p.lexer.ExpectOrInsertSemicolon() - return js_ast.Stmt{Loc: loc, Data: js_ast.STypeScriptShared} - } } case js_lexer.TAsterisk: diff --git a/internal/js_parser/ts_parser_test.go b/internal/js_parser/ts_parser_test.go index 7df46476aca..3512dcc188b 100644 --- a/internal/js_parser/ts_parser_test.go +++ b/internal/js_parser/ts_parser_test.go @@ -2540,11 +2540,13 @@ func TestTSImportEqualsInNamespace(t *testing.T) { func TestTSTypeOnlyImport(t *testing.T) { expectPrintedTS(t, "import type foo from 'bar'; x", "x;\n") expectPrintedTS(t, "import type foo from 'bar'\nx", "x;\n") + expectPrintedTS(t, "import type from from 'bar'\nx", "x;\n") expectPrintedTS(t, "import type * as foo from 'bar'; x", "x;\n") expectPrintedTS(t, "import type * as foo from 'bar'\nx", "x;\n") expectPrintedTS(t, "import type {foo, bar as baz} from 'bar'; x", "x;\n") expectPrintedTS(t, "import type {'foo' as bar} from 'bar'\nx", "x;\n") expectPrintedTS(t, "import type foo = require('bar'); x", "x;\n") + expectPrintedTS(t, "import type from = require('bar'); x", "x;\n") expectPrintedTS(t, "import type foo = bar.baz; x", "x;\n") expectPrintedTS(t, "import type = bar; type", "const type = bar;\ntype;\n")