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 template tags for service usages in no-unused-services #1899

Merged
merged 3 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/rules/no-unused-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const {
MACROS_TO_TRACKED_ARGUMENT_COUNT,
} = require('../utils/computed-property-macros');
const Stack = require('../utils/stack');
const { TEMPLATE_TAG_PLACEHOLDER } = require('ember-template-imports/src/util');
const recast = require('ember-template-recast');

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
Expand All @@ -24,7 +26,7 @@ module.exports = {
hasSuggestions: true,
schema: [],
messages: {
main: 'The service `{{name}}` is not referenced in this JS file and might be unused (note: it could still be used in a corresponding handlebars template file, mixin, or parent/child class).',
main: 'The service `{{name}}` is not referenced in this file and might be unused (note: it could still be used in a corresponding handlebars template file, mixin, or parent/child class).',
removeServiceInjection: 'Remove the service injection.',
},
},
Expand Down Expand Up @@ -170,7 +172,22 @@ module.exports = {
return;
}

if (
if (node.callee.name === TEMPLATE_TAG_PLACEHOLDER) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we make this into a common util? Seems like something most rules will start needing to know about

(Can always do it when the 2nd use case shows up)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point! I'll wait for the next case to show up -- I'm sure there are things with the recast parsing that can be extracted out cleverly too, but I think it would be helpful to see some more usages of it first in order to know what common patterns people use.

// This is the <template></template> tag placeholder
// We now have to parse the template elements here to see if the services are used
const templateElements = node.arguments[0]?.quasis ?? [];
for (const templateElem of templateElements) {
const ast = recast.parse(templateElem.value.raw);
recast.traverse(ast, {
PathExpression(templateNode) {
if (templateNode.head.type === 'ThisHead') {
const tail = templateNode.tail[0];
currentClass.uses.add(tail);
}
},
});
}
} else if (
emberUtils.isComputedProp(node, importedEmberName, importedComputedName, {
includeMacro: true,
})
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
"css-tree": "^2.0.4",
"ember-rfc176-data": "^0.3.15",
"ember-template-imports": "^3.4.2",
"ember-template-recast": "^6.1.4",
"eslint-utils": "^3.0.0",
"estraverse": "^5.2.0",
"lodash.camelcase": "^4.1.1",
Expand Down
49 changes: 49 additions & 0 deletions tests/lib/rules-preprocessor/gjs-gts-processor-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ function initESLint(parser = '@babel/eslint-parser') {
'no-unused-vars': 'error',
'ember/no-get': 'off',
'ember/no-array-prototype-extensions': 'error',
'ember/no-unused-services': 'error',
},
},
});
Expand Down Expand Up @@ -116,6 +117,23 @@ const valid = [
}`,
parser: '@typescript-eslint/parser',
},
{
filename: 'my-component.gjs',
code: `
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';

export default class MyComponent extends Component {
@service foo;

<template>
{{this.foo}}
<div></div>
foobar
</template>
}
`,
},
/**
* TODO: SKIP this scenario. Tracked in https://github.com/ember-cli/eslint-plugin-ember/issues/1685
{
Expand Down Expand Up @@ -285,6 +303,37 @@ const invalid = [
},
],
},
{
filename: 'my-component.gjs',
code: `
import Component from '@glimmer/component';
import { inject as service } from '@ember/service';

export default class MyComponent extends Component {
@service foo;

@service bar;

<template>
{{this.foo.bar}}
{{this.bartender}}
<div>this.bar</div>
this.bar.foo
something.bar
</template>
}
`,
errors: [
{
message:
'The service `bar` is not referenced in this file and might be unused (note: it could still be used in a corresponding handlebars template file, mixin, or parent/child class).',
line: 8,
endLine: 8,
endColumn: 22,
column: 9,
},
],
},
];

describe('template-vars', () => {
Expand Down
68 changes: 63 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -535,19 +535,37 @@
resolved "https://registry.yarnpkg.com/@gar/promisify/-/promisify-1.1.3.tgz#555193ab2e3bb3b6adc3d551c9c030d9e860daf6"
integrity sha512-k2Ty1JcVojjJFwrg/ThKi2ujJ7XNLYaFGNB/bWT9wGR+oSMJHMa5w+CUq6p/pVrKeNNgA7pCqEcjSnHVoqJQFw==

"@glimmer/env@0.1.7":
"@glimmer/env@0.1.7", "@glimmer/env@^0.1.7":
version "0.1.7"
resolved "https://registry.yarnpkg.com/@glimmer/env/-/env-0.1.7.tgz#fd2d2b55a9029c6b37a6c935e8c8871ae70dfa07"
integrity sha512-JKF/a9I9jw6fGoz8kA7LEQslrwJ5jms5CXhu/aqkBWk+PmZ6pTl8mlb/eJ/5ujBGTiQzBhy5AIWF712iA+4/mw==

"@glimmer/global-context@0.84.3":
version "0.84.3"
resolved "https://registry.yarnpkg.com/@glimmer/global-context/-/global-context-0.84.3.tgz#f8bf2cda9562716f2ddf3f96837e7559600635c4"
integrity sha512-8Oy9Wg5IZxMEeAnVmzD2NkObf89BeHoFSzJgJROE/deutd3rxg83mvlOez4zBBGYwnTb+VGU2LYRpet92egJjA==
dependencies:
"@glimmer/env" "^0.1.7"

"@glimmer/interfaces@0.84.3":
version "0.84.3"
resolved "https://registry.yarnpkg.com/@glimmer/interfaces/-/interfaces-0.84.3.tgz#629777a4abe373b0785656f6c8d08989f5784805"
integrity sha512-dk32ykoNojt0mvEaIW6Vli5MGTbQo58uy3Epj7ahCgTHmWOKuw/0G83f2UmFprRwFx689YTXG38I/vbpltEjzg==
dependencies:
"@simple-dom/interface" "^1.4.0"

"@glimmer/syntax@^0.84.2":
"@glimmer/reference@^0.84.3":
version "0.84.3"
resolved "https://registry.yarnpkg.com/@glimmer/reference/-/reference-0.84.3.tgz#6420ad9c102633ac83939fd1b2457269d21fb632"
integrity sha512-lV+p/aWPVC8vUjmlvYVU7WQJsLh319SdXuAWoX/SE3pq340BJlAJiEcAc6q52y9JNhT57gMwtjMX96W5Xcx/qw==
dependencies:
"@glimmer/env" "^0.1.7"
"@glimmer/global-context" "0.84.3"
"@glimmer/interfaces" "0.84.3"
"@glimmer/util" "0.84.3"
"@glimmer/validator" "0.84.3"

"@glimmer/syntax@^0.84.2", "@glimmer/syntax@^0.84.3":
version "0.84.3"
resolved "https://registry.yarnpkg.com/@glimmer/syntax/-/syntax-0.84.3.tgz#4045a1708cef7fd810cff42fe6deeba40c7286d0"
integrity sha512-ioVbTic6ZisLxqTgRBL2PCjYZTFIwobifCustrozRU2xGDiYvVIL0vt25h2c1ioDsX59UgVlDkIK4YTAQQSd2A==
Expand All @@ -566,6 +584,14 @@
"@glimmer/interfaces" "0.84.3"
"@simple-dom/interface" "^1.4.0"

"@glimmer/validator@0.84.3", "@glimmer/validator@^0.84.3":
version "0.84.3"
resolved "https://registry.yarnpkg.com/@glimmer/validator/-/validator-0.84.3.tgz#cd83b7f9ab78953f23cc11a32d83d7f729c54df2"
integrity sha512-RTBV4TokUB0vI31UC7ikpV7lOYpWUlyqaKV//pRC4pexYMlmqnVhkFrdiimB/R1XyNdUOQUmnIAcdic39NkbhQ==
dependencies:
"@glimmer/env" "^0.1.7"
"@glimmer/global-context" "0.84.3"

"@handlebars/parser@~2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@handlebars/parser/-/parser-2.0.0.tgz#5e8b7298f31ff8f7b260e6b7363c7e9ceed7d9c5"
Expand Down Expand Up @@ -1605,7 +1631,7 @@ async-disk-cache@^1.2.1:
rsvp "^3.0.18"
username-sync "^1.0.2"

async-promise-queue@^1.0.3:
async-promise-queue@^1.0.3, async-promise-queue@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/async-promise-queue/-/async-promise-queue-1.0.5.tgz#cb23bce9fce903a133946a700cc85f27f09ea49d"
integrity sha512-xi0aQ1rrjPWYmqbwr18rrSKbSaXIeIwSd1J4KAgVfkq8utNbdZoht7GfvfY6swFUAMJ9obkc4WPJmtGwl+B8dw==
Expand Down Expand Up @@ -2270,6 +2296,11 @@ color-name@~1.1.4:
resolved "https://registry.yarnpkg.com/color-name/-/color-name-1.1.4.tgz#c2a09a87acbde69543de6f63fa3995c826c536a2"
integrity sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==

colors@^1.4.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/colors/-/colors-1.4.0.tgz#c50491479d4c1bdaed2c9ced32cf7c7dc2360f78"
integrity sha512-a+UqTh4kgZg/SlGvfbzDHpgRu7AAQOmmqRHJnxhRZICKFUT91brVhNNt58CMWU9PsBbv3PDCZUHbVxuDiH2mtA==

combined-stream@^1.0.8:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
Expand All @@ -2282,6 +2313,11 @@ commander@^10.0.0, commander@~10.0.1:
resolved "https://registry.yarnpkg.com/commander/-/commander-10.0.1.tgz#881ee46b4f77d1c1dccc5823433aa39b022cbe06"
integrity sha512-y4Mg2tXshplEbSGzx7amzPwKKOCGuoSRP/CjEdwwk0FOGlUbq6lKuoyDZTNZkmxHdJtp54hdfY/JUrdL7Xfdug==

commander@^8.3.0:
version "8.3.0"
resolved "https://registry.yarnpkg.com/commander/-/commander-8.3.0.tgz#4837ea1b2da67b9c616a67afbb0fafee567bca66"
integrity sha512-OkTL9umf+He2DZkUq8f8J9of7yL6RJKI24dVITBmNfZBmri9zYZQrKkuXiKhyfPSu8tUhnVBB1iKXevvnlR4Ww==

concat-map@0.0.1:
version "0.0.1"
resolved "https://registry.yarnpkg.com/concat-map/-/concat-map-0.0.1.tgz#d8a96bd77fd68df7793a73036a3ba0d5405d477b"
Expand Down Expand Up @@ -2667,6 +2703,23 @@ ember-template-imports@^3.4.2:
string.prototype.matchall "^4.0.6"
validate-peer-dependencies "^1.1.0"

ember-template-recast@^6.1.4:
version "6.1.4"
resolved "https://registry.yarnpkg.com/ember-template-recast/-/ember-template-recast-6.1.4.tgz#e964c184adfd876878009f8aa0b84c95633fce20"
integrity sha512-fCh+rOK6z+/tsdkTbOE+e7f84P6ObnIRQrCCrnu21E4X05hPeradikIkRMhJdxn4NWrxitfZskQDd37TR/lsNQ==
dependencies:
"@glimmer/reference" "^0.84.3"
"@glimmer/syntax" "^0.84.3"
"@glimmer/validator" "^0.84.3"
async-promise-queue "^1.0.5"
colors "^1.4.0"
commander "^8.3.0"
globby "^11.0.3"
ora "^5.4.0"
slash "^3.0.0"
tmp "^0.2.1"
workerpool "^6.4.0"

emittery@^0.13.1:
version "0.13.1"
resolved "https://registry.yarnpkg.com/emittery/-/emittery-0.13.1.tgz#c04b8c3457490e0847ae51fced3af52d338e3dad"
Expand Down Expand Up @@ -3553,7 +3606,7 @@ globby@13.1.4, globby@^13.1.2:
merge2 "^1.4.1"
slash "^4.0.0"

globby@^11.1.0:
globby@^11.0.3, globby@^11.1.0:
version "11.1.0"
resolved "https://registry.yarnpkg.com/globby/-/globby-11.1.0.tgz#bd4be98bb042f83d796f7e3811991fbe82a0d34b"
integrity sha512-jhIXaOzy1sb8IyocaruWSn1TjmnBVs8Ayhcy83rmxNJ8q2uWKCAj3CnJY+KpGSXCueAPc0i05kVvVKtP1t9S3g==
Expand Down Expand Up @@ -5940,7 +5993,7 @@ ora@6.3.1:
strip-ansi "^7.0.1"
wcwidth "^1.0.1"

ora@^5.4.1:
ora@^5.4.0, ora@^5.4.1:
version "5.4.1"
resolved "https://registry.yarnpkg.com/ora/-/ora-5.4.1.tgz#1b2678426af4ac4a509008e5e4ac9e9959db9e18"
integrity sha512-5b6Y85tPxZZ7QytO+BQzysW31HJku27cRIlkbAXaNx+BdcVi+LlRFmVXzeF6a7JCwJpyw5c4b+YSVImQIrBpuQ==
Expand Down Expand Up @@ -7837,6 +7890,11 @@ word-wrap@^1.2.3, word-wrap@~1.2.3:
resolved "https://registry.yarnpkg.com/word-wrap/-/word-wrap-1.2.3.tgz#610636f6b1f703891bd34771ccb17fb93b47079c"
integrity sha512-Hz/mrNwitNRh/HUAtM/VT/5VH+ygD6DV7mYKZAtHOrbs8U7lvPS6xf7EJKMF0uW1KJCl0H701g3ZGus+muE5vQ==

workerpool@^6.4.0:
version "6.4.0"
resolved "https://registry.yarnpkg.com/workerpool/-/workerpool-6.4.0.tgz#f8d5cfb45fde32fa3b7af72ad617c3369567a462"
integrity sha512-i3KR1mQMNwY2wx20ozq2EjISGtQWDIfV56We+yGJ5yDs8jTwQiLLaqHlkBHITlCuJnYlVRmXegxFxZg7gqI++A==

wrap-ansi@^6.0.1, wrap-ansi@^6.2.0:
version "6.2.0"
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-6.2.0.tgz#e9393ba07102e6c91a3b221478f0257cd2856e53"
Expand Down