Didi is a import lib for diagram js, it is heavy used. so we need to understand it. this is the annotate function. it is used to put the params to the function or bind the params to fuction on the $inject field
import {
isArray,
isClass
} from './util';
/**
* @typedef {import('./types').InjectAnnotated } InjectAnnotated
*/
/**
* @template T
*
* @params {[...string[], T] | ...string[], T} args
*
* @return {T & InjectAnnotated}
*/
export function annotate() {
var args = Array.prototype.slice.call(arguments);
if (args.length === 1 && isArray(args[0])) {
args = args[0];
}
var fn = args.pop();
fn.$inject = args;
return fn;
}
// Current limitations:
// - can't put into "function arg" comments
// function /* (no parenthesis like this) */ (){}
// function abc( /* xx (no parenthesis like this) */ a, b) {}
//
// Just put the comment before function or inside:
// /* (((this is fine))) */ function(a, b) {}
// function abc(a) { /* (((this is fine))) */}
//
// - can't reliably auto-annotate constructor; we'll match the
// first constructor(...) pattern found which may be the one
// of a nested class, too.
var CONSTRUCTOR_ARGS = /constructor\s*[^(]*\(\s*([^)]*)\)/m;
var FN_ARGS = /^(?:async )?(?:function\s*)?[^(]*\(\s*([^)]*)\)/m;
var FN_ARG = /\/\*([^*]*)\*\//m;
export function parseAnnotations(fn) {
if (typeof fn !== 'function') {
throw new Error('Cannot annotate "' + fn + '". Expected a function!');
}
var match = fn.toString().match(isClass(fn) ? CONSTRUCTOR_ARGS : FN_ARGS);
// may parse class without constructor
if (!match) {
return [];
}
return match[1] && match[1].split(',').map(function(arg) {
match = arg.match(FN_ARG);
return match ? match[1].trim() : arg.trim();
}) || [];
}
The parseAnnotations is to get the args, there must be bug here, when the arg has a ',' in it, it will cause problem when split by ','