Skip to content

Commit

Permalink
fix #3225: allow a newline after export type
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Jul 12, 2023
1 parent cc25614 commit bf165fc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,20 @@

## Unreleased

* Allow a newline in the middle of TypeScript `export type` statement ([#3225](https://github.com/evanw/esbuild/issues/3225))

Previously esbuild incorrectly rejected the following valid TypeScript code:

```ts
export type
{ T };

export type
* as foo from 'bar';
```

Code that uses a newline after `export type` is now allowed starting with this release.

* Rewrite `.js` to `.ts` inside packages with `exports` ([#3201](https://github.com/evanw/esbuild/issues/3201))

Packages with the `exports` field are supposed to disable node's path resolution behavior that allows you to import a file with a different extension than the one in the source code (for example, importing `foo/bar` to get `foo/bar.js`). And TypeScript has behavior where you can import a non-existent `.js` file and you will get the `.ts` file instead. Previously the presence of the `exports` field caused esbuild to disable all extension manipulation stuff which included both node's implicit file extension searching and TypeScript's file extension swapping. However, TypeScript appears to always apply file extension swapping even in this case. So with this release, esbuild will now rewrite `.js` to `.ts` even inside packages with `exports`.
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 @@ -6766,7 +6766,7 @@ func (p *parser) parseStmt(opts parseStmtOpts) js_ast.Stmt {
// "export type foo = ..."
typeRange := p.lexer.Range()
p.lexer.Next()
if p.lexer.HasNewlineBefore {
if p.lexer.HasNewlineBefore && p.lexer.Token != js_lexer.TOpenBrace && p.lexer.Token != js_lexer.TAsterisk {
p.log.AddError(&p.tracker, logger.Range{Loc: logger.Loc{Start: typeRange.End()}},
"Unexpected newline after \"type\"")
panic(js_lexer.LexerPanic{})
Expand Down
3 changes: 3 additions & 0 deletions internal/js_parser/ts_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,9 @@ func TestTSTypes(t *testing.T) {
expectPrintedTS(t, "type x = {0: number, readonly 1: boolean}\n[]", "[];\n")
expectPrintedTS(t, "type x = {'a': number, readonly 'b': boolean}\n[]", "[];\n")
expectPrintedTS(t, "type\nFoo = {}", "type;\nFoo = {};\n")
expectPrintedTS(t, "export type\n{ Foo } \n x", "x;\n")
expectPrintedTS(t, "export type\n* from 'foo' \n x", "x;\n")
expectPrintedTS(t, "export type\n* as ns from 'foo' \n x", "x;\n")
expectParseErrorTS(t, "export type\nFoo = {}", "<stdin>: ERROR: Unexpected newline after \"type\"\n")
expectPrintedTS(t, "let x: {x: 'a', y: false, z: null}", "let x;\n")
expectPrintedTS(t, "let x: {foo(): void}", "let x;\n")
Expand Down

0 comments on commit bf165fc

Please sign in to comment.