-
-
Notifications
You must be signed in to change notification settings - Fork 681
Add vue/no-unregistered-components
rule
#1114
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 12 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
83a3b84
Add `vue/no-unregistered-components` rule
jesusgn90 e3d7614
Remove rule from configs/essential
jesusgn90 d892805
Extend allowed ignore patterns
jesusgn90 9c1aa2e
Add note about globally/mixins registered components + use strings fo…
jesusgn90 281bc06
(auto) update rules index
jesusgn90 762bcfc
(auto) update rules lib index
jesusgn90 805f0a3
Fix PR review's concerns
jesusgn90 c5b0e7d
Add more rule tests to cover latest changes
jesusgn90 68db54a
Correct + add test for `suspense` and `teleport`
jesusgn90 6126d35
Restore not intended change in package.json
jesusgn90 d0b15ef
Progress with PR review
jesusgn90 b23457d
Handle edge case `<component is="div" />`
jesusgn90 7c3198c
Progress with PR review
jesusgn90 7f6e275
Remove unused block
jesusgn90 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,137 @@ | ||
--- | ||
pageClass: rule-details | ||
sidebarDepth: 0 | ||
title: vue/no-unregistered-components | ||
description: disallow using components that are not registered inside templates | ||
--- | ||
# vue/no-unregistered-components | ||
> disallow using components that are not registered inside templates | ||
|
||
## :book: Rule Details | ||
|
||
This rule reports components that haven't been registered and are being used in the template. | ||
|
||
::: warning Note | ||
This rule cannot check globally registered components and components registered in mixins | ||
unless you add them as part of the ignored patterns. `component`, `suspense` and `teleport` | ||
are ignored by default. | ||
::: | ||
|
||
<eslint-code-block :rules="{'vue/no-unregistered-components': ['error']}"> | ||
|
||
```vue | ||
<!-- ✓ GOOD --> | ||
<template> | ||
<div> | ||
<h2>Lorem ipsum</h2> | ||
<the-modal> | ||
<component is="TheInput" /> | ||
<component :is="'TheDropdown'" /> | ||
<TheButton>CTA</TheButton> | ||
</the-modal> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
import TheButton from 'components/TheButton.vue' | ||
import TheModal from 'components/TheModal.vue' | ||
import TheInput from 'components/TheInput.vue' | ||
import TheDropdown from 'components/TheDropdown.vue' | ||
|
||
export default { | ||
components: { | ||
TheButton, | ||
TheModal, | ||
TheInput, | ||
TheDropdown, | ||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/no-unregistered-components': ['error']}"> | ||
|
||
```vue | ||
<!-- ✗ BAD --> | ||
<template> | ||
<div> | ||
<h2>Lorem ipsum</h2> | ||
<TheModal /> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
components: { | ||
|
||
} | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :wrench: Options | ||
|
||
```json | ||
{ | ||
"vue/no-unregistered-components": ["error", { | ||
"ignorePatterns": [] | ||
}] | ||
} | ||
``` | ||
|
||
- `ignorePatterns` Suppresses all errors if component name matches one or more patterns. | ||
|
||
### `ignorePatterns: ['custom(\\-\\w+)+']` | ||
|
||
<eslint-code-block :rules="{'vue/no-unregistered-components': ['error', { 'ignorePatterns': ['custom(\\-\\w+)+'] }]}"> | ||
|
||
```vue | ||
<!-- ✓ GOOD --> | ||
<template> | ||
<div> | ||
<h2>Lorem ipsum</h2> | ||
<CustomComponent /> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
components: { | ||
|
||
}, | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
<eslint-code-block :rules="{'vue/no-unregistered-components': ['error', { 'ignorePatterns': ['custom(\\-\\w+)+'] }]}"> | ||
|
||
```vue | ||
<!-- ✗ BAD --> | ||
<template> | ||
<div> | ||
<h2>Lorem ipsum</h2> | ||
<WarmButton /> | ||
</div> | ||
</template> | ||
|
||
<script> | ||
export default { | ||
components: { | ||
|
||
}, | ||
} | ||
</script> | ||
``` | ||
|
||
</eslint-code-block> | ||
|
||
## :mag: Implementation | ||
|
||
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-unregistered-components.js) | ||
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-unregistered-components.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,134 @@ | ||
/** | ||
* @fileoverview Report used components that are not registered | ||
* @author Jesús Ángel González Novez | ||
*/ | ||
'use strict' | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
const utils = require('eslint-plugin-vue/lib/utils') | ||
const casing = require('eslint-plugin-vue/lib/utils/casing') | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule helpers | ||
// ------------------------------------------------------------------------------ | ||
|
||
const VUE_BUILT_IN_COMPONENTS = [ | ||
'component', | ||
'suspense', | ||
'teleport', | ||
'transition', | ||
'transition-group', | ||
'keep-alive', | ||
'slot' | ||
] | ||
/** | ||
* Check whether the given node is a built-in component or not. | ||
* | ||
* Includes `suspense` and `teleport` from Vue 3. | ||
* | ||
* @param {ASTNode} node The start tag node to check. | ||
* @returns {boolean} `true` if the node is a built-in component. | ||
*/ | ||
const isBuiltInComponent = (node) => { | ||
const rawName = node && casing.kebabCase(node.rawName) | ||
return utils.isHtmlElementNode(node) && | ||
!utils.isHtmlWellKnownElementName(node.rawName) && | ||
VUE_BUILT_IN_COMPONENTS.indexOf(rawName) > -1 | ||
} | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
module.exports = { | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: 'disallow using components that are not registered inside templates', | ||
categories: null, | ||
recommended: false, | ||
url: 'https://eslint.vuejs.org/rules/no-unregistered-components.html' | ||
}, | ||
fixable: null, | ||
schema: [{ | ||
type: 'object', | ||
properties: { | ||
ignorePatterns: { | ||
type: 'array' | ||
} | ||
}, | ||
additionalProperties: false | ||
}] | ||
}, | ||
|
||
create (context) { | ||
const options = context.options[0] || {} | ||
const ignorePatterns = options.ignorePatterns || [] | ||
const usedComponentNodes = [] | ||
const registeredComponents = [] | ||
let templateLocation | ||
|
||
return utils.defineTemplateBodyVisitor(context, { | ||
VElement (node) { | ||
if ( | ||
(!utils.isHtmlElementNode(node) && !utils.isSvgElementNode(node)) || | ||
utils.isHtmlWellKnownElementName(node.rawName) || | ||
utils.isSvgWellKnownElementName(node.rawName) || | ||
isBuiltInComponent(node) | ||
) { | ||
return | ||
} | ||
|
||
usedComponentNodes.push({ node, name: node.rawName }) | ||
}, | ||
"VAttribute[directive=true][key.name.name='bind'][key.argument.name='is']" (node) { | ||
jesusgn90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( | ||
!node.value || | ||
node.value.type !== 'VExpressionContainer' || | ||
!node.value.expression | ||
) return | ||
|
||
if (node.value.expression.type === 'Literal') { | ||
if (utils.isHtmlWellKnownElementName(node.value.expression.value)) return | ||
usedComponentNodes.push({ node, name: node.value.expression.value }) | ||
} | ||
}, | ||
"VAttribute[directive=false][key.name='is']" (node) { | ||
if (utils.isHtmlWellKnownElementName(node.value.value)) return | ||
jesusgn90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
usedComponentNodes.push({ node, name: node.value.value }) | ||
}, | ||
"VElement[name='template']" (rootNode) { | ||
templateLocation = templateLocation || rootNode.loc.start | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This variable is no longer needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove the templateLocation variable. |
||
}, | ||
"VElement[name='template']:exit" (rootNode) { | ||
const registeredComponentNames = registeredComponents.map(({ name }) => casing.kebabCase(name)) | ||
|
||
usedComponentNodes | ||
.filter(({ name }) => { | ||
const kebabCaseName = casing.kebabCase(name) | ||
if (ignorePatterns.find(pattern => { | ||
const regExp = new RegExp(pattern) | ||
return regExp.test(kebabCaseName) || | ||
regExp.test(casing.pascalCase(name)) || | ||
regExp.test(casing.camelCase(name)) || | ||
regExp.test(casing.snakeCase(name)) || | ||
regExp.test(name) | ||
})) return false | ||
return registeredComponentNames.indexOf(kebabCaseName) === -1 | ||
jesusgn90 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
.forEach(({ node, name }) => context.report({ | ||
node, | ||
message: 'The "{{name}}" component has been used but not registered.', | ||
data: { | ||
name | ||
} | ||
})) | ||
} | ||
}, utils.executeOnVue(context, (obj) => { | ||
registeredComponents.push(...utils.getRegisteredComponents(obj)) | ||
})) | ||
} | ||
} |
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.