diff --git a/ci/release/changelogs/next.md b/ci/release/changelogs/next.md index 0d8e40dfa..d3ac356a3 100644 --- a/ci/release/changelogs/next.md +++ b/ci/release/changelogs/next.md @@ -3,6 +3,7 @@ #### Improvements 🧹 - ELK nodes with > 1 connection grow to ensure padding around ports [#981](https://github.com/terrastruct/d2/pull/981) +- Using a style keyword incorrectly in connections returns clear error message [#989](https://github.com/terrastruct/d2/pull/989) #### Bugfixes ⛑️ diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 1a4ea66bd..6fa9106f3 100644 --- a/d2compiler/compile.go +++ b/d2compiler/compile.go @@ -450,6 +450,11 @@ func (c *compiler) compileEdge(obj *d2graph.Object, e *d2ir.Edge) { func (c *compiler) compileEdgeField(edge *d2graph.Edge, f *d2ir.Field) { keyword := strings.ToLower(f.Name) + _, isStyleReserved := d2graph.StyleKeywords[keyword] + if isStyleReserved { + c.errorf(f.LastRef().AST(), "%v must be style.%v", f.Name, f.Name) + return + } _, isReserved := d2graph.SimpleReservedKeywords[keyword] if isReserved { c.compileReserved(edge.Attributes, f) diff --git a/d2compiler/compile_test.go b/d2compiler/compile_test.go index 451e82a1a..0444f00ae 100644 --- a/d2compiler/compile_test.go +++ b/d2compiler/compile_test.go @@ -1117,6 +1117,24 @@ x: { `, expErr: `d2/testdata/d2compiler/TestCompile/shape_edge_style.d2:3:2: key "animated" can only be applied to edges`, }, + { + name: "edge_invalid_style", + + text: `x -> y: { + opacity: 0.5 +} +`, + expErr: `d2/testdata/d2compiler/TestCompile/edge_invalid_style.d2:2:3: opacity must be style.opacity`, + }, + { + name: "obj_invalid_style", + + text: `x: { + opacity: 0.5 +} +`, + expErr: `d2/testdata/d2compiler/TestCompile/obj_invalid_style.d2:2:3: opacity must be style.opacity`, + }, { name: "edge_chain_map", diff --git a/testdata/d2compiler/TestCompile/edge_invalid_style.exp.json b/testdata/d2compiler/TestCompile/edge_invalid_style.exp.json new file mode 100644 index 000000000..ec8465277 --- /dev/null +++ b/testdata/d2compiler/TestCompile/edge_invalid_style.exp.json @@ -0,0 +1,12 @@ +{ + "graph": null, + "err": { + "ioerr": null, + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/edge_invalid_style.d2,1:2:12-1:9:19", + "errmsg": "d2/testdata/d2compiler/TestCompile/edge_invalid_style.d2:2:3: opacity must be style.opacity" + } + ] + } +} diff --git a/testdata/d2compiler/TestCompile/obj_invalid_style.exp.json b/testdata/d2compiler/TestCompile/obj_invalid_style.exp.json new file mode 100644 index 000000000..4f9d5590b --- /dev/null +++ b/testdata/d2compiler/TestCompile/obj_invalid_style.exp.json @@ -0,0 +1,12 @@ +{ + "graph": null, + "err": { + "ioerr": null, + "errs": [ + { + "range": "d2/testdata/d2compiler/TestCompile/obj_invalid_style.d2,1:2:7-1:9:14", + "errmsg": "d2/testdata/d2compiler/TestCompile/obj_invalid_style.d2:2:3: opacity must be style.opacity" + } + ] + } +}