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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
const ember = require('../utils/ember'); | ||
const types = require('../utils/types'); | ||
const { getImportIdentifier } = require('../utils/import'); | ||
|
||
//------------------------------------------------------------------------------ | ||
// General rule - Prevent using a getter inside computed properties. | ||
|
@@ -94,9 +95,25 @@ module.exports = { | |
} | ||
}; | ||
|
||
let importedEmberName; | ||
let importedComputedName; | ||
|
||
return { | ||
ImportDeclaration(node) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. Working on the new API in this PR: #910 |
||
if (node.source.value === 'ember') { | ||
importedEmberName = importedEmberName || getImportIdentifier(node, 'ember'); | ||
} | ||
if (node.source.value === '@ember/object') { | ||
importedComputedName = | ||
importedComputedName || getImportIdentifier(node, '@ember/object', 'computed'); | ||
} | ||
}, | ||
|
||
CallExpression(node) { | ||
if (ember.isComputedProp(node) && node.arguments.length > 0) { | ||
if ( | ||
ember.isComputedProp(node, importedEmberName, importedComputedName) && | ||
node.arguments.length > 0 | ||
) { | ||
if (requireGetters === 'always-with-setter') { | ||
requireGetterOnlyWithASetterInComputedProperty(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.
FWIW, there is nothing wrong with this (from a technical level):
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.