From 6e362300f17645310bb4ce8b73152a611fdb3751 Mon Sep 17 00:00:00 2001 From: Adrian Hesketh Date: Tue, 14 Nov 2023 09:06:18 +0000 Subject: [PATCH] refactor: rename Go expressions at template level --- .version | 2 +- generator/generator.go | 4 ++-- generator/generator_test.go | 2 +- parser/v2/templatefile.go | 6 +++--- parser/v2/templatefile_test.go | 8 ++++---- parser/v2/types.go | 10 +++++----- 6 files changed, 16 insertions(+), 16 deletions(-) diff --git a/.version b/.version index ddf504a1a..e482fe7a5 100644 --- a/.version +++ b/.version @@ -1 +1 @@ -0.2.472 \ No newline at end of file +0.2.473 \ No newline at end of file diff --git a/generator/generator.go b/generator/generator.go index faf7acfbc..b703a6a49 100644 --- a/generator/generator.go +++ b/generator/generator.go @@ -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 } @@ -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 diff --git a/generator/generator_test.go b/generator/generator_test.go index c2e85874d..562a2e827 100644 --- a/generator/generator_test.go +++ b/generator/generator_test.go @@ -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", }, diff --git a/parser/v2/templatefile.go b/parser/v2/templatefile.go index d6afd2000..090b407a4 100644 --- a/parser/v2/templatefile.go +++ b/parser/v2/templatefile.go @@ -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. @@ -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 @@ -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 diff --git a/parser/v2/templatefile_test.go b/parser/v2/templatefile_test.go index a21d8d5f0..51f61e409 100644 --- a/parser/v2/templatefile_test.go +++ b/parser/v2/templatefile_test.go @@ -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]) } @@ -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]) } diff --git a/parser/v2/types.go b/parser/v2/types.go index 53f4211ec..dbe92e1aa 100644 --- a/parser/v2/types.go +++ b/parser/v2/types.go @@ -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. @@ -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)