• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ota-meshi / eslint-plugin-yml / 6447659798

08 Oct 2023 12:38PM UTC coverage: 89.978% (+0.1%) from 89.876%
6447659798

push

github

web-flow
feat: use eslint-compat-utils (#270)

* feat: use eslint-compat-utils

* Create fluffy-phones-battle.md

1514 of 1763 branches covered (0.0%)

Branch coverage included in aggregate %.

107 of 107 new or added lines in 31 files covered. (100.0%)

2230 of 2398 relevant lines covered (92.99%)

635.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.68
/src/rules/block-mapping-question-indicator-newline.ts
1
import { createRule } from "../utils";
6✔
2
import { isQuestion } from "../utils/ast-utils";
1✔
3
import { getSourceCode } from "../utils/compat";
1✔
4

5
export default createRule("block-mapping-question-indicator-newline", {
1✔
6
  meta: {
7
    docs: {
8
      description: "enforce consistent line breaks after `?` indicator",
9
      categories: ["standard"],
10
      extensionRule: false,
11
      layout: true,
12
    },
13
    fixable: "whitespace",
14
    schema: [
15
      {
16
        enum: ["always", "never"],
17
      },
18
    ],
19
    messages: {
20
      unexpectedLinebreakAfterIndicator:
21
        "Unexpected line break after this `?` indicator.",
22
      expectedLinebreakAfterIndicator:
23
        "Expected a line break after this `?` indicator.",
24
    },
25
    type: "layout",
26
  },
27
  create(context) {
28
    const sourceCode = getSourceCode(context);
40✔
29
    if (!sourceCode.parserServices.isYAML) {
40!
30
      return {};
×
31
    }
32
    const option: "never" | "always" = context.options[0] || "never";
40✔
33

34
    return {
40✔
35
      YAMLMapping(node) {
36
        if (node.style !== "block") {
69!
37
          return;
×
38
        }
39
        for (const pair of node.pairs) {
69✔
40
          const key = pair.key;
187✔
41
          if (!key) {
187✔
42
            continue;
2✔
43
          }
44
          const question = sourceCode.getFirstToken(pair);
185✔
45
          if (!question || !isQuestion(question)) {
185✔
46
            continue;
132✔
47
          }
48

49
          const hasNewline = question.loc.end.line < key.loc.start.line;
53✔
50
          if (hasNewline) {
53✔
51
            if (option === "never") {
26✔
52
              context.report({
4✔
53
                loc: question.loc,
54
                messageId: "unexpectedLinebreakAfterIndicator",
55
                fix(fixer) {
56
                  const spaceCount =
57
                    key.loc.start.column - question.loc.end.column;
4✔
58
                  if (spaceCount < 1 && key.loc.start.line < key.loc.end.line) {
4!
59
                    // Stop auto-fix as it can break the indentation of multi-line key.
60
                    return null;
×
61
                  }
62
                  const spaces = " ".repeat(Math.max(spaceCount, 1));
4✔
63
                  return fixer.replaceTextRange(
4✔
64
                    [question.range[1], key.range[0]],
65
                    spaces,
66
                  );
67
                },
68
              });
69
            }
70
          } else {
71
            if (option === "always") {
27✔
72
              context.report({
22✔
73
                loc: question.loc,
74
                messageId: "expectedLinebreakAfterIndicator",
75
                fix(fixer) {
76
                  const spaces = `\n${" ".repeat(key.loc.start.column)}`;
22✔
77
                  return fixer.replaceTextRange(
22✔
78
                    [question.range[1], key.range[0]],
79
                    spaces,
80
                  );
81
                },
82
              });
83
            }
84
          }
85
        }
86
      },
87
    };
88
  },
89
});
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc