-
-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #370 from bmish/no-unnecessary-route-path-option
feat: add new rule 'no-unnecessary-route-path-option'.
- Loading branch information
Showing
6 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
## Disallow unnecessary route `path` option | ||
|
||
### Rule name: `no-unnecessary-route-path-option` | ||
|
||
When defining a route, it's not necessary to specify the `path` option if it matches the route name. | ||
|
||
### Rule Details | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```js | ||
this.route('blog-posts', { path: '/blog-posts' }); | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```js | ||
this.route('blog-posts'); | ||
``` | ||
|
||
```js | ||
this.route('blog-posts', { path: '/blog' }); | ||
``` | ||
|
||
### References | ||
|
||
* [Ember Routing Guide](https://guides.emberjs.com/release/routing/) | ||
|
||
### Related Rules | ||
|
||
* [no-capital-letters-in-routes](no-capital-letters-in-routes.md) | ||
* [routes-segments-snake-case](routes-segments-snake-case.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
'use strict'; | ||
|
||
const utils = require('../utils/utils'); | ||
const ember = require('../utils/ember'); | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
const ERROR_MESSAGE = 'Do not provide unnecessary `path` option which matches the route name.'; | ||
|
||
module.exports = { | ||
meta: { | ||
docs: { | ||
description: 'Disallow unnecessary route `path` option', | ||
category: 'Best Practices', | ||
recommended: false | ||
}, | ||
fixable: null, | ||
ERROR_MESSAGE | ||
}, | ||
|
||
create(context) { | ||
return { | ||
CallExpression(node) { | ||
if (!ember.isRoute(node)) { | ||
return; | ||
} | ||
|
||
const hasExplicitPathOption = utils.isObjectExpression(node.arguments[1]) && hasPropertyWithKeyName(node.arguments[1], 'path'); | ||
if (!hasExplicitPathOption) { | ||
return; | ||
} | ||
|
||
const pathOptionNode = getPropertyByKeyName(node.arguments[1], 'path'); | ||
const pathOptionValue = pathOptionNode.value.value; | ||
const routeName = node.arguments[0].value; | ||
|
||
if (pathMatchesRouteName(pathOptionValue, routeName)) { | ||
context.report({ | ||
node: pathOptionNode, | ||
message: ERROR_MESSAGE | ||
}); | ||
} | ||
} | ||
}; | ||
} | ||
}; | ||
|
||
function hasPropertyWithKeyName(objectExpression, keyName) { | ||
return getPropertyByKeyName(objectExpression, keyName) !== undefined; | ||
} | ||
|
||
function getPropertyByKeyName(objectExpression, keyName) { | ||
return objectExpression.properties.find(property => property.key.name === keyName); | ||
} | ||
|
||
function pathMatchesRouteName(path, routeName) { | ||
const pathWithoutOptionalLeadingSlash = path.substring(0, 1) === '/' ? path.substring(1) : path; | ||
return pathWithoutOptionalLeadingSlash === routeName; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//------------------------------------------------------------------------------ | ||
// Requirements | ||
//------------------------------------------------------------------------------ | ||
|
||
const rule = require('../../../lib/rules/no-unnecessary-route-path-option'); | ||
const RuleTester = require('eslint').RuleTester; | ||
|
||
const { ERROR_MESSAGE } = rule; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Tests | ||
//------------------------------------------------------------------------------ | ||
|
||
const ruleTester = new RuleTester(); | ||
ruleTester.run('no-unnecessary-route-path-option', rule, { | ||
valid: [ | ||
'this.route("blog");', | ||
'this.route("blog", function() {});', | ||
'this.route("blog", { path: "/" });', | ||
'this.route("blog", { path: "blog-posts" });', | ||
'this.route("blog", { path: "blog-posts" }, function() {});', | ||
'this.route("blog", { path: "/blog-posts" });', | ||
'this.route("blog", { path: "blog-posts", otherOption: true });', | ||
|
||
// With dynamic segment: | ||
'this.route("blog", { path: ":blog" });', | ||
'this.route("blog", { path: "/:blog" });', | ||
'this.route("blog", { path: "blog/:blog_id" });', | ||
|
||
// With wildcard segment: | ||
'this.route("blog", { path: "*blog" });', | ||
'this.route("blog", { path: "/*blog" });', | ||
'this.route("blog", { path: "blog/*blog" });', | ||
|
||
// Not Ember's route function: | ||
'test();', | ||
"test('blog');", | ||
"test('blog', { path: 'blog' });", | ||
"test('blog', { path: '/blog' });", | ||
"this.test('blog');", | ||
"this.test('blog', { path: 'blog' });", | ||
"this.test('blog', { path: '/blog' });", | ||
"MyClass.route('blog');", | ||
"MyClass.route('blog', { path: 'blog' });", | ||
"MyClass.route('blog', { path: '/blog' });", | ||
"route.unrelatedFunction('blog', { path: 'blog' });", | ||
"this.route.unrelatedFunction('blog', { path: 'blog' });" | ||
], | ||
invalid: [ | ||
{ | ||
code: 'this.route("blog", { path: "blog" });', | ||
errors: [{ message: ERROR_MESSAGE, type: 'Property' }] | ||
}, | ||
{ | ||
code: 'this.route("blog", { path: "blog" }, function() {});', | ||
errors: [{ message: ERROR_MESSAGE, type: 'Property' }] | ||
}, | ||
{ | ||
code: 'this.route("blog", { path: "/blog" });', | ||
errors: [{ message: ERROR_MESSAGE, type: 'Property' }] | ||
}, | ||
{ | ||
code: 'this.route("blog", { path: "/blog", otherOption: true });', | ||
errors: [{ message: ERROR_MESSAGE, type: 'Property' }] | ||
} | ||
] | ||
}); |