js-doc-parse icon indicating copy to clipboard operation
js-doc-parse copied to clipboard

UMD modules

Open adros opened this issue 9 years ago • 1 comments

I have a module that I use in both Node and Dojo, so I have some kind of UMD mechanism:

({
	define : typeof define != "undefined" ? define : function(deps, factory) {
		module.exports = factory();
	}
}).define([], function() {

Is there any way to use this syntax and correctly parse the docs? Currently it fails to load this file as dependency of other AMD module.

Or could you give me a hint, what would be the best way to implement custom 'callHandler'? The doc says it's possible, but there are not instructions how should a callHandler be implmented.

adros avatar Nov 15 '16 09:11 adros

If anyone interested, I have succeed in creating custom callHandler for UMD:

define([
    "dojo/_base/lang",
    "dojo/AdapterRegistry",
    "jsdocparser/lib/env",
    "jsdocparser/lib/callHandler/amd",
    "jsdocparser/lib/console"
], function(lang, AdapterRegistry, env, amd, console) {

    var handlers = new AdapterRegistry();

    //we can reuse same processing as amd, just detection is different
    //js-doc-parser was modified to expose '_handleDefine' function
    handlers.register("define", isUmd, amd._handleDefine);

    function isUmd(callInfo) {
        // we consider callInfo to be UMD definition if identifier has 2 items, 
        // first one is objet, and second one is Identifier with name 'define'

        // Sample of UMD module
        //      ({
        //          define : typeof define != "undefined" ? define : function(deps, factory) {
        //              module.exports = factory();
        //          }
        //      }).define([], function() {

        var identifier = callInfo.identifier;

        if (identifier.length == 2 //
                && identifier[0].type == "object" //
                && identifier[1].type == "Identifier" //
                && identifier[1].name == "define") {
            console.log("Detected module as UMD", callInfo.callee.file && callInfo.callee.file.moduleId);
            return true;
        }
        return false;
    }

    return handlers;
});

adros avatar Nov 16 '16 09:11 adros