Skip to content

Commit

Permalink
Merge pull request #1297 from raido/fix-if-else-no-shadow-route
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish committed Sep 20, 2021
2 parents fe15a4e + d37ca0e commit 863afc7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 7 deletions.
36 changes: 29 additions & 7 deletions lib/rules/no-shadow-route-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,9 @@ module.exports = {
return;
}

if (
routeMap.has(routeInfo.route.fullPathWithGenericParams) &&
!isNestedRouteWithSamePath(routeInfo)
) {
const existingRouteInfo = routeMap.get(routeInfo.route.fullPathWithGenericParams);
const routeLookupKey = `${routeInfo.route.blockStatementsTreePrefix}::${routeInfo.route.fullPathWithGenericParams}`;
if (routeMap.has(routeLookupKey) && !isNestedRouteWithSamePath(routeInfo)) {
const existingRouteInfo = routeMap.get(routeLookupKey);
context.report({
node,
message: buildErrorMessage({
Expand All @@ -93,8 +91,8 @@ module.exports = {
});
return;
}
if (!routeMap.has(routeInfo.route.fullPathWithGenericParams)) {
routeMap.set(routeInfo.route.fullPathWithGenericParams, routeInfo);
if (!routeMap.has(routeLookupKey)) {
routeMap.set(routeLookupKey, routeInfo);
}
},
};
Expand All @@ -118,6 +116,10 @@ function getRouteInfo(node, scopeManager) {

const routeName = getRouteName(node).stringValue;

// We gather block statement ranges as prefix for route lookup paths
// Example: "121-150-130-135"
const blockStatementsTreePrefix = lookupIfElseBlockStatementsTreePrefix(node).join('-');

// We replace "////post/something" -> "/post/something".
// As that what nesting of / configured routes means for Ember router.
return {
Expand All @@ -128,6 +130,7 @@ function getRouteInfo(node, scopeManager) {
fullPathWithGenericParams: trimRootLevelNestedRoutes(
convertPathToGenericForMatching([...parentRoutes, basePath])
),
blockStatementsTreePrefix,
source: {
loc: node.loc,
},
Expand Down Expand Up @@ -252,3 +255,22 @@ function isString(value) {
function isValidRouteInfo(routeInfo) {
return routeInfo !== null;
}

function lookupIfElseBlockStatementsTreePrefix(node) {
const inspectedNode = node.parent;
let stack = [];
if (inspectedNode) {
if (inspectedNode.type === 'BlockStatement') {
if (inspectedNode.parent.type === 'IfStatement') {
if (
inspectedNode.parent.consequent === inspectedNode ||
inspectedNode.parent.alternate === inspectedNode
) {
stack.push(inspectedNode.range.join('-'));
}
}
}
stack = [...stack, ...lookupIfElseBlockStatementsTreePrefix(inspectedNode)];
}
return stack;
}
31 changes: 31 additions & 0 deletions tests/lib/rules/no-shadow-route-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,37 @@ ruleTester.run('no-shadow-route-definition', rule, {
this.route('route');
});`,

// Test if else conditions for route definitions
`if (this.options.getTreatmentIsEnabled('test-key')) {
this.route('new-route', { path: '/test' });
} else {
this.route('old-route', { path: '/test' });
}`,

`if (this.options.getTreatmentIsEnabled('test-key')) {
this.route('new-route', { path: '/test' });
} else {
if (false) {
this.route('old-route', { path: '/test' });
} else {
this.route('old-route-v2', { path: '/test' });
}
}`,

`if (this.options.getTreatmentIsEnabled('test-key')) {
if (true) {
this.route('new-route', { path: '/test' });
} else {
this.route('new-route-v2', { path: '/test' });
}
} else {
if (false) {
this.route('old-route', { path: '/test' });
} else {
this.route('old-route-v2', { path: '/test' });
}
}`,

// Not Ember's route function:
'test();',
"test('blog');",
Expand Down

0 comments on commit 863afc7

Please sign in to comment.