Skip to content

feat(no-restricted-html-elements): support array of elements #2750

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

Merged
merged 2 commits into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/crazy-impalas-pick.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'eslint-plugin-vue': minor
---

[vue/no-restricted-html-elements](https://eslint.vuejs.org/rules/no-restricted-html-elements.html) now accepts multiple elements in each entry.
18 changes: 9 additions & 9 deletions docs/rules/no-restricted-html-elements.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,16 @@ This rule takes a list of strings, where each string is an HTML element name to

```json
{
"vue/no-restricted-html-elements": ["error", "button", "marquee"]
"vue/no-restricted-html-elements": ["error", "a", "marquee"]
}
```

<eslint-code-block :rules="{'vue/no-restricted-html-elements': ['error', 'button', 'marquee']}">
<eslint-code-block :rules="{'vue/no-restricted-html-elements': ['error', 'a', 'marquee']}">

```vue
<template>
<!-- ✗ BAD -->
<button></button>
<a></a>
<marquee></marquee>
</template>
```
Expand All @@ -60,8 +60,8 @@ Alternatively, the rule also accepts objects.
"vue/no-restricted-html-elements": [
"error",
{
"element": "button",
"message": "Prefer use of our custom <AppButton /> component"
"element": ["a", "RouterLink"],
"message": "Prefer the use of <NuxtLink> component"
},
{
"element": "marquee",
Expand All @@ -73,18 +73,18 @@ Alternatively, the rule also accepts objects.

The following properties can be specified for the object.

- `element` ... Specify the html element.
- `element` ... Specify the HTML element or an array of HTML elements.
- `message` ... Specify an optional custom message.

### `{ "element": "marquee" }, { "element": "button" }`
### `{ "element": "marquee" }, { "element": "a" }`

<eslint-code-block :rules="{'vue/no-restricted-html-elements': ['error', { element: 'marquee' }, { element: 'button' }]}">
<eslint-code-block :rules="{'vue/no-restricted-html-elements': ['error', { element: 'marquee' }, { element: 'a' }]}">

```vue
<template>
<!-- ✗ BAD -->
<marquee></marquee>
<button></button>
<a></a>
</template>
```

Expand Down
16 changes: 13 additions & 3 deletions lib/rules/no-restricted-html-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,12 @@ module.exports = {
{
type: 'object',
properties: {
element: { type: 'string' },
element: {
oneOf: [
{ type: 'string' },
{ type: 'array', items: { type: 'string' } }
]
},
message: { type: 'string', minLength: 1 }
},
required: ['element'],
Expand Down Expand Up @@ -55,9 +60,12 @@ module.exports = {
}

for (const option of context.options) {
const element = option.element || option
const restrictedItem = option.element || option
const elementsToRestrict = Array.isArray(restrictedItem)
? restrictedItem
: [restrictedItem]

if (element === node.rawName) {
if (elementsToRestrict.includes(node.rawName)) {
context.report({
messageId: option.message ? 'customMessage' : 'forbiddenElement',
data: {
Expand All @@ -66,6 +74,8 @@ module.exports = {
},
node: node.startTag
})

return
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions tests/lib/rules/no-restricted-html-elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ tester.run('no-restricted-html-elements', rule, {
filename: 'test.vue',
code: '<template><div class="foo"><Button type="button"></Button></div></template>',
options: ['button']
},
{
filename: 'test.vue',
code: '<template><main><article></article></main></template>',
options: [{ element: ['div', 'span'] }]
}
],
invalid: [
Expand Down Expand Up @@ -69,6 +74,28 @@ tester.run('no-restricted-html-elements', rule, {
column: 11
}
]
},
{
filename: 'test.vue',
code: '<template><a></a><RouterLink></RouterLink></template>',
options: [
{
element: ['a', 'RouterLink'],
message: 'Prefer the use of <NuxtLink> component'
}
],
errors: [
{
message: 'Prefer the use of <NuxtLink> component',
line: 1,
column: 11
},
{
message: 'Prefer the use of <NuxtLink> component',
line: 1,
column: 18
}
]
}
]
})
Loading