Skip to content

Catch assignment in no-side-effects rule #790

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

Merged
merged 1 commit into from
Apr 15, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/rules/no-side-effects.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ This rule currently disallows the following side-effect-causing statements insid

* `this.set('x', 123);`
* `this.setProperties({ x: 123 });`
* `this.x = 123;`

Note that other side effects like network requests should be avoided as well.

Expand Down
11 changes: 10 additions & 1 deletion lib/rules/no-side-effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ function isEmberSet(node, emberImportAliasName) {
);
}

function isThisSet(node) {
return (
types.isAssignmentExpression(node) &&
types.isMemberExpression(node.left) &&
types.isThisExpression(node.left.object) &&
types.isIdentifier(node.left.property)
);
}

/**
* Recursively finds calls that could be side effects in a computed property function body.
*
Expand All @@ -39,7 +48,7 @@ function findSideEffects(computedPropertyBody, emberImportAliasName) {

new Traverser().traverse(computedPropertyBody, {
enter(child) {
if (isEmberSet(child, emberImportAliasName)) {
if (isEmberSet(child, emberImportAliasName) || isThisSet(child)) {
results.push(child);
}
},
Expand Down
15 changes: 14 additions & 1 deletion tests/lib/rules/no-side-effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ eslintTester.run('no-side-effects', rule, {
'testAmount: computed("test.length", { get() { return ""; }, set() { setProperties(); }})',
'let foo = computed("test", function() { someMap.setProperties(); return ""; })',
'import Ember from "ember"; import Foo from "some-other-thing"; let foo = computed("test", function() { Foo.set(this, "testAmount", test.length); return ""; });',

'import Ember from "ember"; import Foo from "some-other-thing"; let foo = computed("test", function() { Foo.setProperties(); return ""; });',
'computed("test", function() { this.test; })',
'computed("test", function() { this.myFunction(); })',
'computed("test", function() { let x = 123; x = 456; someObject.x = 123; })',

// Decorators:
{
Expand Down Expand Up @@ -256,5 +258,16 @@ eslintTester.run('no-side-effects', rule, {
ecmaFeatures: { legacyDecorators: true },
},
},

{
code: 'computed("test", function() { this.x = 123; })',
output: null,
errors: [
{
message: ERROR_MESSAGE,
type: 'AssignmentExpression',
},
],
},
],
});