Skip to content

Commit 81e22aa

Browse files
committed
internal/ast: add Declarationish for Declaration or Raw
The name is kind of bad but it's ok for now.
1 parent bd904b3 commit 81e22aa

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

internal/ast/ast.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ type Block interface {
5353
type DeclarationBlock struct {
5454
Span
5555

56-
Declarations []*Declaration
56+
Declarations []Declarationish
57+
}
58+
59+
// Declarationish is a Declaration or a Raw value.
60+
type Declarationish interface {
61+
Node
62+
isDeclaration()
5763
}
5864

5965
// QualifiedRuleBlock is a block containing a set of rules.
@@ -82,3 +88,9 @@ type Declaration struct {
8288
// Important is whether or not the declaration was marked !important.
8389
Important bool
8490
}
91+
92+
func (Declaration) isDeclaration() {}
93+
func (Raw) isDeclaration() {}
94+
95+
var _ Declarationish = Declaration{}
96+
var _ Declarationish = Raw{}

internal/transformer/transformer.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -145,14 +145,20 @@ func (t *transformer) transformNodes(nodes []ast.Node) []ast.Node {
145145
return
146146
}
147147

148-
newDecls := make([]*ast.Declaration, 0, len(declBlock.Declarations))
148+
newDecls := make([]ast.Declarationish, 0, len(declBlock.Declarations))
149149
for _, decl := range declBlock.Declarations {
150-
if strings.HasPrefix(decl.Property, "--") && t.variables != nil {
151-
t.variables[decl.Property] = decl.Values
152-
continue
150+
switch d := decl.(type) {
151+
case *ast.Declaration:
152+
153+
if strings.HasPrefix(d.Property, "--") && t.variables != nil {
154+
t.variables[d.Property] = d.Values
155+
continue
156+
}
157+
newDecls = append(newDecls, d)
158+
default:
159+
newDecls = append(newDecls, d)
153160
}
154161

155-
newDecls = append(newDecls, decl)
156162
}
157163

158164
declBlock.Declarations = newDecls
@@ -375,11 +381,16 @@ func (t *transformer) transformBlock(block ast.Block) ast.Block {
375381
return block
376382
}
377383

378-
func (t *transformer) transformDeclarations(decls []*ast.Declaration) []*ast.Declaration {
379-
newDecls := make([]*ast.Declaration, 0, len(decls))
380-
for _, d := range decls {
381-
d.Values = t.transformValues(d.Values)
382-
newDecls = append(newDecls, d)
384+
func (t *transformer) transformDeclarations(decls []ast.Declarationish) []ast.Declarationish {
385+
newDecls := make([]ast.Declarationish, 0, len(decls))
386+
for _, decl := range decls {
387+
switch d := decl.(type) {
388+
case *ast.Declaration:
389+
d.Values = t.transformValues(d.Values)
390+
newDecls = append(newDecls, d)
391+
default:
392+
newDecls = append(newDecls, d)
393+
}
383394
}
384395

385396
return newDecls

0 commit comments

Comments
 (0)