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 indentBlockSequences option to yml/indent rule #200

Merged
merged 2 commits into from
Dec 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/early-moose-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-yml": minor
---

feat: add `indentBlockSequences` option to `yml/indent` rule
53 changes: 52 additions & 1 deletion docs/rules/indent.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,60 @@ This rule reports enforces a consistent indentation style. The default style is
yml/indent:
- error
- 2
- indentBlockSequences: true
```

Specify the number of indents.
- Number option ... Specify the number of indents.
- Object option
- `indentBlockSequences` ... Specifies whether block sequences should be indented or not (when in a mapping).

### `"indentBlockSequences": true` (default)

<eslint-code-block fix>

<!-- eslint-skip -->

```yaml
# eslint yml/indent: [error, 2, { indentBlockSequences: true }]

# ✓ GOOD
key1:
- a
- b
- c

# ✗ BAD
key2:
- a
- b
- c
```

</eslint-code-block>

### `"indentBlockSequences": false`

<eslint-code-block fix>

<!-- eslint-skip -->

```yaml
# eslint yml/indent: [error, 2, { indentBlockSequences: false }]

# ✓ GOOD
key1:
- a
- b
- c

# ✗ BAD
key2:
- a
- b
- c
```

</eslint-code-block>

## :couple: Related rules

Expand Down
120 changes: 87 additions & 33 deletions src/rules/indent.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AST } from "yaml-eslint-parser";
import { createRule } from "../utils";
import { hasTabIndent, getNumOfIndent } from "../utils/yaml";
import type { YAMLToken, Fix, RuleFixer } from "../types";
import type { YAMLToken, Fix, RuleFixer, RuleContext } from "../types";
import { isHyphen, isQuestion, isColon } from "../utils/ast-utils";

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -49,6 +49,32 @@ type LineIndentLastScalarData = {
expectedIndent: number;
};

/**
* Parse options
*/
function parseOptions(context: RuleContext) {
const [indentOption, objectOptions] = context.options as [
number | undefined,
(
| {
indentBlockSequences?: boolean;
}
| undefined
)
];
const numOfIndent = getNumOfIndent(context, indentOption);
let indentBlockSequences = true;
if (objectOptions) {
if (objectOptions.indentBlockSequences === false) {
indentBlockSequences = false;
}
}
return {
numOfIndent,
indentBlockSequences,
};
}

export default createRule("indent", {
meta: {
docs: {
Expand All @@ -63,6 +89,13 @@ export default createRule("indent", {
type: "integer",
minimum: 2,
},
{
type: "object",
properties: {
indentBlockSequences: { type: "boolean" },
},
additionalProperties: false,
},
],
messages: {
wrongIndentation:
Expand All @@ -80,7 +113,7 @@ export default createRule("indent", {
return {};
}

const numOfIndent = getNumOfIndent(context, context.options[0]);
const { numOfIndent, indentBlockSequences } = parseOptions(context);

const sourceCode = context.getSourceCode();

Expand Down Expand Up @@ -178,6 +211,21 @@ export default createRule("indent", {
}
}

/**
* Calculate the indentation offset for the values in the mapping.
*/
function calcMappingPairValueIndentOffset(
node: AST.YAMLWithMeta | AST.YAMLContent | null
) {
if (indentBlockSequences || !node) {
return 1;
}
if (node.type === "YAMLSequence" && node.style === "block") {
return 0;
}
return 1;
}

const documents: AST.YAMLDocument[] = [];
return {
YAMLDocument(node) {
Expand Down Expand Up @@ -238,44 +286,27 @@ export default createRule("indent", {
}
}
},
// eslint-disable-next-line complexity -- X(

YAMLPair(node) {
const pairFirst = sourceCode.getFirstToken(node);
let questionToken: YAMLToken | null = null;
let keyToken: YAMLToken | null = null;
let colonToken: YAMLToken | null = null;
let valueToken: YAMLToken | null = null;
if (isQuestion(pairFirst)) {
const keyToken = node.key && sourceCode.getFirstToken(node.key);
const colonToken = findColonToken();
const valueToken = node.value && sourceCode.getFirstToken(node.value);

const questionToken = isQuestion(pairFirst) ? pairFirst : null;
if (questionToken) {
// ? a: b
questionToken = pairFirst;
marks.add(questionToken);
}

if (node.value) {
valueToken = sourceCode.getFirstToken(node.value);
colonToken = sourceCode.getTokenBefore(node.value, isColon);
}
if (node.key) {
keyToken = sourceCode.getFirstToken(node.key);
if (!colonToken) {
const token = sourceCode.getTokenAfter(node.key, isColon);
if (token && token.range[0] < node.range[1]) {
colonToken = token;
}
}
}
if (!colonToken) {
const tokens = sourceCode.getTokens(node, isColon);
if (tokens.length) {
colonToken = tokens[0];
if (keyToken) {
setOffset(
keyToken,
calcMappingPairValueIndentOffset(node.key),
questionToken
);
}
}

if (keyToken) {
if (questionToken) {
setOffset(keyToken, 1, questionToken);
}
}
if (colonToken) {
marks.add(colonToken);
if (questionToken) {
Expand All @@ -288,11 +319,34 @@ export default createRule("indent", {
}
if (valueToken) {
if (colonToken) {
setOffset(valueToken, 1, colonToken);
setOffset(
valueToken,
calcMappingPairValueIndentOffset(node.value),
colonToken
);
} else if (keyToken) {
// Probably not reach.
setOffset(valueToken, 1, keyToken);
}
}

/** Find colon indicator token */
function findColonToken() {
if (node.value) {
return sourceCode.getTokenBefore(node.value, isColon);
}
if (node.key) {
const token = sourceCode.getTokenAfter(node.key, isColon);
if (token && token.range[0] < node.range[1]) {
return token;
}
}
const tokens = sourceCode.getTokens(node, isColon);
if (tokens.length) {
return tokens[0];
}
return null;
}
},
YAMLWithMeta(node) {
const anchorToken =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[
{
"message": "Expected indentation of 2 spaces but found 0 spaces.",
"line": 8,
"column": 1
},
{
"message": "Expected indentation of 2 spaces but found 0 spaces.",
"line": 9,
"column": 1
},
{
"message": "Expected indentation of 2 spaces but found 0 spaces.",
"line": 10,
"column": 1
},
{
"message": "Expected indentation of 2 spaces but found 1 spaces.",
"line": 13,
"column": 1
},
{
"message": "Expected indentation of 4 spaces but found 2 spaces.",
"line": 14,
"column": 1
},
{
"message": "Expected indentation of 4 spaces but found 2 spaces.",
"line": 15,
"column": 1
},
{
"message": "Expected indentation of 4 spaces but found 2 spaces.",
"line": 16,
"column": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# {"options":[2, {"indentBlockSequences": true}]}
a:
- a
- b
- c

b:
- a
- b
- c

c:
-
- a
- b
- c

?
- a
- b
- c
:
- a
- b
- c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# indent/invalid/block-sequence-with-indent-block-sequences01-input.yml
a:
- a
- b
- c

b:
- a
- b
- c

c:
-
- a
- b
- c

?
- a
- b
- c
:
- a
- b
- c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[
{
"message": "Expected indentation of 0 spaces but found 2 spaces.",
"line": 3,
"column": 1
},
{
"message": "Expected indentation of 0 spaces but found 2 spaces.",
"line": 4,
"column": 1
},
{
"message": "Expected indentation of 0 spaces but found 2 spaces.",
"line": 5,
"column": 1
},
{
"message": "Expected indentation of 0 spaces but found 1 spaces.",
"line": 13,
"column": 1
},
{
"message": "Expected indentation of 0 spaces but found 2 spaces.",
"line": 19,
"column": 1
},
{
"message": "Expected indentation of 0 spaces but found 2 spaces.",
"line": 20,
"column": 1
},
{
"message": "Expected indentation of 0 spaces but found 2 spaces.",
"line": 21,
"column": 1
},
{
"message": "Expected indentation of 0 spaces but found 2 spaces.",
"line": 23,
"column": 1
},
{
"message": "Expected indentation of 0 spaces but found 2 spaces.",
"line": 24,
"column": 1
},
{
"message": "Expected indentation of 0 spaces but found 2 spaces.",
"line": 25,
"column": 1
}
]