From d4cfc679e55a2565acc1c6be2b392a6bf3cf3b6d Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 6 Mar 2023 22:42:52 -0800 Subject: [PATCH] give err msg for invalid edge style --- d2compiler/compile.go | 5 +++++ d2compiler/compile_test.go | 18 ++++++++++++++++++ .../TestCompile/edge_invalid_style.exp.json | 12 ++++++++++++ .../TestCompile/obj_invalid_style.exp.json | 12 ++++++++++++ 4 files changed, 47 insertions(+) create mode 100644 testdata/d2compiler/TestCompile/edge_invalid_style.exp.json create mode 100644 testdata/d2compiler/TestCompile/obj_invalid_style.exp.json diff --git a/d2compiler/compile.go b/d2compiler/compile.go index 1a4ea66bd0..6fa9106f3a 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 451e82a1ac..0444f00ae5 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 0000000000..ec84652774 --- /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 0000000000..4f9d5590bf --- /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" + } + ] + } +}