{"version":3,"file":"codegen.mjs","names":[],"sources":["../../src/ast/codegen.ts"],"sourcesContent":["import type { PrintOptions } from \"esrap\";\nimport type { Program } from \"oxc-parser\";\n\nimport { print } from \"esrap\";\nimport ts from \"esrap/languages/ts\";\nimport tsx from \"esrap/languages/tsx\";\n\n/**\n * Source map class from `esrap`.\n */\ntype SourceMap = {\n    version: number;\n    sources: string[];\n    names: string[];\n    mappings: string;\n    sourcesContent: string[];\n    toString: () => string;\n    toUrl: () => string;\n};\n\nconst isString = (value: string | null): value is string => value !== null;\n\nconst toSourceMap = (map: any): SourceMap => {\n    return {\n        version: map.version,\n        sources: map.sources.filter(isString),\n        names: map.names,\n        mappings: map.mappings,\n        sourcesContent: map.sourcesContent.filter(isString),\n        toString: map.toString,\n        toUrl: map.toUrl,\n    };\n};\n\ntype CodegenOptions = {\n    file: string;\n    code?: string;\n    program: Program;\n};\n\ntype CodegenResult = {\n    code: string;\n    map: SourceMap;\n};\n\nconst codegen = (options: CodegenOptions): CodegenResult => {\n    const printOptions: PrintOptions = {\n        sourceMapSource: options.file,\n        sourceMapContent: options.code,\n        sourceMapEncodeMappings: true,\n        indent: \"    \",\n    };\n\n    // tsx / jsx\n    if (options.file.endsWith(\".tsx\") || options.file.endsWith(\".jsx\")) {\n        const { code, map } = print(options.program, tsx(), printOptions);\n\n        return {\n            code,\n            map: toSourceMap(map),\n        };\n    }\n\n    // ts / js\n    const { code, map } = print(options.program, ts(), printOptions);\n\n    return {\n        code,\n        map: toSourceMap(map),\n    };\n};\n\nexport type { CodegenOptions, CodegenResult, SourceMap };\nexport { codegen };\n"],"mappings":";;;;AAoBA,MAAM,YAAY,UAA0C,UAAU;AAEtE,MAAM,eAAe,QAAwB;CACzC,OAAO;EACH,SAAS,IAAI;EACb,SAAS,IAAI,QAAQ,OAAO,QAAQ;EACpC,OAAO,IAAI;EACX,UAAU,IAAI;EACd,gBAAgB,IAAI,eAAe,OAAO,QAAQ;EAClD,UAAU,IAAI;EACd,OAAO,IAAI;CACf;AACJ;AAaA,MAAM,WAAW,YAA2C;CACxD,MAAM,eAA6B;EAC/B,iBAAiB,QAAQ;EACzB,kBAAkB,QAAQ;EAC1B,yBAAyB;EACzB,QAAQ;CACZ;CAGA,IAAI,QAAQ,KAAK,SAAS,MAAM,KAAK,QAAQ,KAAK,SAAS,MAAM,GAAG;EAChE,MAAM,EAAE,MAAM,QAAQ,MAAM,QAAQ,SAAS,IAAI,GAAG,YAAY;EAEhE,OAAO;GACH;GACA,KAAK,YAAY,GAAG;EACxB;CACJ;CAGA,MAAM,EAAE,MAAM,QAAQ,MAAM,QAAQ,SAAS,GAAG,GAAG,YAAY;CAE/D,OAAO;EACH;EACA,KAAK,YAAY,GAAG;CACxB;AACJ"}