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

Avoid crash with inject decorator in no-restricted-service-injections rule #1869

Merged
merged 1 commit into from
May 21, 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
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' }],
},
],
});