Skip to content

Commit

Permalink
feat: Add option in block-sequence-hyphen-indicator-newline (#259)
Browse files Browse the repository at this point in the history
* feat: Add an option to block-sequence-hyphen-indicator-newline

* test: Rename test suite `block-mapping-only` to `nested-hyphen-and-block-mapping-newline`

* test: new test suite for `block-sequence-hyphen-indicator-newline`

* docs: Update for new option in `block-sequence-hyphen-indicator-newline`

* test: add test case for block mapping

* Create thick-eyes-march.md

---------

Co-authored-by: Yosuke Ota <otameshiyo23@gmail.com>
  • Loading branch information
sun-yryr and ota-meshi authored Sep 11, 2023
1 parent 928b129 commit b73d852
Show file tree
Hide file tree
Showing 148 changed files with 1,403 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-eyes-march.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-yml": minor
---

feat: add `blockMapping` option in `block-sequence-hyphen-indicator-newline`
22 changes: 22 additions & 0 deletions docs/rules/block-sequence-hyphen-indicator-newline.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ yml/block-sequence-hyphen-indicator-newline:
- error
- never # or "always"
- nestedHyphen: always # or "never"
- blockMapping: never # or "always"
```
- Styles
Expand All @@ -49,6 +50,27 @@ yml/block-sequence-hyphen-indicator-newline:

- Properties
- `nestedHyphen` ... Specifies the style to apply to nested hyphens. default `"always"`
- `blockMapping` ... Specifies the style to apply to block mapping. If unspecified, the value of `Styles` is used.

### `"blockMapping": "always"`

<eslint-code-block fix>

<!-- eslint-skip -->

```yaml
# eslint yml/block-sequence-hyphen-indicator-newline: ['error', 'never', { blockMapping: 'always' }]
# ✓ GOOD
-
keyA: "GOOD"
- { keyB: "GOOD" }
# ✗ BAD
- key: "BAD"
```

</eslint-code-block>

## :couple: Related rules

Expand Down
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
Expand Up @@ -2,7 +2,8 @@
"options": [
"always",
{
"nestedHyphen": "always"
"nestedHyphen": "always",
"blockMapping": "always"
}
],
"settings": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[
{
"message": "Expected a line break after this `-` indicator.",
"line": 2,
"column": 1
},
{
"message": "Expected a line break after this `-` indicator.",
"line": 3,
"column": 1
},
{
"message": "Expected a line break after this `-` indicator.",
"line": 3,
"column": 3
},
{
"message": "Expected a line break after this `-` indicator.",
"line": 4,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
- a: a
- - b: b
- {c: c}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# block-sequence-hyphen-indicator-newline/invalid/yaml-test-suite-for-always/mapping-input.yaml
-
a: a
-
-
b: b
-
{c: c}
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 @@
- foo: bar
- - baz
- baz
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-newline/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-newline/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-newline/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-newline/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,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-newline/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-newline/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-newline/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-newline/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-newline/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
}
]
Loading

0 comments on commit b73d852

Please sign in to comment.