Skip to content

Commit

Permalink
fix #2070: ignore export as namespace statements
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Mar 2, 2022
1 parent 2b644d6 commit 50dc6ae
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

## Unreleased

* Parse and discard TypeScript `export as namespace` statements ([#2070](https://github.com/evanw/esbuild/issues/2070))

TypeScript `.d.ts` type declaration files can sometimes contain statements of the form `export as namespace foo;`. I believe these serve to declare that the module adds a property of that name to the global object. You aren't supposed to feed `.d.ts` files to esbuild so this normally doesn't matter, but sometimes esbuild can end up having to parse them. One such case is if you import a type-only package who's `main` field in `package.json` is a `.d.ts` file.

Previously esbuild only allowed `export as namespace` statements inside a `declare` context:

```ts
declare module Foo {
export as namespace foo;
}
```

Now esbuild will also allow these statements outside of a `declare` context:

```ts
export as namespace foo;
```

These statements are still just ignored and discarded.

* Strip import assertions from unrecognized `import()` expressions ([#2036](https://github.com/evanw/esbuild/issues/2036))

The new "import assertions" JavaScript language feature adds an optional second argument to dynamic `import()` expressions, which esbuild does support. However, this optional argument must be stripped when targeting older JavaScript environments for which this second argument would be a syntax error. Previously esbuild failed to strip this second argument in cases when the first argument to `import()` wasn't a string literal. This problem is now fixed:
Expand Down
2 changes: 1 addition & 1 deletion internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5952,7 +5952,7 @@ func (p *parser) parseStmt(opts parseStmtOpts) js_ast.Stmt {
return p.parseStmt(opts)
}

if opts.isTypeScriptDeclare && p.lexer.IsContextualKeyword("as") {
if p.lexer.IsContextualKeyword("as") {
// "export as namespace ns;"
p.lexer.Next()
p.lexer.ExpectContextualKeyword("namespace")
Expand Down
4 changes: 4 additions & 0 deletions internal/js_parser/ts_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1389,6 +1389,10 @@ func TestTSDeclare(t *testing.T) {
expectParseErrorTS(t, "function fn(x: any, ...y, ) {}", "<stdin>: ERROR: Expected \")\" but found \",\"\n")
expectParseErrorTS(t, "function fn(x: any, ...y: any, )", "<stdin>: ERROR: Expected \")\" but found \",\"\n")
expectParseErrorTS(t, "function fn(x: any, ...y: any, ) {}", "<stdin>: ERROR: Expected \")\" but found \",\"\n")

// This declares a global module
expectPrintedTS(t, "export as namespace ns", "")
expectParseErrorTS(t, "export as namespace ns.foo", "<stdin>: ERROR: Expected \";\" but found \".\"\n")
}

func TestTSDecorator(t *testing.T) {
Expand Down

0 comments on commit 50dc6ae

Please sign in to comment.