Skip to content

Commit

Permalink
Avoid further decorator detection crashes in `no-restricted-service-i…
Browse files Browse the repository at this point in the history
…njections` rule (#1871)
  • Loading branch information
bmish committed May 21, 2023
1 parent 3f9a95f commit 9233899
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 2 deletions.
8 changes: 7 additions & 1 deletion lib/rules/no-restricted-service-injections.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,13 @@ module.exports = {
// Find the service decorator.
const serviceDecorator =
decoratorUtils.findDecorator(node, 'service') ||
decoratorUtils.findDecorator(node, 'inject');
decoratorUtils.findDecorator(node, 'inject') ||
decoratorUtils.findDecorator(node, 'Ember.inject.service');

if (!serviceDecorator) {
// TODO: emberUtils.isInjectedServiceProp() must have detected an injection that findDecorator() didn't.
return;
}

// Get the service name either from the string argument or from the property name.
const serviceName =
Expand Down
22 changes: 21 additions & 1 deletion tests/lib/rules/no-restricted-service-injections.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ ruleTester.run('no-restricted-service-injections', rule, {
code: `${SERVICE_IMPORT} Component.extend({ myService: service(SOME_VARIABLE) })`,
options: [{ paths: ['app/components'], services: ['my-service'] }],
},
{
// TODO: This should be invalid. With `inject.service` decorator.
filename: 'app/components/path.js',
code: `${INJECT_IMPORT} class MyComponent extends Component { @inject.service('myService') randomName }`,
output: null,
options: [{ paths: ['app/components'], services: ['my-service'] }],
},
],
invalid: [
{
Expand Down Expand Up @@ -156,7 +163,7 @@ ruleTester.run('no-restricted-service-injections', rule, {
],
},
{
// With decorator with camelized service name argument:
// With decorator with camelized service name argument (inject):
filename: 'app/components/path.js',
code: `${INJECT_IMPORT} class MyComponent extends Component { @inject('myService') randomName }`,
output: null,
Expand Down Expand Up @@ -220,6 +227,19 @@ ruleTester.run('no-restricted-service-injections', rule, {
},
],
},
{
// With chained decorator
filename: 'app/components/path.js',
code: `${EMBER_IMPORT} class MyComponent extends Component { @Ember.inject.service('myService') randomName }`,
output: null,
options: [{ paths: ['app/components'], services: ['my-service'] }],
errors: [
{
message: DEFAULT_ERROR_MESSAGE,
// type could be ClassProperty (ESLint v7) or PropertyDefinition (ESLint v8)
},
],
},
{
// With custom error message:
filename: 'app/components/path.js',
Expand Down

0 comments on commit 9233899

Please sign in to comment.