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: "