Skip to content

Commit

Permalink
allow "in" inside template inside "for" (#835)
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed Feb 18, 2021
1 parent 0c78d67 commit ccd8950
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 0 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## Unreleased

* Fix some JavaScript parsing edge cases ([#835](https://github.com/evanw/esbuild/issues/835))

This release fixes the following edge cases, which were detected by a fuzzer:

* Code using `in` inside a template literal inside a for loop initializer such as ``for (let x = `${a in b ? '0' : '1'}`; false; );`` is now allowed. Previously the `in` operator was incorrectly considered to be part of a for-in loop.

## 0.8.47

* Release native binaries for the Apple M1 chip ([#550](https://github.com/evanw/esbuild/issues/550))
Expand Down
8 changes: 8 additions & 0 deletions internal/js_parser/js_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4095,6 +4095,11 @@ func (p *parser) parseJSXElement(loc logger.Loc) js_ast.Expr {

func (p *parser) parseTemplateParts(includeRaw bool) []js_ast.TemplatePart {
parts := []js_ast.TemplatePart{}

// Allow "in" inside template literals
oldAllowIn := p.allowIn
p.allowIn = true

for {
p.lexer.Next()
value := p.parseExpr(js_ast.LLowest)
Expand All @@ -4111,6 +4116,9 @@ func (p *parser) parseTemplateParts(includeRaw bool) []js_ast.TemplatePart {
break
}
}

p.allowIn = oldAllowIn

return parts
}

Expand Down
1 change: 1 addition & 0 deletions internal/js_parser/js_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,7 @@ func TestFor(t *testing.T) {
expectPrinted(t, "for (var x = a + b in c);", "x = a + b;\nfor (var x in c)\n ;\n")

// Make sure "in" rules are disabled
expectPrinted(t, "for (var x = `${y in z}`;;);", "for (var x = `${y in z}`; ; )\n ;\n")
expectPrinted(t, "for (var {[x in y]: z} = {};;);", "for (var {[x in y]: z} = {}; ; )\n ;\n")
expectPrinted(t, "for (var {x = y in z} = {};;);", "for (var {x = y in z} = {}; ; )\n ;\n")
expectPrinted(t, "for (var [x = y in z] = {};;);", "for (var [x = y in z] = {}; ; )\n ;\n")
Expand Down

0 comments on commit ccd8950

Please sign in to comment.