Repository: ember-cli/eslint-plugin-ember
Pull request #774 (2020-04-13 14:26:32)

Catch spaces in `no-invalid-dependent-keys` rule
Dependent keys should not have spaces in them.


Patch:
diff --git a/docs/rules/no-invalid-dependent-keys.md b/docs/rules/no-invalid-dependent-keys.md
index 1a4daaca..618cdbe8 100644
--- a/docs/rules/no-invalid-dependent-keys.md
+++ b/docs/rules/no-invalid-dependent-keys.md
@@ -17,10 +17,6 @@ Currently implemented checks:
 - Invalid position of `@each` or `[]`
 - Leading or trailing periods
 
-Not yet implemented checks:
-
-- Invalid characters
-
 ## Examples
 
 Examples of **incorrect** code for this rule:
@@ -70,6 +66,15 @@ export default Component.extend({
 });
 ```
 
+```js
+export default Component.extend({
+  // Space:
+  userId: computed('user .id', {
+    // Code
+  })
+});
+```
+
 Examples of **correct** code for this rule:
 
 ```js
diff --git a/lib/rules/no-invalid-dependent-keys.js b/lib/rules/no-invalid-dependent-keys.js
index a5cc2f82..bd7ff96e 100644
--- a/lib/rules/no-invalid-dependent-keys.js
+++ b/lib/rules/no-invalid-dependent-keys.js
@@ -12,6 +12,7 @@ const ERROR_MESSAGE_UNNECESSARY_BRACES = 'Found unnecessary braces in dependent
 const ERROR_MESSAGE_TERMINAL_AT_EACH = 'Found terminal `@each`, use `[]` instead';
 const ERROR_MESSAGE_MIDDLE_BRACKETS = '`[]` should only be used at the end of the dependent key';
 const ERROR_MESSAGE_LEADING_TRAILING_PERIOD = 'Found leading or trailing period in dependent key';
+const ERROR_MESSAGE_INVALID_CHARACTER = 'Found invalid character in dependent key';
 
 function hasUnbalancedBraces(str) {
   const foundBraces = str.match(/[{}]/g) || [];
@@ -52,6 +53,7 @@ module.exports = {
   ERROR_MESSAGE_TERMINAL_AT_EACH,
   ERROR_MESSAGE_MIDDLE_BRACKETS,
   ERROR_MESSAGE_LEADING_TRAILING_PERIOD,
+  ERROR_MESSAGE_INVALID_CHARACTER,
 
   create(context) {
     return {
@@ -128,6 +130,17 @@ module.exports = {
               },
             });
           }
+
+          if (node.value.includes(' ')) {
+            context.report({
+              node,
+              message: ERROR_MESSAGE_INVALID_CHARACTER,
+              fix(fixer) {
+                const nodeText = sourceCode.getText(node);
+                return fixer.replaceText(node, nodeText.replace(/ /g, '')); // Remove all spaces.
+              },
+            });
+          }
         });
       },
     };


Test patch:
diff --git a/tests/lib/rules/no-invalid-dependent-keys.js b/tests/lib/rules/no-invalid-dependent-keys.js
index 6cb2adca..38ab8291 100644
--- a/tests/lib/rules/no-invalid-dependent-keys.js
+++ b/tests/lib/rules/no-invalid-dependent-keys.js
@@ -7,6 +7,7 @@ const {
   ERROR_MESSAGE_TERMINAL_AT_EACH,
   ERROR_MESSAGE_MIDDLE_BRACKETS,
   ERROR_MESSAGE_LEADING_TRAILING_PERIOD,
+  ERROR_MESSAGE_INVALID_CHARACTER,
 } = rule;
 
 // ------------------------------------------------------------------------------
@@ -190,6 +191,18 @@ eslintTester.run('no-invalid-dependent-keys', rule, {
       ],
     },
 
+    // Spaces
+    {
+      code: 'computed(" foo . bar", function() {})',
+      output: 'computed("foo.bar", function() {})',
+      errors: [
+        {
+          message: ERROR_MESSAGE_INVALID_CHARACTER,
+          type: 'Literal',
+        },
+      ],
+    },
+
     // Decorator
     {
       code: "class Test { @computed('foo.{name,place') get someProp() { return true; } }",