forked from emberjs/ember.js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[BUGFIX release] Deprecate using
controller
for {{with}}
- Loading branch information
Showing
4 changed files
with
87 additions
and
3 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
64 changes: 64 additions & 0 deletions
64
packages/ember-template-compiler/lib/plugins/deprecate-with-controller.js
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 @@ | ||
import Ember from 'ember-metal/core'; | ||
import calculateLocationDisplay from 'ember-template-compiler/system/calculate-location-display'; | ||
|
||
/** | ||
@module ember | ||
@submodule ember-template-compiler | ||
*/ | ||
|
||
/** | ||
An HTMLBars AST transformation that deprecates usage of `controller` with the `{{with}}` | ||
helper. | ||
@private | ||
@class DeprecateWithController | ||
*/ | ||
function DeprecateWithController(options) { | ||
// set later within HTMLBars to the syntax package | ||
this.syntax = null; | ||
this.options = options || {}; | ||
} | ||
|
||
/** | ||
@private | ||
@method transform | ||
@param {AST} ast The AST to be transformed. | ||
*/ | ||
DeprecateWithController.prototype.transform = function DeprecateWithController_transform(ast) { | ||
const pluginContext = this; | ||
const walker = new pluginContext.syntax.Walker(); | ||
const moduleName = pluginContext.options.moduleName; | ||
|
||
walker.visit(ast, function(node) { | ||
if (pluginContext.validate(node)) { | ||
let moduleInfo = calculateLocationDisplay(moduleName, node.loc); | ||
|
||
Ember.deprecate( | ||
`Using the {{with}} helper with a \`controller\` specified ${moduleInfo}is deprecated and will be removed in 2.0.0.`, | ||
false, | ||
{ id: 'ember-template-compiler.with-controller', until: '2.0.0' } | ||
); | ||
} | ||
}); | ||
|
||
return ast; | ||
}; | ||
|
||
DeprecateWithController.prototype.validate = function TransformWithAsToHash_validate(node) { | ||
return (node.type === 'BlockStatement' || node.type === 'MustacheStatement') && | ||
node.path.original === 'with' && | ||
hashPairForKey(node.hash, 'controller'); | ||
}; | ||
|
||
function hashPairForKey(hash, key) { | ||
for (let i = 0, l = hash.pairs.length; i < l; i++) { | ||
let pair = hash.pairs[i]; | ||
if (pair.key === key) { | ||
return pair; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
export default DeprecateWithController; |
13 changes: 13 additions & 0 deletions
13
packages/ember-template-compiler/tests/plugins/deprecate-with-controller-test.js
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,13 @@ | ||
import { compile } from 'ember-template-compiler'; | ||
|
||
QUnit.module('ember-template-compiler: deprecate-with-controller'); | ||
|
||
QUnit.test('Using `{{with}}` with `controller` hash argument provides a deprecation', function() { | ||
expect(1); | ||
|
||
expectDeprecation(function() { | ||
compile('{{#with controller="foo"}}{{/with}}', { | ||
moduleName: 'foo/bar/baz' | ||
}); | ||
}, `Using the {{with}} helper with a \`controller\` specified ('foo/bar/baz' @ L1:C0) is deprecated and will be removed in 2.0.0.`); | ||
}); |