-
-
Notifications
You must be signed in to change notification settings - Fork 680
Add new rule: no-restricted-html-elements #1820
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
Changes from 18 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
555b64b
Add new rule: html-forbid-elements
doug-wade 994bbe9
PR Feedback
doug-wade 40c20cd
Update lib/rules/no-restricted-html-elements.js
doug-wade 2f837ba
Update docs/rules/no-restricted-html-elements.md
doug-wade d32eac1
Update lib/rules/no-restricted-html-elements.js
doug-wade 59d1c5d
Update tests/lib/rules/no-restricted-html-elements.js
doug-wade 409f48a
Update tests/lib/rules/no-restricted-html-elements.js
doug-wade b7ca92b
Update tests/lib/rules/no-restricted-html-elements.js
doug-wade 4a62c15
Update tests/lib/rules/no-restricted-html-elements.js
doug-wade 51c0f9b
Update tests/lib/rules/no-restricted-html-elements.js
doug-wade 28b7883
Update tests/lib/rules/no-restricted-html-elements.js
doug-wade 6800118
Update docs/rules/no-restricted-html-elements.md
doug-wade 0202bce
Update tests/lib/rules/no-restricted-html-elements.js
doug-wade d87c63b
Update docs/rules/no-restricted-html-elements.md
doug-wade 55fd543
Update docs/rules/no-restricted-html-elements.md
doug-wade b989b15
Update docs/rules/no-restricted-html-elements.md
doug-wade 3812f9f
run npm update
doug-wade 07cc3a8
Remove messages block
doug-wade 33a4d90
Update docs/rules/no-restricted-html-elements.md
ota-meshi cdb4636
Update docs/rules/no-restricted-html-elements.md
ota-meshi daa01ec
Fix demo site
ota-meshi File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-restricted-html-elements | ||
description: disallow specific HTML elements | ||
--- | ||
# vue/no-restricted-html-elements | ||
|
||
> disallow specific HTML elements | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
|
||
## :book: Rule Details | ||
|
||
This rule allows you to specify HTML elements that you don't want to use in your application. | ||
|
||
<eslint-code-block :rules="{'vue/no-restricted-html-elements': ['error', 'marquee', 'button'] }"> | ||
|
||
```vue | ||
<!-- ✓ GOOD --> | ||
<p></p> | ||
<input /> | ||
<br /> | ||
|
||
<!-- ✗ BAD --> | ||
<button></button> | ||
<marquee></marquee> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
This rule takes a list of strings, where each string is an HTML element name to be restricted: | ||
|
||
```json | ||
{ | ||
"vue/no-restricted-html-elements": ["error", "button", "marquee"] | ||
} | ||
``` | ||
|
||
<eslint-code-block :rules="{'vue/no-restricted-block': ['error', 'button', 'marquee']}"> | ||
|
||
```vue | ||
<!-- ✗ BAD --> | ||
<button></button> | ||
<marquee></marquee> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
Alternatively, the rule also accepts objects. | ||
|
||
```json | ||
{ | ||
"vue/no-restricted-html-elements": [ | ||
"error", | ||
{ | ||
"element": "button", | ||
"message": "Prefer use of our custom <AppButton /> component" | ||
}, | ||
{ | ||
"element": "marquee", | ||
"message": "Do not use deprecated HTML tags" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
The following properties can be specified for the object. | ||
|
||
- `element` ... Specify the html element. | ||
- `message` ... Specify an optional custom message. | ||
|
||
### `{ "element": "marquee" }, { "element": "button" }` | ||
|
||
<eslint-code-block :rules="{'vue/no-restricted-block': ['error', { element: 'marquee' }, { element: 'button' }]}"> | ||
ota-meshi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
```vue | ||
<!-- ✗ BAD --> | ||
<marquee></marquee> | ||
<button></button> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-restricted-html-elements.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-restricted-html-elements.js) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* @author Doug Wade <douglas.b.wade@gmail.com> | ||
*/ | ||
|
||
'use strict' | ||
|
||
const utils = require('../utils') | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'disallow specific HTML elements', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-restricted-html-elements.html' | ||
}, | ||
fixable: null, | ||
schema: { | ||
type: 'array', | ||
items: { | ||
oneOf: [ | ||
{ type: 'string' }, | ||
{ | ||
type: 'object', | ||
properties: { | ||
element: { type: 'string' }, | ||
message: { type: 'string', minLength: 1 } | ||
}, | ||
required: ['element'], | ||
additionalProperties: false | ||
} | ||
] | ||
}, | ||
uniqueItems: true, | ||
minItems: 0 | ||
} | ||
}, | ||
/** | ||
* @param {RuleContext} context - The rule context. | ||
* @returns {RuleListener} AST event handlers. | ||
*/ | ||
create(context) { | ||
return utils.defineTemplateBodyVisitor(context, { | ||
/** | ||
* @param {VElement} node | ||
*/ | ||
VElement(node) { | ||
if (!utils.isHtmlElementNode(node)) { | ||
return | ||
} | ||
|
||
context.options.forEach((option) => { | ||
const message = | ||
option.message || | ||
`Unexpected use of forbidden HTML element ${node.rawName}.` | ||
const element = option.element || option | ||
|
||
if (element === node.rawName) { | ||
context.report({ | ||
message, | ||
node: node.startTag | ||
}) | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* @author Doug Wade | ||
*/ | ||
'use strict' | ||
|
||
const RuleTester = require('eslint').RuleTester | ||
const rule = require('../../../lib/rules/no-restricted-html-elements') | ||
|
||
const tester = new RuleTester({ | ||
parser: require.resolve('vue-eslint-parser'), | ||
parserOptions: { ecmaVersion: 2015 } | ||
}) | ||
|
||
tester.run('no-restricted-html-elements', rule, { | ||
valid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: '', | ||
options: ['button'] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div class="foo"></div></template>', | ||
options: ['button'] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><button type="button"></button></template>', | ||
options: ['div'] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div class="foo"><Button type="button"></Button></div></template>', | ||
options: ['button'] | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div><button type="button"></button><div></template>', | ||
errors: [ | ||
{ | ||
message: 'Unexpected use of forbidden HTML element button.', | ||
line: 1, | ||
column: 16 | ||
} | ||
], | ||
options: ['button'] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><div class="foo"><button type="button"></button></div></template>', | ||
errors: [ | ||
{ | ||
message: 'Unexpected use of forbidden HTML element div.', | ||
line: 1, | ||
column: 11 | ||
} | ||
], | ||
options: ['div'] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: '<template><marquee>foo</marquee></template>', | ||
errors: [ | ||
{ | ||
message: 'Custom error', | ||
line: 1, | ||
column: 11 | ||
} | ||
], | ||
options: [{ element: 'marquee', message: 'Custom error' }] | ||
} | ||
] | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.