eslint-plugin-vue-no-static-computed 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +61 -0
- package/index.js +9 -0
- package/lib/no-static-computed.js +65 -0
- package/package.json +37 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Steven Degele
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# eslint-plugin-vue-no-static-computed
|
|
2
|
+
|
|
3
|
+
ESLint rule to disallow Vue `computed()` wrapping purely static values with no reactive dependencies.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install --save-dev eslint-plugin-vue-no-static-computed
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
In your ESLint flat config (`eslint.config.js`):
|
|
14
|
+
|
|
15
|
+
```js
|
|
16
|
+
import vueNoStaticComputed from 'eslint-plugin-vue-no-static-computed'
|
|
17
|
+
|
|
18
|
+
export default [
|
|
19
|
+
{
|
|
20
|
+
plugins: {
|
|
21
|
+
'vue-no-static-computed': vueNoStaticComputed,
|
|
22
|
+
},
|
|
23
|
+
rules: {
|
|
24
|
+
'vue-no-static-computed/no-static-computed': 'error',
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
]
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Rule Details
|
|
31
|
+
|
|
32
|
+
`no-static-computed` flags `computed()` calls where the getter returns a value with no reactive dependencies. These should be plain variables instead.
|
|
33
|
+
|
|
34
|
+
### Examples
|
|
35
|
+
|
|
36
|
+
**Bad:**
|
|
37
|
+
|
|
38
|
+
```js
|
|
39
|
+
import { computed } from 'vue'
|
|
40
|
+
|
|
41
|
+
const items = computed(() => ['a', 'b', 'c'])
|
|
42
|
+
const config = computed(() => ({ key: 'value' }))
|
|
43
|
+
const label = computed(() => 'hello')
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
**Good:**
|
|
47
|
+
|
|
48
|
+
```js
|
|
49
|
+
import { computed, ref } from 'vue'
|
|
50
|
+
|
|
51
|
+
const items = ['a', 'b', 'c']
|
|
52
|
+
const config = { key: 'value' }
|
|
53
|
+
const label = 'hello'
|
|
54
|
+
|
|
55
|
+
const count = ref(0)
|
|
56
|
+
const doubled = computed(() => count.value * 2)
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
## License
|
|
60
|
+
|
|
61
|
+
MIT
|
package/index.js
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @type {import('eslint').Rule.RuleModule}
|
|
3
|
+
*/
|
|
4
|
+
export default {
|
|
5
|
+
meta: {
|
|
6
|
+
type: 'suggestion',
|
|
7
|
+
docs: {
|
|
8
|
+
description: 'Disallow computed() wrapping purely static values with no reactive dependencies',
|
|
9
|
+
url: 'https://github.com/arghmeleg/eslint-plugin-vue-no-static-computed#rule-details',
|
|
10
|
+
},
|
|
11
|
+
messages: {
|
|
12
|
+
noStaticComputed:
|
|
13
|
+
'This computed() has no reactive dependencies. Use a plain variable instead.',
|
|
14
|
+
},
|
|
15
|
+
schema: [],
|
|
16
|
+
},
|
|
17
|
+
create(context) {
|
|
18
|
+
return {
|
|
19
|
+
CallExpression(node) {
|
|
20
|
+
if (node.callee.type !== 'Identifier' || node.callee.name !== 'computed') return
|
|
21
|
+
if (node.arguments.length === 0) return
|
|
22
|
+
|
|
23
|
+
const arg = node.arguments[0]
|
|
24
|
+
if (arg.type !== 'ArrowFunctionExpression' && arg.type !== 'FunctionExpression') return
|
|
25
|
+
|
|
26
|
+
const body = arg.body
|
|
27
|
+
// For arrow functions with expression body: computed(() => [...])
|
|
28
|
+
// For block body: computed(() => { return [...] })
|
|
29
|
+
const expr =
|
|
30
|
+
body.type !== 'BlockStatement'
|
|
31
|
+
? body
|
|
32
|
+
: body.body.length === 1 &&
|
|
33
|
+
body.body[0].type === 'ReturnStatement' &&
|
|
34
|
+
body.body[0].argument
|
|
35
|
+
? body.body[0].argument
|
|
36
|
+
: null
|
|
37
|
+
|
|
38
|
+
if (expr && isStatic(expr)) {
|
|
39
|
+
context.report({ node, messageId: 'noStaticComputed' })
|
|
40
|
+
}
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function isStatic(node) {
|
|
47
|
+
switch (node.type) {
|
|
48
|
+
case 'Literal':
|
|
49
|
+
return true
|
|
50
|
+
case 'TemplateLiteral':
|
|
51
|
+
return node.expressions.length === 0
|
|
52
|
+
case 'ArrayExpression':
|
|
53
|
+
return node.elements.every((el) => el !== null && isStatic(el))
|
|
54
|
+
case 'ObjectExpression':
|
|
55
|
+
return node.properties.every(
|
|
56
|
+
(prop) =>
|
|
57
|
+
prop.type === 'Property' &&
|
|
58
|
+
!prop.computed &&
|
|
59
|
+
isStatic(prop.value) &&
|
|
60
|
+
(prop.key.type === 'Identifier' || prop.key.type === 'Literal'),
|
|
61
|
+
)
|
|
62
|
+
default:
|
|
63
|
+
return false
|
|
64
|
+
}
|
|
65
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "eslint-plugin-vue-no-static-computed",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "ESLint rule to disallow Vue computed() wrapping purely static values with no reactive dependencies",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Steven Degele",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/arghmeleg/eslint-plugin-vue-no-static-computed.git"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"exports": "./index.js",
|
|
13
|
+
"main": "./index.js",
|
|
14
|
+
"files": [
|
|
15
|
+
"index.js",
|
|
16
|
+
"lib/",
|
|
17
|
+
"LICENSE"
|
|
18
|
+
],
|
|
19
|
+
"scripts": {
|
|
20
|
+
"test": "node tests/no-static-computed.test.js"
|
|
21
|
+
},
|
|
22
|
+
"keywords": [
|
|
23
|
+
"eslint",
|
|
24
|
+
"eslintplugin",
|
|
25
|
+
"eslint-plugin",
|
|
26
|
+
"vue",
|
|
27
|
+
"computed",
|
|
28
|
+
"static",
|
|
29
|
+
"composition-api"
|
|
30
|
+
],
|
|
31
|
+
"peerDependencies": {
|
|
32
|
+
"eslint": ">=8.0.0"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"eslint": "^9.39.2"
|
|
36
|
+
}
|
|
37
|
+
}
|