diff --git a/docs/rules/no-unused-services.md b/docs/rules/no-unused-services.md index ca84a87480..e00ab4838d 100644 --- a/docs/rules/no-unused-services.md +++ b/docs/rules/no-unused-services.md @@ -14,6 +14,8 @@ By removing unused service injections, we can reduce the amount of code we have - A mixin or parent class that the current class extends from - A child class that extends from the current class +The rule also skips any class that passes its `this` to a function or `new` call (for example `helper(this)`, `obj.method(this)` or `new Foo(this)`). The called code could use any service, so the rule cannot tell which ones are unused. Calls to `get`, `set`, `getProperties` or `setProperties` from `@ember/object` do not count. + Given these significant limitations, the rule is not currently recommended for production usage, but some may find it useful to experiment with. The rule will not be added to the `recommended` configuration unless the limitations can be addressed. ## Examples diff --git a/lib/rules/no-unused-services.js b/lib/rules/no-unused-services.js index 6d7c7c2855..e6ede09dfc 100644 --- a/lib/rules/no-unused-services.js +++ b/lib/rules/no-unused-services.js @@ -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 = [ + 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 ( diff --git a/tests/lib/rules/no-unused-services.js b/tests/lib/rules/no-unused-services.js index 6cdf113457..5a8468f3c0 100644 --- a/tests/lib/rules/no-unused-services.js +++ b/tests/lib/rules/no-unused-services.js @@ -188,8 +188,57 @@ ruleTester.run('no-unused-services', rule, { `${SERVICE_IMPORT} Component.extend({});`, 'const foo = service();', "import ComputedProperty from '@ember/object/computed';", + /* `this` is passed somewhere, so the rule is turned off for the class */ + `${SERVICE_IMPORT} const helper = (c) => c.${SERVICE_NAME}; class MyClass { @service() ${SERVICE_NAME}; get foo() { return helper(this); } }`, + `${SERVICE_IMPORT} import helper from './helper'; class MyClass { @service() ${SERVICE_NAME}; foo() { return helper(1, this); } }`, + `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; foo() { return this.other.method(this); } }`, + `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; foo() { return new Foo(this); } }`, + `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; foo() { return [1].map(() => helper(this)); } }`, + `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; constructor() { super(); register(this); } }`, + `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; foo = helper(this); }`, + `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; foo = () => helper(this); }`, + `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; foo() { return helper?.(this); } }`, + `${SERVICE_IMPORT} Component.extend({ ${SERVICE_NAME}: service(), foo() { return helper(this); } });`, + // `get` is not from @ember/object here, so it is an unknown function that gets `this` + `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; fooFunc() {${emberObjectUses1}} }`, + `${SERVICE_IMPORT} Component.extend({ ${SERVICE_NAME}: service(), fooFunc() {${emberObjectUses1}} });`, + `${SERVICE_IMPORT}${EO_IMPORTS} Component.extend({ ${SERVICE_NAME}: service(), foo: computed(function () { return helper(this); }) });`, + // Any `this` passed inside the class body counts, like any `this.foo` counts as a use. + `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; foo() { return function () { return helper(this); }; } }`, + `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; foo() { return class { bar() { return helper(this); } }; } }`, + `${SERVICE_IMPORT} Component.extend({ ${SERVICE_NAME}: service(), foo: helper(this) });`, + `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; static foo = helper(this); }`, + `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; static foo() { return helper(this); } }`, + `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; static { helper(this); } }`, + `${SERVICE_IMPORT} Component.extend({ ${SERVICE_NAME}: service(), foo() { return [1].map(function () { return helper(this); }); } });`, ], invalid: [ + /* `this` is not passed anywhere */ + { + code: `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; foo() { return helper(this.other); } }`, + output: null, + errors: [{ messageId: 'main' }], + }, + { + code: `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; foo() { class Inner { bar() { return helper(this); } } return Inner; } }`, + output: null, + errors: [{ messageId: 'main' }], + }, + { + code: `${SERVICE_IMPORT} import {set} from '@ember/object'; class MyClass { @service() ${SERVICE_NAME}; foo() { set(this, 'unrelatedProp', 1); } }`, + output: null, + errors: [{ messageId: 'main' }], + }, + { + code: `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; foo() { const { other } = this; return other === this; } }`, + output: null, + errors: [{ messageId: 'main' }], + }, + { + code: `${SERVICE_IMPORT} helper(this); class MyClass { @service() ${SERVICE_NAME}; }`, + output: null, + errors: [{ messageId: 'main' }], + }, { code: `${SERVICE_IMPORT} class MyClass { @service('foo') ${SERVICE_NAME}; fooFunc() {${nonUses}} }`, output: null, @@ -303,39 +352,6 @@ ruleTester.run('no-unused-services', rule, { }, ], }, - /* Using get/getProperties without @ember/object import */ - { - code: `${SERVICE_IMPORT} class MyClass { @service() ${SERVICE_NAME}; fooFunc() {${emberObjectUses1}} }`, - output: null, - errors: [ - { - messageId: 'main', - suggestions: [ - { - messageId: 'removeServiceInjection', - output: `${SERVICE_IMPORT} class MyClass { fooFunc() {${emberObjectUses1}} }`, - }, - ], - // type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8) - }, - ], - }, - { - code: `${SERVICE_IMPORT} Component.extend({ ${SERVICE_NAME}: service(), fooFunc() {${emberObjectUses1}} });`, - output: null, - errors: [ - { - messageId: 'main', - suggestions: [ - { - messageId: 'removeServiceInjection', - output: `${SERVICE_IMPORT} Component.extend({ fooFunc() {${emberObjectUses1}} });`, - }, - ], - type: 'Property', - }, - ], - }, /* Using get/getProperties with @ember/object import for an unrelatedProp */ { code: `${SERVICE_IMPORT}${EO_IMPORTS} class MyClass { @service() ${SERVICE_NAME}; fooFunc() {${emberObjectUses2}} }`,