@html-eslint/eslint-plugin 0.16.0 → 0.17.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,6 +29,7 @@ const noMultipleEmptyLines = require("./no-multiple-empty-lines");
29
29
  const noAccesskeyAttrs = require("./no-accesskey-attrs");
30
30
  const noRestrictedAttrs = require("./no-restricted-attrs");
31
31
  const noTrailingSpaces = require("./no-trailing-spaces");
32
+ const requireAttrs = require("./require-attrs");
32
33
 
33
34
  module.exports = {
34
35
  "require-lang": requireLang,
@@ -46,6 +47,7 @@ module.exports = {
46
47
  quotes: quotes,
47
48
  "id-naming-convention": idNamingConvention,
48
49
  "no-obsolete-tags": noObsoleteTags,
50
+ "require-attrs": requireAttrs,
49
51
  "require-closing-tags": requireClosingTags,
50
52
  "require-meta-description": requireMetaDescription,
51
53
  "require-frame-title": requireFrameTitle,
@@ -0,0 +1,110 @@
1
+ /**
2
+ * @typedef {import("../types").Rule} Rule
3
+ */
4
+
5
+ const { RULE_CATEGORY } = require("../constants");
6
+
7
+ const MESSAGE_IDS = {
8
+ MISSING: "missing",
9
+ UNEXPECTED: "unexpected",
10
+ };
11
+
12
+ /**
13
+ * @type {Rule}
14
+ */
15
+ module.exports = {
16
+ meta: {
17
+ type: "code",
18
+
19
+ docs: {
20
+ description: "Require specified attributes",
21
+ category: RULE_CATEGORY.BEST_PRACTICE,
22
+ recommended: false,
23
+ },
24
+ fixable: null,
25
+ schema: {
26
+ type: "array",
27
+ items: {
28
+ type: "object",
29
+ properties: {
30
+ tag: { type: "string" },
31
+ attr: { type: "string" },
32
+ value: { type: "string" },
33
+ },
34
+ required: ["tag", "attr"],
35
+ additionalProperties: false,
36
+ },
37
+ },
38
+ messages: {
39
+ [MESSAGE_IDS.MISSING]: "Missing '{{attr}}' attributes for '{{tag}}' tag",
40
+ [MESSAGE_IDS.UNEXPECTED]:
41
+ "Unexpected '{{attr}}' attributes value. '{{expected}}' is expected",
42
+ },
43
+ },
44
+
45
+ create(context) {
46
+ const options = context.options || [];
47
+ const tagOptionsMap = new Map();
48
+
49
+ options.forEach((option) => {
50
+ const tagName = option.tag.toLowerCase();
51
+ if (tagOptionsMap.has(tagName)) {
52
+ tagOptionsMap.set(tagName, [...tagOptionsMap.get(tagName), option]);
53
+ } else {
54
+ tagOptionsMap.set(tagName, [option]);
55
+ }
56
+ });
57
+
58
+ function check(node, tagName) {
59
+ const tagOptions = tagOptionsMap.get(tagName);
60
+ const attributes = node.attributes || [];
61
+
62
+ tagOptions.forEach((option) => {
63
+ const attrName = option.attr;
64
+ const attr = attributes.find(
65
+ (attr) => attr.key && attr.key.value === attrName
66
+ );
67
+ if (!attr) {
68
+ context.report({
69
+ messageId: MESSAGE_IDS.MISSING,
70
+ node,
71
+ data: {
72
+ attr: attrName,
73
+ tag: tagName,
74
+ },
75
+ });
76
+ } else if (
77
+ attr.value &&
78
+ typeof option.value === "string" &&
79
+ attr.value.value !== option.value
80
+ ) {
81
+ context.report({
82
+ messageId: MESSAGE_IDS.UNEXPECTED,
83
+ node: attr,
84
+ data: {
85
+ attr: attrName,
86
+ expected: option.value,
87
+ },
88
+ });
89
+ }
90
+ });
91
+ }
92
+
93
+ return {
94
+ [["StyleTag", "ScriptTag"].join(",")](node) {
95
+ const tagName = node.type === "StyleTag" ? "style" : "script";
96
+ if (!tagOptionsMap.has(tagName)) {
97
+ return;
98
+ }
99
+ check(node, tagName);
100
+ },
101
+ Tag(node) {
102
+ const tagName = node.name.toLowerCase();
103
+ if (!tagOptionsMap.has(tagName)) {
104
+ return;
105
+ }
106
+ check(node, tagName);
107
+ },
108
+ };
109
+ },
110
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@html-eslint/eslint-plugin",
3
- "version": "0.16.0",
3
+ "version": "0.17.0",
4
4
  "description": "ESLint plugin for html",
5
5
  "author": "yeonjuan",
6
6
  "homepage": "https://github.com/yeonjuan/html-eslint#readme",
@@ -40,11 +40,11 @@
40
40
  "accessibility"
41
41
  ],
42
42
  "devDependencies": {
43
- "@html-eslint/parser": "^0.16.0",
43
+ "@html-eslint/parser": "^0.17.0",
44
44
  "@types/eslint": "^7.2.10",
45
45
  "@types/estree": "^0.0.47",
46
46
  "es-html-parser": "^0.0.8",
47
47
  "typescript": "^4.4.4"
48
48
  },
49
- "gitHead": "a866203b2c65d63b3f101b16c8d7d1a3bde32c75"
49
+ "gitHead": "4ff87ae244278c889699622cba70400595ea069d"
50
50
  }