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

Check imports when detecting computed properties in many rules #909

Merged
merged 1 commit into from Aug 9, 2020
Merged

Check imports when detecting computed properties in many rules #909

merged 1 commit into from Aug 9, 2020

Conversation

bmish
Copy link
Member

@bmish bmish commented Aug 8, 2020

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 for detecting computed properties.

CC: @mongoose700

Helps with #590.

@bmish bmish added the Bug label Aug 8, 2020
}
return isModule(node, 'computed');
node.callee.property.name === 'computed') ||
// Ember.computed().readOnly() or computed().readOnly()
Copy link
Collaborator

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().

Copy link
Member Author

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.

Copy link
Collaborator

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 } = {}
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 add comments explaining these arguments?

Copy link
Member Author

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.

importedEmberName,
importedComputedName,
{ includeSuffix, includeMacro } = {}
) {
const allowedMemberExpNames = new Set(['volatile', 'readOnly', 'property', 'meta']);
Copy link
Collaborator

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.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

return {
ImportDeclaration(node) {
Copy link
Collaborator

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.

Copy link
Member

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
  }
};

Copy link
Member Author

@bmish bmish Aug 8, 2020

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.

Copy link
Member Author

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.
Comment on lines +98 to +99
let importedEmberName;
let importedComputedName;
Copy link
Member

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.

Copy link
Collaborator

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.

Copy link
Member Author

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.

Copy link
Member

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.

@bmish bmish merged commit 0e44781 into ember-cli:master Aug 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants