-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
no-unnecessary-route-path-option.js
61 lines (50 loc) · 1.77 KB
/
no-unnecessary-route-path-option.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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;
}