{"version":3,"file":"var-decl.mjs","names":[],"sources":["../../../../src/modules/preprocessor/preprocess/var-decl.ts"],"sourcesContent":["import type {\n    CallExpression,\n    Directive,\n    IdentifierReference,\n    MemberExpression,\n    Program,\n    Statement,\n    VariableDeclarator,\n} from \"oxc-parser\";\n\nimport type { Specifier } from \"#/@types/specifier\";\nimport type { CompilerContext } from \"#/contexts/compiler\";\nimport type { TransformIdentResult } from \"#/modules/preprocessor/preprocess/transform/ident\";\nimport type { TransformMemberExprResult } from \"#/modules/preprocessor/preprocess/transform/member\";\n\nimport { cloneDeep } from \"es-toolkit\";\n\nimport { transformIdent } from \"#/modules/preprocessor/preprocess/transform/ident\";\nimport { transformMemberExpr } from \"#/modules/preprocessor/preprocess/transform/member\";\n\ntype PreprocessVarDeclOptions = {\n    context: CompilerContext;\n    program: Program;\n    namespaces: readonly string[];\n    includedFunctions: readonly string[];\n    specifiers: readonly Specifier[];\n};\n\ntype PreprocessVarDeclResult = {\n    program: Program;\n};\n\nconst preprocessVarDecl = (\n    options: PreprocessVarDeclOptions,\n): PreprocessVarDeclResult => {\n    const result: Program = cloneDeep(options.program);\n\n    for (let i: number = 0; i < result.body.length; i++) {\n        const body: Directive | Statement | undefined = result.body[i];\n\n        if (!body) continue;\n\n        if (body.type !== \"VariableDeclaration\") continue;\n\n        for (let j: number = 0; j < body.declarations.length; j++) {\n            const decl: VariableDeclarator | undefined = body.declarations[j];\n\n            if (!decl) continue;\n\n            if (!decl.init) continue;\n\n            if (decl.init.type !== \"CallExpression\") continue;\n\n            const call: CallExpression = decl.init;\n\n            const va: string =\n                decl.id.type === \"Identifier\" ? decl.id.name : `var_${i}_${j}`;\n\n            // const abc = x.y();\n            if (call.callee.type === \"MemberExpression\") {\n                const member: MemberExpression = call.callee;\n\n                if (member.object.type !== \"Identifier\") continue;\n\n                if (!options.namespaces.includes(member.object.name)) continue;\n\n                if (member.property.type !== \"Identifier\") continue;\n\n                if (!options.includedFunctions.includes(member.property.name))\n                    continue;\n\n                const resultTransform: TransformMemberExprResult =\n                    transformMemberExpr({\n                        context: options.context,\n                        va,\n                        member,\n                        arguments: call.arguments,\n                    });\n\n                decl.init = resultTransform.object;\n            }\n\n            // const abc = x();\n            else if (call.callee.type === \"Identifier\") {\n                const ident: IdentifierReference = call.callee;\n\n                if (!options.includedFunctions.includes(ident.name)) continue;\n\n                const resultTransform: TransformIdentResult = transformIdent({\n                    context: options.context,\n                    va,\n                    ident,\n                    arguments: call.arguments,\n                });\n\n                decl.init = resultTransform.object;\n            }\n        }\n    }\n\n    return {\n        program: result,\n    };\n};\n\nexport type { PreprocessVarDeclOptions, PreprocessVarDeclResult };\nexport { preprocessVarDecl };\n"],"mappings":";;;;AAgCA,MAAM,qBACF,YAC0B;CAC1B,MAAM,SAAkB,UAAU,QAAQ,OAAO;CAEjD,KAAK,IAAI,IAAY,GAAG,IAAI,OAAO,KAAK,QAAQ,KAAK;EACjD,MAAM,OAA0C,OAAO,KAAK;EAE5D,IAAI,CAAC,MAAM;EAEX,IAAI,KAAK,SAAS,uBAAuB;EAEzC,KAAK,IAAI,IAAY,GAAG,IAAI,KAAK,aAAa,QAAQ,KAAK;GACvD,MAAM,OAAuC,KAAK,aAAa;GAE/D,IAAI,CAAC,MAAM;GAEX,IAAI,CAAC,KAAK,MAAM;GAEhB,IAAI,KAAK,KAAK,SAAS,kBAAkB;GAEzC,MAAM,OAAuB,KAAK;GAElC,MAAM,KACF,KAAK,GAAG,SAAS,eAAe,KAAK,GAAG,OAAO,OAAO,EAAE,GAAG;GAG/D,IAAI,KAAK,OAAO,SAAS,oBAAoB;IACzC,MAAM,SAA2B,KAAK;IAEtC,IAAI,OAAO,OAAO,SAAS,cAAc;IAEzC,IAAI,CAAC,QAAQ,WAAW,SAAS,OAAO,OAAO,IAAI,GAAG;IAEtD,IAAI,OAAO,SAAS,SAAS,cAAc;IAE3C,IAAI,CAAC,QAAQ,kBAAkB,SAAS,OAAO,SAAS,IAAI,GACxD;IAUJ,KAAK,OAPD,oBAA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