Skip to content

Commit a4e3f0f

Browse files
authored
Fixed false positives in no-side-effects-in-computed-properties (#1027)
1 parent 8cd3a41 commit a4e3f0f

File tree

3 files changed

+58
-6
lines changed

3 files changed

+58
-6
lines changed

lib/rules/no-side-effects-in-computed-properties.js

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,38 @@ module.exports = {
2424

2525
create (context) {
2626
const forbiddenNodes = []
27+
let scopeStack = { upper: null, body: null }
28+
29+
function onFunctionEnter (node) {
30+
scopeStack = { upper: scopeStack, body: node.body }
31+
}
32+
33+
function onFunctionExit () {
34+
scopeStack = scopeStack.upper
35+
}
2736

2837
return Object.assign({},
2938
{
39+
':function': onFunctionEnter,
40+
':function:exit': onFunctionExit,
41+
3042
// this.xxx <=|+=|-=>
3143
'AssignmentExpression' (node) {
3244
if (node.left.type !== 'MemberExpression') return
3345
if (utils.parseMemberExpression(node.left)[0] === 'this') {
34-
forbiddenNodes.push(node)
46+
forbiddenNodes.push({
47+
node,
48+
targetBody: scopeStack.body
49+
})
3550
}
3651
},
3752
// this.xxx <++|-->
3853
'UpdateExpression > MemberExpression' (node) {
3954
if (utils.parseMemberExpression(node)[0] === 'this') {
40-
forbiddenNodes.push(node)
55+
forbiddenNodes.push({
56+
node,
57+
targetBody: scopeStack.body
58+
})
4159
}
4260
},
4361
// this.xxx.func()
@@ -46,19 +64,23 @@ module.exports = {
4664
const MUTATION_REGEX = /(this.)((?!(concat|slice|map|filter)\().)[^\)]*((push|pop|shift|unshift|reverse|splice|sort|copyWithin|fill)\()/g
4765

4866
if (MUTATION_REGEX.test(code)) {
49-
forbiddenNodes.push(node)
67+
forbiddenNodes.push({
68+
node,
69+
targetBody: scopeStack.body
70+
})
5071
}
5172
}
5273
},
5374
utils.executeOnVue(context, (obj) => {
5475
const computedProperties = utils.getComputedProperties(obj)
5576

5677
computedProperties.forEach(cp => {
57-
forbiddenNodes.forEach(node => {
78+
forbiddenNodes.forEach(({ node, targetBody }) => {
5879
if (
5980
cp.value &&
6081
node.loc.start.line >= cp.value.loc.start.line &&
61-
node.loc.end.line <= cp.value.loc.end.line
82+
node.loc.end.line <= cp.value.loc.end.line &&
83+
targetBody === cp.value
6284
) {
6385
context.report({
6486
node: node,

lib/utils/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -822,7 +822,7 @@ module.exports = {
822822
isFunc = true
823823
} else {
824824
if (n.computed) {
825-
parsedCallee.push('[]')
825+
parsedCallee.push('[]' + (isFunc ? '()' : ''))
826826
} else if (n.property.type === 'Identifier') {
827827
parsedCallee.push(n.property.name + (isFunc ? '()' : ''))
828828
}

tests/lib/rules/no-side-effects-in-computed-properties.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,36 @@ ruleTester.run('no-side-effects-in-computed-properties', rule, {
143143
}
144144
})`,
145145
parserOptions
146+
},
147+
{
148+
code: `Vue.component('test', {
149+
computed: {
150+
test () {
151+
return {
152+
action1() {
153+
this.something++
154+
},
155+
action2() {
156+
this.something = 1
157+
},
158+
action3() {
159+
this.something.reverse()
160+
}
161+
}
162+
},
163+
}
164+
})`,
165+
parserOptions
166+
},
167+
{
168+
code: `Vue.component('test', {
169+
computed: {
170+
test () {
171+
return this.something['a']().reverse()
172+
},
173+
}
174+
})`,
175+
parserOptions
146176
}
147177
],
148178
invalid: [

0 commit comments

Comments
 (0)