Skip to content

Add catchRouterMain option (default false) to no-private-routing-service rule #871

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

Merged
merged 1 commit into from
Jun 28, 2020
Merged

Add catchRouterMain option (default false) to no-private-routing-service rule #871

merged 1 commit into from
Jun 28, 2020

Conversation

bmish
Copy link
Member

@bmish bmish commented Jun 28, 2020

Disallows this in favor of using the public routing service:

getOwner(this).lookup('router:main');

Fixes #762.

CC: @mongoose700 @mehulkar

@@ -6,6 +6,7 @@ Disallow the use of:

* The private `-routing` service
* The private `_routerMicrolib` property (when the `catchRouterMicrolib` option is enabled)
* The private `router:main` property (when the `catchRouterMain` option is enabled)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are there any plans to rename this to _router:main or something like that, to make it more obvious that it shouldn't be used?

Copy link
Member Author

Choose a reason for hiding this comment

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

No, I think it will just be removed eventually, and until that happens, it's the job of linting to make it clear that it shouldn't be used.

https://github.com/emberjs/rfcs/blob/master/text/0095-router-service.md#actively-discourage-use-of-private-api

@@ -93,6 +104,24 @@ module.exports = {
context.report({ node, message: ROUTER_MICROLIB_ERROR_MESSAGE });
}
},

CallExpression(node) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we also check OptionalCallExpression for owner?.lookup('router:main')?

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess so! I updated to catch that. Of course, it would be pretty bad practice to use optional chaining since lookup should always exist on owner.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This example would be the scenario where owner is undefined. For that, we'd need to handle owner.lookup?.('router:main')

},
{
// Optional chaining.
code: "this?.owner?.lookup('router:main');",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you do lookup?.( here?

Copy link
Member Author

Choose a reason for hiding this comment

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

Done.

@bmish bmish merged commit 5a1af69 into ember-cli:master Jun 28, 2020
// x?.lookup('router:main')
if (
catchRouterMain &&
(types.isMemberExpression(node.callee) || types.isOptionalMemberExpression(node.callee)) &&
Copy link
Collaborator

@mongoose700 mongoose700 Jun 28, 2020

Choose a reason for hiding this comment

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

It could make sense to make an isOptionalMemberOrMemberExpression function.

Copy link
Member Author

Choose a reason for hiding this comment

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

That makes sense given we'll probably need to use this combination in many places...

Copy link
Collaborator

Choose a reason for hiding this comment

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

It could almost make sense to rename isMemberExpression to isNonoptionalMemberExpression, to remind people that they should also consider the optional version. Though that could stray to far from the simple is... methods.

Copy link
Member Author

Choose a reason for hiding this comment

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

I like that idea.

node.callee.property.name === 'lookup' &&
node.arguments.length === 1 &&
types.isLiteral(node.arguments[0]) &&
typeof node.arguments[0].value === 'string' &&
Copy link
Collaborator

Choose a reason for hiding this comment

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

This check isn't actually necessary, since you're also checking its value with ===.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's true.

@bmish bmish changed the title Add catchRouterMain option to no-private-routing-service rule Add catchRouterMain option (default false) to no-private-routing-service rule Jun 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Prevent "bad" router usages
2 participants