Skip to content

Commit

Permalink
fix(no-shadow-route): support if else statements for route definitions
Browse files Browse the repository at this point in the history
Resolves #1268
  • Loading branch information
raido committed Sep 14, 2021
1 parent 732bee9 commit 89445ba
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 4 deletions.
30 changes: 26 additions & 4 deletions lib/rules/no-shadow-route-definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ module.exports = {
return;
}

const routeLookupKey = `${routeInfo.route.blockStatementsTreePrefix}::${routeInfo.route.fullPathWithGenericParams}`;
if (
routeMap.has(routeInfo.route.fullPathWithGenericParams) &&
routeMap.has(routeLookupKey) &&
!isNestedRouteWithSamePath(routeInfo)
) {
const existingRouteInfo = routeMap.get(routeInfo.route.fullPathWithGenericParams);
const existingRouteInfo = routeMap.get(routeLookupKey);
context.report({
node,
message: buildErrorMessage({
Expand All @@ -93,8 +94,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 +119,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 +133,7 @@ function getRouteInfo(node, scopeManager) {
fullPathWithGenericParams: trimRootLevelNestedRoutes(
convertPathToGenericForMatching([...parentRoutes, basePath])
),
blockStatementsTreePrefix,
source: {
loc: node.loc,
},
Expand Down Expand Up @@ -252,3 +258,19 @@ 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 89445ba

Please sign in to comment.