Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 52e0770

Browse filesBrowse files
authored
fix(no-use-computed-property-like-method): extract LogicalExpression (#2438)
1 parent 45ce14e commit 52e0770
Copy full SHA for 52e0770

File tree

Expand file treeCollapse file tree

2 files changed

+138
-18
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+138
-18
lines changed

‎lib/rules/no-use-computed-property-like-method.js

Copy file name to clipboardExpand all lines: lib/rules/no-use-computed-property-like-method.js
+30-18Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,26 +68,39 @@ function resolvedExpressions(context, node) {
6868
* @returns {Iterable<Expression>}
6969
*/
7070
function* extractResolvedExpressions(node) {
71-
if (node.type === 'Identifier') {
72-
const variable = utils.findVariableByIdentifier(context, node)
73-
if (variable) {
74-
for (const ref of variable.references) {
75-
const id = ref.identifier
76-
if (id.parent.type === 'VariableDeclarator') {
77-
if (id.parent.id === id && id.parent.init) {
78-
yield* resolvedExpressionsInternal(id.parent.init)
71+
switch (node.type) {
72+
case 'Identifier': {
73+
const variable = utils.findVariableByIdentifier(context, node)
74+
if (variable) {
75+
for (const ref of variable.references) {
76+
const id = ref.identifier
77+
if (id.parent.type === 'VariableDeclarator') {
78+
if (id.parent.id === id && id.parent.init) {
79+
yield* resolvedExpressionsInternal(id.parent.init)
80+
}
81+
} else if (
82+
id.parent.type === 'AssignmentExpression' &&
83+
id.parent.left === id
84+
) {
85+
yield* resolvedExpressionsInternal(id.parent.right)
7986
}
80-
} else if (
81-
id.parent.type === 'AssignmentExpression' &&
82-
id.parent.left === id
83-
) {
84-
yield* resolvedExpressionsInternal(id.parent.right)
8587
}
8688
}
89+
90+
break
91+
}
92+
case 'ConditionalExpression': {
93+
yield* resolvedExpressionsInternal(node.consequent)
94+
yield* resolvedExpressionsInternal(node.alternate)
95+
96+
break
97+
}
98+
case 'LogicalExpression': {
99+
yield* resolvedExpressionsInternal(node.left)
100+
yield* resolvedExpressionsInternal(node.right)
101+
102+
break
87103
}
88-
} else if (node.type === 'ConditionalExpression') {
89-
yield* resolvedExpressionsInternal(node.consequent)
90-
yield* resolvedExpressionsInternal(node.alternate)
91104
}
92105
}
93106
}
@@ -103,7 +116,7 @@ function resolvedExpressions(context, node) {
103116
* props: {
104117
* propA: String, // => String
105118
* propB: {
106-
* type: Number // => String
119+
* type: Number // => Number
107120
* },
108121
* }
109122
*/
@@ -189,7 +202,6 @@ function maybeFunction(context, node) {
189202
expr.type === 'Literal' ||
190203
expr.type === 'TemplateLiteral' ||
191204
expr.type === 'BinaryExpression' ||
192-
expr.type === 'LogicalExpression' ||
193205
expr.type === 'UnaryExpression' ||
194206
expr.type === 'UpdateExpression'
195207
) {

‎tests/lib/rules/no-use-computed-property-like-method.js

Copy file name to clipboardExpand all lines: tests/lib/rules/no-use-computed-property-like-method.js
+108Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,78 @@ tester.run('no-use-computed-property-like-method', rule, {
564564
}
565565
</script>
566566
`
567+
},
568+
{
569+
// expression may be a function: https://github.com/vuejs/eslint-plugin-vue/issues/2037
570+
filename: 'test.vue',
571+
code: `
572+
<script>
573+
export default {
574+
props: {
575+
propsFunction: {
576+
type: Function,
577+
default: undefined
578+
},
579+
propsNumber: {
580+
type: Number,
581+
}
582+
},
583+
computed: {
584+
computedReturnPropsFunction() {
585+
return this.propsFunction ? this.propsFunction : this.propsFunctionDefault
586+
},
587+
computedReturnMaybeFunction() {
588+
return this.propsFunction ? this.propsFunction : this.propsNumber
589+
}
590+
},
591+
methods: {
592+
fn() {
593+
this.computedReturnPropsFunction
594+
this.computedReturnPropsFunction()
595+
this.computedReturnMaybeFunction
596+
this.computedReturnMaybeFunction()
597+
},
598+
propsFunctionDefault() {}
599+
}
600+
}
601+
</script>
602+
`
603+
},
604+
{
605+
// expression may be a function: https://github.com/vuejs/eslint-plugin-vue/issues/2037
606+
filename: 'test.vue',
607+
code: `
608+
<script>
609+
export default {
610+
props: {
611+
propsFunction: {
612+
type: Function,
613+
default: undefined
614+
},
615+
propsNumber: {
616+
type: Number,
617+
}
618+
},
619+
computed: {
620+
computedReturnPropsFunction() {
621+
return this.propsFunction || this.propsFunctionDefault
622+
},
623+
computedReturnMaybeFunction() {
624+
return this.propsFunction || this.propsNumber
625+
}
626+
},
627+
methods: {
628+
fn() {
629+
this.computedReturnPropsFunction
630+
this.computedReturnPropsFunction()
631+
this.computedReturnMaybeFunction
632+
this.computedReturnMaybeFunction()
633+
},
634+
propsFunctionDefault() {}
635+
}
636+
}
637+
</script>
638+
`
567639
}
568640
],
569641
invalid: [
@@ -1096,6 +1168,42 @@ tester.run('no-use-computed-property-like-method', rule, {
10961168
</script>
10971169
`,
10981170
errors: ['Use x instead of x().']
1171+
},
1172+
{
1173+
// expression may be a function: https://github.com/vuejs/eslint-plugin-vue/issues/2037
1174+
filename: 'test.vue',
1175+
code: `
1176+
<script>
1177+
export default {
1178+
props: {
1179+
propsNumber: {
1180+
type: Number
1181+
},
1182+
propsString: {
1183+
type: String
1184+
},
1185+
},
1186+
computed: {
1187+
computedReturnNotFunction1() {
1188+
return this.propsString ? this.propsString : this.propsNumber
1189+
},
1190+
computedReturnNotFunction2() {
1191+
return this.propsString || this.propsNumber
1192+
},
1193+
},
1194+
methods: {
1195+
fn() {
1196+
this.computedReturnNotFunction1()
1197+
this.computedReturnNotFunction2()
1198+
}
1199+
}
1200+
}
1201+
</script>
1202+
`,
1203+
errors: [
1204+
'Use this.computedReturnNotFunction1 instead of this.computedReturnNotFunction1().',
1205+
'Use this.computedReturnNotFunction2 instead of this.computedReturnNotFunction2().'
1206+
]
10991207
}
11001208
]
11011209
})

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.