MCPcopy
hub / github.com/ternjs/tern / canonicalType

Function canonicalType

lib/infer.js:276–307  ·  view source on GitHub ↗
(types)

Source from the content-addressed store, hash-verified

274 };
275
276 function canonicalType(types) {
277 var arrays = 0, fns = 0, objs = 0, prim = null;
278 for (var i = 0; i < types.length; ++i) {
279 var tp = types[i];
280 if (tp instanceof Arr) ++arrays;
281 else if (tp instanceof Fn) ++fns;
282 else if (tp instanceof Obj) ++objs;
283 else if (tp instanceof Prim) {
284 if (prim && tp.name != prim.name) return null;
285 prim = tp;
286 }
287 }
288 var kinds = (arrays && 1) + (fns && 1) + (objs && 1) + (prim && 1);
289 if (kinds > 1) return null;
290 if (prim) return prim;
291
292 var maxScore = 0, maxTp = null;
293 for (var i = 0; i < types.length; ++i) {
294 var tp = types[i], score = 0;
295 if (arrays) {
296 score = tp.getProp("<i>").isEmpty() ? 1 : 2;
297 } else if (fns) {
298 score = 1;
299 for (var j = 0; j < tp.args.length; ++j) if (!tp.args[j].isEmpty()) ++score;
300 if (!tp.retval.isEmpty()) ++score;
301 } else if (objs) {
302 score = tp.name ? 100 : 2;
303 }
304 if (score >= maxScore) { maxScore = score; maxTp = tp; }
305 }
306 return maxTp;
307 }
308
309 // PROPAGATION STRATEGIES
310

Callers 1

infer.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…