diff --git a/lib/rules/forbid-tags.js b/lib/rules/forbid-tags.js
index 542af37..c812975 100644
--- a/lib/rules/forbid-tags.js
+++ b/lib/rules/forbid-tags.js
@@ -10,7 +10,7 @@ module.exports = {
fixable: null,
messages: {
forbiddenTag: "<{{tag}}> is forbidden",
- forbiddenTagWithMessage: "<{{tag}}> is forbidden, {{message}}",
+ forbiddenTagWithMessage: "{{message}}",
},
schema: [
{
@@ -30,6 +30,26 @@ module.exports = {
required: ["tag"],
additionalProperties: false,
},
+ {
+ type: "object",
+ properties: {
+ tag: { type: "string" },
+ message: { type: "string" },
+ disableAttrs: { type: "array" },
+ },
+ required: ["tag", "disableAttrs"],
+ additionalProperties: false,
+ },
+ {
+ type: "object",
+ properties: {
+ tag: { type: "string" },
+ message: { type: "string" },
+ skipAttrs: { type: "array" },
+ },
+ required: ["tag", "skipAttrs"],
+ additionalProperties: false,
+ },
],
},
},
@@ -52,11 +72,40 @@ module.exports = {
}
});
return {
- WXElement(node) {
+ WXStartTag(node) {
if (node && node.name && indexedForbidConfigs[node.name]) {
const msg = indexedForbidConfigs[node.name].message;
+ const disableAttrs =
+ indexedForbidConfigs[node.name].disableAttrs || [];
+ const skipAttrs = indexedForbidConfigs[node.name].skipAttrs || [];
+ const attrs = new Set(
+ (node.attributes || []).map((attr) => attr.key)
+ );
+
+ if (disableAttrs.length) {
+ if (disableAttrs.some((attr) => attrs.has(attr))) {
+ context.report({
+ node: node,
+ messageId: "forbiddenTagWithMessage",
+ data: {
+ tag: node.name,
+ message: msg
+ ? msg
+ : `If you need to use the following attributes [${disableAttrs.join(
+ ", "
+ )}], you can't use <${node.name} />`,
+ },
+ });
+ }
+ return;
+ }
+
+ if (skipAttrs.length && skipAttrs.some((attr) => attrs.has(attr))) {
+ return;
+ }
+
context.report({
- node: node.startTag ? node.startTag : node,
+ node: node,
messageId: msg ? "forbiddenTagWithMessage" : "forbiddenTag",
data: { tag: node.name, message: msg },
});
diff --git a/tests/rules/forbid-tags.js b/tests/rules/forbid-tags.js
index 11334d3..2b6e13e 100644
--- a/tests/rules/forbid-tags.js
+++ b/tests/rules/forbid-tags.js
@@ -19,6 +19,14 @@ tester.run("forbid-tags", rule, {
code: "",
options: [{ forbid: [{ tag: "button" }] }],
},
+ {
+ code: "",
+ options: [{ forbid: [{ tag: "view", skipAttrs: ["hover-class"] }] }],
+ },
+ {
+ code: "",
+ options: [{ forbid: [{ tag: "v", disableAttrs: ["hover-class"] }] }],
+ },
],
invalid: [
{
@@ -72,5 +80,36 @@ tester.run("forbid-tags", rule, {
},
],
},
+ {
+ code: "",
+ options: [
+ {
+ forbid: [
+ {
+ tag: "view",
+ message:
+ " is forbidden, but if you want to set hover-class you must use ",
+ skipAttrs: ["hover-class", "hover-start-time"],
+ },
+ ],
+ },
+ ],
+ errors: [
+ ` is forbidden, but if you want to set hover-class you must use `,
+ ],
+ },
+ {
+ code: "",
+ options: [
+ {
+ forbid: [
+ { tag: "v", disableAttrs: ["hover-class", "hover-start-time"] },
+ ],
+ },
+ ],
+ errors: [
+ `If you need to use the following attributes [hover-class, hover-start-time], you can't use `,
+ ],
+ },
],
});
diff --git a/website/docs/rules/forbid-tags.md b/website/docs/rules/forbid-tags.md
index 52ce7f4..20e680f 100644
--- a/website/docs/rules/forbid-tags.md
+++ b/website/docs/rules/forbid-tags.md
@@ -53,16 +53,86 @@ You may want to forbid usage of certain tags in favor of others, (e.g. forbid al
+```js
+// following code use this config
+{
+ "wxml/forbid-tags": [
+ "error",
+ {
+ "forbid":
+ [
+ {
+ "tag": "view",
+ "message": "If you need use hover-class you should use otherwise use ",
+ "skipAttrs": ["hover-class"]
+ }
+ ]
+ }
+ ]
+}
+```
+
+
+
+```wxml
+
+text
+
+
+text
+```
+
+
+
+```js
+// following code use this config
+{
+ "wxml/forbid-tags": [
+ "error",
+ {
+ "forbid":
+ [
+ {
+ "tag": "v",
+ "message": "If you need use hover-class you should use otherwise use ",
+ "disableAttrs": ["hover-class"]
+ }
+ ]
+ }
+ ]
+}
+```
+
+
+
+```wxml
+
+
+text
+
+
+
+text
+```
+
+
+
::: tip 💡 tips
You can edit code via online editor, it's online REPL, try to fix eslint problem !
:::
+## History
+
+| Version | Changes
+|:---|:---|
+| v0.7.5 | Add `disableAttrs` and `skipAttrs` config support |
+
## Config
```typescript
-"wxml/forbid-tags": [, { "forbid": [] }]
+"wxml/forbid-tags": [, { "forbid": [] }]
```
## Version