Skip to content

Instantly share code, notes, and snippets.

@bebraw
Created May 21, 2011 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bebraw/984325 to your computer and use it in GitHub Desktop.
Save bebraw/984325 to your computer and use it in GitHub Desktop.
"this" question material (JSShaper).
// current result
var Button = new Class({
initialize: function() {
this.color = 'blue';
this.on('click', function() {
that.color = 'red';
})
}
});
// desired result
var Button = new Class({
initialize: function() {
var that = this;
this.color = 'blue';
this.on('click', function() {
that.color = 'red';
})
}
});
// current input
var Button = new Class({
initialize: function() {
this.color = 'blue';
this.on('click', function() {
_.color = 'red';
})
}
});
// desired input
var Button = new Class({
initialize: function() {
this.color = 'blue';
this.on('click', function() {
_color = 'red';
})
}
});
"use strict"; "use restrict";
var Shaper = Shaper || require("shaper.js") || Shaper;
Shaper("this", function(root) {
return Shaper.traverseTree(root, {
pre: function(node, ref) {
var prefix = '_.';
var childThisName = 'that';
var expr = Shaper.parseExpression(prefix + '$');
if(Shaper.match(expr, node)) {
var thisExpr = Shaper.parseExpression(childThisName + '.$');
Shaper.replace(thisExpr, node.children[1]);
Shaper.cloneComments(thisExpr, node);
return ref.set(thisExpr);
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment