Skip to content

Commit

Permalink
Add support for app.container.lookup() deprecation
Browse files Browse the repository at this point in the history
  • Loading branch information
minichate committed Dec 12, 2015
1 parent f2f5603 commit b9252b8
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 0 deletions.
1 change: 1 addition & 0 deletions .integration_jscsrc
Expand Up @@ -29,5 +29,6 @@
"disallowHandlebarsHelpers": true,
"disallowInitializerArity": true,
"disallowPrivateRegistryProperty": true,
"disallowAppInstanceContainer": true,
"disallowPrototypeExtension": true
}
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -105,6 +105,8 @@ None. Ember 2.0 removed support for the above deprecations.

- `disallowPrivateRegistryProperty` will warn you if you use one of the private `app.registry.*` deprecations. See http://emberjs.com/deprecations/v2.x/#toc_ember-application-registry-ember-applicationinstance-registry for details.

- `disallowAppInstanceContainer` will warn you if you use the private `app.container.lookup()` depreprecation in an initializer. See http://emberjs.com/deprecations/v2.x/#toc_ember-application-registry-ember-applicationinstance-registry for details.

### Other Ember best practices

- `disallowPrototypeExtension` will warn you if you are using `.property()`, `.observes()` or `observesBefore()`. See http://guides.emberjs.com/v1.10.0/configuring-ember/disabling-prototype-extensions/#toc_functions for details.
Expand Down
1 change: 1 addition & 0 deletions lib/index.js
Expand Up @@ -24,4 +24,5 @@ module.exports = function(configuration) {
configuration.registerRule(require('./rules/disallow-handlebarshelpers'));
configuration.registerRule(require('./rules/disallow-initializerarity'));
configuration.registerRule(require('./rules/disallow-privateregistryproperty'));
configuration.registerRule(require('./rules/disallow-appinstancecontainer'));
};
64 changes: 64 additions & 0 deletions lib/rules/disallow-appinstancecontainer.js
@@ -0,0 +1,64 @@
var assert = require('assert');

module.exports = function() {};

module.exports.prototype.getOptionName = function() {
return 'disallowAppInstanceContainer';
};

module.exports.prototype.configure = function(options) {
assert(
options === true || options === false,
this.getOptionName() + ' option requires a true value or should be removed'
);
};

module.exports.prototype.check = function(file, errors) {
if (file.getFilename().indexOf('initializer') < 0) {
return;
}

file.iterateNodesByType('Property', function(node) {
if (node.key.name !== 'initialize') {
return;
}

if (node.value.type === 'Identifier') {
// this is a reference to a declared variable
var identifier = node.value.name;

file.iterateNodesByType('Identifier', function(variable) {
if (variable.name === identifier && variable.parentNode.init) {
node = variable.parentNode.init;
}
});
} else {
node = node.value;
}

if (node.type === 'FunctionExpression') {
var appInstance = node.params[0];
if (!appInstance) {
return;
}

file.iterateNodesByType('MemberExpression', function(member) {
if (!member.object) {
return;
}
var variable = member.object;

if (variable.name === appInstance.name) {
if (member.property.name === 'container') {
errors.cast({
message: 'application.container.lookup is deprecated in Ember 2.1',
line: member.property.loc.start.line,
column: member.property.loc.start.column,
additional: member.property
});
}
}
});
}
});
};
62 changes: 62 additions & 0 deletions test/lib/rules/disallow-appinstancecontainer.js
@@ -0,0 +1,62 @@
describe('lib/rules/disallow-appinstancecontainer', function () {
var checker = global.checker({
plugins: ['./lib/index']
});

describe('not configured', function() {

it('should report with undefined', function() {
global.expect(function() {
checker.configure({disallowAppInstanceContainer: undefined});
}).to.throws(/requires a true value/i);
});

it('should report with an object', function() {
global.expect(function() {
checker.configure({disallowAppInstanceContainer: {}});
}).to.throws(/requires a true value/i);
});

});

describe('with true', function() {
checker.rules({disallowAppInstanceContainer: true});

checker.cases([
/* jshint ignore:start */
{
it: 'should not report appinstance.lookup',
filename: 'app/initializer/foo.js',
code: function() {
var initialize = function(appInstance) {
var store = appInstance.lookup('service:store');

store.pushPayload('<payload here>');
}

return {
name: 'preload-store',
initialize: initialize
}
}
}, {
it: 'should report appinstance.container',
filename: 'app/initializer/foo.js',
errors: 1,
code: function() {
var initialize = function(appInstance) {
var store = appInstance.container.lookup('service:store');

store.pushPayload('<payload here>');
}

return {
name: 'preload-store',
initialize: initialize
}
}
}
/* jshint ignore:end */
]);
});
});

0 comments on commit b9252b8

Please sign in to comment.