Skip to content

Commit

Permalink
Fix false negatives for hyphen in yml/plain-scalar rule (#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi committed Feb 3, 2021
1 parent 99b6b55 commit 08daf98
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/rules/plain-scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const SYMBOLS = new Set([
"{",
"}",
// sequence
"-",
// "-",
"[",
"]",
",",
Expand Down Expand Up @@ -62,11 +62,16 @@ export default createRule("plain-scalar", {
function canToPlain(
node: AST.YAMLDoubleQuotedScalar | AST.YAMLSingleQuotedScalar,
) {
for (const char of node.value) {
for (let index = 0; index < node.value.length; index++) {
const char = node.value[index]
if (SYMBOLS.has(char)) {
return false
}
}
if (/^\s*-\s+/u.test(node.value)) {
// “-” indicator
return false
}
const parent =
node.parent.type === "YAMLWithMeta"
? node.parent.parent
Expand Down
22 changes: 22 additions & 0 deletions tests/fixtures/rules/plain-scalar/invalid/hyphen-test-errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"message": "Must use plain style scalar.",
"line": 2,
"column": 1
},
{
"message": "Must use plain style scalar.",
"line": 4,
"column": 1
},
{
"message": "Must use plain style scalar.",
"line": 5,
"column": 1
},
{
"message": "Must use plain style scalar.",
"line": 6,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# {}
"-a": b
"- a": b
"-- a": b
"a-a": b
"a - a": b
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# plain-scalar/invalid/hyphen-test-input.yml
-a: b
"- a": b
-- a: b
a-a: b
a - a: b

0 comments on commit 08daf98

Please sign in to comment.