@html-eslint/eslint-plugin 0.19.0 → 0.20.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.
@@ -30,6 +30,7 @@ const noAccesskeyAttrs = require("./no-accesskey-attrs");
30
30
  const noRestrictedAttrs = require("./no-restricted-attrs");
31
31
  const noTrailingSpaces = require("./no-trailing-spaces");
32
32
  const requireAttrs = require("./require-attrs");
33
+ const noRestrictedAttrValues = require("./no-restricted-attr-values");
33
34
 
34
35
  module.exports = {
35
36
  "require-lang": requireLang,
@@ -64,4 +65,5 @@ module.exports = {
64
65
  "no-accesskey-attrs": noAccesskeyAttrs,
65
66
  "no-restricted-attrs": noRestrictedAttrs,
66
67
  "no-trailing-spaces": noTrailingSpaces,
68
+ "no-restricted-attr-values": noRestrictedAttrValues,
67
69
  };
@@ -0,0 +1,141 @@
1
+ /**
2
+ * @typedef {import("../types").Rule} Rule
3
+ * @typedef {{attrPatterns: string[], attrValuePatterns: string[], message?: string}[]} Options
4
+ */
5
+
6
+ const { RULE_CATEGORY } = require("../constants");
7
+
8
+ const MESSAGE_IDS = {
9
+ RESTRICTED: "restricted",
10
+ };
11
+
12
+ /**
13
+ * @type {Rule}
14
+ */
15
+ module.exports = {
16
+ meta: {
17
+ type: "code",
18
+
19
+ docs: {
20
+ description: "Disallow specified attributes",
21
+ category: RULE_CATEGORY.BEST_PRACTICE,
22
+ recommended: false,
23
+ },
24
+
25
+ fixable: null,
26
+ schema: {
27
+ type: "array",
28
+
29
+ items: {
30
+ type: "object",
31
+ required: ["attrPatterns", "attrValuePatterns"],
32
+ properties: {
33
+ attrPatterns: {
34
+ type: "array",
35
+ items: {
36
+ type: "string",
37
+ },
38
+ },
39
+ attrValuePatterns: {
40
+ type: "array",
41
+ items: {
42
+ type: "string",
43
+ },
44
+ },
45
+ message: {
46
+ type: "string",
47
+ },
48
+ },
49
+ },
50
+ },
51
+ messages: {
52
+ [MESSAGE_IDS.RESTRICTED]:
53
+ "'{{attrValuePatterns}}' is restricted from being used.",
54
+ },
55
+ },
56
+
57
+ create(context) {
58
+ /**
59
+ * @type {Options}
60
+ */
61
+ const options = context.options;
62
+ const checkers = options.map((option) => new PatternChecker(option));
63
+
64
+ return {
65
+ [["Tag", "StyleTag", "ScriptTag"].join(",")](node) {
66
+ node.attributes.forEach((attr) => {
67
+ if (
68
+ !attr.key ||
69
+ !attr.key.value ||
70
+ !attr.value ||
71
+ typeof attr.value.value !== "string"
72
+ ) {
73
+ return;
74
+ }
75
+
76
+ const matched = checkers.find((checker) =>
77
+ checker.test(attr.key.value, attr.value.value)
78
+ );
79
+
80
+ if (!matched) {
81
+ return;
82
+ }
83
+
84
+ const result = {
85
+ node: attr,
86
+ message: "",
87
+ };
88
+
89
+ const customMessage = matched.getMessage();
90
+
91
+ if (customMessage) {
92
+ result.message = customMessage;
93
+ } else {
94
+ result.messageId = MESSAGE_IDS.RESTRICTED;
95
+ }
96
+
97
+ context.report({
98
+ ...result,
99
+ data: { attrValuePatterns: attr.value.value },
100
+ });
101
+ });
102
+ },
103
+ };
104
+ },
105
+ };
106
+
107
+ class PatternChecker {
108
+ /**
109
+ * @param {Options[number]} option
110
+ */
111
+ constructor(option) {
112
+ this.option = option;
113
+ this.attrRegExps = option.attrPatterns.map(
114
+ (pattern) => new RegExp(pattern, "u")
115
+ );
116
+
117
+ this.valueRegExps = option.attrValuePatterns.map(
118
+ (pattern) => new RegExp(pattern, "u")
119
+ );
120
+ this.message = option.message;
121
+ }
122
+
123
+ /**
124
+ * @param {string} attrName
125
+ * @param {string} attrValue
126
+ * @returns {boolean}
127
+ */
128
+ test(attrName, attrValue) {
129
+ const result =
130
+ this.attrRegExps.some((exp) => exp.test(attrName)) &&
131
+ this.valueRegExps.some((exp) => exp.test(attrValue));
132
+ return result;
133
+ }
134
+
135
+ /**
136
+ * @returns {string}
137
+ */
138
+ getMessage() {
139
+ return this.message || "";
140
+ }
141
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@html-eslint/eslint-plugin",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "ESLint plugin for html",
5
5
  "author": "yeonjuan",
6
6
  "homepage": "https://github.com/yeonjuan/html-eslint#readme",
@@ -22,13 +22,14 @@
22
22
  },
23
23
  "scripts": {
24
24
  "test": "jest --coverage",
25
- "check:ts": "tsc"
25
+ "check:ts": "tsc",
26
+ "lint": "eslint . --ext .js"
26
27
  },
27
28
  "bugs": {
28
29
  "url": "https://github.com/yeonjuan/html-eslint/issues"
29
30
  },
30
31
  "engines": {
31
- "node": ">=8.10.0"
32
+ "node": "^12.22.0 || ^14.17.0 || >=16.0.0"
32
33
  },
33
34
  "keywords": [
34
35
  "eslint",
@@ -40,11 +41,11 @@
40
41
  "accessibility"
41
42
  ],
42
43
  "devDependencies": {
43
- "@html-eslint/parser": "^0.19.0",
44
+ "@html-eslint/parser": "^0.20.0",
44
45
  "@types/eslint": "^7.2.10",
45
46
  "@types/estree": "^0.0.47",
46
47
  "es-html-parser": "^0.0.8",
47
48
  "typescript": "^4.4.4"
48
49
  },
49
- "gitHead": "9b8998a8253a7e6ad18c75ef7dedd23e0b0ec1f4"
50
+ "gitHead": "ac1a79f12abdce059a1b0e046cf87ea8e5c34291"
50
51
  }