Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed false positives in no-side-effects-in-computed-properties #1027

Merged
merged 1 commit into from Feb 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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