forked from phcode-dev/staging.phcode.dev
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcondense.js
More file actions
1 lines (1 loc) · 7.23 KB
/
Copy pathcondense.js
File metadata and controls
1 lines (1 loc) · 7.23 KB
1
!function(root,mod){"object"==typeof exports&&"object"==typeof module?mod(exports,require("./infer")):"function"==typeof define&&define.amd?define(["exports","./infer"],mod):mod(root.tern||(root.tern={}),tern)}(this,function(exports,infer){"use strict";function State(origins,name,options){this.origins=origins,this.cx=infer.cx(),this.server=options.server||this.cx.parent||{signal:function(){}},this.maxOrigin=-1/0;for(var i=0;i<origins.length;++i)this.maxOrigin=Math.max(this.maxOrigin,this.cx.origins.indexOf(origins[i]));this.output={"!name":name,"!define":{}},this.options=options,this.types=Object.create(null),this.altPaths=Object.create(null),this.patchUp=[],this.roots=Object.create(null)}function pathLen(path){for(var len=1,pos=0,dot;-1!=(dot=path.indexOf(".",pos));)pos=dot+1,len+="!"==path.charAt(pos)?10:1;return len}function hop(obj,prop){return Object.prototype.hasOwnProperty.call(obj,prop)}function isSimpleInstance(o){return o.proto&&!(o instanceof infer.Fn)&&o.proto!=infer.cx().protos.Object&&o.proto.hasCtor&&!o.hasCtor}function reach(type,path,id,state,byName){var actual=type.getType(!1);if(actual){var orig=type.origin||actual.origin,relevant=!1;if(orig){var origPos;if(state.cx.origins.indexOf(orig)>state.maxOrigin)return;relevant=state.isTarget(orig)}var newPath=path?path+"."+id:id,oldPath=actual.path,shorter;if(!oldPath||pathLen(oldPath)>pathLen(newPath)){if(actual instanceof infer.Prim||(actual.path=newPath),actual.reached(newPath,state,!relevant)&&relevant){var data=state.types[oldPath];data?(delete state.types[oldPath],state.altPaths[oldPath]=actual):data={type:actual},data.span=state.getSpan(type)||actual!=type&&state.isTarget(actual.origin)&&state.getSpan(actual)||data.span,data.doc=type.doc||actual!=type&&state.isTarget(actual.origin)&&actual.doc||data.doc,data.data=actual.metaData,data.byName=null==data.byName?!!byName:data.byName&&byName,state.types[newPath]=data}}else relevant&&(state.altPaths[newPath]=actual)}}function reachByName(aval,path,id,state){var type=aval.getType();type&&reach(type,path,id,state,!0)}function patchUpSimpleInstance(obj,state){var path=obj.proto.hasCtor.path;for(var prop in path?obj.nameOverride="+"+path:path=obj.path,obj.props)reach(obj.props[prop],path,prop,state)}function createPath(parts,state){for(var base=state.output,defs=state.output["!define"],i=0,path;i<parts.length;++i){var part=parts[i];path=path?path+"."+part:part;var me=state.types[path];"!"==part.charAt(0)||me&&me.byName?hop(defs,path)?base=defs[path]:defs[path]=base={}:base=hop(base,parts[i])?base[part]:base[part]={}}return base}function store(out,info,state){var name=typeName(info.type);if(name!=info.type.path&&"?"!=name)out["!type"]=name;else if(info.type.proto&&info.type.proto!=state.cx.protos.Object){var protoName=typeName(info.type.proto);"?"!=protoName&&(out["!proto"]=protoName)}info.span&&(out["!span"]=info.span),info.doc&&(out["!doc"]=info.doc),info.data&&(out["!data"]=info.data)}function storeAlt(path,type,state){var parts=path.split("."),last=parts.pop();if("!"!=last[0]){var known=state.types[parts.join(".")],base=createPath(parts,state);known&&known.type.constructor!=infer.Obj||hop(base,last)||(base[last]=type.nameOverride||type.path)}}exports.condense=function(origins,name,options){"string"==typeof origins&&(origins=[origins]);var state=new State(origins,name||origins[0],options||{});for(var path in state.server.signal("preCondenseReach",state),state.cx.topScope.path="<top>",state.cx.topScope.reached("",state),state.roots)reach(state.roots[path],null,path,state);for(var i=0;i<state.patchUp.length;++i)patchUpSimpleInstance(state.patchUp[i],state);for(var path in state.server.signal("postCondenseReach",state),state.types)store(createPath(path.split("."),state),state.types[path],state);for(var path in state.altPaths)storeAlt(path,state.altPaths[path],state);var hasDef=!1;for(var _def in state.output["!define"]){hasDef=!0;break}return hasDef||delete state.output["!define"],state.server.signal("postCondense",state),simplify(state.output,state.options.sortOutput)},State.prototype.isTarget=function(origin){return this.origins.indexOf(origin)>-1},State.prototype.getSpan=function(node){if(0==this.options.spans||!this.isTarget(node.origin))return null;if(node.span)return node.span;var srv=this.cx.parent,file;if(!srv||!node.originNode||!(file=srv.findFile(node.origin)))return null;var start=node.originNode.start,end=node.originNode.end,pStart=file.asLineChar(start),pEnd=file.asLineChar(end);return start+"["+pStart.line+":"+pStart.ch+"]-"+end+"["+pEnd.line+":"+pEnd.ch+"]"},infer.Prim.prototype.reached=function(){return!0},infer.Arr.prototype.reached=function(path,state,concrete){if(concrete)return!0;if(this.tuple)for(var i=0;i<this.tuple;i++)reachByName(this.getProp(String(i)),path,String(i),state);else reachByName(this.getProp("<i>"),path,"<i>",state);return!0},infer.Fn.prototype.reached=function(path,state,concrete){if(infer.Obj.prototype.reached.call(this,path,state,concrete),!concrete){for(var i=0;i<this.args.length;++i)reachByName(this.args[i],path,"!"+i,state);reachByName(this.retval,path,"!ret",state)}return!0},infer.Obj.prototype.reached=function(path,state,concrete){if(isSimpleInstance(this)&&!this.condenseForceInclude)return-1==state.patchUp.indexOf(this)&&state.patchUp.push(this),!0;this.proto&&!concrete&&reach(this.proto,path,"!proto",state);var hasProps=!1;for(var prop in this.props)reach(this.props[prop],path,prop,state),hasProps=!0;return!!(hasProps||this.condenseForceInclude||this instanceof infer.Fn)||(this.nameOverride="?",!1)};var typeNameStack=[];function typeName(value){var isType=value instanceof infer.Type;if(isType){if(typeNameStack.indexOf(value)>-1)return value.path||"?";typeNameStack.push(value)}var name=value.typeName();return isType&&typeNameStack.pop(),name}function simplify(data,sort){if("object"!=typeof data)return data;var sawType=!1,sawOther=!1;for(var prop in data)"!type"==prop?sawType=!0:sawOther=!0,"!data"!=prop&&(data[prop]=simplify(data[prop],sort));return sawType&&!sawOther?data["!type"]:sort?sortObject(data):data}function sortObject(obj){var props=[],out={};for(var prop in obj)props.push(prop);props.sort();for(var i=0;i<props.length;++i){var prop;out[prop=props[i]]=obj[prop]}return out}infer.AVal.prototype.typeName=function(){if(0==this.types.length)return"?";if(1==this.types.length)return typeName(this.types[0]);var simplified=infer.simplifyTypes(this.types);if(simplified.length>2)return"?";for(var strs=[],i=0;i<simplified.length;i++)strs.push(typeName(simplified[i]));return strs.join("|")},infer.ANull.typeName=function(){return"?"},infer.Prim.prototype.typeName=function(){return this.name},infer.Sym.prototype.typeName=function(){return this.asPropName},infer.Arr.prototype.typeName=function(){if(!this.tuple)return"["+typeName(this.getProp("<i>"))+"]";for(var content=[],i=0;i<this.tuple;i++)content.push(typeName(this.getProp(String(i))));return"["+content.join(", ")+"]"},infer.Fn.prototype.typeName=function(){for(var out=this.generator?"fn*(":"fn(",i=0;i<this.args.length;++i){i&&(out+=", ");var name=this.argNames[i];name&&"?"!=name&&(out+=name+": "),out+=typeName(this.args[i])}return out+=")",this.computeRetSource?out+=" -> "+this.computeRetSource:this.retval.isEmpty()||(out+=" -> "+typeName(this.retval)),out},infer.Obj.prototype.typeName=function(){return this.nameOverride?this.nameOverride:this.path?this.path:"?"}});