Skip to content

Commit

Permalink
Merge pull request #2251 from alixander/validate-values
Browse files Browse the repository at this point in the history
d2compiler: validate that reserved keywords have values
  • Loading branch information
alixander authored Dec 15, 2024
2 parents f3d2120 + 9e26318 commit 1ea36d5
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions ci/release/changelogs/next.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- This is a breaking change. Previously Latex blocks required escaping the backslash. So
for older D2 versions, you should remove the excess backslashes.
- Links: non-http url scheme links are supported (e.g. `x.link: vscode://file/`) [#2237](https://github.com/terrastruct/d2/issues/2237)
- Compiler: reserved keywords with missing values error instead of silently doing nothing [#2251](https://github.com/terrastruct/d2/pull/2251)

#### Bugfixes ⛑️

Expand Down
6 changes: 2 additions & 4 deletions d2compiler/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,6 @@ func (c *compiler) compileMap(obj *d2graph.Object, m *d2ir.Map) {
}
}
}
} else {
c.errorf(class.LastRef().AST(), "class missing value")
}

for _, className := range classNames {
Expand Down Expand Up @@ -505,6 +503,8 @@ func (c *compiler) compileReserved(attrs *d2graph.Attributes, f *d2ir.Field) {
default:
c.errorf(f.LastPrimaryKey(), "reserved field %v does not accept composite", f.Name.ScalarString())
}
} else {
c.errorf(f.LastRef().AST(), `reserved field "%v" must have a value`, f.Name.ScalarString())
}
return
}
Expand Down Expand Up @@ -817,8 +817,6 @@ func (c *compiler) compileEdgeMap(edge *d2graph.Edge, m *d2ir.Map) {
}
}
}
} else {
c.errorf(class.LastRef().AST(), "class missing value")
}

for _, className := range classNames {
Expand Down
14 changes: 13 additions & 1 deletion d2compiler/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,18 @@ x: {
tassert.Equal(t, "200", g.Objects[0].Top.Value)
},
},
{
name: "reserved_missing_values",
text: `foobar: {
width
bottom
left
right
}
`,
expErr: `d2/testdata/d2compiler/TestCompile/reserved_missing_values.d2:2:3: reserved field "width" must have a value
d2/testdata/d2compiler/TestCompile/reserved_missing_values.d2:4:3: reserved field "left" must have a value`,
},
{
name: "positions_negative",
text: `hey: {
Expand Down Expand Up @@ -3066,7 +3078,7 @@ object: {
name: "no-class-primary",
text: `x.class
`,
expErr: `d2/testdata/d2compiler/TestCompile/no-class-primary.d2:1:3: class missing value`,
expErr: `d2/testdata/d2compiler/TestCompile/no-class-primary.d2:1:3: reserved field "class" must have a value`,
},
{
name: "no-class-inside-classes",
Expand Down
2 changes: 1 addition & 1 deletion testdata/d2compiler/TestCompile/no-class-primary.exp.json

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

15 changes: 15 additions & 0 deletions testdata/d2compiler/TestCompile/reserved_missing_values.exp.json

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

0 comments on commit 1ea36d5

Please sign in to comment.