• 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

89.06
/src/rules/require-string-key.ts
1
import type { AST } from "yaml-eslint-parser";
6✔
2
import { createRule } from "../utils";
1✔
3
import { getSourceCode } from "../utils/compat";
1✔
4

5
export default createRule("require-string-key", {
1✔
6
  meta: {
7
    docs: {
8
      description: "disallow mapping keys other than strings",
9
      categories: null,
10
      extensionRule: false,
11
      layout: false,
12
    },
13
    schema: [],
14
    messages: {
15
      expectedString: "The key must be a string.",
16
    },
17
    type: "suggestion",
18
  },
19
  create(context) {
20
    const sourceCode = getSourceCode(context);
23✔
21
    if (!sourceCode.parserServices.isYAML) {
23!
22
      return {};
×
23
    }
24

25
    let anchors: Record<string, AST.YAMLAnchor[]> = {};
23✔
26

27
    /**
28
     * Find Anchor
29
     */
30
    function findAnchor(alias: AST.YAMLAlias) {
31
      const target: {
32
        anchor: null | AST.YAMLAnchor;
33
        distance: number;
34
      } = {
1✔
35
        anchor: null,
36
        distance: Infinity,
37
      };
38
      for (const anchor of anchors[alias.name] || []) {
1!
39
        if (anchor.range[0] < alias.range[0]) {
1!
40
          const distance = alias.range[0] - anchor.range[0];
1✔
41
          if (target.distance >= distance) {
1!
42
            target.anchor = anchor;
1✔
43
            target.distance = distance;
1✔
44
          }
45
        }
46
      }
47
      return target.anchor;
1✔
48
    }
49

50
    /**
51
     * Checks if the given node is string
52
     */
53
    function isStringNode(
54
      node: AST.YAMLContent | AST.YAMLWithMeta | null,
55
    ): boolean {
56
      if (!node) {
76✔
57
        return false;
8✔
58
      }
59
      if (node.type === "YAMLWithMeta") {
68✔
60
        if (node.tag && node.tag.tag === "tag:yaml.org,2002:str") {
8✔
61
          return true;
2✔
62
        }
63
        return isStringNode(node.value);
6✔
64
      }
65
      if (node.type === "YAMLAlias") {
60✔
66
        const anchor = findAnchor(node);
1✔
67
        if (!anchor) {
1!
68
          return false;
×
69
        }
70
        return isStringNode(anchor.parent);
1✔
71
      }
72
      if (node.type !== "YAMLScalar") {
59✔
73
        return false;
12✔
74
      }
75

76
      return typeof node.value === "string";
47✔
77
    }
78

79
    return {
23✔
80
      YAMLDocument() {
81
        anchors = {};
23✔
82
      },
83
      YAMLAnchor(node: AST.YAMLAnchor) {
84
        const list = anchors[node.name] || (anchors[node.name] = []);
9✔
85
        list.push(node);
9✔
86
      },
87
      YAMLPair(node) {
88
        if (!isStringNode(node.key)) {
69✔
89
          context.report({
27✔
90
            node: node.key || node,
34✔
91
            messageId: "expectedString",
92
          });
93
        }
94
      },
95
    };
96
  },
97
});
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