Skip to content

Commit

Permalink
fix class
Browse files Browse the repository at this point in the history
style validation

changelog
  • Loading branch information
alixander committed Apr 27, 2023
1 parent 5714e34 commit 595fafc
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- ELK self loops get distributed around the object instead of stacking [#1232](https://github.com/terrastruct/d2/pull/1232)
- ELK preserves order of objects in cycles [#1235](https://github.com/terrastruct/d2/pull/1235)
- Improper usages of `class` and `style` get error messages [#1254](https://github.com/terrastruct/d2/pull/1254)

#### Bugfixes ⛑️

Expand Down
5 changes: 5 additions & 0 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
return
} else if f.Name == "style" {
if f.Map() == nil {
c.errorf(f.LastRef().AST(), `"style" expected to be set to a map, or contain an additional keyword like "style.opacity: 0.4"`)
return
}
c.compileStyle(&obj.Attributes, f.Map())
Expand Down Expand Up @@ -481,6 +482,10 @@ func (c *compiler) compileStyle(attrs *d2graph.Attributes, m *d2ir.Map) {
}

func (c *compiler) compileStyleField(attrs *d2graph.Attributes, f *d2ir.Field) {
if _, ok := d2graph.StyleKeywords[f.Name]; !ok {
c.errorf(f.LastRef().AST(), `invalid style keyword: "%s"`, f.Name)
return
}
if f.Primary() == nil {
return
}
Expand Down
18 changes: 18 additions & 0 deletions d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1678,6 +1678,24 @@ x.a.b`,
}`,
expErr: `d2/testdata/d2compiler/TestCompile/no-nested-columns-class.d2:3:5: class fields cannot have children`,
},
{
name: "improper-class-ref",

text: `myobj.class.style.stroke-dash: 3`,
expErr: `d2/testdata/d2compiler/TestCompile/improper-class-ref.d2:1:7: "class" must be the last part of the key`,
},
{
name: "tail-style",

text: `myobj.style: 3`,
expErr: `d2/testdata/d2compiler/TestCompile/tail-style.d2:1:7: "style" expected to be set to a map, or contain an additional keyword like "style.opacity: 0.4"`,
},
{
name: "bad-style-nesting",

text: `myobj.style.style.stroke-dash: 3`,
expErr: `d2/testdata/d2compiler/TestCompile/bad-style-nesting.d2:1:13: invalid style keyword: "style"`,
},
{
name: "edge_to_style",

Expand Down
4 changes: 4 additions & 0 deletions d2ir/d2ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,10 @@ func (m *Map) ensureField(i int, kp *d2ast.KeyPath, refctx *RefContext) (*Field,
head = strings.ToLower(head)
}

if head == "class" && i < len(kp.Path)-1 {
return nil, d2parser.Errorf(kp.Path[i].Unbox(), `"class" must be the last part of the key`)
}

if head == "_" {
return nil, d2parser.Errorf(kp.Path[i].Unbox(), `parent "_" can only be used in the beginning of paths, e.g. "_.x"`)
}
Expand Down
12 changes: 12 additions & 0 deletions testdata/d2compiler/TestCompile/bad-style-nesting.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions testdata/d2compiler/TestCompile/improper-class-ref.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions testdata/d2compiler/TestCompile/tail-style.exp.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 595fafc

Please sign in to comment.