-
-
Notifications
You must be signed in to change notification settings - Fork 681
Add no-side-effects-in-computed-properties
rule
#69
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
Add no-side-effects-in-computed-properties
rule
#69
Conversation
Move component detection logic to utils
878dd5c
to
774a169
Compare
|
||
const computedProperties = computedPropertiesNode.value.properties | ||
|
||
return computedProperties.map(cp => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in case when there is only
computed: {
foo: {
set () {}
}
}
returned value is going to be
return [
{
key: '....',
value: undefined
}
]
And code when its used has to check for that case or we should filter it here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, thanks @armano2
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michalsnik i think its better to check it here cus we are going propably to add rule about checking if computed propery has get
and we can do that by just checking it .value
is not undefined
We should be careful about nested functions if we use |
Move component detection logic to utils
…hub.com:vuejs/eslint-plugin-vue into add-no-side-effects-in-computed-properties-rule
Agree @mysticatea, but given the way vue works and the |
|
||
if (cp.value.type === 'FunctionExpression') { | ||
value = cp.value.body | ||
} else if (cp.value.type === 'ObjectExpression') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
coverage from unit test never enters this part ObjectExpression
p.value.type === 'ObjectExpression' | ||
)[0] | ||
|
||
if (!computedPropertiesNode) { return [] } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unit test never enters this if
|
||
return computedProperties | ||
.map(cp => { | ||
const key = cp.key.name |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@michalsnik cp.key
can be undefined if you are using experimentalObjectRestSpread
Solution: if cp.type === 'Property'
TypeError: Cannot read property 'name' of undefined
example:
export default {
computed: {
...mapGetters({})
}
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good point, thank you @armano2 for pointing that!
|
||
if (memberExpression.type === 'ThisExpression') { | ||
members.push('this') | ||
} else if (memberExpression.type === 'Identifier') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unit test never enters here
}, | ||
// this.xxx <++|--> | ||
'UpdateExpression > MemberExpression' (node) { | ||
if (parseMemberExpression(node)[0] === 'this') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all tests are for this.
…hub.com:vuejs/eslint-plugin-vue into add-no-side-effects-in-computed-properties-rule
Thanks for suggestions @armano2! I moved two helper functions to utils and added separate tests for them. Please take another look. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems fine right now
Okay, let's push this forward. We're still in beta phase and it's not enabled by default so I think it's good to go. |
This PR implements rule proposed in #58
This rule checks for existance of any of the following inside all computed properties:
AssignmentExpression
that starts withthis.
(e.g.this.x = 'asd'
,this.x += 1
)UpdateExpression
that starts withthis.
(e.gthis.x++
)CallExpression
that starts withthis.
and calls one of the mutable methods (push
,pop
,shift
,unshift
,reverse
,splice
,sort
,copyWithin
,fill
) not followed by any of those that remove the reference (like:slice
,map
,filter
,concat
)Feel free to comment if I missed anything. At this point I'd like to stick with those simple checks and then we can investigate it further if we can check anything more than that. If believe this is a good starting point.
Left to do:
this.anything(
that is not assigned anywhere should be considered unexpected side effectAssignmentExpression
andUpdateExpression
too, instead of parsing those simple chunks of AST)