Skip to content

Commit

Permalink
supports converting simplified commonjs wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
johnkpaul authored and thlorenz committed Aug 20, 2013
1 parent b514f00 commit 9a46cdf
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
25 changes: 18 additions & 7 deletions lib/get-wrapper.js
Expand Up @@ -28,9 +28,6 @@ function getReturnStatement(body) {
function getWrapper(ast, getReturn) {
/* TODO: currently not handling:
* - define({ .. }) "Simple Name/Value Pairs" case (http://requirejs.org/docs/api.html#defsimple)
* - define(function(require, exports, module) {
var a = require('a');
}) - Simplified CommonJs Wrapper (http://requirejs.org/docs/api.html#cjsmodule)
*/

var wrappers = ast.body
Expand All @@ -48,10 +45,24 @@ function getWrapper(ast, getReturn) {
if (!args.length) return null;

// we found a requirejs wrapper
var paths, fn;
var paths, fn, params;
if (args[0].type === syntax.FunctionExpression) {
// define(callback)
paths = [];
var declsWithRequire = expression.arguments[0].body.body.filter(function(decl){
return decl.type === "VariableDeclaration"
&& decl.declarations[0].init.callee
&& decl.declarations[0].init.callee.name === 'require';
});

paths = declsWithRequire.map(function(decl){
return decl.declarations[0].init.arguments[0].value;
});

params = declsWithRequire.map(function(decl){
return decl.declarations[0].id.name;
});

var headEndWithRequire = declsWithRequire[declsWithRequire.length - 1].range[1] + 1;
fn = args[0];
} else {
// define([..], callback)
Expand All @@ -61,9 +72,9 @@ function getWrapper(ast, getReturn) {

paths = args[0].elements.map(function (x) { return x.value; });
fn = args[1];
params = fn && fn.params ? fn.params.map(function (x) { return x.name; }) : [];
}

var params = fn && fn.params ? fn.params.map(function (x) { return x.name; }) : [];
var fnInnerBody = fn && fn.body ? fn.body.body : null;

var returnStmt;
Expand All @@ -90,7 +101,7 @@ function getWrapper(ast, getReturn) {
, params: params
, return: returnStmt
, body: fnInnerBody
, head: { start: expression.range[0], end: headEnd }
, head: { start: expression.range[0], end: headEndWithRequire ? headEndWithRequire : headEnd }
, tail: { start: tailStart, end: expression.range[1] + 1 }
};
})
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/define-simplified-wrapper.js
@@ -0,0 +1,19 @@
define(function (require) {
var director = require('director');
var github = require('github-index');
var blog = require('blog-index');
var about = require('about-index');
var githubContent = require('github-content');
var blogContent = require('blog-content');
var aboutContent = require('about-content');

var currentNav = null
, navs = {
github : github.init
, blog : blog.init
, about : about.init
}
;

return navs;
});
17 changes: 17 additions & 0 deletions test/fixtures/define-simplified-wrapper.upgraded.js
@@ -0,0 +1,17 @@
var director = require('./resolved/director');
var github = require('./resolved/github-index');
var blog = require('./resolved/blog-index');
var about = require('./resolved/about-index');
var githubContent = require('./resolved/github-content');
var blogContent = require('./resolved/blog-content');
var aboutContent = require('./resolved/about-content');

var currentNav = null
, navs = {
github : github.init
, blog : blog.init
, about : about.init
}
;

module.exports = navs;
1 change: 1 addition & 0 deletions test/upgrade.js
Expand Up @@ -48,6 +48,7 @@ function run(t, fixture, opts) {
}

[ [ 'upgrades code with define wrapper with multiple dependencies', 'define-multiline' ]
, [ 'upgrades code with define commonjs simplified wrapper with multiple dependencies', 'define-simplified-wrapper' ]
, [ 'upgrades code with define wrapper with multiple dependencies using comma style', 'define-multiline-comma', commaOpts ]
, [ 'upgrades code with define wrapper with multiple dependencies using comma-first style' , 'define-multiline-comma-first', commaFirstOpts ]
, [ 'upgrades code with define wrapper function definition before wrapper' , 'define-function-before' ]
Expand Down

0 comments on commit 9a46cdf

Please sign in to comment.