Repository navigation
Skip classes that pass this to a call in no-unused-services
#2887
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
Changes from all commits
56b1216
6fd02f4
4e8d14c
9913b0a
1cb3713
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 |
|---|---|---|
|
|
@@ -40,6 +40,8 @@ module.exports = { | |
| let importedInjectName; | ||
| let importedObserverName; | ||
| let importedObservesName; | ||
| let importedSetName; | ||
| let importedSetPropertiesName; | ||
| const macros = getMacros(); | ||
| const importedMacros = {}; | ||
|
|
||
|
|
@@ -60,8 +62,9 @@ module.exports = { | |
| */ | ||
| function reportInstances() { | ||
| const currentClass = classStack.pop(); | ||
| const { services, uses } = currentClass; | ||
| if (Object.keys(services).length === 0) { | ||
| const { services, uses, passesThis } = currentClass; | ||
| // A class that passes its `this` to a call: we cannot know what the call reads from it. | ||
| if (Object.keys(services).length === 0 || passesThis) { | ||
| return; | ||
| } | ||
|
|
||
|
|
@@ -92,6 +95,30 @@ module.exports = { | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * Turns the rule off for the current class when it passes `this` to a call: | ||
| * `fn(this)`, `obj.method(this)`, `new Foo(this)`, `fn?.(this)`. | ||
| * `get(this, 'foo')` / `set(this, ...)` from `@ember/object` do not count. | ||
| * @param {ThisExpression} node | ||
| */ | ||
| function checkThisPassed(node) { | ||
| const currentClass = classStack.peek(); | ||
| if (!currentClass) { | ||
| return; | ||
| } | ||
| const { callee } = node.parent; | ||
| const emberObjectFunctions = [ | ||
|
Contributor
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. why are we checking these specifically?
Contributor
Author
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. Because the rule already understands those four. If they counted as a passed |
||
| importedGetName, | ||
| importedGetPropertiesName, | ||
| importedSetName, | ||
| importedSetPropertiesName, | ||
| ].filter(Boolean); | ||
| if (callee.type === 'Identifier' && emberObjectFunctions.includes(callee.name)) { | ||
| return; | ||
| } | ||
| currentClass.passesThis = true; | ||
| } | ||
|
|
||
| // Handle either ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8). | ||
| function visitClassPropertyOrPropertyDefinition(node) { | ||
| // Handles: | ||
|
|
@@ -127,6 +154,10 @@ module.exports = { | |
| getImportIdentifier(node, '@ember/object', 'getProperties'); | ||
| importedObserverName = | ||
| importedObserverName || getImportIdentifier(node, '@ember/object', 'observer'); | ||
| importedSetName = importedSetName || getImportIdentifier(node, '@ember/object', 'set'); | ||
| importedSetPropertiesName = | ||
| importedSetPropertiesName || | ||
| getImportIdentifier(node, '@ember/object', 'setProperties'); | ||
| } | ||
| if (node.source.value === '@ember/object/computed') { | ||
| for (const spec of node.specifiers) { | ||
|
|
@@ -370,6 +401,9 @@ module.exports = { | |
| currentClass.uses.add(name); | ||
| } | ||
| }, | ||
| // fn(this), new Foo(this) | ||
| 'CallExpression > ThisExpression.arguments, NewExpression > ThisExpression.arguments': | ||
| checkThisPassed, | ||
| VariableDeclarator(node) { | ||
| const currentClass = classStack.peek(); | ||
| if ( | ||
|
|
||
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 don't think we need all this -- we already have a
ThisExpression, and we can already know via other visitors if we are within an Optional/Call/New Expression.but also! for the
thistracking -- have you explored if ESLint's ScopeManager correctly associates athiswith the class?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 call on both, done in 9913b0a.
thisStackis gone. The class for athisnow comes from the scope manager: start atgetScope(thisNode), skip arrow function scopes, then look at that scope's block. A method or field value gives its class (static ones don't count), and afunctioninsideFoo.extend({...})gives that classic class. ESLint's ScopeManager doesn't recordthisas a reference (it only tracks identifiers), so this short walk over the scope chain is still needed, but it only touches scopes, not the AST.'CallExpression > ThisExpression.arguments, NewExpression > ThisExpression.arguments'.fn?.(this)is covered too, since it's aCallExpressionin ESTree.const self = thiscases to keep it small. Happy to add{ owner: this }back if you want it.Checked with both
@babel/eslint-parserand@typescript-eslint/parser(arrow fieldfoo = () => helper(this)counts;static {}blocks and static methods don't). Full suite and lint pass.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 is looking much better already
the impl of this PR is already like 200 lines shorten than when it started!