-
-
Notifications
You must be signed in to change notification settings - Fork 668
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
restricted-component-names
rule (#2611)
Co-authored-by: Flo Edelmann <[email protected]>
- Loading branch information
1 parent
9ddf3e5
commit bed816b
Showing
6 changed files
with
230 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 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 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,66 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/restricted-component-names | ||
description: enforce using only specific in component names | ||
--- | ||
|
||
# vue/restricted-component-names | ||
|
||
> enforce using only specific 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/restricted-component-names': ['error'] }"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<button/> | ||
<keep-alive></keep-alive> | ||
<!-- ✗ BAD --> | ||
<custom-component /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"vue/restricted-component-names": ["error", { | ||
"allow": [] | ||
}] | ||
} | ||
``` | ||
|
||
### `"allow"` | ||
|
||
<eslint-code-block :rules="{'vue/restricted-component-names': ['error', { 'allow': ['/^custom-/'] }]}"> | ||
|
||
```vue | ||
<template> | ||
<!-- ✓ GOOD --> | ||
<custom-component /> | ||
<!-- ✗ BAD --> | ||
<my-component /> | ||
</template> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :couple: Related Rules | ||
|
||
- [vue/no-restricted-component-names](./no-restricted-component-names.md) | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/restricted-component-names.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/restricted-component-names.js) |
This file contains 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 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,80 @@ | ||
/** | ||
* @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 vue2builtinComponents = require('../utils/vue2-builtin-components') | ||
const vue3builtinComponents = require('../utils/vue3-builtin-components') | ||
|
||
const reservedNames = new Set([ | ||
...htmlElements, | ||
...deprecatedHtmlElements, | ||
...svgElements, | ||
...vue2builtinComponents, | ||
...vue3builtinComponents | ||
]) | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'enforce using only specific component names', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/restricted-component-names.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 allowed.' | ||
} | ||
}, | ||
/** @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 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/restricted-component-names') | ||
|
||
const tester = new RuleTester({ | ||
languageOptions: { | ||
parser: require('vue-eslint-parser'), | ||
ecmaVersion: 2020, | ||
sourceType: 'module' | ||
} | ||
}) | ||
|
||
tester.run('restricted-component-names', 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 | ||
} | ||
] | ||
} | ||
] | ||
}) |