Repository: ember-cli/eslint-plugin-ember
Pull request #373 (2019-01-11 07:36:12)

no-unnecessary-route-path-option: Add support for `--fix`


Patch:
diff --git a/README.md b/README.md
index 260b0ac1..2addbecd 100644
--- a/README.md
+++ b/README.md
@@ -96,7 +96,7 @@ The `--fix` option on the command line automatically fixes problems reported by
 | :white_check_mark::wrench: | [no-old-shims](./docs/rules/no-old-shims.md) | Prevents usage of old shims for modules |
 | :white_check_mark: | [no-on-calls-in-components](./docs/rules/no-on-calls-in-components.md) | Prevents usage of `on` to call lifecycle hooks in components |
 | :white_check_mark: | [no-restricted-resolver-tests](./docs/rules/no-restricted-resolver-tests.md) | Prevents the use of patterns that use the restricted resolver in tests. |
-|  | [no-unnecessary-route-path-option](./docs/rules/no-unnecessary-route-path-option.md) | Disallow unnecessary route `path` option |
+| :wrench: | [no-unnecessary-route-path-option](./docs/rules/no-unnecessary-route-path-option.md) | Disallow unnecessary route `path` option |
 | :wrench: | [use-ember-get-and-set](./docs/rules/use-ember-get-and-set.md) | Enforces usage of Ember.get and Ember.set |
 
 
diff --git a/lib/rules/no-unnecessary-route-path-option.js b/lib/rules/no-unnecessary-route-path-option.js
index 8c50c0b3..54537cc3 100644
--- a/lib/rules/no-unnecessary-route-path-option.js
+++ b/lib/rules/no-unnecessary-route-path-option.js
@@ -16,7 +16,7 @@ module.exports = {
       category: 'Best Practices',
       recommended: false
     },
-    fixable: null,
+    fixable: 'code',
     ERROR_MESSAGE
   },
 
@@ -39,7 +39,16 @@ module.exports = {
         if (pathMatchesRouteName(pathOptionValue, routeName)) {
           context.report({
             node: pathOptionNode,
-            message: ERROR_MESSAGE
+            message: ERROR_MESSAGE,
+            fix(fixer) {
+              const sourceCode = context.getSourceCode();
+
+              // If the `path` option is the only property in the object, remove the entire object.
+              const shouldRemoveObject = node.arguments[1].properties.length === 1;
+              const nodeToRemove = shouldRemoveObject ? node.arguments[1] : pathOptionNode;
+
+              return removeCommaSeparatedNode(nodeToRemove, sourceCode, fixer);
+            }
           });
         }
       }
@@ -59,3 +68,18 @@ function pathMatchesRouteName(path, routeName) {
   const pathWithoutOptionalLeadingSlash = path.substring(0, 1) === '/' ? path.substring(1) : path;
   return pathWithoutOptionalLeadingSlash === routeName;
 }
+
+function removeCommaSeparatedNode(node, sourceCode, fixer) {
+  const tokenBefore = sourceCode.getTokenBefore(node);
+  const tokenAfter = sourceCode.getTokenAfter(node);
+
+  const removeComma =
+    isCommaToken(tokenAfter) ? fixer.remove(tokenAfter) : fixer.remove(tokenBefore);
+  const removeNode = fixer.remove(node);
+
+  return [removeComma, removeNode];
+}
+
+function isCommaToken(token) {
+  return token.value === ',' && token.type === 'Punctuator';
+}


Test patch:
diff --git a/tests/lib/rules/no-unnecessary-route-path-option.js b/tests/lib/rules/no-unnecessary-route-path-option.js
index 19beb7a4..d9788445 100644
--- a/tests/lib/rules/no-unnecessary-route-path-option.js
+++ b/tests/lib/rules/no-unnecessary-route-path-option.js
@@ -49,18 +49,27 @@ ruleTester.run('no-unnecessary-route-path-option', rule, {
   invalid: [
     {
       code: 'this.route("blog", { path: "blog" });',
+      output: 'this.route("blog" );',
       errors: [{ message: ERROR_MESSAGE, type: 'Property' }]
     },
     {
       code: 'this.route("blog", { path: "blog" }, function() {});',
+      output: 'this.route("blog",  function() {});',
       errors: [{ message: ERROR_MESSAGE, type: 'Property' }]
     },
     {
       code: 'this.route("blog", { path: "/blog" });',
+      output: 'this.route("blog" );',
       errors: [{ message: ERROR_MESSAGE, type: 'Property' }]
     },
     {
       code: 'this.route("blog", { path: "/blog", otherOption: true });',
+      output: 'this.route("blog", {  otherOption: true });',
+      errors: [{ message: ERROR_MESSAGE, type: 'Property' }]
+    },
+    {
+      code: 'this.route("blog", { otherOption: true, path: "/blog" });',
+      output: 'this.route("blog", { otherOption: true  });',
       errors: [{ message: ERROR_MESSAGE, type: 'Property' }]
     }
   ]