Skip to content

Commit

Permalink
refactor: rename Go expressions at template level
Browse files Browse the repository at this point in the history
  • Loading branch information
a-h committed Nov 14, 2023
1 parent f0dc112 commit 6e36230
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion .version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.472
0.2.473
4 changes: 2 additions & 2 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ func (g *generator) writeImports() error {
func (g *generator) writeTemplateNodes() error {
for i := 0; i < len(g.tf.Nodes); i++ {
switch n := g.tf.Nodes[i].(type) {
case parser.GoExpression:
case parser.TemplateFileGoExpression:
if err := g.writeGoExpression(n); err != nil {
return err
}
Expand Down Expand Up @@ -270,7 +270,7 @@ func (g *generator) writeCSS(n parser.CSSTemplate) error {
return nil
}

func (g *generator) writeGoExpression(n parser.GoExpression) (err error) {
func (g *generator) writeGoExpression(n parser.TemplateFileGoExpression) (err error) {
r, err := g.w.Write(n.Expression.Value)
if err != nil {
return err
Expand Down
2 changes: 1 addition & 1 deletion generator/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestGeneratorSourceMap(t *testing.T) {
w: NewRangeWriter(w),
sourceMap: parser.NewSourceMap(),
}
exp := parser.GoExpression{
exp := parser.TemplateFileGoExpression{
Expression: parser.Expression{
Value: "line1\nline2",
},
Expand Down
6 changes: 3 additions & 3 deletions parser/v2/templatefile.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func (p TemplateFileParser) Parse(pi *parse.Input) (tf TemplateFile, ok bool, er
}
var newLine string
newLine, _, _ = parse.NewLine.Parse(pi)
tf.Header = append(tf.Header, GoExpression{Expression: NewExpression(line+newLine, from, pi.Position())})
tf.Header = append(tf.Header, TemplateFileGoExpression{Expression: NewExpression(line+newLine, from, pi.Position())})
}

// Strip any whitespace between the template declaration and the first template.
Expand Down Expand Up @@ -165,7 +165,7 @@ outer:
// Take the code so far.
if code.Len() > 0 {
expr := NewExpression(strings.TrimSpace(code.String()), from, pi.Position())
tf.Nodes = append(tf.Nodes, GoExpression{Expression: expr})
tf.Nodes = append(tf.Nodes, TemplateFileGoExpression{Expression: expr})
}
// Carry on parsing.
break inner
Expand All @@ -181,7 +181,7 @@ outer:
if _, isEOF, _ := parse.EOF[string]().Parse(pi); isEOF {
if code.Len() > 0 {
expr := NewExpression(strings.TrimSpace(code.String()), from, pi.Position())
tf.Nodes = append(tf.Nodes, GoExpression{Expression: expr})
tf.Nodes = append(tf.Nodes, TemplateFileGoExpression{Expression: expr})
}
// Stop parsing.
break outer
Expand Down
8 changes: 4 additions & 4 deletions parser/v2/templatefile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ const y = "456"
}
t.Fatalf("expected 3 nodes, got %d nodes, %v", len(tf.Nodes), nodeTypes)
}
expr, isGoExpression := tf.Nodes[0].(GoExpression)
expr, isGoExpression := tf.Nodes[0].(TemplateFileGoExpression)
if !isGoExpression {
t.Errorf("0: expected expression, got %t", tf.Nodes[2])
}
if expr.Expression.Value != `const x = "123"` {
t.Errorf("0: unexpected expression: %q", expr.Expression.Value)
}
expr, isGoExpression = tf.Nodes[2].(GoExpression)
expr, isGoExpression = tf.Nodes[2].(TemplateFileGoExpression)
if !isGoExpression {
t.Errorf("2: expected expression, got %t", tf.Nodes[2])
}
Expand Down Expand Up @@ -110,14 +110,14 @@ const y = ` + "`456`"
}
t.Fatalf("expected 3 nodes, got %d nodes, %v", len(tf.Nodes), nodeTypes)
}
expr, isGoExpression := tf.Nodes[0].(GoExpression)
expr, isGoExpression := tf.Nodes[0].(TemplateFileGoExpression)
if !isGoExpression {
t.Errorf("0: expected expression, got %t", tf.Nodes[2])
}
if expr.Expression.Value != `const x = "123"` {
t.Errorf("0: unexpected expression: %q", expr.Expression.Value)
}
expr, isGoExpression = tf.Nodes[2].(GoExpression)
expr, isGoExpression = tf.Nodes[2].(TemplateFileGoExpression)
if !isGoExpression {
t.Errorf("2: expected expression, got %t", tf.Nodes[2])
}
Expand Down
10 changes: 5 additions & 5 deletions parser/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ type Expression struct {

type TemplateFile struct {
// Header contains comments or whitespace at the top of the file.
Header []GoExpression
Header []TemplateFileGoExpression
// Package expression.
Package Package
// Nodes in the file.
Expand Down Expand Up @@ -144,13 +144,13 @@ type TemplateFileNode interface {
Write(w io.Writer, indent int) error
}

// GoExpression within a TemplateFile
type GoExpression struct {
// TemplateFileGoExpression within a TemplateFile
type TemplateFileGoExpression struct {
Expression Expression
}

func (exp GoExpression) IsTemplateFileNode() bool { return true }
func (exp GoExpression) Write(w io.Writer, indent int) error {
func (exp TemplateFileGoExpression) IsTemplateFileNode() bool { return true }
func (exp TemplateFileGoExpression) Write(w io.Writer, indent int) error {
data, err := format.Source([]byte(exp.Expression.Value))
if err != nil {
return writeIndent(w, indent, exp.Expression.Value)
Expand Down

0 comments on commit 6e36230

Please sign in to comment.