-
-
Notifications
You must be signed in to change notification settings - Fork 690
⭐️ New: Add rule no-reserved-component-names
#757
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 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
76b9fc4
:star: add rule no-reserved-component-names
shadskii e5fe027
Increase test coverage
shadskii 4c446b9
Merge remote-tracking branch 'upstream/master'
shadskii 33f5917
Checking elements against element lists
shadskii 2c04447
Merge remote-tracking branch 'upstream/master'
shadskii 792c29b
:hammer: Update PR with ota-meshi suggestions
shadskii 8975ab0
:hammer: Lint locally registered components
shadskii 8fe1d77
Merge remote-tracking branch 'upstream/master'
shadskii 52d5508
:ok_hand: Adding tests to validate slot and template
shadskii c4328d1
:hammer: Linting for deprecated elements
shadskii 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# vue/no-reserved-component-names | ||
> disallow the use of reserved names in component definitions | ||
|
||
- :gear: This rule is included in all of `"plugin:vue/essential"`, `"plugin:vue/recommended"`, and `"plugin:vue/strongly-recommended"`. | ||
|
||
## :book: Rule Details | ||
|
||
This rule prevents name collisions between vue components and standard html elements. | ||
|
||
<eslint-code-block :rules="{'vue/no-reserved-component-names': ['error']}"> | ||
|
||
```vue | ||
<script> | ||
/* ✗ BAD */ | ||
export default { | ||
name: 'div' | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :books: Further reading | ||
|
||
- [List of html elements](https://developer.mozilla.org/en-US/docs/Web/HTML/Element) | ||
- [List of SVG elements](https://developer.mozilla.org/en-US/docs/Web/SVG/Element) | ||
- [Kebab case elements](https://stackoverflow.com/questions/22545621/do-custom-elements-require-a-dash-in-their-name/22545622#22545622) | ||
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,113 @@ | ||
/** | ||
* @fileoverview disallow the use of reserved names in component definitions | ||
* @author Jake Hassel <https://github.com/shadskii> | ||
*/ | ||
'use strict' | ||
|
||
const utils = require('../utils') | ||
const casing = require('../utils/casing') | ||
|
||
const htmlElements = require('../utils/html-elements.json') | ||
const svgElements = require('../utils/svg-elements.json') | ||
|
||
const kebabCaseElements = [ | ||
'annotation-xml', | ||
'color-profile', | ||
'font-face', | ||
'font-face-src', | ||
'font-face-uri', | ||
'font-face-format', | ||
'font-face-name', | ||
'missing-glyph' | ||
] | ||
|
||
const isLowercase = (word) => (/[a-z]/.test(word)) | ||
shadskii marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const capitalizeFirstLetter = (word) => word[0].toUpperCase() + word.substring(1, word.length) | ||
|
||
const RESERVED_NAMES = new Set( | ||
[ | ||
...kebabCaseElements, | ||
...kebabCaseElements.map(casing.pascalCase), | ||
...htmlElements, | ||
...htmlElements.map(capitalizeFirstLetter), | ||
...svgElements, | ||
...svgElements.filter(isLowercase).map(capitalizeFirstLetter) | ||
]) | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'disallow the use of reserved names in component definitions', | ||
category: 'essential', | ||
shadskii marked this conversation as resolved.
Show resolved
Hide resolved
|
||
url: 'https://eslint.vuejs.org/rules/no-reserved-component-names.html' | ||
}, | ||
fixable: null, | ||
schema: [] | ||
}, | ||
|
||
create (context) { | ||
function canVerify (node) { | ||
return node.type === 'Literal' || ( | ||
node.type === 'TemplateLiteral' && | ||
node.expressions.length === 0 && | ||
node.quasis.length === 1 | ||
) | ||
} | ||
|
||
function reportIfInvalid (node) { | ||
let nodeValue | ||
if (node.type === 'TemplateLiteral') { | ||
const quasis = node.quasis[0] | ||
nodeValue = quasis.value.cooked | ||
} else { | ||
nodeValue = node.value | ||
} | ||
if (RESERVED_NAMES.has(nodeValue)) { | ||
context.report({ | ||
node: node, | ||
message: 'Name "{{value}}" is reserved.', | ||
data: { | ||
value: nodeValue | ||
} | ||
}) | ||
} | ||
} | ||
|
||
return Object.assign({}, | ||
{ | ||
"CallExpression > MemberExpression > Identifier[name='component']" (node) { | ||
const parent = node.parent.parent | ||
const calleeObject = utils.unwrapTypes(parent.callee.object) | ||
|
||
if (calleeObject.type === 'Identifier' && | ||
calleeObject.name === 'Vue' && | ||
parent.arguments && | ||
parent.arguments.length === 2 | ||
) { | ||
const argument = parent.arguments[0] | ||
|
||
if (canVerify(argument)) { | ||
reportIfInvalid(argument) | ||
} | ||
} | ||
} | ||
}, | ||
utils.executeOnVue(context, (obj) => { | ||
const node = obj.properties | ||
.find(item => ( | ||
item.type === 'Property' && | ||
item.key.name === 'name' && | ||
canVerify(item.value) | ||
)) | ||
|
||
if (!node) return | ||
reportIfInvalid(node.value) | ||
}) | ||
) | ||
} | ||
} |
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.