From 52e0770ba8cba3a5f5f31f4aec53ac88c441af7d Mon Sep 17 00:00:00 2001 From: Wayne Zhang Date: Sun, 14 Apr 2024 19:31:03 +0800 Subject: [PATCH 1/4] fix(no-use-computed-property-like-method): extract LogicalExpression (#2438) --- .../no-use-computed-property-like-method.js | 48 +++++--- .../no-use-computed-property-like-method.js | 108 ++++++++++++++++++ 2 files changed, 138 insertions(+), 18 deletions(-) diff --git a/lib/rules/no-use-computed-property-like-method.js b/lib/rules/no-use-computed-property-like-method.js index 5d5bd8b4a..6e89ad263 100644 --- a/lib/rules/no-use-computed-property-like-method.js +++ b/lib/rules/no-use-computed-property-like-method.js @@ -68,26 +68,39 @@ function resolvedExpressions(context, node) { * @returns {Iterable} */ function* extractResolvedExpressions(node) { - if (node.type === 'Identifier') { - const variable = utils.findVariableByIdentifier(context, node) - if (variable) { - for (const ref of variable.references) { - const id = ref.identifier - if (id.parent.type === 'VariableDeclarator') { - if (id.parent.id === id && id.parent.init) { - yield* resolvedExpressionsInternal(id.parent.init) + switch (node.type) { + case 'Identifier': { + const variable = utils.findVariableByIdentifier(context, node) + if (variable) { + for (const ref of variable.references) { + const id = ref.identifier + if (id.parent.type === 'VariableDeclarator') { + if (id.parent.id === id && id.parent.init) { + yield* resolvedExpressionsInternal(id.parent.init) + } + } else if ( + id.parent.type === 'AssignmentExpression' && + id.parent.left === id + ) { + yield* resolvedExpressionsInternal(id.parent.right) } - } else if ( - id.parent.type === 'AssignmentExpression' && - id.parent.left === id - ) { - yield* resolvedExpressionsInternal(id.parent.right) } } + + break + } + case 'ConditionalExpression': { + yield* resolvedExpressionsInternal(node.consequent) + yield* resolvedExpressionsInternal(node.alternate) + + break + } + case 'LogicalExpression': { + yield* resolvedExpressionsInternal(node.left) + yield* resolvedExpressionsInternal(node.right) + + break } - } else if (node.type === 'ConditionalExpression') { - yield* resolvedExpressionsInternal(node.consequent) - yield* resolvedExpressionsInternal(node.alternate) } } } @@ -103,7 +116,7 @@ function resolvedExpressions(context, node) { * props: { * propA: String, // => String * propB: { - * type: Number // => String + * type: Number // => Number * }, * } */ @@ -189,7 +202,6 @@ function maybeFunction(context, node) { expr.type === 'Literal' || expr.type === 'TemplateLiteral' || expr.type === 'BinaryExpression' || - expr.type === 'LogicalExpression' || expr.type === 'UnaryExpression' || expr.type === 'UpdateExpression' ) { diff --git a/tests/lib/rules/no-use-computed-property-like-method.js b/tests/lib/rules/no-use-computed-property-like-method.js index 63ddc0dd2..4a5e25ebe 100644 --- a/tests/lib/rules/no-use-computed-property-like-method.js +++ b/tests/lib/rules/no-use-computed-property-like-method.js @@ -564,6 +564,78 @@ tester.run('no-use-computed-property-like-method', rule, { } ` + }, + { + // expression may be a function: https://github.com/vuejs/eslint-plugin-vue/issues/2037 + filename: 'test.vue', + code: ` + + ` + }, + { + // expression may be a function: https://github.com/vuejs/eslint-plugin-vue/issues/2037 + filename: 'test.vue', + code: ` + + ` } ], invalid: [ @@ -1096,6 +1168,42 @@ tester.run('no-use-computed-property-like-method', rule, { `, errors: ['Use x instead of x().'] + }, + { + // expression may be a function: https://github.com/vuejs/eslint-plugin-vue/issues/2037 + filename: 'test.vue', + code: ` + + `, + errors: [ + 'Use this.computedReturnNotFunction1 instead of this.computedReturnNotFunction1().', + 'Use this.computedReturnNotFunction2 instead of this.computedReturnNotFunction2().' + ] } ] }) From a4be0fcb30953a058e3abc0b6b6589f14ab0c5e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Ot=C3=A1vio=20Carvalho?= Date: Sun, 14 Apr 2024 08:31:38 -0300 Subject: [PATCH 2/4] Support Pinia methods in `vue/no-unused-properties` (#2441) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Luiz Otávio Carvalho --- lib/rules/no-unused-properties.js | 8 +- tests/lib/rules/no-unused-properties.js | 1100 +++++++++++++++++++---- 2 files changed, 918 insertions(+), 190 deletions(-) diff --git a/lib/rules/no-unused-properties.js b/lib/rules/no-unused-properties.js index d09aca5c2..7eb7eb75d 100644 --- a/lib/rules/no-unused-properties.js +++ b/lib/rules/no-unused-properties.js @@ -496,12 +496,16 @@ module.exports = { let groupName = null if (/^mapMutations|mapActions$/u.test(node.callee.name)) { groupName = 'methods' - } else if (/^mapState|mapGetters$/u.test(node.callee.name)) { + } else if ( + /^mapState|mapGetters|mapWritableState$/u.test(node.callee.name) + ) { groupName = 'computed' } if (!groupName || node.arguments.length === 0) return - const arg = node.arguments[0] + // On Pinia the store is always the first argument + const arg = + node.arguments.length === 2 ? node.arguments[1] : node.arguments[0] if (arg.type === 'ObjectExpression') { // e.g. // `mapMutations({ add: 'increment' })` diff --git a/tests/lib/rules/no-unused-properties.js b/tests/lib/rules/no-unused-properties.js index 65b185298..64e858d31 100644 --- a/tests/lib/rules/no-unused-properties.js +++ b/tests/lib/rules/no-unused-properties.js @@ -53,6 +53,7 @@ const unreferencedOptions = { tester.run('no-unused-properties', rule, { valid: [ + // vuex getters { filename: 'test.vue', code: ` @@ -74,6 +75,68 @@ tester.run('no-unused-properties', rule, { `, options: allOptions }, + { + filename: 'test.vue', + code: ` + + + ` + }, + { + filename: 'test.vue', + code: ` + + + ` + }, + { + filename: 'test.vue', + code: ` + + + ` + }, + { + filename: 'test.vue', + code: ` + + + ` + }, + + // vuex mutations { filename: 'test.vue', code: ` @@ -136,6 +199,8 @@ tester.run('no-unused-properties', rule, { ` }, + + // vuex actions { filename: 'test.vue', code: ` @@ -196,6 +261,8 @@ tester.run('no-unused-properties', rule, { ` }, + + // vuex state { filename: 'test.vue', code: ` @@ -251,7 +318,7 @@ tester.run('no-unused-properties', rule, { @@ -260,27 +327,37 @@ tester.run('no-unused-properties', rule, { ` }, + + // pinia getters { filename: 'test.vue', code: ` - ` + `, + options: allOptions }, { filename: 'test.vue', code: `