Skip to content

Commit

Permalink
Add vue/no-undef-components rule and deprecate `vue/no-unregistered…
Browse files Browse the repository at this point in the history
…-components` rule (#1763)

* Add `vue/no-undef-components-in-script-setup` rule

* fix test case

* Renamed rule and marked no-unregistered-components as deprecated

* update doc

* update doc

* fix doc

* Update docs/rules/no-undef-components.md

Co-authored-by: Flo Edelmann <florian-edelmann@online.de>

Co-authored-by: Flo Edelmann <florian-edelmann@online.de>
  • Loading branch information
ota-meshi and FloEdelmann committed Jan 19, 2022
1 parent 09bc534 commit ee1e1e5
Show file tree
Hide file tree
Showing 7 changed files with 1,379 additions and 2 deletions.
3 changes: 2 additions & 1 deletion docs/rules/README.md
Expand Up @@ -341,8 +341,8 @@ For example:
| [vue/no-static-inline-styles](./no-static-inline-styles.md) | disallow static inline `style` attributes | |
| [vue/no-template-target-blank](./no-template-target-blank.md) | disallow target="_blank" attribute without rel="noopener noreferrer" | |
| [vue/no-this-in-before-route-enter](./no-this-in-before-route-enter.md) | disallow `this` usage in a `beforeRouteEnter` method | |
| [vue/no-undef-components](./no-undef-components.md) | disallow use of undefined components in `<template>` | |
| [vue/no-undef-properties](./no-undef-properties.md) | disallow undefined properties | |
| [vue/no-unregistered-components](./no-unregistered-components.md) | disallow using components that are not registered inside templates | |
| [vue/no-unsupported-features](./no-unsupported-features.md) | disallow unsupported Vue.js syntax on the specified version | :wrench: |
| [vue/no-unused-properties](./no-unused-properties.md) | disallow unused properties | |
| [vue/no-unused-refs](./no-unused-refs.md) | disallow unused refs | |
Expand Down Expand Up @@ -414,3 +414,4 @@ The following rules extend the rules provided by ESLint itself and apply them to
| [vue/experimental-script-setup-vars](./experimental-script-setup-vars.md) | (no replacement) |
| [vue/name-property-casing](./name-property-casing.md) | [vue/component-definition-name-casing](./component-definition-name-casing.md) |
| [vue/no-confusing-v-for-v-if](./no-confusing-v-for-v-if.md) | [vue/no-use-v-if-with-v-for](./no-use-v-if-with-v-for.md) |
| [vue/no-unregistered-components](./no-unregistered-components.md) | [vue/no-undef-components](./no-undef-components.md) |
178 changes: 178 additions & 0 deletions docs/rules/no-undef-components.md
@@ -0,0 +1,178 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/no-undef-components
description: disallow use of undefined components in `<template>`
---
# vue/no-undef-components

> disallow use of undefined components in `<template>`
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>

This rule warns reports component that are used in the `<template>`, but that are not defined in the `<script setup>` or the Options API's `components` section.

Undefined components will be resolved from globally registered components. However, if you are not using global components, you can use this rule to prevent run-time errors.

::: warning Note
This rule cannot check globally registered components and components registered in mixins
unless you add them as part of the ignored patterns.
:::

<eslint-code-block :rules="{'vue/no-undef-components': ['error']}">

```vue
<script setup>
import Foo from './Foo.vue'
</script>
<template>
<!-- ✓ GOOD -->
<Foo />
<!-- ✗ BAD -->
<Bar />
</template>
```

</eslint-code-block>

<eslint-code-block :rules="{'vue/no-undef-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-undef-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-undef-components": ["error", {
"ignorePatterns": []
}]
}
```

- `ignorePatterns` Suppresses all errors if component name matches one or more patterns.

### `ignorePatterns: ['custom(\\-\\w+)+']`

<eslint-code-block :rules="{'vue/no-undef-components': ['error', { 'ignorePatterns': ['custom(\\-\\w+)+'] }]}">

```vue
<script setup>
</script>
<template>
<!-- ✓ GOOD -->
<CustomComponent />
<!-- ✗ BAD -->
<Bar />
</template>
```

</eslint-code-block>

<eslint-code-block :rules="{'vue/no-undef-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-undef-components': ['error', { 'ignorePatterns': ['custom(\\-\\w+)+'] }]}">

```vue
<!-- ✗ BAD -->
<template>
<div>
<h2>Lorem ipsum</h2>
<WarmButton />
</div>
</template>
<script>
export default {
components: {
},
}
</script>
```

</eslint-code-block>

## :couple: Related Rules

- [vue/no-undef-properties](./no-undef-properties.md)

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-undef-components.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-undef-components.js)
2 changes: 2 additions & 0 deletions docs/rules/no-unregistered-components.md
Expand Up @@ -9,6 +9,8 @@ since: v7.0.0

> disallow using components that are not registered inside templates
- :warning: This rule was **deprecated** and replaced by [vue/no-undef-components](no-undef-components.md) rule.

## :book: Rule Details

This rule reports components that haven't been registered and are being used in the template.
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -128,6 +128,7 @@ module.exports = {
'no-template-target-blank': require('./rules/no-template-target-blank'),
'no-textarea-mustache': require('./rules/no-textarea-mustache'),
'no-this-in-before-route-enter': require('./rules/no-this-in-before-route-enter'),
'no-undef-components': require('./rules/no-undef-components'),
'no-undef-properties': require('./rules/no-undef-properties'),
'no-unregistered-components': require('./rules/no-unregistered-components'),
'no-unsupported-features': require('./rules/no-unsupported-features'),
Expand Down

0 comments on commit ee1e1e5

Please sign in to comment.