Skip to content

Commit d6c8b40

Browse files
Lisi LinhartLisi Linhart
authored andcommitted
fix(require-setup-store-properties-export): change warning only to reactive variables (ref, reactive)
1 parent b25d14e commit d6c8b40

File tree

2 files changed

+41
-15
lines changed

2 files changed

+41
-15
lines changed

src/rules/require-setup-store-properties-export.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { createEslintRule } from '../utils/rule-creator'
22

33
export const RULE_NAME = 'require-setup-store-properties-export'
4-
export type MESSAGE_IDS = 'missingVariables' | 'noReturns'
4+
export type MESSAGE_IDS = 'missingVariables'
55
type Options = []
66

77
export default createEslintRule<Options, MESSAGE_IDS>({
@@ -13,9 +13,8 @@ export default createEslintRule<Options, MESSAGE_IDS>({
1313
},
1414
schema: [],
1515
messages: {
16-
noReturns:
17-
'All variables declared inside defineStore must be returned in the return statement.',
18-
missingVariables: 'Missing exports in return statement: {{variableNames}}'
16+
missingVariables:
17+
'Missing state variable exports in return statement: {{variableNames}}'
1918
}
2019
},
2120
defaultOptions: [],
@@ -28,21 +27,34 @@ export default createEslintRule<Options, MESSAGE_IDS>({
2827
node.arguments.length === 2 &&
2928
node.arguments[1].type !== 'ObjectExpression'
3029
) {
30+
function isRefOrReactiveCall(node) {
31+
return (
32+
node.type === 'CallExpression' &&
33+
node.callee.type === 'Identifier' &&
34+
(node.callee.name === 'ref' || node.callee.name === 'reactive')
35+
)
36+
}
37+
3138
const returnStatement = node.arguments[1].body.body.find(
3239
(statement) => statement.type === 'ReturnStatement'
3340
)
3441

35-
const declaredVariables = node.arguments[1].body.body
42+
const declaredStateVariables = node.arguments[1].body.body
3643
.filter((statement) => statement.type === 'VariableDeclaration')
3744
.map((declaration) =>
38-
declaration.declarations.map((declarator) => declarator.id.name)
45+
declaration.declarations
46+
.filter((declarator) => isRefOrReactiveCall(declarator.init))
47+
.map((declarator) => declarator.id.name)
3948
)
4049
.flat()
4150

42-
if (!returnStatement && declaredVariables.length > 0) {
51+
if (!returnStatement && declaredStateVariables.length > 0) {
4352
context.report({
4453
node,
45-
messageId: 'noReturns'
54+
messageId: 'missingVariables',
55+
data: {
56+
variableNames: declaredStateVariables.join(', ')
57+
}
4658
})
4759
return
4860
}
@@ -51,7 +63,7 @@ export default createEslintRule<Options, MESSAGE_IDS>({
5163
(property) => property.key.name
5264
)
5365

54-
const missingVariables = declaredVariables.filter(
66+
const missingVariables = declaredStateVariables.filter(
5567
(variable) => !returnedVariables.includes(variable)
5668
)
5769

tests/rules/require-setup-store-properties-export.test.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ ruleTester.run(RULE_NAME, rule, {
77
valid: [
88
`export const useCounterStore = defineStore('counter', () => {
99
const count = ref(0)
10-
const name = ref('Eduardo')
10+
const something = 'Something'
11+
const obj = reactive({ count })
1112
const doubleCount = computed(() => count.value * 2)
1213
function increment() {
1314
count.value++
1415
}
1516
16-
return { count, name, doubleCount, increment }
17+
return { count, something, obj }
1718
})`,
1819
`export const useCounterStore = defineStore('counter', {
1920
state: () => ({ count: 0, name: 'Eduardo' }),
@@ -25,24 +26,36 @@ ruleTester.run(RULE_NAME, rule, {
2526
this.count++
2627
},
2728
},
29+
})`,
30+
`export const useCounterStore = defineStore('counter', () => {
31+
const count = ref(0)
32+
const something = 'Something'
33+
const obj = reactive({ count })
34+
const doubleCount = computed(() => count.value * 2)
35+
function increment() {
36+
count.value++
37+
}
38+
39+
return { count, something, obj, doubleCount, increment }
2840
})`
2941
],
3042
invalid: [
3143
{
3244
code: `export const useCounterStore = defineStore('counter', () => {
3345
const count = ref(0)
34-
const name = ref('Eduardo')
46+
const something = 'Something'
47+
const obj = reactive({ count })
3548
const doubleCount = computed(() => count.value * 2)
3649
function increment() {
3750
count.value++
3851
}
3952
40-
return { doubleCount, increment }
53+
return { increment }
4154
})`,
4255
errors: [
4356
{
4457
messageId: 'missingVariables',
45-
data: { variableNames: 'count, name' }
58+
data: { variableNames: 'count, obj' }
4659
}
4760
]
4861
},
@@ -57,7 +70,8 @@ ruleTester.run(RULE_NAME, rule, {
5770
})`,
5871
errors: [
5972
{
60-
messageId: 'noReturns'
73+
messageId: 'missingVariables',
74+
data: { variableNames: 'count, name' }
6175
}
6276
]
6377
}

0 commit comments

Comments
 (0)