Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support if/else route definitions in no-shadow-route-definition rule #1297

Merged
merged 1 commit into from
Sep 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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}`;
Copy link
Contributor Author

@raido raido Sep 14, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For routes that are not within if else blocks, key will look like ::<path> - we could set it global::<path> for easier understanding what's the contents of route map. 🤷‍♂️

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