Skip to content

Commit

Permalink
Fix false negatives for exclamation and question mark in yml/plain-sc…
Browse files Browse the repository at this point in the history
…alar rule
  • Loading branch information
ota-meshi committed Feb 3, 2021
1 parent 08daf98 commit 3ed01c6
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 8 deletions.
35 changes: 27 additions & 8 deletions src/rules/plain-scalar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { createRule } from "../utils"

const SYMBOLS = new Set([
// mapping
"?",
// "?",
":",
"{",
"}",
Expand All @@ -22,9 +22,9 @@ const SYMBOLS = new Set([
"|",
"+",
// tags
"!",
"<",
">",
// "!",
// "<",
// ">",
// directives
"%",
// quoted
Expand Down Expand Up @@ -56,21 +56,40 @@ export default createRule("plain-scalar", {

const sourceCode = context.getSourceCode()

/* eslint-disable complexity -- X( */
/**
* Check if it can be converted to plain.
*/
function canToPlain(
/* eslint-enable complexity -- X( */
node: AST.YAMLDoubleQuotedScalar | AST.YAMLSingleQuotedScalar,
) {
if (node.value !== node.value.trim()) {
return false
}
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
if (index === 0) {
if (char === "-" || char === "?") {
const next = node.value[index + 1]
if (next && !next.trim()) {
// "-" indicator or "?" indicator
return false
}
} else if (char === "!") {
const next = node.value[index + 1]
if (
next &&
(!next.trim() || next === "!" || next === "<")
) {
// "!" indicator
return false
}
}
}
}
const parent =
node.parent.type === "YAMLWithMeta"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[
{
"message": "Must use plain style scalar.",
"line": 2,
"column": 1
},
{
"message": "Must use plain style scalar.",
"line": 2,
"column": 8
},
{
"message": "Must use plain style scalar.",
"line": 3,
"column": 1
},
{
"message": "Must use plain style scalar.",
"line": 3,
"column": 9
},
{
"message": "Must use plain style scalar.",
"line": 4,
"column": 1
},
{
"message": "Must use plain style scalar.",
"line": 4,
"column": 10
},
{
"message": "Must use plain style scalar.",
"line": 5,
"column": 1
},
{
"message": "Must use plain style scalar.",
"line": 5,
"column": 10
},
{
"message": "Must use plain style scalar.",
"line": 6,
"column": 1
},
{
"message": "Must use plain style scalar.",
"line": 6,
"column": 11
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# {}
"a!a": "b !b"
"a!!a": "b !!b"
"a!! a": "b !! b"
"a!<a>": "b !<b>"
"a! <a>": "b ! <b>"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# plain-scalar/invalid/exclamation-test-input.yml
a!a: b !b
a!!a: b !!b
a!! a: b !! b
a!<a>: b !<b>
a! <a>: b ! <b>
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/question-test-input.yml
?a: b
"? a": b
?? a: b
a?a: b
a ? a: b
42 changes: 42 additions & 0 deletions tests/fixtures/rules/plain-scalar/invalid/test-errors.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[
{
"message": "Must use plain style scalar.",
"line": 3,
"column": 3
},
{
"message": "Must use plain style scalar.",
"line": 3,
"column": 12
},
{
"message": "Must use plain style scalar.",
"line": 3,
"column": 22
},
{
"message": "Must use plain style scalar.",
"line": 3,
"column": 33
},
{
"message": "Must use plain style scalar.",
"line": 4,
"column": 3
},
{
"message": "Must use plain style scalar.",
"line": 4,
"column": 19
},
{
"message": "Must use plain style scalar.",
"line": 5,
"column": 3
},
{
"message": "Must use plain style scalar.",
"line": 5,
"column": 18
}
]
6 changes: 6 additions & 0 deletions tests/fixtures/rules/plain-scalar/invalid/test-input.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# {}
{
"hello": "Hello!", "goodbye": "Goodbye!",
"good-morning": "Good Morning",
"how-are-you": "How are you doing?"
}
6 changes: 6 additions & 0 deletions tests/fixtures/rules/plain-scalar/invalid/test-output.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# plain-scalar/invalid/test-input.yaml
{
hello: Hello!, goodbye: Goodbye!,
good-morning: Good Morning,
how-are-you: How are you doing?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# {}
"!a": "!b"
"!!a": "!!b"
"!! a": "!! b"
"!<a>": "!<b>"
"! <a>": "! <b>"

0 comments on commit 3ed01c6

Please sign in to comment.