Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow restricting classes by regexp in vue/no-restricted-class #2013

Merged
merged 3 commits into from Oct 19, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
15 changes: 12 additions & 3 deletions lib/rules/no-restricted-class.js
Expand Up @@ -5,16 +5,25 @@
'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<string>} forbiddenClasses
*/
const reportForbiddenClass = (className, node, context, forbiddenClasses) => {
if (forbiddenClasses.has(className)) {
const isForbidden = forbiddenClasses.some((cl) => {
if (regexp.isRegExp(cl)) {
const re = regexp.toRegExp(cl)
return re.test(className)
} else {
FloEdelmann marked this conversation as resolved.
Show resolved Hide resolved
return className.includes(cl)
}
})
if (isForbidden) {
const loc = node.value ? node.value.loc : node.loc
context.report({
node,
Expand Down Expand Up @@ -112,7 +121,7 @@ module.exports = {

/** @param {RuleContext} context */
create(context) {
const forbiddenClasses = new Set(context.options || [])
const forbiddenClasses = context.options || []

return utils.defineTemplateBodyVisitor(context, {
/**
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>`,
FloEdelmann marked this conversation as resolved.
Show resolved Hide resolved
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)/']
}
]
})