import*as Platform from"../platform/platform.js";import*as TextUtils from"../text_utils/text_utils.js";import*as AcornLoose from"../third_party/acorn-loose/package/dist/acorn-loose.mjs";import*as Acorn from"../third_party/acorn/acorn.js";import{ECMA_VERSION}from"./AcornTokenizer.js";import{ESTreeWalker}from"./ESTreeWalker.js";export let Chunk;export function javaScriptOutline(e){let t,n=[],r=0;try{t=Acorn.parse(e,{ecmaVersion:ECMA_VERSION,ranges:!1})}catch(y){t=AcornLoose.parse(e,{ecmaVersion:ECMA_VERSION,ranges:!1})}const i=Platform.StringUtilities.findLineEndingIndexes(e),o=new TextUtils.TextCursor.TextCursor(i);return new ESTreeWalker((function(e){if("ClassDeclaration"===e.type)s(e.id);else if("VariableDeclarator"===e.type&&e.init&&u(e.init))s(e.id);else if("AssignmentExpression"===e.type&&p(e.left)&&u(e.right))s(e.left);else if("Property"===e.type&&p(e.key)&&u(e.value))s(e.key);else if("FunctionDeclaration"===e.type)a(e.id,e);else if("VariableDeclarator"===e.type&&e.init&&c(e.init))a(e.id,e.init);else if("AssignmentExpression"===e.type&&p(e.left)&&c(e.right))a(e.left,e.right);else if(("MethodDefinition"===e.type||"Property"===e.type)&&p(e.key)&&c(e.value)){const t=[];"get"!==e.kind&&"set"!==e.kind||t.push(e.kind),"static"in e&&e.static&&t.push("static"),a(e.key,e.value,t.join(" "))}return})).walk(t),{chunk:n,isLastChunk:!0};function s(e){const t="class "+l(e);o.advance(e.start),m({name:t,line:o.lineNumber(),column:o.columnNumber(),arguments:void 0})}function a(e,t,n){let r=l(e);const i=t;i.generator&&(r="*"+r),n&&(r=n+" "+r),i.async&&(r="async "+r),o.advance(e.start),m({name:r,line:o.lineNumber(),column:o.columnNumber(),arguments:f(i.params)})}function p(e){return!!e&&("MemberExpression"===e.type?!e.computed&&"Identifier"===e.property.type:"Identifier"===e.type)}function c(e){return!!e&&("FunctionExpression"===e.type||"ArrowFunctionExpression"===e.type)}function u(e){return void 0!==e&&"ClassExpression"===e.type}function l(e){"MemberExpression"===e.type&&(e=e.property),console.assert("Identifier"===e.type,"Cannot extract identifier from unknown type: "+e.type);return e.name}function f(e){const t=[];for(const n of e)"Identifier"===n.type?t.push(n.name):"RestElement"===n.type&&"Identifier"===n.argument.type?t.push("..."+n.argument.name):console.error("Error: unexpected function parameter type: "+n.type);return"("+t.join(", ")+")"}function m(e){n.push(e),o.offset()-r<1e5||(postMessage({chunk:n,isLastChunk:!1}),n=[],r=o.offset())}}