-
-
Notifications
You must be signed in to change notification settings - Fork 679
Add vue/no-restricted-component-names
rule
#2210
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 all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
05082d0
Add `vue/no-restricted-component-names` rule
ItMaga 3bd9aed
lint fixes
ItMaga 16b785b
fix message format
ItMaga d593a76
fix tests
ItMaga b4a395f
fix docs
ItMaga 5ac4312
resolve suggestions
ItMaga a716ec9
change vue visitor
ItMaga 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,89 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-restricted-component-names | ||
description: disallow specific component names | ||
--- | ||
# vue/no-restricted-component-names | ||
|
||
> disallow specific component names | ||
|
||
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge> | ||
- :bulb: Some problems reported by this rule are manually fixable by editor [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions). | ||
|
||
## :book: Rule Details | ||
|
||
This rule allows you to specify component names that you don't want to use in your application. | ||
|
||
<eslint-code-block :rules="{'vue/no-restricted-component-names': ['error', 'Disallow']}"> | ||
|
||
```vue | ||
<!-- ✗ BAD --> | ||
<script> | ||
export default { | ||
name: 'Disallow', | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/no-restricted-component-names': ['error', 'Disallow']}"> | ||
|
||
```vue | ||
<!-- ✓ GOOD --> | ||
<script> | ||
export default { | ||
name: 'Allow', | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
This rule takes a list of strings, where each string is a component name or pattern to be restricted: | ||
|
||
```json | ||
{ | ||
"vue/no-restricted-component-names": ["error", "foo", "/^Disallow/"] | ||
} | ||
``` | ||
|
||
Alternatively, you can specify an object with a `name` property and an optional `message` and `suggest` property: | ||
|
||
```json | ||
{ | ||
"vue/no-restricted-component-names": [ | ||
"error", | ||
{ | ||
"name": "Disallow", | ||
"message": "Please do not use `Disallow` as a component name", | ||
"suggest": "allow" | ||
}, | ||
{ | ||
"name": "/^custom/", | ||
"message": "Please do not use component names starting with 'custom'" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
<eslint-code-block :rules="{'vue/no-restricted-component-names': ['error', { name: 'Disallow', message: 'Please do not use \'Disallow\' as a component name', suggest: 'allow'}]}"> | ||
|
||
```vue | ||
<!-- ✗ BAD --> | ||
<script> | ||
export default { | ||
name: 'Disallow', | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-restricted-component-names.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-restricted-component-names.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,157 @@ | ||
/** | ||
* @author ItMaga <https://github.com/ItMaga> | ||
* See LICENSE file in root directory for full license. | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
const casing = require('../utils/casing') | ||
const { isRegExp, toRegExp } = require('../utils/regexp') | ||
|
||
/** | ||
* @typedef {object} OptionParsed | ||
* @property { (name: string) => boolean } test | ||
* @property {string|undefined} [message] | ||
* @property {string|undefined} [suggest] | ||
*/ | ||
|
||
/** | ||
* @param {string} str | ||
* @returns {(str: string) => boolean} | ||
* @private | ||
*/ | ||
function buildMatcher(str) { | ||
if (isRegExp(str)) { | ||
const regex = toRegExp(str) | ||
return (s) => regex.test(s) | ||
} | ||
return (s) => s === casing.pascalCase(str) || s === casing.kebabCase(str) | ||
} | ||
|
||
/** | ||
* @param {string|{name: string, message?: string, suggest?: string}} option | ||
* @returns {OptionParsed} | ||
* @private | ||
* */ | ||
function parseOption(option) { | ||
if (typeof option === 'string') { | ||
const matcher = buildMatcher(option) | ||
return { test: matcher } | ||
} | ||
const parsed = parseOption(option.name) | ||
parsed.message = option.message | ||
parsed.suggest = option.suggest | ||
return parsed | ||
} | ||
|
||
/** | ||
* @param {Property | AssignmentProperty} property | ||
* @param {string | undefined} suggest | ||
* @returns {Rule.SuggestionReportDescriptor[]} | ||
* @private | ||
* */ | ||
function createSuggest(property, suggest) { | ||
if (!suggest) { | ||
return [] | ||
} | ||
|
||
return [ | ||
{ | ||
fix(fixer) { | ||
return fixer.replaceText(property.value, JSON.stringify(suggest)) | ||
}, | ||
messageId: 'suggest', | ||
data: { suggest } | ||
} | ||
] | ||
} | ||
|
||
module.exports = { | ||
meta: { | ||
hasSuggestions: true, | ||
type: 'suggestion', | ||
docs: { | ||
description: 'disallow specific component names', | ||
categories: undefined, | ||
url: 'https://eslint.vuejs.org/rules/no-restricted-component-names.html' | ||
}, | ||
fixable: null, | ||
schema: { | ||
type: 'array', | ||
items: { | ||
oneOf: [ | ||
{ type: 'string' }, | ||
{ | ||
type: 'object', | ||
properties: { | ||
name: { type: 'string' }, | ||
message: { type: 'string', minLength: 1 }, | ||
suggest: { type: 'string' } | ||
}, | ||
required: ['name'], | ||
additionalProperties: false | ||
} | ||
] | ||
}, | ||
uniqueItems: true, | ||
minItems: 0 | ||
}, | ||
messages: { | ||
// eslint-disable-next-line eslint-plugin/report-message-format | ||
disallow: '{{message}}', | ||
suggest: 'Instead, change to `{{suggest}}`.' | ||
} | ||
}, | ||
/** @param {RuleContext} context */ | ||
create(context) { | ||
/** @type {OptionParsed[]} */ | ||
const options = context.options.map(parseOption) | ||
|
||
/** | ||
* @param {ObjectExpression} node | ||
*/ | ||
function verify(node) { | ||
const property = utils.findProperty(node, 'name') | ||
if (!property) return | ||
|
||
const propertyName = utils.getStaticPropertyName(property) | ||
if (propertyName === 'name' && property.value.type === 'Literal') { | ||
const componentName = property.value.value?.toString() | ||
if (!componentName) { | ||
return | ||
} | ||
|
||
for (const option of options) { | ||
if (option.test(componentName)) { | ||
context.report({ | ||
node: property.value, | ||
messageId: 'disallow', | ||
data: { | ||
message: | ||
option.message || | ||
`Using component name \`${componentName}\` is not allowed.` | ||
}, | ||
suggest: createSuggest(property, option.suggest) | ||
}) | ||
} | ||
} | ||
} | ||
} | ||
|
||
return utils.compositingVisitors( | ||
utils.defineVueVisitor(context, { | ||
onVueObjectEnter(node) { | ||
verify(node) | ||
} | ||
}), | ||
utils.defineScriptSetupVisitor(context, { | ||
onDefineOptionsEnter(node) { | ||
const expression = node.arguments[0] | ||
if (expression.type === 'ObjectExpression') { | ||
verify(expression) | ||
} | ||
} | ||
}) | ||
) | ||
} | ||
} |
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.