Repository: ember-cli/eslint-plugin-ember
Pull request #646 (2020-01-13 22:32:09)

Fix crash with default imports in `no-observers` and `no-computed-properties-in-native-classes` rules
Fix to ensure that this example does not crash:

```
import EmberObject from '@ember/object';
```

with this error:

```
TypeError: Cannot read property 'name' of undefined
```

Follow-up to #639 and #640.


Patch:
diff --git a/lib/rules/no-computed-properties-in-native-classes.js b/lib/rules/no-computed-properties-in-native-classes.js
index 91e25000..a82c8e4f 100644
--- a/lib/rules/no-computed-properties-in-native-classes.js
+++ b/lib/rules/no-computed-properties-in-native-classes.js
@@ -16,7 +16,7 @@ function findComputedNodes(nodeBody) {
     return (
       node.source.value === '@ember/object/computed' ||
       (node.source.value === '@ember/object' &&
-        node.specifiers.some(s => s.imported.name === 'computed'))
+        node.specifiers.some(s => (s.imported ? s.imported.name : s.local.name) === 'computed'))
     );
   });
 }
diff --git a/lib/rules/no-observers.js b/lib/rules/no-observers.js
index b9265f56..7609c9b2 100644
--- a/lib/rules/no-observers.js
+++ b/lib/rules/no-observers.js
@@ -58,7 +58,9 @@ module.exports = {
 
         if (
           (importSource === '@ember/object' &&
-            node.specifiers.some(s => s.imported.name === 'observer')) ||
+            node.specifiers.some(
+              s => (s.imported ? s.imported.name : s.local.name) === 'observer'
+            )) ||
           importSource === '@ember/object/observers'
         ) {
           report(node);


Test patch:
diff --git a/tests/lib/rules/no-computed-properties-in-native-classes.js b/tests/lib/rules/no-computed-properties-in-native-classes.js
index 74ee4c19..7d9359a3 100644
--- a/tests/lib/rules/no-computed-properties-in-native-classes.js
+++ b/tests/lib/rules/no-computed-properties-in-native-classes.js
@@ -37,14 +37,19 @@ ruleTester.run('no-computed-properties-in-native-classes', rule, {
 
       export default class MyComponent extends Component {}
     `,
+
+    // Unrelated import statements:
+    "import EmberObject from '@ember/object';",
+    "import { run } from '@ember/runloop';",
+    "import { run as renamedRun } from '@ember/runloop';",
   ],
   invalid: [
     {
       code: `
       import { computed } from '@ember/object';
-      
+
       export default class MyComponent extends Component {
-      
+
       }
       `,
       output: null,
@@ -53,9 +58,9 @@ ruleTester.run('no-computed-properties-in-native-classes', rule, {
     {
       code: `
       import { computed as thinking } from '@ember/object';
-      
+
       export default class MyComponent extends Component {
-      
+
       }
       `,
       output: null,
@@ -64,9 +69,9 @@ ruleTester.run('no-computed-properties-in-native-classes', rule, {
     {
       code: `
       import { and, or, alias } from '@ember/object/computed';
-      
+
       export default class MyComponent extends Component {
-      
+
       }
       `,
       output: null,
diff --git a/tests/lib/rules/no-observers.js b/tests/lib/rules/no-observers.js
index e7bd8acc..e76e7557 100644
--- a/tests/lib/rules/no-observers.js
+++ b/tests/lib/rules/no-observers.js
@@ -26,6 +26,12 @@ eslintTester.run('no-observers', rule, {
   valid: [
     'export default Controller.extend();',
     'export default Controller.extend({actions: {},});',
+
+    // Unrelated import statements:
+    "import EmberObject from '@ember/object';",
+    "import { run } from '@ember/runloop';",
+    "import { run as renamedRun } from '@ember/runloop';",
+
     `
     import { action } from '@ember/object';
     class FooComponent extends Component {