Skip to content

Commit b23dd4d

Browse files
authored
Fix unmatched closing brace in template literal (#460)
* fix(#424): handle unmatched closing brace in template literals * chore: add changeset Co-authored-by: Nate Moore <[email protected]>
1 parent 02add77 commit b23dd4d

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

.changeset/proud-cheetahs-sort.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@astrojs/compiler': patch
3+
---
4+
5+
Fix handling of unmatched close brace in template literals

internal/token.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,9 +1309,12 @@ func (z *Tokenizer) readTagAttrExpression() {
13091309
z.attrTemplateLiteralStack = append(z.attrTemplateLiteralStack, 0)
13101310
}
13111311
case '}':
1312-
z.attrExpressionStack--
1313-
if z.attrExpressionStack == 0 && z.allTagAttrExpressionsClosed() {
1314-
return
1312+
inTemplateLiteral := len(z.attrTemplateLiteralStack) >= z.attrExpressionStack && z.attrTemplateLiteralStack[z.attrExpressionStack-1] > 0
1313+
if !inTemplateLiteral {
1314+
z.attrExpressionStack--
1315+
if z.attrExpressionStack == 0 && z.allTagAttrExpressionsClosed() {
1316+
return
1317+
}
13151318
}
13161319
}
13171320
}

internal/token_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -376,10 +376,20 @@ func TestBasic(t *testing.T) {
376376
[]TokenType{StartTagToken, StartExpressionToken, TextToken, StartTagToken, StartExpressionToken, TextToken, EndExpressionToken, EndTagToken, TextToken, EndExpressionToken, EndTagToken, StartTagToken, TextToken, EndTagToken},
377377
},
378378
{
379-
"single brace",
379+
"single open brace",
380380
"<main id={`{`}></main>",
381381
[]TokenType{StartTagToken, EndTagToken},
382382
},
383+
{
384+
"single close brace",
385+
"<main id={`}`}></main>",
386+
[]TokenType{StartTagToken, EndTagToken},
387+
},
388+
{
389+
"extra close brace",
390+
"<main id={`${}}`}></main>",
391+
[]TokenType{StartTagToken, EndTagToken},
392+
},
383393
{
384394
"Markdown codeblock",
385395
fmt.Sprintf(`<Markdown>

0 commit comments

Comments
 (0)