Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert require statements to imports #51

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions test/fixtures/amd_with_require.after.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import a from 'a';
import b from 'b';
import c from 'c';
import g from 'g';
var d, e, f;

// let's define some things
var myModule = {
doStuffWithThings(val) {
return a.morgify(val);
},
};

export default myModule;
15 changes: 15 additions & 0 deletions test/fixtures/amd_with_require.before.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
define(function(require) {
var a = require('a');
var b = require('b');
var c = require('c');
var d, e, f, g = require('g');

// let's define some things
var myModule = {
doStuffWithThings(val) {
return a.morgify(val);
},
};

return myModule;
});
7 changes: 7 additions & 0 deletions test/transforms/amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@ describe('AMD transform', function() {
var result = amdTransform({ source: src }, { jscodeshift: jscodeshift });
assert.equal(result, expectedSrc);
});

it('should convert define(function(require) {})', function() {
var src = fs.readFileSync(path.resolve(__dirname, '../fixtures/amd_with_require.before.js')).toString();
var expectedSrc = fs.readFileSync(path.resolve(__dirname, '../fixtures/amd_with_require.after.js')).toString();
var result = amdTransform({ source: src }, { jscodeshift: jscodeshift });
assert.equal(result, expectedSrc);
});
});
18 changes: 18 additions & 0 deletions test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,24 @@ describe('util.getRecastConfig', function() {
});
})

describe('util.filterRedundantComments', function() {
it('should remove comments that already appear in the source comment array', function() {
var source = [
{ value: 'comment one' },
{ value: 'comment two' },
{ value: 'comment three' }
];

var test = [
{ value: 'comment one' },
{ value: 'new comment' }
];

var filtered = utils.filterRedundantComments(source, test);
assert.deepEqual(filtered, [{ value: 'new comment' }]);
});
});

/******************************************
* Test Helpers...
******************************************/
Expand Down
60 changes: 45 additions & 15 deletions transforms/amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
* amd - Replace define([]) calls with es6 import/exports
*/

var util = require('../utils/main');
const util = require('../utils/main');

module.exports = function(file, api, options) {
var j = api.jscodeshift;
var root = j(file.source);
var leadingComment = root.find(j.Program).get('body', 0).node.leadingComments;
const j = api.jscodeshift;
const root = j(file.source);
const leadingComment = root.find(j.Program).get('body', 0).node.leadingComments;

/**
* Convert an `return` to `export default`.
Expand All @@ -33,37 +33,67 @@ module.exports = function(file, api, options) {
.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);
let body = returnToExport(p.value.arguments[0].body.body);

let comments = p.parent.value.comments || [];

// replace any var x = require('x') with import x from 'x';
let importStatements = [];
body.filter(path => path.type === 'VariableDeclaration')
.forEach(path => {
path.declarations.forEach((d, i) => {
if (d.init
&& d.init.type === 'CallExpression'
&& d.init.callee.name === 'require') {
path.declarations.splice(i, 1);
var moduleName = d.init.arguments[0].value;
var variableName = d.id.name;
importStatements.push(util.createImportStatement(moduleName, variableName));
}
});
});

// remove any variable declarations which are now empty
body = body.filter(path => {
if (path.type !== 'VariableDeclaration') {
return true;
}
return path.declarations.length !== 0;
});

// add the body after the import statements
Array.prototype.push.apply(importStatements, body);

return j(p.parent).replaceWith(body);
// add any comments at the top
importStatements[0].comments = util.filterRedundantComments(leadingComment, comments);

return j(p.parent).replaceWith(importStatements);
}

// 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;
let props = p.value.arguments[0].elements;
let comments = p.parent.value.comments || [];
let importStatements = props.map(function(prop, i) {
let moduleName = prop.value;
let 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;
console.log(comments.value);
importStatements[0].comments = util.filterRedundantComments(leadingComment, comments);

// done
return j(p.parent).replaceWith(returnToExport(importStatements));
}

});

// re-add comment to to the top
Expand Down
9 changes: 9 additions & 0 deletions utils/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ var util = {

hasParentOfType: function(node, type) {
return util.findParentOfType(node, type) !== false;
},

filterRedundantComments: function(commentsOrig, commentsNew) {
if (!commentsOrig || !commentsNew) {
// return all comments if there were no original ones
return commentsNew;
}
return commentsNew.filter(comment =>
!commentsOrig.some(c => c.value === comment.value));
}
};

Expand Down