Skip to content

Commit 852096e

Browse files
authored
Add vue/no-undef-properties rule (#1472)
* Add `vue/no-undef-properties` rule * support script setup * refactor
1 parent 31a5afa commit 852096e

File tree

8 files changed

+1877
-43
lines changed

8 files changed

+1877
-43
lines changed

docs/rules/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ For example:
319319
| [vue/no-static-inline-styles](./no-static-inline-styles.md) | disallow static inline `style` attributes | |
320320
| [vue/no-template-target-blank](./no-template-target-blank.md) | disallow target="_blank" attribute without rel="noopener noreferrer" | |
321321
| [vue/no-this-in-before-route-enter](./no-this-in-before-route-enter.md) | disallow `this` usage in a `beforeRouteEnter` method | |
322+
| [vue/no-undef-properties](./no-undef-properties.md) | disallow undefined properties | |
322323
| [vue/no-unregistered-components](./no-unregistered-components.md) | disallow using components that are not registered inside templates | |
323324
| [vue/no-unsupported-features](./no-unsupported-features.md) | disallow unsupported Vue.js syntax on the specified version | :wrench: |
324325
| [vue/no-unused-properties](./no-unused-properties.md) | disallow unused properties | |

docs/rules/no-undef-properties.md

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
---
2+
pageClass: rule-details
3+
sidebarDepth: 0
4+
title: vue/no-undef-properties
5+
description: disallow undefined properties
6+
---
7+
# vue/no-undef-properties
8+
9+
> disallow undefined properties
10+
11+
- :exclamation: <badge text="This rule has not been released yet." vertical="middle" type="error"> ***This rule has not been released yet.*** </badge>
12+
13+
## :book: Rule Details
14+
15+
This rule warns of using undefined properties.
16+
This rule can help you locate potential errors resulting from misspellings property names, and implicitly added properties.
17+
18+
::: warning Note
19+
This rule cannot detect properties defined in other files or components.
20+
Note that there are many false positives if you are using mixins.
21+
:::
22+
23+
<eslint-code-block :rules="{'vue/no-undef-properties': ['error']}">
24+
25+
```vue
26+
<template>
27+
<!-- ✓ GOOD -->
28+
<div>{{ name }}: {{ count }}</div>
29+
<!-- ✗ BAD -->
30+
<div>{{ label }}: {{ cnt }}</div>
31+
</template>
32+
<script setup>
33+
const prop = defineProps(['name', 'def'])
34+
let count = 0
35+
36+
/* ✓ GOOD */
37+
watch(() => prop.def, () => console.log('Updated!'))
38+
39+
/* ✗ BAD */
40+
watch(() => prop.undef, () => console.log('Updated!'))
41+
</script>
42+
```
43+
44+
</eslint-code-block>
45+
46+
<eslint-code-block :rules="{'vue/no-undef-properties': ['error']}">
47+
48+
```vue
49+
<template>
50+
<!-- ✓ GOOD -->
51+
<div>{{ name }}: {{ count }}</div>
52+
<!-- ✗ BAD -->
53+
<div>{{ label }}: {{ cnt }}</div>
54+
</template>
55+
<script>
56+
export default {
57+
props: ['name'],
58+
data () {
59+
return {
60+
count: 0
61+
}
62+
},
63+
methods: {
64+
click() {
65+
/* ✓ GOOD */
66+
this.count++
67+
68+
/* ✗ BAD */
69+
this.cnt++
70+
}
71+
}
72+
}
73+
</script>
74+
```
75+
76+
</eslint-code-block>
77+
78+
## :wrench: Options
79+
80+
```json
81+
{
82+
"vue/no-undef-properties": ["error", {
83+
"ignores": ["/^\\$/"]
84+
}]
85+
}
86+
```
87+
88+
- `ignores` (`string[]`) ... An array of property names or patterns that have already been defined property, or property to ignore from the check. Default is `["/^\\$/"]`.
89+
90+
### `"ignores": ["/^\\$/"]` (default)
91+
92+
<eslint-code-block :rules="{'vue/no-undef-properties': ['error', {ignores: ['/^\\$/']}]}">
93+
94+
```vue
95+
<template>
96+
<!-- ✓ GOOD -->
97+
<div>{{ $t('foo') }}</div>
98+
</template>
99+
<script>
100+
export default {
101+
mounted() {
102+
/* ✓ GOOD */
103+
const hash = this.$route.hash
104+
}
105+
}
106+
</script>
107+
```
108+
109+
</eslint-code-block>
110+
111+
## :mag: Implementation
112+
113+
- [Rule source](https://github.com/vuejs/eslint-plugin-vue/blob/master/lib/rules/no-undef-properties.js)
114+
- [Test source](https://github.com/vuejs/eslint-plugin-vue/blob/master/tests/lib/rules/no-undef-properties.js)

lib/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ module.exports = {
119119
'no-template-target-blank': require('./rules/no-template-target-blank'),
120120
'no-textarea-mustache': require('./rules/no-textarea-mustache'),
121121
'no-this-in-before-route-enter': require('./rules/no-this-in-before-route-enter'),
122+
'no-undef-properties': require('./rules/no-undef-properties'),
122123
'no-unregistered-components': require('./rules/no-unregistered-components'),
123124
'no-unsupported-features': require('./rules/no-unsupported-features'),
124125
'no-unused-components': require('./rules/no-unused-components'),

0 commit comments

Comments
 (0)