Skip to content

Commit

Permalink
Avoid crash with inject decorator in `no-restricted-service-injecti…
Browse files Browse the repository at this point in the history
…ons` rule (#1869)
  • Loading branch information
bmish committed May 21, 2023
1 parent de2dfa5 commit 71be77f
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/rules/no-restricted-service-injections.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ module.exports = {
}

// Find the service decorator.
const serviceDecorator = decoratorUtils.findDecorator(node, 'service');
const serviceDecorator =
decoratorUtils.findDecorator(node, 'service') ||
decoratorUtils.findDecorator(node, 'inject');

// Get the service name either from the string argument or from the property name.
const serviceName =
Expand Down
36 changes: 36 additions & 0 deletions tests/lib/rules/no-restricted-service-injections.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const rule = require('../../../lib/rules/no-restricted-service-injections');
const { DEFAULT_ERROR_MESSAGE } = rule;

const EMBER_IMPORT = "import Ember from 'ember';";
const INJECT_IMPORT = "import {inject} from '@ember/service';";
const SERVICE_IMPORT = "import {inject as service} from '@ember/service';";
const NEW_SERVICE_IMPORT = "import {service} from '@ember/service';";

Expand All @@ -23,6 +24,12 @@ ruleTester.run('no-restricted-service-injections', rule, {
code: `${SERVICE_IMPORT} Component.extend({ myService: service() })`,
options: [{ paths: ['app/components'], services: ['abc'] }],
},
{
// Service name doesn't match (with property name):
filename: 'app/components/path.js',
code: `${INJECT_IMPORT} Component.extend({ myService: inject() })`,
options: [{ paths: ['app/components'], services: ['abc'] }],
},
{
// Service name doesn't match (with property name):
filename: 'app/components/path.js',
Expand Down Expand Up @@ -148,6 +155,19 @@ ruleTester.run('no-restricted-service-injections', rule, {
},
],
},
{
// With decorator with camelized service name argument:
filename: 'app/components/path.js',
code: `${INJECT_IMPORT} class MyComponent extends Component { @inject('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 decorator with dasherized service name argument:
filename: 'app/components/path.js',
Expand Down Expand Up @@ -236,5 +256,21 @@ ruleTester.run('no-restricted-service-injections', rule, {
options: [{ services: ['my-service'] }],
errors: [{ message: DEFAULT_ERROR_MESSAGE, type: 'Property' }],
},

{
// TypeScript annotated injection (TypeScript).
code: `
import Route from '@ember/routing/route';
${INJECT_IMPORT}
import type Store from '@ember-data/store';
export default class ApplicationRoute extends Route {
@inject declare store: Store;
}
`,
output: null,
options: [{ services: ['store'] }],
parser: require.resolve('@typescript-eslint/parser'),
errors: [{ message: DEFAULT_ERROR_MESSAGE, type: 'PropertyDefinition' }],
},
],
});

0 comments on commit 71be77f

Please sign in to comment.