Skip to content

Commit

Permalink
fix #846: remove speculative errors from log
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 19, 2021
1 parent d7eb896 commit f6dc899
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@

Accessing layout-related properties can trigger a layout and accessing storage-related properties can throw an exception if certain privacy settings are enabled. Both of these behaviors are considered side effects.

* Fix a TypeScript parser regression ([#846](https://github.com/evanw/esbuild/issues/846))

Restrictions on array and object destructuring patterns in the previous release introduced a regression where arrays or objects in TypeScript code could fail to parse if they were wrapped in a double layer of parentheses. This was due to the speculative parsing of arrow function arguments. The regression has been fixed.

## 0.8.48

* Fix some parsing edge cases ([#835](https://github.com/evanw/esbuild/issues/835))
Expand Down
12 changes: 8 additions & 4 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -2402,6 +2402,10 @@ func (p *parser) convertExprToBindingAndInitializer(expr js_ast.Expr, invalidLog
return binding, initializer, invalidLog
}

// Note: do not write to "p.log" in this function. Any errors due to conversion
// from expression to binding should be written to "invalidLog" instead. That
// way we can potentially keep this as an expression if it turns out it's not
// needed as a binding after all.
func (p *parser) convertExprToBinding(expr js_ast.Expr, invalidLog []logger.Loc) (js_ast.Binding, []logger.Loc) {
switch e := expr.Data.(type) {
case *js_ast.EMissing:
Expand All @@ -2412,10 +2416,10 @@ func (p *parser) convertExprToBinding(expr js_ast.Expr, invalidLog []logger.Loc)

case *js_ast.EArray:
if e.CommaAfterSpread.Start != 0 {
p.log.AddRangeError(&p.source, logger.Range{Loc: e.CommaAfterSpread, Len: 1}, "Unexpected \",\" after rest pattern")
invalidLog = append(invalidLog, e.CommaAfterSpread)
}
if e.IsParenthesized {
p.log.AddRangeError(&p.source, p.source.RangeOfOperatorBefore(expr.Loc, "("), "Unexpected \"(\" before array pattern")
invalidLog = append(invalidLog, p.source.RangeOfOperatorBefore(expr.Loc, "(").Loc)
}
p.markSyntaxFeature(compat.Destructuring, p.source.RangeOfOperatorAfter(expr.Loc, "["))
items := []js_ast.ArrayBinding{}
Expand All @@ -2440,10 +2444,10 @@ func (p *parser) convertExprToBinding(expr js_ast.Expr, invalidLog []logger.Loc)

case *js_ast.EObject:
if e.CommaAfterSpread.Start != 0 {
p.log.AddRangeError(&p.source, logger.Range{Loc: e.CommaAfterSpread, Len: 1}, "Unexpected \",\" after rest pattern")
invalidLog = append(invalidLog, e.CommaAfterSpread)
}
if e.IsParenthesized {
p.log.AddRangeError(&p.source, p.source.RangeOfOperatorBefore(expr.Loc, "("), "Unexpected \"(\" before object pattern")
invalidLog = append(invalidLog, p.source.RangeOfOperatorBefore(expr.Loc, "(").Loc)
}
p.markSyntaxFeature(compat.Destructuring, p.source.RangeOfOperatorAfter(expr.Loc, "{"))
properties := []js_ast.PropertyBinding{}
Expand Down
8 changes: 4 additions & 4 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,8 +525,8 @@ func TestDecls(t *testing.T) {

expectParseError(t, "var [...x,] = []", "<stdin>: error: Unexpected \",\" after rest pattern\n")
expectParseError(t, "var {...x,} = {}", "<stdin>: error: Unexpected \",\" after rest pattern\n")
expectParseError(t, "([...x,] = []) => {}", "<stdin>: error: Unexpected \",\" after rest pattern\n")
expectParseError(t, "({...x,} = {}) => {}", "<stdin>: error: Unexpected \",\" after rest pattern\n")
expectParseError(t, "([...x,] = []) => {}", "<stdin>: error: Invalid binding pattern\n")
expectParseError(t, "({...x,} = {}) => {}", "<stdin>: error: Invalid binding pattern\n")

expectPrinted(t, "[b, ...c] = d", "[b, ...c] = d;\n")
expectPrinted(t, "([b, ...c] = d)", "[b, ...c] = d;\n")
Expand Down Expand Up @@ -554,8 +554,8 @@ func TestDecls(t *testing.T) {
expectParseError(t, "({}) = {}", "<stdin>: error: Invalid assignment target\n")
expectParseError(t, "[([])] = [[]]", "<stdin>: error: Invalid assignment target\n")
expectParseError(t, "({x: ({})} = {x: {}})", "<stdin>: error: Invalid assignment target\n")
expectParseError(t, "(([]) = []) => {}", "<stdin>: error: Unexpected \"(\" before array pattern\n")
expectParseError(t, "(({}) = {}) => {}", "<stdin>: error: Unexpected \"(\" before object pattern\n")
expectParseError(t, "(([]) = []) => {}", "<stdin>: error: Invalid binding pattern\n")
expectParseError(t, "(({}) = {}) => {}", "<stdin>: error: Invalid binding pattern\n")
expectParseError(t, "function f(([]) = []) {}", "<stdin>: error: Expected identifier but found \"(\"\n")
expectParseError(t, "function f(({}) = {}) {}", "<stdin>: error: Expected identifier but found \"(\"\n")

Expand Down
9 changes: 9 additions & 0 deletions internal/js_parser/ts_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1080,6 +1080,15 @@ func TestTSDecl(t *testing.T) {
expectParseErrorTS(t, "var a!", "<stdin>: error: Expected \":\" but found end of file\n")
expectParseErrorTS(t, "var a! = ", "<stdin>: error: Expected \":\" but found \"=\"\n")
expectParseErrorTS(t, "var a!, b", "<stdin>: error: Expected \":\" but found \",\"\n")

expectPrinted(t, "a ? ({b}) => {} : c", "a ? ({b}) => {\n} : c;\n")
expectPrinted(t, "a ? (({b}) => {}) : c", "a ? ({b}) => {\n} : c;\n")
expectPrinted(t, "a ? (({b})) : c", "a ? {b} : c;\n")
expectParseError(t, "a ? (({b})) => {} : c", "<stdin>: error: Invalid binding pattern\n")
expectPrintedTS(t, "a ? ({b}) => {} : c", "a ? ({b}) => {\n} : c;\n")
expectPrintedTS(t, "a ? (({b}) => {}) : c", "a ? ({b}) => {\n} : c;\n")
expectPrintedTS(t, "a ? (({b})) : c", "a ? {b} : c;\n")
expectParseErrorTS(t, "a ? (({b})) => {} : c", "<stdin>: error: Invalid binding pattern\n")
}

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

0 comments on commit f6dc899

Please sign in to comment.