Skip to content

Commit

Permalink
fix: check for nested services and nested service config
Browse files Browse the repository at this point in the history
  • Loading branch information
rtablada committed Dec 30, 2022
1 parent 61ebd4f commit 1c4a0fd
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/rules/no-implicit-injections.js
Expand Up @@ -5,7 +5,6 @@ const emberUtils = require('../utils/ember');
const { getImportIdentifier } = require('../utils/import');
const Stack = require('../utils/stack');
const types = require('../utils/types');
const kebabCase = require('lodash.kebabcase');
const camelCase = require('lodash.camelcase');

const defaultServiceConfig = { service: 'store', moduleNames: ['Route', 'Controller'] };
Expand Down Expand Up @@ -110,6 +109,11 @@ module.exports = {
config.service.toLowerCase() === config.service,
'Service name should be passed in kebab-case (all lower case)'
);

assert(
!config.service.includes('/') || config.propertyName,
'Nested services must declare a property name'
);
}

// State being tracked for this file.
Expand Down Expand Up @@ -211,7 +215,7 @@ module.exports = {
node,
messageId: 'main',
data: {
serviceName: kebabCase(failedConfig.service),
serviceName: failedConfig.service,
},
fix(fixer) {
const sourceCode = context.getSourceCode();
Expand Down
52 changes: 50 additions & 2 deletions tests/lib/rules/no-implicit-injections.js
Expand Up @@ -29,6 +29,9 @@ const MEDIA_CONFIG = {
const FEATURE_CHECKER_CONFIG = {
denyList: [{ service: 'feature', propertyName: 'featureChecker' }],
};
const NESTED_SERVICE_CONFIG = {
denyList: [{ service: 'cart/checkout', propertyName: 'checkout' }],
};

function createClassUsage(serviceDefinition) {
return {
Expand Down Expand Up @@ -216,16 +219,33 @@ ruleTester.run('no-implicit-injections', rule, {
{
filename: 'routes/index.js',
code: `
import { inject as service } from '@ember/service';
import Route from '@ember/routing/route';
export default class IndexRoute extends Route {
featureChecker = null;
@service('feature') featureChecker;
get canVisitCheckout() {
return this.featureChecker.isEnabled('checkout');
}
}`,
options: [MEDIA_CONFIG],
options: [FEATURE_CHECKER_CONFIG],
},
// Can work with services with nested module paths
{
filename: 'routes/index.js',
code: `
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class IndexRoute extends Route {
@service('cart/checkout') checkout;
model() {
return this.checkout.viewCart();
}
}`,
options: [NESTED_SERVICE_CONFIG],
},
// Ignores use outside of classes
{
Expand Down Expand Up @@ -729,6 +749,34 @@ get canVisitCheckout() {
errors: [{ messageId: 'main', data: { serviceName: 'feature' }, type: 'MemberExpression' }],
},

// Can work with services with nested module paths
{
filename: 'routes/index.js',
code: `
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class IndexRoute extends Route {
model() {
return this.checkout.viewCart();
}
}`,
output: `
import Route from '@ember/routing/route';
import { inject as service } from '@ember/service';
export default class IndexRoute extends Route {
@service('cart/checkout') checkout;
model() {
return this.checkout.viewCart();
}
}`,
options: [NESTED_SERVICE_CONFIG],
errors: [
{ messageId: 'main', data: { serviceName: 'cart/checkout' }, type: 'MemberExpression' },
],
},

// Check use and fix in legacy ember components
{
filename: 'pods/index.js',
Expand Down

0 comments on commit 1c4a0fd

Please sign in to comment.