-
-
Notifications
You must be signed in to change notification settings - Fork 681
feat: add restricted-component-names
rule
#2611
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 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
235076b
feat: init rule
waynzh 3f1ea34
docs: update
waynzh 148cc44
refactor: rename
waynzh 902db38
feat: update suggestions
waynzh 29a42bf
Update lib/rules/restricted-component-names.js
waynzh 0e2a3e5
remove unused
waynzh 8e0ba1c
Update lib/rules/restricted-component-names.js
waynzh 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,62 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/valid-component-name | ||
description: enforce consistency in component names | ||
--- | ||
|
||
# vue/valid-component-name | ||
|
||
> enforce consistency in component names | ||
|
||
- :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 enforces consistency in component names. | ||
|
||
<eslint-code-block :rules="{ 'vue/valid-component-name': ['error'] }"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<button/> | ||
<keep-alive></keep-alive> | ||
|
||
<!-- ✗ BAD --> | ||
<custom-component /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"vue/valid-component-name": ["error", { | ||
"allow": [] | ||
}] | ||
} | ||
``` | ||
|
||
### `"allow"` | ||
|
||
<eslint-code-block :rules="{'vue/valid-component-name': ['error', { 'allow': ['/^custom-/'] }]}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<custom-component /> | ||
|
||
<!-- ✗ BAD --> | ||
<my-component /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/valid-component-name.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/valid-component-name.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,101 @@ | ||
/** | ||
* @author Wayne Zhang | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
const { toRegExp } = require('../utils/regexp') | ||
|
||
const htmlElements = require('../utils/html-elements.json') | ||
const deprecatedHtmlElements = require('../utils/deprecated-html-elements.json') | ||
const svgElements = require('../utils/svg-elements.json') | ||
|
||
const RESERVED_NAMES_IN_VUE = new Set( | ||
require('../utils/vue2-builtin-components') | ||
) | ||
const RESERVED_NAMES_IN_VUE3 = new Set( | ||
require('../utils/vue3-builtin-components') | ||
) | ||
const kebabCaseElements = [ | ||
'annotation-xml', | ||
'color-profile', | ||
'font-face', | ||
'font-face-src', | ||
'font-face-uri', | ||
'font-face-format', | ||
'font-face-name', | ||
'missing-glyph' | ||
] | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const RESERVED_NAMES_IN_HTML = new Set(htmlElements) | ||
const RESERVED_NAMES_IN_OTHERS = new Set([ | ||
...deprecatedHtmlElements, | ||
...kebabCaseElements, | ||
...svgElements | ||
]) | ||
|
||
const reservedNames = new Set([ | ||
...RESERVED_NAMES_IN_HTML, | ||
...RESERVED_NAMES_IN_VUE, | ||
...RESERVED_NAMES_IN_VUE3, | ||
...RESERVED_NAMES_IN_OTHERS | ||
]) | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'enforce consistency in component names', | ||
FloEdelmann marked this conversation as resolved.
Show resolved
Hide resolved
|
||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/valid-component-name.html' | ||
}, | ||
fixable: null, | ||
schema: [ | ||
{ | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
allow: { | ||
type: 'array', | ||
items: { type: 'string' }, | ||
uniqueItems: true, | ||
additionalItems: false | ||
} | ||
} | ||
} | ||
], | ||
messages: { | ||
invalidName: 'Component name "{{name}}" is not valid.' | ||
waynzh marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
const options = context.options[0] || {} | ||
/** @type {RegExp[]} */ | ||
const allow = (options.allow || []).map(toRegExp) | ||
|
||
/** @param {string} name */ | ||
function isAllowedTarget(name) { | ||
return reservedNames.has(name) || allow.some((re) => re.test(name)) | ||
} | ||
|
||
return utils.defineTemplateBodyVisitor(context, { | ||
VElement(node) { | ||
const name = node.rawName | ||
if (isAllowedTarget(name)) { | ||
return | ||
} | ||
|
||
context.report({ | ||
node, | ||
loc: node.loc, | ||
messageId: 'invalidName', | ||
data: { | ||
name | ||
} | ||
}) | ||
} | ||
}) | ||
} | ||
} |
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,78 @@ | ||
/** | ||
* @author Wayne Zhang | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const RuleTester = require('../../eslint-compat').RuleTester | ||
const rule = require('../../../lib/rules/valid-component-name') | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
parser: require('vue-eslint-parser'), | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}) | ||
|
||
tester.run('valid-component-name', rule, { | ||
valid: [ | ||
'<template><keep-alive></keep-alive></template>', | ||
'<template><button/></template>', | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<foo-button/> | ||
<div-bar/> | ||
</template> | ||
`, | ||
options: [{ allow: ['/^foo-/', '/-bar$/'] }] | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<Button/> | ||
<foo-button/> | ||
</template> | ||
`, | ||
errors: [ | ||
{ | ||
messageId: 'invalidName', | ||
data: { name: 'Button' }, | ||
line: 3 | ||
}, | ||
{ | ||
messageId: 'invalidName', | ||
data: { name: 'foo-button' }, | ||
line: 4 | ||
} | ||
] | ||
}, | ||
{ | ||
filename: 'test.vue', | ||
code: ` | ||
<template> | ||
<bar-button/> | ||
<foo/> | ||
</template> | ||
`, | ||
options: [{ allow: ['/^foo-/', 'bar'] }], | ||
errors: [ | ||
{ | ||
messageId: 'invalidName', | ||
data: { name: 'bar-button' }, | ||
line: 3 | ||
}, | ||
{ | ||
messageId: 'invalidName', | ||
data: { name: 'foo' }, | ||
line: 4 | ||
} | ||
] | ||
} | ||
] | ||
}) |
Oops, something went wrong.
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.