|
| 1 | +/** |
| 2 | + * @fileoverview Forbid certain classes from being used |
| 3 | + * @author Tao Bojlen |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +// ------------------------------------------------------------------------------ |
| 8 | +// Requirements |
| 9 | +// ------------------------------------------------------------------------------ |
| 10 | +const utils = require('../utils') |
| 11 | + |
| 12 | +// ------------------------------------------------------------------------------ |
| 13 | +// Helpers |
| 14 | +// ------------------------------------------------------------------------------ |
| 15 | +/** |
| 16 | + * Report a forbidden class |
| 17 | + * @param {string} className |
| 18 | + * @param {*} node |
| 19 | + * @param {RuleContext} context |
| 20 | + * @param {Set<string>} forbiddenClasses |
| 21 | + */ |
| 22 | +const reportForbiddenClass = (className, node, context, forbiddenClasses) => { |
| 23 | + if (forbiddenClasses.has(className)) { |
| 24 | + const loc = node.value ? node.value.loc : node.loc |
| 25 | + context.report({ |
| 26 | + node, |
| 27 | + loc, |
| 28 | + messageId: 'forbiddenClass', |
| 29 | + data: { |
| 30 | + class: className |
| 31 | + } |
| 32 | + }) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * @param {Expression} node |
| 38 | + * @param {boolean} [textOnly] |
| 39 | + * @returns {IterableIterator<{ className:string, reportNode: ESNode }>} |
| 40 | + */ |
| 41 | +function* extractClassNames(node, textOnly) { |
| 42 | + if (node.type === 'Literal') { |
| 43 | + yield* `${node.value}` |
| 44 | + .split(/\s+/) |
| 45 | + .map((className) => ({ className, reportNode: node })) |
| 46 | + return |
| 47 | + } |
| 48 | + if (node.type === 'TemplateLiteral') { |
| 49 | + for (const templateElement of node.quasis) { |
| 50 | + yield* templateElement.value.cooked |
| 51 | + .split(/\s+/) |
| 52 | + .map((className) => ({ className, reportNode: templateElement })) |
| 53 | + } |
| 54 | + for (const expr of node.expressions) { |
| 55 | + yield* extractClassNames(expr, true) |
| 56 | + } |
| 57 | + return |
| 58 | + } |
| 59 | + if (node.type === 'BinaryExpression') { |
| 60 | + if (node.operator !== '+') { |
| 61 | + return |
| 62 | + } |
| 63 | + yield* extractClassNames(node.left, true) |
| 64 | + yield* extractClassNames(node.right, true) |
| 65 | + return |
| 66 | + } |
| 67 | + if (textOnly) { |
| 68 | + return |
| 69 | + } |
| 70 | + if (node.type === 'ObjectExpression') { |
| 71 | + for (const prop of node.properties) { |
| 72 | + if (prop.type !== 'Property') { |
| 73 | + continue |
| 74 | + } |
| 75 | + const classNames = utils.getStaticPropertyName(prop) |
| 76 | + if (!classNames) { |
| 77 | + continue |
| 78 | + } |
| 79 | + yield* classNames |
| 80 | + .split(/\s+/) |
| 81 | + .map((className) => ({ className, reportNode: prop.key })) |
| 82 | + } |
| 83 | + return |
| 84 | + } |
| 85 | + if (node.type === 'ArrayExpression') { |
| 86 | + for (const element of node.elements) { |
| 87 | + if (element == null) { |
| 88 | + continue |
| 89 | + } |
| 90 | + if (element.type === 'SpreadElement') { |
| 91 | + continue |
| 92 | + } |
| 93 | + yield* extractClassNames(element) |
| 94 | + } |
| 95 | + return |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +// ------------------------------------------------------------------------------ |
| 100 | +// Rule Definition |
| 101 | +// ------------------------------------------------------------------------------ |
| 102 | +module.exports = { |
| 103 | + meta: { |
| 104 | + type: 'problem', |
| 105 | + docs: { |
| 106 | + description: 'disallow specific classes in Vue components', |
| 107 | + url: 'https://eslint.vuejs.org/rules/no-restricted-class.html', |
| 108 | + categories: undefined |
| 109 | + }, |
| 110 | + fixable: null, |
| 111 | + messages: { |
| 112 | + forbiddenClass: "'{{class}}' class is not allowed." |
| 113 | + }, |
| 114 | + schema: { |
| 115 | + type: 'array', |
| 116 | + items: { |
| 117 | + type: 'string' |
| 118 | + } |
| 119 | + } |
| 120 | + }, |
| 121 | + |
| 122 | + /** @param {RuleContext} context */ |
| 123 | + create(context) { |
| 124 | + const forbiddenClasses = new Set(context.options || []) |
| 125 | + |
| 126 | + return utils.defineTemplateBodyVisitor(context, { |
| 127 | + /** |
| 128 | + * @param {VAttribute & { value: VLiteral } } node |
| 129 | + */ |
| 130 | + 'VAttribute[directive=false][key.name="class"]'(node) { |
| 131 | + node.value.value |
| 132 | + .split(/\s+/) |
| 133 | + .forEach((className) => |
| 134 | + reportForbiddenClass(className, node, context, forbiddenClasses) |
| 135 | + ) |
| 136 | + }, |
| 137 | + |
| 138 | + /** @param {VExpressionContainer} node */ |
| 139 | + "VAttribute[directive=true][key.name.name='bind'][key.argument.name='class'] > VExpressionContainer.value"( |
| 140 | + node |
| 141 | + ) { |
| 142 | + if (!node.expression) { |
| 143 | + return |
| 144 | + } |
| 145 | + |
| 146 | + for (const { className, reportNode } of extractClassNames( |
| 147 | + /** @type {Expression} */ (node.expression) |
| 148 | + )) { |
| 149 | + reportForbiddenClass(className, reportNode, context, forbiddenClasses) |
| 150 | + } |
| 151 | + } |
| 152 | + }) |
| 153 | + } |
| 154 | +} |
0 commit comments