Skip to content

Commit

Permalink
Adding dependency inference for module.declare(factory) signature.
Browse files Browse the repository at this point in the history
  • Loading branch information
domenic committed Dec 7, 2011
1 parent 536a5f9 commit 29b9509
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
23 changes: 22 additions & 1 deletion nobleModules.js
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,7 @@
}
//#endregion

//#region Module declaration implementation helpers
function initializeMainModule(dependencies, moduleFactory) {
moduleObjectFactory.setMainModuleExports({});
memoizeAndProvideDependencies(MAIN_MODULE_ID, dependencies, moduleFactory, function onMainModuleReady() {
Expand All @@ -616,10 +617,30 @@
});
}

// RegExp and function based on BravoJS, which says "mostly borrowed from FlyScript."
// NB: requireRegExp has two capturing clauses: one for double quotes, and one for single.
// It does not capture the quotes (unlike BravoJS/FlyScript).
var requireRegExp = /\/\/.*|\/\*[\s\S]*?\*\/|"(?:\\[\s\S]|[^"\\])*"|'(?:\\[\s\S]|[^'\\])*'|[;=(,:!^]\s*\/(?:\\.|[^\/\\])+\/|(?:^|\W)\s*require\s*\(\s*(?:"((?:\\[\s\S]|[^"\\])*)"|'((?:\\[\s\S]|[^'\\])*)')\s*\)/g;

function scrapeDependenciesFrom(rawSource) {
var dependencies = [];

var result = null;
while ((result = requireRegExp.exec(rawSource)) !== null) {
var moduleIdentifier = result[1] || result[2];
if (moduleIdentifier) {
dependencies.push(moduleIdentifier);
}
}

return dependencies;
}
//#endregion

function declareImpl(dependencies, moduleFactory) {
if (moduleFactory === undefined) {
moduleFactory = dependencies;
dependencies = [];
dependencies = scrapeDependenciesFrom(moduleFactory.toString());
}

if (!globalModule.main) {
Expand Down
10 changes: 10 additions & 0 deletions test/demos/squares.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module.declare(function(require, exports, module) {
var area = require("./area");
var squarePerimeter = require ( './perimeter').square;

// var commentedOut = require("./diamond/bottom");
/* var noGoodEither = require("./diamond/bottom"); */

exports.area = area.square;
exports.perimeter = squarePerimeter;
})
13 changes: 13 additions & 0 deletions test/moduleNamespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@ asyncTest("declare: accepts labeled dependency objects and correctly provides th
});
});

asyncModuleTest("declare: infers dependencies if only a factory function is given", function () {
module.provide(["demos/squares"], function () {
var squares = require("demos/squares");

strictEqual(squares.area(3), 9, "demos/area dependency was inferred and works");
strictEqual(squares.perimeter(3), 12, "demos/perimeter dependency was inferred and works");

strictEqual(require.isMemoized(require.id("demos/diamond/bottom")), false, "Commented-out demos/diamond/bottom dependency was not memoized");

start();
});
});

asyncModuleTest("load: when called twice in a row for the same module, both callbacks fire", function (require, exports, module) {
var numberOfLoadsSoFar = 0;

Expand Down

0 comments on commit 29b9509

Please sign in to comment.