!function(mod){"object"==typeof exports&&"object"==typeof module?mod(require("../lib/infer"),require("../lib/tern"),require("../lib/comment"),require("acorn"),require("acorn-walk")):"function"==typeof define&&define.amd?define(["../lib/infer","../lib/tern","../lib/comment","acorn/dist/acorn","acorn-walk/dist/walk"],mod):mod(tern,tern,tern.comment,acorn,acorn.walk)}(function(infer,tern,comment,acorn,walk){"use strict";var WG_MADEUP=1,WG_STRONG=101;function postParse(ast,text){function attachComments(node){comment.ensureCommentsBefore(text,node)}walk.simple(ast,{VariableDeclaration:attachComments,FunctionDeclaration:attachComments,MethodDefinition:attachComments,Property:attachComments,AssignmentExpression:function(node){"="==node.operator&&attachComments(node)},CallExpression:function(node){isDefinePropertyCall(node)&&attachComments(node)},ExportNamedDeclaration:attachComments,ExportDefaultDeclaration:attachComments,ClassDeclaration:attachComments})}function isDefinePropertyCall(node){return"MemberExpression"==node.callee.type&&"Object"==node.callee.object.name&&"defineProperty"==node.callee.property.name&&node.arguments.length>=3&&"string"==typeof node.arguments[1].value}function postInfer(ast,scope){jsdocParseTypedefs(ast.sourceFile.text,scope),walk.simple(ast,{VariableDeclaration:function(node,scope){var decl=node.declarations[0].id;node.commentsBefore&&"Identifier"==decl.type&&interpretComments(node,node.commentsBefore,scope,scope.getProp(node.declarations[0].id.name))},FunctionDeclaration:function(node,scope){node.commentsBefore&&interpretComments(node,node.commentsBefore,scope,scope.getProp(node.id.name),node.scope.fnType)},ClassDeclaration:function(node,scope){node.commentsBefore&&interpretComments(node,node.commentsBefore,scope,scope.getProp(node.id.name),node.objType)},AssignmentExpression:function(node,scope){node.commentsBefore&&interpretComments(node,node.commentsBefore,scope,infer.expressionType({node:node.left,state:scope}))},ObjectExpression:function(node,scope){for(var i=0;i"!=name&&prop.commentsBefore&&interpretComments(prop,prop.commentsBefore,scope,node.objType.getProp(name))}}},Class:function(node,scope){if(node.objType){var proto=node.objType.getProp("prototype").getObjType();if(proto)for(var i=0;i"!=(name=infer.propName(method))&&interpretComments(method,method.commentsBefore,scope,proto.getProp(name)))}}},CallExpression:function(node,scope){if(node.commentsBefore&&isDefinePropertyCall(node)){var type=infer.expressionType({node:node.arguments[0],state:scope}).getObjType();if(type&&type instanceof infer.Obj){var prop=type.props[node.arguments[1].value];prop&&interpretComments(node,node.commentsBefore,scope,prop)}}},ExportNamedDeclaration:function(node,scope){node.commentsBefore&&node.declaration&&"FunctionDeclaration"===node.declaration.type&&interpretComments(node.declaration,node.commentsBefore,scope,scope.getProp(node.declaration.id.name),node.declaration.scope.fnType)},ExportDefaultDeclaration:function(node,scope){node.commentsBefore&&node.declaration&&"FunctionDeclaration"===node.declaration.type&&interpretComments(node.declaration,node.commentsBefore,scope,scope.getProp(node.declaration.id.name),node.declaration.scope.fnType)}},infer.searchVisitor,scope)}function postLoadDef(data){var defs=data["!typedef"],cx=infer.cx(),orig=data["!name"];if(defs)for(var name in defs)cx.parent.mod.jsdocTypedefs[name]=maybeInstance(infer.def.parse(defs[name],orig,name),name)}function stripLeadingChars(lines){for(var head,i=1;i=0;i--){var text=stripLeadingChars(comments[i].split(/\r\n?|\n/)).join("\n");if(text){aval instanceof infer.AVal&&(aval.doc=text),type&&(type.doc=text);break}}}function skipSpace(str,pos){for(;/\s/.test(str.charAt(pos));)++pos;return pos}function isIdentifier(string){if(!acorn.isIdentifierStart(string.charCodeAt(0)))return!1;for(var i=1;i"!=str.charAt(pos++))return null;inner=inAngles.type}type=new infer.Arr(inner)}else if(/^object$/i.test(word)){if(type=new infer.Obj(!0),"."==str.charAt(pos)&&"<"==str.charAt(pos+1)){var key=parseType(scope,str,pos+2);if(!key)return null;if(pos=skipSpace(str,key.end),","!=str.charAt(pos++))return null;var val=parseType(scope,str,pos);if(!val)return null;if(pos=skipSpace(str,val.end),madeUp=key.madeUp||val.madeUp,">"!=str.charAt(pos++))return null;val.type.propagate(type.defProp(""))}}else{for(;46==str.charCodeAt(pos)||acorn.isIdentifierChar(str.charCodeAt(pos));)++pos;var path=str.slice(start,pos),cx=infer.cx(),defs=cx.parent&&cx.parent.mod.jsdocTypedefs,found;defs&&path in defs?type=defs[path]:(found=infer.def.parsePath(path,scope).getObjType())?type=maybeInstance(found,path):(cx.jsdocPlaceholders||(cx.jsdocPlaceholders=Object.create(null)),type=path in cx.jsdocPlaceholders?cx.jsdocPlaceholders[path]:cx.jsdocPlaceholders[path]=new infer.Obj(null,path),madeUp=!0)}}return{type:type,end:pos,madeUp:madeUp}}function maybeInstance(type,path){if(type instanceof infer.Fn&&/(?:^|\.)[A-Z][^\.]*$/.test(path)){var proto=type.getProp("prototype").getObjType();if(proto instanceof infer.Obj)return infer.getInstance(proto)}return type}function parseTypeOuter(scope,str,pos){if(pos=skipSpace(str,pos||0),"{"!=str.charAt(pos))return null;var result=parseType(scope,str,pos+1);if(!result)return null;var end=skipSpace(str,result.end);return"}"!=str.charAt(end)?null:(result.end=end+1,result)}function jsdocInterpretComments(node,scope,aval,comments){for(var type,args,ret,foundOne,self,parsed,i=0;i").getType().defProp(argname)))}isObjProp||((args||(args=Object.create(null)))[argname]=parsed)}}else self=foundOne=!0;foundOne&&applyType(type,self,args,ret,node,aval)}function jsdocParseTypedefs(text,scope){for(var cx=infer.cx(),re=/\s@typedef\s+(.*)/g,m;m=re.exec(text);){var parsed=parseTypeOuter(scope,m[1]),name=parsed&&m[1].slice(parsed.end).match(/^\s*(\S+)/);if(name&&parsed.type instanceof infer.Obj){for(var rest=text.slice(m.index+m[0].length);m=/\s+@prop(?:erty)?\s+(.*)/.exec(rest);){var propType=parseTypeOuter(scope,m[1]),propName;propType&&(propName=m[1].slice(propType.end).match(/^\s*(\S+)/))&&propType.type.propagate(parsed.type.defProp(propName[1])),rest=rest.slice(m[0].length)}cx.parent.mod.jsdocTypedefs[name[1]]=parsed.type}}}function propagateWithWeight(type,target){var weight=infer.cx().parent.mod.docComment.weight;type.type.propagate(target,weight||(type.madeUp?WG_MADEUP:void 0))}function isFunExpr(node){return"FunctionExpression"==node.type||"ArrowFunctionExpression"==node.type}function applyType(type,self,args,ret,node,aval){var fn;if("VariableDeclaration"==node.type){var decl=node.declarations[0];decl.init&&isFunExpr(decl.init)&&(fn=decl.init.scope.fnType)}else"FunctionDeclaration"==node.type?fn=node.scope.fnType:"AssignmentExpression"==node.type?isFunExpr(node.right)&&(fn=node.right.scope.fnType):"CallExpression"==node.type||"ClassDeclaration"===node.type||isFunExpr(node.value)&&(fn=node.value.scope.fnType);if(fn&&(args||ret||self)){if(args)for(var i=0;i