Skip to content

Commit

Permalink
Allow restricting classes by regexp in vue/no-restricted-class (#2013)
Browse files Browse the repository at this point in the history
  • Loading branch information
kindoflew committed Oct 19, 2022
1 parent 2479d2f commit 0d2607f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/rules/no-restricted-class.md
Expand Up @@ -20,7 +20,7 @@ in the rule configuration.

```json
{
"vue/no-restricted-class": ["error", "forbidden", "forbidden-two", "forbidden-three"]
"vue/no-restricted-class": ["error", "forbidden", "forbidden-two", "forbidden-three", "/^for(bidden|gotten)/"]
}
```

Expand Down
34 changes: 30 additions & 4 deletions lib/rules/no-restricted-class.js
Expand Up @@ -5,16 +5,27 @@
'use strict'

const utils = require('../utils')
const regexp = require('../utils/regexp')

/**
* Report a forbidden class
* @param {string} className
* @param {*} node
* @param {RuleContext} context
* @param {Set<string>} forbiddenClasses
* @param {Array<RegExp>} forbiddenClassesRegexps
*/
const reportForbiddenClass = (className, node, context, forbiddenClasses) => {
if (forbiddenClasses.has(className)) {
const reportForbiddenClass = (
className,
node,
context,
forbiddenClasses,
forbiddenClassesRegexps
) => {
if (
forbiddenClasses.has(className) ||
forbiddenClassesRegexps.some((re) => re.test(className))
) {
const loc = node.value ? node.value.loc : node.loc
context.report({
node,
Expand Down Expand Up @@ -113,14 +124,23 @@ module.exports = {
/** @param {RuleContext} context */
create(context) {
const forbiddenClasses = new Set(context.options || [])
const forbiddenClassesRegexps = (context.options || [])
.filter((cl) => regexp.isRegExp(cl))
.map((cl) => regexp.toRegExp(cl))

return utils.defineTemplateBodyVisitor(context, {
/**
* @param {VAttribute & { value: VLiteral } } node
*/
'VAttribute[directive=false][key.name="class"]'(node) {
for (const className of node.value.value.split(/\s+/)) {
reportForbiddenClass(className, node, context, forbiddenClasses)
reportForbiddenClass(
className,
node,
context,
forbiddenClasses,
forbiddenClassesRegexps
)
}
},

Expand All @@ -135,7 +155,13 @@ module.exports = {
for (const { className, reportNode } of extractClassNames(
/** @type {Expression} */ (node.expression)
)) {
reportForbiddenClass(className, reportNode, context, forbiddenClasses)
reportForbiddenClass(
className,
reportNode,
context,
forbiddenClasses,
forbiddenClassesRegexps
)
}
}
})
Expand Down
14 changes: 14 additions & 0 deletions tests/lib/rules/no-restricted-class.js
Expand Up @@ -30,6 +30,10 @@ ruleTester.run('no-restricted-class', rule, {
{
code: `<template><div :class="'' + {forbidden: true}">Content</div></template>`,
options: ['forbidden']
},
{
code: `<template><div class="allowed">Content</div></template>`,
options: ['/^for(bidden|gotten)/']
}
],

Expand Down Expand Up @@ -113,6 +117,16 @@ ruleTester.run('no-restricted-class', rule, {
}
],
options: ['forbidden']
},
{
code: `<template><div class="forbidden allowed" /></template>`,
errors: [
{
message: "'forbidden' class is not allowed.",
type: 'VAttribute'
}
],
options: ['/^for(bidden|gotten)/']
}
]
})

0 comments on commit 0d2607f

Please sign in to comment.