UNPKG

@fink/loxia

Version:

A fink to JavaScript compiler.

330 lines (278 loc) 9.66 kB
const types_0 = require("@babel/types"); const identifier_0 = types_0.identifier; const expressionStatement_0 = types_0.expressionStatement; const variableDeclaration_0 = types_0.variableDeclaration; const variableDeclarator_0 = types_0.variableDeclarator; const { map: map_0 } = require("@fink/std-lib/iter.js"); const { slice: slice_0 } = require("@fink/std-lib/str.js"); const { add: add_0 } = require("../context.js"); const { get_js: get_js_0 } = require("../context.js"); const { get_js_literal: get_js_literal_0 } = require("../context.js"); const { with_loc: with_loc_0 } = require("../context.js"); const { transform_exprs: transform_exprs_0 } = require("../transform.js"); const { escape_ident: escape_ident_0 } = require("../identifier/init.js"); const { get_runtime_imports: get_runtime_imports_0 } = require("./import.js"); const transform_specifiers_0 = drec_1 => { const specifiers_0 = drec_1.specifiers; const arg_0 = identifier_0(`require`); const init_0 = types_0.callExpression(arg_0, [drec_1.source]); const first_0 = specifiers_0[0]; const val_1 = types_0.isImportDefaultSpecifier(first_0); /* istanbul ignore else */ if (true === val_1) { const item_2 = variableDeclarator_0(specifiers_0[0].local, init_0); const result_8 = variableDeclaration_0(`const`, [item_2]); return result_8; } const val_0 = types_0.isImportSpecifier(first_0); /* istanbul ignore else */ if (true === val_0) { const pfn_0 = map_0(node_0 => { const result_5 = types_0.objectProperty(node_0.imported, node_0.local, false, node_0.imported.name === node_0.local.name); return result_5; }); const ppr_0 = pfn_0(specifiers_0); const ppr_1 = (prtl_0 => { return [...prtl_0]; })(ppr_0); const arg_11 = types_0.objectPattern([...ppr_1]); const item_1 = variableDeclarator_0(arg_11, init_0); const result_6 = variableDeclaration_0(`const`, [item_1]); return result_6; } const result_3 = expressionStatement_0(init_0); return result_3; }; const module_transforms_0 = { ImportDeclaration: path_0 => { const arg_18 = transform_specifiers_0(path_0.node); path_0.replaceWith(arg_18); return [][0]; } }; const get_exports_0 = (...args_8) => { get_exports_0: do { const dlst_3 = args_8[0]; const hdm_0 = dlst_3[0]; const prop_0 = undefined === hdm_0 ? false : hdm_0; const tail_7 = dlst_3.slice(1); const hdm_1 = args_8[1]; const decls_0 = undefined === hdm_1 ? [] : hdm_1; const hdm_2 = args_8[2]; const dflt_decl_0 = undefined === hdm_2 ? [] : hdm_2; /* istanbul ignore else */ if (prop_0 === false) { let ret_7; ret_7: { /* istanbul ignore else */ if (null != decls_0) { /* istanbul ignore else */ if (undefined !== decls_0[0]) { const arg_54 = variableDeclaration_0(`const`, decls_0); const item_7 = types_0.exportNamedDeclaration(arg_54); ret_7 = [item_7]; /* istanbul ignore next */ break ret_7; } } ret_7 = []; /* istanbul ignore next */ break ret_7; } const named_decls_0 = ret_7; return [...named_decls_0, ...dflt_decl_0]; } const key_39 = prop_0.key; const value_2 = prop_0.value; /* istanbul ignore else */ if (null != key_39) { /* istanbul ignore else */ if (key_39.type === `StringLiteral`) { /* istanbul ignore else */ if (key_39.value === `default`) { const dd_0 = types_0.exportDefaultDeclaration(value_2); const arg_45 = escape_ident_0(key_39.value); const arg_44 = identifier_0(arg_45); const ident_2 = with_loc_0(key_39, arg_44); const arg_48 = variableDeclarator_0(ident_2, value_2); const decl_2 = with_loc_0(key_39, arg_48); args_8 = [tail_7, [...decls_0, decl_2], [...dflt_decl_0, dd_0]]; continue get_exports_0; } } } /* istanbul ignore else */ if (null != key_39) { /* istanbul ignore else */ if (key_39.type === `StringLiteral`) { const arg_33 = escape_ident_0(key_39.value); const arg_32 = identifier_0(arg_33); const ident_1 = with_loc_0(key_39, arg_32); const arg_36 = variableDeclarator_0(ident_1, value_2); const decl_1 = with_loc_0(key_39, arg_36); args_8 = [tail_7, [...decls_0, decl_1], dflt_decl_0]; continue get_exports_0; } } const arg_22 = escape_ident_0(key_39.name); const arg_21 = identifier_0(arg_22); const ident_0 = with_loc_0(key_39, arg_21); const arg_25 = variableDeclarator_0(ident_0, value_2); const decl_0 = with_loc_0(key_39, arg_25); args_8 = [tail_7, [...decls_0, decl_0], dflt_decl_0]; continue get_exports_0; } while (true); }; const assign_st_0 = (left_12, right_1, value_5) => { const arg_60 = identifier_0(left_12); const arg_59 = types_0.memberExpression(arg_60, right_1); const arg_57 = types_0.assignmentExpression(`=`, arg_59, value_5); const result_28 = expressionStatement_0(arg_57); return result_28; }; const get_cjs_exports_0 = (...args_10) => { get_cjs_exports_0: do { const dlst_4 = args_10[0]; const hdm_3 = dlst_4[0]; const prop_1 = undefined === hdm_3 ? false : hdm_3; const tail_15 = dlst_4.slice(1); const hdm_4 = args_10[1]; const exports_5 = undefined === hdm_4 ? [] : hdm_4; /* istanbul ignore else */ if (prop_1 === false) { return exports_5; } const key_50 = prop_1.key; const value_7 = prop_1.value; /* istanbul ignore else */ if (null != key_50) { /* istanbul ignore else */ if (key_50.type === `StringLiteral`) { /* istanbul ignore else */ if (key_50.value === `default`) { const arg_88 = escape_ident_0(key_50.value); const arg_87 = identifier_0(arg_88); const ident_5 = with_loc_0(key_50, arg_87); const arg_91 = assign_st_0(`exports`, ident_5, value_7); const exp_2 = with_loc_0(key_50, arg_91); const arg_98 = identifier_0(`exports`); const arg_96 = assign_st_0(`module`, arg_98, value_7); const dflt_0 = with_loc_0(key_50, arg_96); args_10 = [tail_15, [dflt_0, ...exports_5, exp_2]]; continue get_cjs_exports_0; } } } /* istanbul ignore else */ if (null != key_50) { /* istanbul ignore else */ if (key_50.type === `StringLiteral`) { const arg_77 = escape_ident_0(key_50.value); const arg_76 = identifier_0(arg_77); const ident_4 = with_loc_0(key_50, arg_76); const arg_80 = assign_st_0(`exports`, ident_4, value_7); const exp_1 = with_loc_0(key_50, arg_80); args_10 = [tail_15, [...exports_5, exp_1]]; continue get_cjs_exports_0; } } const arg_66 = escape_ident_0(key_50.name); const arg_65 = identifier_0(arg_66); const ident_3 = with_loc_0(key_50, arg_65); const arg_69 = assign_st_0(`exports`, ident_3, value_7); const exp_0 = with_loc_0(key_50, arg_69); args_10 = [tail_15, [...exports_5, exp_0]]; continue get_cjs_exports_0; } while (true); }; const transform_mod_exports_0 = (expr_0, ctx_0) => { const drec_6 = get_js_0(expr_0[0].args[0], ctx_0); const exports_7 = drec_6.properties; let ret_13; ret_13: { /* istanbul ignore else */ if (null != ctx_0) { /* istanbul ignore else */ if (ctx_0.module_type === `cjs`) { const result_47 = get_cjs_exports_0(exports_7); ret_13 = result_47; /* istanbul ignore next */ break ret_13; } } const result_46 = get_exports_0(exports_7); ret_13 = result_46; /* istanbul ignore next */ break ret_13; } const export_statements_0 = ret_13; return [export_statements_0, ctx_0]; }; const transfrom_module_0 = (exprs_0, ctx_1) => { const dlst_6 = transform_exprs_0(exprs_0, ctx_1); const next_ctx_0 = dlst_6[1]; const drctvs_1 = get_js_literal_0(exprs_0.at(-1)[0].args[1], next_ctx_0); let ret_15; ret_15: { /* istanbul ignore else */ if (null != drctvs_1) { const itm_8 = drctvs_1.elements; /* istanbul ignore else */ if (null != itm_8) { const itm_9 = itm_8[0]; /* istanbul ignore else */ if (null != itm_9) { /* istanbul ignore else */ if (itm_9.type === `TemplateLiteral`) { const arg_111 = slice_0(drctvs_1.elements[0].quasis[0].value.raw, 2); const item_14 = types_0.interpreterDirective(arg_111); ret_15 = [item_14]; /* istanbul ignore next */ break ret_15; } } } } ret_15 = []; /* istanbul ignore next */ break ret_15; } const directives_0 = ret_15; const runtime_imports_0 = get_runtime_imports_0(next_ctx_0); const arg_115 = types_0.program([...runtime_imports_0, ...dlst_6[0]], [], `module`, ...directives_0); const js_0 = types_0.file(arg_115); return { ...next_ctx_0, js: js_0 }; }; const add_module_0 = ctx_2 => { const pfn_3 = add_0(`mod`, transform_mod_exports_0); const ppr_2 = pfn_3(ctx_2); return ppr_2; }; exports.transform_specifiers = transform_specifiers_0; exports.module_transforms = module_transforms_0; exports.get_exports = get_exports_0; exports.assign_st = assign_st_0; exports.get_cjs_exports = get_cjs_exports_0; exports.transform_mod_exports = transform_mod_exports_0; exports.transfrom_module = transfrom_module_0; exports.add_module = add_module_0;