Skip to content

Commit 3b38ce3

Browse files
committed
fix #2991: crash with inline typescript decorator
1 parent 0c8a0a9 commit 3b38ce3

File tree

3 files changed

+22
-2
lines changed

3 files changed

+22
-2
lines changed

CHANGELOG.md

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* Fix a crash when parsing inline TypeScript decorators ([#2991](https://github.com/evanw/esbuild/issues/2991))
6+
7+
Previously esbuild's TypeScript parser crashed when parsing TypeScript decorators if the definition of the decorator was inlined into the decorator itself:
8+
9+
```ts
10+
@(function sealed(constructor: Function) {
11+
Object.seal(constructor);
12+
Object.seal(constructor.prototype);
13+
})
14+
class Foo {}
15+
```
16+
17+
This crash was not noticed earlier because this edge case did not have test coverage. The crash is fixed in this release.
18+
319
## 0.17.11
420

521
* Fix the `alias` feature to always prefer the longest match ([#2963](https://github.com/evanw/esbuild/issues/2963))

internal/js_parser/ts_parser.go

-2
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,6 @@ func (p *parser) parseTypeScriptDecorators(tsDecoratorScope *js_ast.Scope) []js_
12451245
p.currentScope = tsDecoratorScope
12461246

12471247
for p.lexer.Token == js_lexer.TAt {
1248-
loc := p.lexer.Loc()
12491248
p.lexer.Next()
12501249

12511250
// Parse a new/call expression with "exprFlagTSDecorator" so we ignore
@@ -1257,7 +1256,6 @@ func (p *parser) parseTypeScriptDecorators(tsDecoratorScope *js_ast.Scope) []js_
12571256
//
12581257
// This matches the behavior of the TypeScript compiler.
12591258
value := p.parseExprWithFlags(js_ast.LNew, exprFlagTSDecorator)
1260-
value.Loc = loc
12611259
tsDecorators = append(tsDecorators, value)
12621260
}
12631261

internal/js_parser/ts_parser_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -1773,6 +1773,12 @@ func TestTSDecorator(t *testing.T) {
17731773
"<stdin>: ERROR: Cannot use \"yield\" outside a generator function\n")
17741774
expectParseErrorTS(t, "function foo() { @dec(yield x) class Foo {} }", "<stdin>: ERROR: Cannot use \"yield\" outside a generator function\n")
17751775
expectParseErrorTS(t, "function foo() { class Foo { @dec(yield x) foo() {} } }", "<stdin>: ERROR: Cannot use \"yield\" outside a generator function\n")
1776+
1777+
// Check inline function expressions
1778+
expectPrintedTS(t, "@((x, y) => x + y) class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n (x, y) => x + y\n], Foo);\n")
1779+
expectPrintedTS(t, "@((x, y) => x + y) export class Foo {}", "export let Foo = class {\n};\nFoo = __decorateClass([\n (x, y) => x + y\n], Foo);\n")
1780+
expectPrintedTS(t, "@(function(x, y) { return x + y }) class Foo {}", "let Foo = class {\n};\nFoo = __decorateClass([\n function(x, y) {\n return x + y;\n }\n], Foo);\n")
1781+
expectPrintedTS(t, "@(function(x, y) { return x + y }) export class Foo {}", "export let Foo = class {\n};\nFoo = __decorateClass([\n function(x, y) {\n return x + y;\n }\n], Foo);\n")
17761782
}
17771783

17781784
func TestTSTry(t *testing.T) {

0 commit comments

Comments
 (0)