Skip to content

Commit 9457a91

Browse files
Fix(tokenizer): reckon left bracket which previous char is not $ as raw left bracket in template literal in attribute (#419)
* Fix(tokenizer): reckon left bracket which previous char is not $ as raw left bracket in template literal * Chore(tokenizer): write test about template literal * chore: move template literal test to token_test * chore: add changeset Co-authored-by: Nate Moore <[email protected]>
1 parent c792161 commit 9457a91

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

.changeset/tall-elephants-worry.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 issue with `{` in template literal attributes

internal/token.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,8 +1302,12 @@ func (z *Tokenizer) readTagAttrExpression() {
13021302
z.raw.End = z.data.End
13031303
z.data.End = end
13041304
case '{':
1305-
z.attrExpressionStack++
1306-
z.attrTemplateLiteralStack = append(z.attrTemplateLiteralStack, 0)
1305+
previousChar := z.buf[z.raw.End-2]
1306+
inTemplateLiteral := len(z.attrTemplateLiteralStack) >= z.attrExpressionStack && z.attrTemplateLiteralStack[z.attrExpressionStack-1] > 0
1307+
if !inTemplateLiteral || previousChar == '$' {
1308+
z.attrExpressionStack++
1309+
z.attrTemplateLiteralStack = append(z.attrTemplateLiteralStack, 0)
1310+
}
13071311
case '}':
13081312
z.attrExpressionStack--
13091313
if z.attrExpressionStack == 0 && z.allTagAttrExpressionsClosed() {

internal/token_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,11 @@ func TestBasic(t *testing.T) {
375375
`<select>{[1, 2, 3].map(num => <option>{num}</option>)}</select><div>Hello</div>`,
376376
[]TokenType{StartTagToken, StartExpressionToken, TextToken, StartTagToken, StartExpressionToken, TextToken, EndExpressionToken, EndTagToken, TextToken, EndExpressionToken, EndTagToken, StartTagToken, TextToken, EndTagToken},
377377
},
378+
{
379+
"single brace",
380+
"<main id={`{`}></main>",
381+
[]TokenType{StartTagToken, EndTagToken},
382+
},
378383
{
379384
"Markdown codeblock",
380385
fmt.Sprintf(`<Markdown>

0 commit comments

Comments
 (0)