-
-
Notifications
You must be signed in to change notification settings - Fork 204
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
Add new rule no-implicit-service-injection-argument
#1188
Add new rule no-implicit-service-injection-argument
#1188
Conversation
6fab935
to
772c0f3
Compare
|
||
Disallow omitting the service name argument when injecting a service. | ||
|
||
Some developers may prefer to be more explicit about what service is being injected instead of relying on the implicit and potentially costly lookup/normalization of the service from the property name. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rwjblue is this the right justification for this rule?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@runspired thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's generally the right motivation, though in truth my feelings here have shifted with time.
With classic syntax, the implicit felt extra magical. With native class syntax and decorators, the service name being the key is a lot more clear (and understandable) I feel.
I think at this point what I want is for an error if the service name normalized from the key would be different from resolved name, I'm not sure if we can give the rule enough context for that or not. The key point is to enforce that it would work with the strict-resolver package if desired.
@scalvert or @stefanpenner may have good feedback here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@runspired thanks for the context. I agree with your goal of ensuring the service injection property matches the resolved name to avoid the need for normalization. I'm thinking of proceeding with this plan:
- Merge
no-implicit-service-injection-argument
as-is, always requiring the service injection argument. This would be intended as an optional (non-recommended) rule for people who prefer this style.- In the future, we could potentially make it smarter/configurable about when to require the service injection argument.
- Create a new rule
require-normalized-service-injections
(or similar name) which would both validate that the service being injected actually exists, and that the name it's injected with matches its resolved name (so that it works with ember-strict-resolver). This can be implemented once it's practical for the lint rule to understand the full service resolution logic (including how to find services in addons).
import { inject as service } from '@ember/service'; | ||
|
||
export default class Page extends Component { | ||
@service('service-name') serviceName; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rwjblue are both @service('service-name')
and @service('serviceName')
acceptable? Should we allow just one or both? And which one should we autofix to?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the rule to autofix to the kebab-case service name (i.e. @service('service-name')
) since kebab-case filenames are more common. In the future, once we can actually resolve services from inside the lint rule, we can make this autofixer use the actual resolved service name.
Also see this discussion: #1188 (comment)
772c0f3
to
aacb5a2
Compare
9e04a16
to
7c57229
Compare
5942c09
to
507167f
Compare
507167f
to
16cf66c
Compare
Fixes #195.