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
Check imports when detecting computed properties in many rules #909
Conversation
lib/utils/ember.js
Outdated
} | ||
return isModule(node, 'computed'); | ||
node.callee.property.name === 'computed') || | ||
// Ember.computed().readOnly() or computed().readOnly() |
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.
These should be computed.
rather than computed().
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.
Oh actually I will change this comment to say:
// Ember.computed().volatile() or computed().volatile()
to avoid confusion with the macro.
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.
Ah. It could also help to do Ember.computed(...).volatile()
, since it isn't valid without a function as an argument to the computed
call.
node, | ||
importedEmberName, | ||
importedComputedName, | ||
{ includeSuffix, includeMacro } = {} |
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.
Could you add comments explaining these arguments?
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.
Good idea, added jsdoc comments.
lib/utils/ember.js
Outdated
importedEmberName, | ||
importedComputedName, | ||
{ includeSuffix, includeMacro } = {} | ||
) { | ||
const allowedMemberExpNames = new Set(['volatile', 'readOnly', 'property', 'meta']); |
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.
We could extract this set outside of the function, so that don't have to keep creating it.
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.
Fixed.
return { | ||
ImportDeclaration(node) { |
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.
This logic is getting repeated a lot of times. It could be nice to make a more generic imports
object, and do something like imports = updateImports(imports, node)
. Then isComputedProp
could take that imports
object as an argument.
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.
Ya, or even something like:
let imports = new Map();
return {
ImportDeclaration: gatherImports(imports),
// ...snip...
}
Where the gatherImports
method returns a function that works as a visitor handler itself, or if you also have custom import logic you need you could do this (with the same API):
let imports = new Map();
let importGatherer = gatherImports(imports);
return {
ImportDeclaration(node) {
importGatherer(node);
// other logic
}
};
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.
Definitely agree with this. I may do this as a follow-up so I can focus on this improved import gathering API by itself.
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.
Working on the new API in this PR: #910
Updates our Ember util function `isComputedProp` to consider the names that `Ember` and `computed` are imported under instead of assuming they are imported under the standard names. This util function is used in 10 rules.
let importedEmberName; | ||
let importedComputedName; |
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.
FWIW, there is nothing wrong with this (from a technical level):
import Ember from 'ember';
import Em from 'ember';
import LOL from 'ember;
All in the same module, and IMHO ^ should still trigger the rule. If I understand this code correctly, we are assuming there is only one.
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.
While dealing with this would be a large hassle to just tack on to the current implementation, I think it would be pretty easy to handle with the gatherImports
-style refactor.
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.
Will consider this in the future, although it does seem like it could be pretty inconvenient to handle this edge case. Hopefully, most people are using a rule like import/no-duplicates to avoid this.
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.
Maybe, not sure. I would expect that the goal of the a higher level gatherImports
style API is to abstract this issue away.
Updates our Ember util function
isComputedProp
to consider the names thatEmber
andcomputed
are imported under instead of assuming they are imported under the standard names. This util function is used in 10 rules for detecting computed properties.CC: @mongoose700
Helps with #590.