Merge pull request #1364 from berniexie/no-empty-block-strings

compile: no empty block strings
This commit is contained in:
Bernard Xie 2023-06-02 11:30:08 -07:00 committed by GitHub
commit 69b816c1b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 70 additions and 21 deletions

View file

@ -18,3 +18,4 @@
- Fixes panic using reserved keywords as containers [#1358](https://github.com/terrastruct/d2/pull/1358)
- When multiple classes are applied changing different attributes of arrowheads, they are
all applied instead of only the last one [#1362](https://github.com/terrastruct/d2/pull/1362)
- Prevent empty block strings [#1364](https://github.com/terrastruct/d2/pull/1364)

View file

@ -270,6 +270,9 @@ func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) {
// TODO: Delete instead.
attrs.Label.Value = scalar.ScalarString()
case *d2ast.BlockString:
if strings.TrimSpace(scalar.ScalarString()) == "" {
c.errorf(f.LastPrimaryKey(), "block string cannot be empty")
}
attrs.Language = scalar.Tag
fullTag, ok := ShortToFullLanguageAliases[scalar.Tag]
if ok {

View file

@ -610,6 +610,24 @@ x: {
expErr: `d2/testdata/d2compiler/TestCompile/md_block_string_err.d2:4:19: unexpected text after md block string. See https://d2lang.com/tour/text#advanced-block-strings.
d2/testdata/d2compiler/TestCompile/md_block_string_err.d2:5:1: block string must be terminated with |`,
},
{
name: "no_empty_block_string",
text: `Text: |md |`,
expErr: `d2/testdata/d2compiler/TestCompile/no_empty_block_string.d2:1:1: block string cannot be empty`,
},
{
name: "no_white_spaces_only_block_string",
text: `Text: |md |`,
expErr: `d2/testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.d2:1:1: block string cannot be empty`,
},
{
name: "no_new_lines_only_block_string",
text: `Text: |md
|`,
expErr: `d2/testdata/d2compiler/TestCompile/no_new_lines_only_block_string.d2:1:1: block string cannot be empty`,
},
{
name: "underscore_edge_existing",

View file

@ -26,14 +26,6 @@ func testMeasured(t *testing.T) {
name: "empty-sql_table",
mtexts: []*d2target.MText{},
script: `a: "" { shape: sql_table }
`,
},
{
name: "empty-markdown",
mtexts: []*d2target.MText{},
script: `a: |md
` + " " + `
|
`,
},
}

View file

@ -929,19 +929,6 @@ cf many required: {
style.stroke-width: 8
}
}
`,
},
{
name: "empty_md_measurement",
script: `
a
b: |md
|
c
d
a -> b -> c
`,
},
loadFromFile(t, "slow_grid"),

View file

@ -0,0 +1,12 @@
{
"graph": null,
"err": {
"ioerr": null,
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/no_empty_block_string.d2,0:0:0-0:11:11",
"errmsg": "d2/testdata/d2compiler/TestCompile/no_empty_block_string.d2:1:1: block string cannot be empty"
}
]
}
}

View file

@ -0,0 +1,12 @@
{
"graph": null,
"err": {
"ioerr": null,
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/no_empty_block_strings.d2,0:0:0-0:11:11",
"errmsg": "d2/testdata/d2compiler/TestCompile/no_empty_block_strings.d2:1:1: block string cannot be empty"
}
]
}
}

View file

@ -0,0 +1,12 @@
{
"graph": null,
"err": {
"ioerr": null,
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/no_new_lines_only_block_string.d2,0:0:0-3:1:13",
"errmsg": "d2/testdata/d2compiler/TestCompile/no_new_lines_only_block_string.d2:1:1: block string cannot be empty"
}
]
}
}

View file

@ -0,0 +1,12 @@
{
"graph": null,
"err": {
"ioerr": null,
"errs": [
{
"range": "d2/testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.d2,0:0:0-0:16:16",
"errmsg": "d2/testdata/d2compiler/TestCompile/no_white_spaces_only_block_string.d2:1:1: block string cannot be empty"
}
]
}
}