Skip to content

Instantly share code, notes, and snippets.

@SergioCrisostomo
Forked from kentaromiura/transform.js
Last active September 7, 2015 20:06
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 SergioCrisostomo/f3816524e70d965ff19e to your computer and use it in GitHub Desktop.
Save SergioCrisostomo/f3816524e70d965ff19e to your computer and use it in GitHub Desktop.
MooTools Class to ES 2015
module.exports = function(file, api) {
var j = api.jscodeshift;
var root = j(file.source);
root.find(j.Identifier).forEach(function(p) {
if(p.value.name === 'Class') {
if(p.parentPath.name === 'init'){
var classDefinition = p.parentPath.value.arguments[0];
var varname = p.parentPath.parentPath;
var name = varname.value.id;
var target = varname.parentPath.parentPath
var Extends = null;
var methods = classDefinition.properties.map((prop) => {
var name = prop.key.name
if (name === 'initialize') name = 'constructor';
if (name === 'Extends') {
Extends = j.identifier(prop.value.name);
return {}
}
// TODO: Implements
var body = prop.value.body;
j(body).find(j.MemberExpression).forEach((calls)=>{
if(calls.value.property.name === 'parent'){
j(calls).replaceWith(j.identifier('super'))
}
})
return {name, [name]:body, params:prop.value.params}
})
var definition = [];
for(var method of methods){
if(method.name)
definition.push(j.methodDefinition('init',
j.identifier(method.name),
j.functionExpression(null, method.params, method[method.name])
));
}
var substituteClass = j.classDeclaration(name, j.classBody(definition), Extends)
j(target).replaceWith(substituteClass);
}
}
});
return root.toSource();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment