Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add option in block-sequence-hyphen-indicator-newline #259

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/rules/block-sequence-hyphen-indicator-newline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default createRule("block-sequence-hyphen-indicator-newline", {
type: "object",
properties: {
nestedHyphen: { enum: ["always", "never"] },
blockMapping: { enum: ["always", "never"] },
},
additionalProperties: false,
},
Expand All @@ -37,16 +38,25 @@ export default createRule("block-sequence-hyphen-indicator-newline", {
const style: "never" | "always" = context.options[0] || "never";
const nestedHyphenStyle: "never" | "always" =
context.options[1]?.nestedHyphen || "always";
const blockMappingStyle: "never" | "always" =
context.options[1]?.blockMapping || style;

/**
* Get style from given hyphen
*/
function getStyleOption(hyphen: AST.Token): "never" | "always" {
function getStyleOption(
hyphen: AST.Token,
entry: AST.YAMLContent | AST.YAMLWithMeta,
): "never" | "always" {
const next = sourceCode.getTokenAfter(hyphen);
if (next && isHyphen(next)) {
return nestedHyphenStyle;
}

if (entry.type === "YAMLMapping" && entry.style === "block") {
return blockMappingStyle;
}

return style;
}

Expand All @@ -66,7 +76,7 @@ export default createRule("block-sequence-hyphen-indicator-newline", {

const hasNewline = hyphen.loc.end.line < entry.loc.start.line;
if (hasNewline) {
if (getStyleOption(hyphen) === "never") {
if (getStyleOption(hyphen, entry) === "never") {
context.report({
loc: hyphen.loc,
messageId: "unexpectedLinebreakAfterIndicator",
Expand All @@ -89,7 +99,7 @@ export default createRule("block-sequence-hyphen-indicator-newline", {
});
}
} else {
if (getStyleOption(hyphen) === "always") {
if (getStyleOption(hyphen, entry) === "always") {
context.report({
loc: hyphen.loc,
messageId: "expectedLinebreakAfterIndicator",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "Expected a line break after this `-` indicator.",
"line": 2,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- - s1_i1
- s1_i2
- s2
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# block-sequence-hyphen-indicator-newline/invalid/yaml-test-suite-for-block-mapping-only/3ALJ-input.yaml
-
- s1_i1
- s1_i2
- s2
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"message": "Expected a line break after this `-` indicator.",
"line": 2,
"column": 1
},
{
"message": "Expected a line break after this `-` indicator.",
"line": 3,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- foo: bar
- - baz
- baz
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# block-sequence-hyphen-indicator-newline/invalid/yaml-test-suite-for-block-mapping-only/6BCT-input.yaml
-
foo: bar
-
- baz
- baz
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "Unexpected line break after this `-` indicator.",
"line": 2,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-
"flow in block"
- >
Block scalar
- !!map # Block collection
foo : bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# block-sequence-hyphen-indicator-newline/invalid/yaml-test-suite-for-block-mapping-only/735Y-input.yaml
- "flow in block"
- >
Block scalar
- !!map # Block collection
foo : bar
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "Expected a line break after this `-` indicator.",
"line": 2,
"column": 2
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- key: value
key2: value2
-
key3: value3
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# block-sequence-hyphen-indicator-newline/invalid/yaml-test-suite-for-block-mapping-only/93JH-input.yaml
-
key: value
key2: value2
-
key3: value3
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"message": "Expected a line break after this `-` indicator.",
"line": 4,
"column": 1
},
{
"message": "Expected a line break after this `-` indicator.",
"line": 6,
"column": 1
},
{
"message": "Expected a line break after this `-` indicator.",
"line": 8,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
# Products purchased
- item : Super Hoop
quantity: 1
- item : Basketball
quantity: 4
- item : Big Shoes
quantity: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# block-sequence-hyphen-indicator-newline/invalid/yaml-test-suite-for-block-mapping-only/9U5K-input.yaml
---
# Products purchased
-
item : Super Hoop
quantity: 1
-
item : Basketball
quantity: 4
-
item : Big Shoes
quantity: 1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "Expected a line break after this `-` indicator.",
"line": 4,
"column": 3
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
? a
: - b
- - c
- d
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# block-sequence-hyphen-indicator-newline/invalid/yaml-test-suite-for-block-mapping-only/A2M4-input.yaml
? a
: - b
-
- c
- d
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"message": "Expected a line break after this `-` indicator.",
"line": 2,
"column": 1
},
{
"message": "Expected a line break after this `-` indicator.",
"line": 3,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- bla"keks: foo
- bla]keks: foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# block-sequence-hyphen-indicator-newline/invalid/yaml-test-suite-for-block-mapping-only/AZW3-input.yaml
-
bla"keks: foo
-
bla]keks: foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "Expected a line break after this `-` indicator.",
"line": 6,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
- !!str
-
!!null : a
b: !!str
- !!str : !!null
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# block-sequence-hyphen-indicator-newline/invalid/yaml-test-suite-for-block-mapping-only/FH7J-input.yaml
- !!str
-
!!null : a
b: !!str
-
!!str : !!null
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"message": "Expected a line break after this `-` indicator.",
"line": 6,
"column": 1
},
{
"message": "Expected a line break after this `-` indicator.",
"line": 7,
"column": 1
},
{
"message": "Expected a line break after this `-` indicator.",
"line": 8,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Ordered maps are represented as
# A sequence of mappings, with
# each mapping having one key
--- !!omap
- Mark McGwire: 65
- Sammy Sosa: 63
- Ken Griffy: 58
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# block-sequence-hyphen-indicator-newline/invalid/yaml-test-suite-for-block-mapping-only/J7PZ-input.yaml
# Ordered maps are represented as
# A sequence of mappings, with
# each mapping having one key
--- !!omap
-
Mark McGwire: 65
-
Sammy Sosa: 63
-
Ken Griffy: 58
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"message": "Expected a line break after this `-` indicator.",
"line": 4,
"column": 3
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
block sequence:
- one
- two : three
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# block-sequence-hyphen-indicator-newline/invalid/yaml-test-suite-for-block-mapping-only/JQ4R-input.yaml
block sequence:
- one
-
two : three
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"message": "Unexpected line break after this `-` indicator.",
"line": 11,
"column": 2
},
{
"message": "Expected a line break after this `-` indicator.",
"line": 14,
"column": 2
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
a: "double
quotes" # lala
b: plain
value # lala
c : #lala
d
? # lala
- seq1
: # lala
- #lala
seq2
e: &node # lala
- x: y
block: > # lala
abcde
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# block-sequence-hyphen-indicator-newline/invalid/yaml-test-suite-for-block-mapping-only/RZP5-input.yaml
a: "double
quotes" # lala
b: plain
value # lala
c : #lala
d
? # lala
- seq1
: # lala
- seq2
e: &node # lala
-
x: y
block: > # lala
abcde
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{
"message": "Expected a line break after this `-` indicator.",
"line": 20,
"column": 3
},
{
"message": "Expected a line break after this `-` indicator.",
"line": 24,
"column": 3
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
Time: 2001-11-23 15:01:42 -5
User: ed
Warning:
This is an error message
for the log file
---
Time: 2001-11-23 15:02:31 -5
User: ed
Warning:
A slightly different error
message.
---
Date: 2001-11-23 15:03:17 -5
User: ed
Fatal:
Unknown variable "bar"
Stack:
- file: TopClass.py
line: 23
code: |
x = MoreObject("345\n")
- file: MoreClass.py
line: 58
code: |-
foo = bar
Loading
Loading