Skip to content

Commit

Permalink
Fixed false positives in no-side-effects-in-computed-properties (#1027
Browse files Browse the repository at this point in the history
)
  • Loading branch information
ota-meshi committed Feb 16, 2020
1 parent 8cd3a41 commit a4e3f0f
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
32 changes: 27 additions & 5 deletions lib/rules/no-side-effects-in-computed-properties.js
Expand Up @@ -24,20 +24,38 @@ module.exports = {

create (context) {
const forbiddenNodes = []
let scopeStack = { upper: null, body: null }

function onFunctionEnter (node) {
scopeStack = { upper: scopeStack, body: node.body }
}

function onFunctionExit () {
scopeStack = scopeStack.upper
}

return Object.assign({},
{
':function': onFunctionEnter,
':function:exit': onFunctionExit,

// this.xxx <=|+=|-=>
'AssignmentExpression' (node) {
if (node.left.type !== 'MemberExpression') return
if (utils.parseMemberExpression(node.left)[0] === 'this') {
forbiddenNodes.push(node)
forbiddenNodes.push({
node,
targetBody: scopeStack.body
})
}
},
// this.xxx <++|-->
'UpdateExpression > MemberExpression' (node) {
if (utils.parseMemberExpression(node)[0] === 'this') {
forbiddenNodes.push(node)
forbiddenNodes.push({
node,
targetBody: scopeStack.body
})
}
},
// this.xxx.func()
Expand All @@ -46,19 +64,23 @@ module.exports = {
const MUTATION_REGEX = /(this.)((?!(concat|slice|map|filter)\().)[^\)]*((push|pop|shift|unshift|reverse|splice|sort|copyWithin|fill)\()/g

if (MUTATION_REGEX.test(code)) {
forbiddenNodes.push(node)
forbiddenNodes.push({
node,
targetBody: scopeStack.body
})
}
}
},
utils.executeOnVue(context, (obj) => {
const computedProperties = utils.getComputedProperties(obj)

computedProperties.forEach(cp => {
forbiddenNodes.forEach(node => {
forbiddenNodes.forEach(({ node, targetBody }) => {
if (
cp.value &&
node.loc.start.line >= cp.value.loc.start.line &&
node.loc.end.line <= cp.value.loc.end.line
node.loc.end.line <= cp.value.loc.end.line &&
targetBody === cp.value
) {
context.report({
node: node,
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/index.js
Expand Up @@ -822,7 +822,7 @@ module.exports = {
isFunc = true
} else {
if (n.computed) {
parsedCallee.push('[]')
parsedCallee.push('[]' + (isFunc ? '()' : ''))
} else if (n.property.type === 'Identifier') {
parsedCallee.push(n.property.name + (isFunc ? '()' : ''))
}
Expand Down
30 changes: 30 additions & 0 deletions tests/lib/rules/no-side-effects-in-computed-properties.js
Expand Up @@ -143,6 +143,36 @@ ruleTester.run('no-side-effects-in-computed-properties', rule, {
}
})`,
parserOptions
},
{
code: `Vue.component('test', {
computed: {
test () {
return {
action1() {
this.something++
},
action2() {
this.something = 1
},
action3() {
this.something.reverse()
}
}
},
}
})`,
parserOptions
},
{
code: `Vue.component('test', {
computed: {
test () {
return this.something['a']().reverse()
},
}
})`,
parserOptions
}
],
invalid: [
Expand Down

0 comments on commit a4e3f0f

Please sign in to comment.