Skip to content

Commit

Permalink
Allow top-level conditional expressions (#31)
Browse files Browse the repository at this point in the history
* Allow top-level conditional expressions

* Adding a changeset

* Remove unused frag option
  • Loading branch information
matthewp authored Oct 6, 2021
1 parent 5e577ae commit c9407cd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 16 deletions.
5 changes: 5 additions & 0 deletions .changeset/brown-windows-fold.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@astrojs/compiler": patch
---

Fix for using conditionals at the top-level
29 changes: 25 additions & 4 deletions internal/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,11 +732,10 @@ func beforeHeadIM(p *parser) bool {
// Ignore the token.
return true
case StartExpressionToken:
p.parseImpliedToken(StartTagToken, a.Head, a.Head.String())
p.addExpression()
return true
case EndExpressionToken:
p.addLoc()
p.oe.pop()
p.setOriginalIM()
p.im = expressionIM
return true
}
p.parseImpliedToken(StartTagToken, a.Head, a.Head.String())
Expand Down Expand Up @@ -2358,6 +2357,28 @@ func afterAfterFramesetIM(p *parser) bool {
return true
}

func expressionIM(p *parser) bool {
switch p.tok.Type {
case ErrorToken:
p.oe.pop()
case TextToken:
return textIM(p)
case StartTagToken:
return inBodyIM(p)
case EndTagToken:
return inBodyIM(p)
case EndExpressionToken:
p.addLoc()
p.oe.pop()
p.im = p.originalIM
p.originalIM = nil
return true
}
p.im = p.originalIM
p.originalIM = nil
return p.tok.Type == EndTagToken
}

func ignoreTheRemainingTokens(p *parser) bool {
return true
}
Expand Down
49 changes: 37 additions & 12 deletions internal/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,23 @@ var STYLE_SUFFIX = "];\n$$result.styles.add(...STYLES)\n"
var SCRIPT_PRELUDE = "const SCRIPTS = [\n"
var SCRIPT_SUFFIX = "];\n$$result.scripts.add(...SCRIPTS)\n"

type want struct {
imports string
frontmatter []string
styles []string
code string
scripts []string
}

type testcase struct {
name string
source string
only bool
want want
}

func TestPrinter(t *testing.T) {
type want struct {
imports string
frontmatter []string
styles []string
code string
scripts []string
}
tests := []struct {
name string
source string
want want
}{
tests := []testcase{
{
name: "basic (no frontmatter)",
source: `<button>Click</button>`,
Expand Down Expand Up @@ -493,16 +497,37 @@ import Widget2 from '../components/Widget2.astro';`},
code: `${$$renderComponent($$result,'Component',Component,{},{[name]: () => $$render` + "`" + `<div>Named</div>` + "`" + `,})}`,
},
},
{
name: "condition expressions at the top-level",
source: `{cond && <span></span>}`,
want: want{
imports: "",
frontmatter: []string{},
styles: []string{},
code: "<html><head>${cond && $$render`<span></span>`}</head><body></body></html>",
},
},
}

for _, tt := range tests {
if tt.only {
tests = make([]testcase, 0)
tests = append(tests, tt)
break
}
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// transform output from source
code := test_utils.Dedent(tt.source)

doc, err := tycho.Parse(strings.NewReader(code))

if err != nil {
t.Error(err)
}

hash := tycho.HashFromSource(code)
transform.Transform(doc, transform.TransformOptions{Scope: hash}) // note: we want to test Transform in context here, but more advanced cases could be tested separately
result := PrintToJS(code, doc, transform.TransformOptions{
Expand Down

0 comments on commit c9407cd

Please sign in to comment.