Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for app.container.lookup() deprecation
- Loading branch information
Showing
5 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 */ | ||
]); | ||
}); | ||
}); |