Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert #448 #454

Merged
merged 2 commits into from
Jul 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/serious-mayflies-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@astrojs/compiler': patch
---

Revert [#448](https://github.com/withastro/compiler/pull/448) for now
29 changes: 0 additions & 29 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,35 +648,6 @@ const groups = [[0, 1, 2], [3, 4, 5]];
code: `${$$maybeRenderHead($$result)}<article>${(previous || next) && $$render` + BACKTICK + `<aside>${previous && $$render` + BACKTICK + `<div>Previous Article: <a rel="prev"${$$addAttribute(new URL(previous.link, Astro.site).pathname, "href")}>${previous.text}</a></div>` + BACKTICK + `}${next && $$render` + BACKTICK + `<div>Next Article: <a rel="next"${$$addAttribute(new URL(next.link, Astro.site).pathname, "href")}>${next.text}</a></div>` + BACKTICK + `}</aside>` + BACKTICK + `}</article>`,
},
},
{
name: "nested expressions II",
source: `<article>{(previous || next) && <aside>{previous && <div>Previous Article: <a rel="prev" href={new URL(previous.link, Astro.site).pathname}>{previous.text}</a></div>} {next && <div>Next Article: <a rel="next" href={new URL(next.link, Astro.site).pathname}>{next.text}</a></div>}</aside>}</article>`,
want: want{
code: `${$$maybeRenderHead($$result)}<article>${(previous || next) && $$render` + BACKTICK + `<aside>${previous && $$render` + BACKTICK + `<div>Previous Article: <a rel="prev"${$$addAttribute(new URL(previous.link, Astro.site).pathname, "href")}>${previous.text}</a></div>` + BACKTICK + `} ${next && $$render` + BACKTICK + `<div>Next Article: <a rel="next"${$$addAttribute(new URL(next.link, Astro.site).pathname, "href")}>${next.text}</a></div>` + BACKTICK + `}</aside>` + BACKTICK + `}</article>`,
},
},
{
name: "nested expressions III",
source: `<div>{x.map((x) => x ? <div>{true ? <span>{x}</span> : null}</div> : <div>{false ? null : <span>{x}</span>}</div>)}</div>`,
want: want{
code: "${$$maybeRenderHead($$result)}<div>${x.map((x) => x ? $$render`<div>${true ? $$render`<span>${x}</span>` : null}</div>` : $$render`<div>${false ? null : $$render`<span>${x}</span>`}</div>`)}</div>",
},
},
{
name: "nested expressions IV",
source: `<div>{() => { if (value > 0.25) { return <span>Default</span> } else if (value > 0.5) { return <span>Another</span> } else if (value > 0.75) { return <span>Other</span> } return <span>Yet Other</span> }}</div>`,
want: want{
code: "${$$maybeRenderHead($$result)}<div>${() => { if (value > 0.25) { return $$render`<span>Default</span>`} else if (value > 0.5) { return $$render`<span>Another</span>`} else if (value > 0.75) { return $$render`<span>Other</span>`} return $$render`<span>Yet Other</span>`}}</div>",
},
},
{
name: "nested expressions V",
source: "{rows.map(row => row.map(icon => <li class={`icon-${icon}`}><Icon title={icon.replace(/\\-grid$/, '')} name={`logos/${icon}`} height=\"64\" /></li>))}",
want: want{
code: "${rows.map(row => row.map(icon => $$render`${$$maybeRenderHead($$result)}<li${$$addAttribute(`icon-${icon}`, \"class\")}>${$$renderComponent($$result,'Icon',Icon,{\"title\":(icon.replace(/\\-grid$/, '')),\"name\":(`logos/${icon}`),\"height\":\"64\"})}</li>`))}",
},
},

{
name: "expressions with JS comments",
source: `---
Expand Down
37 changes: 4 additions & 33 deletions internal/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,6 @@ type Tokenizer struct {
// tt is the TokenType of the current token.
tt TokenType
prevTokenType TokenType
prevTokens []Token
fm FrontmatterState
m MarkdownState
// err is the first error encountered during tokenization. It is possible
Expand Down Expand Up @@ -1332,51 +1331,23 @@ func (z *Tokenizer) isAtExpressionBoundary() bool {
return true
}

func (z *Tokenizer) trackPreviousTokens() {
// Reset stack on expression boundaries
if z.tt == StartExpressionToken || z.tt == EndExpressionToken {
z.prevTokens = make([]Token, 0)
}
if z.tt == StartTagToken {
z.prevTokens = append(z.prevTokens, z.Token())
} else if z.tt == EndTagToken {
if len(z.prevTokens) > 0 {
// This is a very simple stack that pops matching closing elements off the stack,
// which is good enough for our purposes.
// We only use this to track when `{` should be `StartExpressionToken` or `TextToken`
for i := 1; i < len(z.prevTokens)+1; i++ {
tok := z.prevTokens[len(z.prevTokens)-i]
if tok.Data == string(z.buf[z.data.Start:z.data.End]) {
if len(z.prevTokens) == 1 {
z.prevTokens = make([]Token, 0)
} else {
z.prevTokens = z.prevTokens[0:i]
z.prevTokens = append(z.prevTokens, z.prevTokens[i:]...)
}
}
}
}
}
}

// Next scans the next token and returns its type.
func (z *Tokenizer) Next() TokenType {
z.trackPreviousTokens()
z.raw.Start = z.raw.End
z.data.Start = z.raw.End
z.data.End = z.raw.End
z.prevTokenType = z.tt

// Properly handle multiple nested expressions
if len(z.expressionStack) > 0 && len(z.prevTokens) == 0 {
// This handles expressions nested inside of Frontmatter elements
// but preserves `{}` as text outside of elements
if z.fm == FrontmatterOpen {
tt := z.Token().Type
switch tt {
case StartTagToken, EndExpressionToken, TextToken:
case StartTagToken, EndTagToken:
default:
z.openBraceIsExpressionStart = false
}
}

if z.rawTag != "" {
if z.rawTag == "plaintext" {
// Read everything up to EOF.
Expand Down
14 changes: 0 additions & 14 deletions internal/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,20 +198,6 @@ func TestBasic(t *testing.T) {
}}</div>`,
[]TokenType{StartTagToken, StartExpressionToken, TextToken, TextToken, TextToken, TextToken, TextToken, TextToken, TextToken, TextToken, TextToken, TextToken, TextToken, TextToken, TextToken, TextToken, TextToken, TextToken, EndExpressionToken, EndTagToken},
},
{
"expression with multiple elements",
`<div>{() => {
if (value > 0.25) {
return <span>Default</span>
} else if (value > 0.5) {
return <span>Another</span>
} else if (value > 0.75) {
return <span>Other</span>
}
return <span>Yet Other</span>
}}</div>`,
[]TokenType{StartTagToken, StartExpressionToken, TextToken, TextToken, TextToken, TextToken, TextToken, StartTagToken, TextToken, EndTagToken, TextToken, TextToken, TextToken, TextToken, StartTagToken, TextToken, EndTagToken, TextToken, TextToken, TextToken, TextToken, StartTagToken, TextToken, EndTagToken, TextToken, TextToken, StartTagToken, TextToken, EndTagToken, TextToken, TextToken, EndExpressionToken, EndTagToken},
},
{
"attribute expression with quoted braces",
`<div value={"{"} />`,
Expand Down