{"version":3,"file":"call-expr.mjs","names":[],"sources":["../../../../src/modules/preprocessor/preprocess/call-expr.ts"],"sourcesContent":["import type {\n    CallExpression,\n    Directive,\n    IdentifierReference,\n    MemberExpression,\n    Program,\n    Statement,\n    VariableDeclaration,\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 PreprocessCallExprOptions = {\n    context: CompilerContext;\n    program: Program;\n    namespaces: readonly string[];\n    includedFunctions: readonly string[];\n    specifiers: readonly Specifier[];\n};\n\ntype PreprocessCallExprResult = {\n    program: Program;\n};\n\nconst preprocessCallExpr = (\n    options: PreprocessCallExprOptions,\n): PreprocessCallExprResult => {\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 !== \"ExpressionStatement\") continue;\n\n        if (body.expression.type !== \"CallExpression\") continue;\n\n        const call: CallExpression = body.expression;\n\n        const va: string = `call_${i}`;\n\n        // x.y();\n        if (call.callee.type === \"MemberExpression\") {\n            const member: MemberExpression = call.callee;\n\n            // x\n            if (member.object.type !== \"Identifier\") continue;\n\n            if (!options.namespaces.includes(member.object.name)) continue;\n\n            // y\n            if (member.property.type === \"Identifier\") {\n                if (!options.includedFunctions.includes(member.property.name))\n                    continue;\n            } else if (member.property.type === \"Literal\") {\n                if (\n                    member.property.value === \"\" ||\n                    member.property.value === null\n                ) {\n                    continue;\n                }\n\n                if (\n                    !options.includedFunctions.includes(\n                        member.property.value.toString(),\n                    )\n                )\n                    continue;\n            }\n\n            const resultTransform: TransformMemberExprResult =\n                transformMemberExpr({\n                    context: options.context,\n                    va,\n                    member,\n                    arguments: call.arguments,\n                });\n\n            result.body[i] = {\n                type: \"VariableDeclaration\",\n                kind: \"const\",\n                declarations: [\n                    {\n                        type: \"VariableDeclarator\",\n                        id: {\n                            type: \"Identifier\",\n                            name: va,\n                            start: 0,\n                            end: 0,\n                        },\n                        init: resultTransform.object,\n                        start: 0,\n                        end: 0,\n                    },\n                ],\n                start: body.start,\n                end: body.end,\n            } satisfies VariableDeclaration;\n        }\n\n        // 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            result.body[i] = {\n                type: \"VariableDeclaration\",\n                kind: \"const\",\n                declarations: [\n                    {\n                        type: \"VariableDeclarator\",\n                        id: {\n                            type: \"Identifier\",\n                            name: va,\n                            start: 0,\n                            end: 0,\n                        },\n                        init: resultTransform.object,\n                        start: 0,\n                        end: 0,\n                    },\n                ],\n                start: body.start,\n                end: body.end,\n            } satisfies VariableDeclaration;\n        }\n    }\n\n    return {\n        program: result,\n    };\n};\n\nexport type { PreprocessCallExprOptions, PreprocessCallExprResult };\nexport { preprocessCallExpr };\n"],"mappings":";;;;AAgCA,MAAM,sBACF,YAC2B;CAC3B,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,IAAI,KAAK,WAAW,SAAS,kBAAkB;EAE/C,MAAM,OAAuB,KAAK;EAElC,MAAM,KAAa,QAAQ;EAG3B,IAAI,KAAK,OAAO,SAAS,oBAAoB;GACzC,MAAM,SAA2B,KAA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