Skip to content
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

Add vue/no-undef-properties rule #1472

Merged
merged 3 commits into from Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/README.md
Expand Up @@ -319,6 +319,7 @@ 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-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 | |
Expand Down
114 changes: 114 additions & 0 deletions docs/rules/no-undef-properties.md
@@ -0,0 +1,114 @@
---
pageClass: rule-details
sidebarDepth: 0
title: vue/no-undef-properties
description: disallow undefined properties
---
# vue/no-undef-properties

> disallow undefined properties

- :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 warns of using undefined properties.
This rule can help you locate potential errors resulting from misspellings property names, and implicitly added properties.

::: warning Note
This rule cannot detect properties defined in other files or components.
Note that there are many false positives if you are using mixins.
:::

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

```vue
<template>
<!-- ✓ GOOD -->
<div>{{ name }}: {{ count }}</div>
<!-- ✗ BAD -->
<div>{{ label }}: {{ cnt }}</div>
</template>
<script setup>
const prop = defineProps(['name', 'def'])
let count = 0

/* ✓ GOOD */
watch(() => prop.def, () => console.log('Updated!'))

/* ✗ BAD */
watch(() => prop.undef, () => console.log('Updated!'))
</script>
```

</eslint-code-block>

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

```vue
<template>
<!-- ✓ GOOD -->
<div>{{ name }}: {{ count }}</div>
<!-- ✗ BAD -->
<div>{{ label }}: {{ cnt }}</div>
</template>
<script>
export default {
props: ['name'],
data () {
return {
count: 0
}
},
methods: {
click() {
/* ✓ GOOD */
this.count++

/* ✗ BAD */
this.cnt++
}
}
}
</script>
```

</eslint-code-block>

## :wrench: Options

```json
{
"vue/no-undef-properties": ["error", {
"ignores": ["/^\\$/"]
}]
}
```

- `ignores` (`string[]`) ... An array of property names or patterns that have already been defined property, or property to ignore from the check. Default is `["/^\\$/"]`.

### `"ignores": ["/^\\$/"]` (default)

<eslint-code-block :rules="{'vue/no-undef-properties': ['error', {ignores: ['/^\\$/']}]}">

```vue
<template>
<!-- ✓ GOOD -->
<div>{{ $t('foo') }}</div>
</template>
<script>
export default {
mounted() {
/* ✓ GOOD */
const hash = this.$route.hash
}
}
</script>
```

</eslint-code-block>

## :mag: Implementation

- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-undef-properties.js)
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-undef-properties.js)
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -119,6 +119,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-properties': require('./rules/no-undef-properties'),
'no-unregistered-components': require('./rules/no-unregistered-components'),
'no-unsupported-features': require('./rules/no-unsupported-features'),
'no-unused-components': require('./rules/no-unused-components'),
Expand Down