Skip to content

Instantly share code, notes, and snippets.

@robertkowalski
Created May 30, 2016 13:32
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 robertkowalski/9167d45c0af6b9abdd4b51a09ef4e039 to your computer and use it in GitHub Desktop.
Save robertkowalski/9167d45c0af6b9abdd4b51a09ef4e039 to your computer and use it in GitHub Desktop.
/**
* amd - Replace define([]) calls with es6 import/exports
*/
var util = require('../utils/main');
module.exports = function(file, api) {
var j = api.jscodeshift;
var root = j(file.source);
var leadingComment = root.find(j.Program).get('body', 0).node.leadingComments;
/**
* Convert an `return` to `export default`.
* @param ast - Function body AST (Array)
*/
function returnToExport(body) {
var exportStatement;
var possibleReturn = body[body.length - 1];
if (possibleReturn && possibleReturn.type === 'ReturnStatement') {
exportStatement = j.exportDeclaration(true, possibleReturn.argument);
body[body.length - 1] = exportStatement;
}
return body;
}
root
.find(j.CallExpression, { callee: { name: 'define' } }) // find require() function calls
.filter(function(p) { return p.parentPath.parentPath.name === 'body'; })
.forEach(function(p) {
var body;
// define(function() { });
if (p.value.arguments.length === 1) {
// convert `return` statement to `export default`
body = returnToExport(p.value.arguments[0].body.body);
return j(p.parent).replaceWith(body);
}
// define(['a', 'b', 'c'], function(a, b, c) { });
if (p.value.arguments.length === 2) {
var props = p.value.arguments[0].elements;
var comments = p.parent.value.comments || [];
var importStatements = props.map(function(prop, i) {
var moduleName = prop.value;
var variableName = p.value.arguments[1].params[i] && p.value.arguments[1].params[i].name;
return util.createImportStatement(moduleName, variableName);
});
// add the body after the import statements
Array.prototype.push.apply(importStatements, p.value.arguments[1].body.body);
// add any comments at the top
//importStatements[0].comments = comments;
// done
return j(p.parent).replaceWith(returnToExport(importStatements));
}
});
// re-add comment to to the top
if (!root.get().node.comments)
root.get().node.comments = leadingComment;
// FIXME: make this a config to pass in?
return root.toSource() + '\n';
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment