Repository: ember-cli/eslint-plugin-ember
Pull request #790 (2020-04-15 16:42:48)

Catch assignment in `no-side-effects` rule
Updates [no-side-effects](https://github.com/ember-cli/eslint-plugin-ember/blob/master/docs/rules/no-side-effects.md) rule to catch `this.x = 123;` assignments in computed property bodies. Part of supporting modern Ember Octane syntax.

Example:

```js
computed("test", function() {
  this.x = 123; // disallowed because this causes a side effect
})
```

Related to #785.


Patch:
diff --git a/docs/rules/no-side-effects.md b/docs/rules/no-side-effects.md
index 19016005..8b370b24 100644
--- a/docs/rules/no-side-effects.md
+++ b/docs/rules/no-side-effects.md
@@ -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.
 
diff --git a/lib/rules/no-side-effects.js b/lib/rules/no-side-effects.js
index 667c869b..11afef3b 100644
--- a/lib/rules/no-side-effects.js
+++ b/lib/rules/no-side-effects.js
@@ -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.
  *
@@ -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);
       }
     },


Test patch:
diff --git a/tests/lib/rules/no-side-effects.js b/tests/lib/rules/no-side-effects.js
index 3f495092..26967464 100644
--- a/tests/lib/rules/no-side-effects.js
+++ b/tests/lib/rules/no-side-effects.js
@@ -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:
     {
@@ -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',
+        },
+      ],
+    },
   ],
 });