UNPKG

@fink/loxia

Version:

A fink to JavaScript compiler.

256 lines (202 loc) 5.88 kB
const types_0 = require("@babel/types"); const identifier_0 = types_0.identifier; const importDeclaration_0 = types_0.importDeclaration; const importSpecifier_0 = types_0.importSpecifier; const stringLiteral_0 = types_0.stringLiteral; const { ends_with: ends_with_0 } = require("@fink/std-lib/str.js"); const { starts_with: starts_with_0 } = require("@fink/std-lib/str.js"); const { slice: slice_0 } = require("@fink/std-lib/str.js"); const { is_str: is_str_0 } = require("@fink/std-lib/str.js"); const { get_refs: get_refs_0 } = require("../../ir/context.js"); const { add: add_0 } = require("../context.js"); const { set_js2: set_js2_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 { escape_ident: escape_ident_0 } = require("../identifier/init.js"); const { ident: ident_0 } = require("../identifier/init.js"); const resolve_ext_0 = (import_url_0, options_0) => { const right_2 = ends_with_0(import_url_0, `.fnk`); /* istanbul ignore else */ if (true === !right_2) { return import_url_0; } const val_1 = starts_with_0(import_url_0, `.`); /* istanbul ignore else */ if (true === val_1) { let ret_2; ret_2: { /* istanbul ignore else */ if (null != options_0) { const itm_3 = options_0[`import`]; /* istanbul ignore else */ if (null != itm_3) { const itm_4 = itm_3.ext; /* istanbul ignore else */ if (null != itm_4) { const val_2 = is_str_0(itm_4.rel); /* istanbul ignore else */ if (true === val_2) { ret_2 = options_0[`import`].ext.rel; /* istanbul ignore next */ break ret_2; } } } } ret_2 = `.js`; /* istanbul ignore next */ break ret_2; } const ext_1 = ret_2; const sx_2 = slice_0(import_url_0, 0, -4); return `${sx_2}${ext_1}`; } let ret_1; ret_1: { /* istanbul ignore else */ if (null != options_0) { const itm_0 = options_0[`import`]; /* istanbul ignore else */ if (null != itm_0) { const itm_1 = itm_0.ext; /* istanbul ignore else */ if (null != itm_1) { const val_0 = is_str_0(itm_1.abs); /* istanbul ignore else */ if (true === val_0) { ret_1 = options_0[`import`].ext.abs; /* istanbul ignore next */ break ret_1; } } } } ret_1 = `.js`; /* istanbul ignore next */ break ret_1; } const ext_0 = ret_1; const sx_0 = slice_0(import_url_0, 0, -4); return `${sx_0}${ext_0}`; }; const get_runtime_imports_0 = ctx_0 => { let ret_4; ret_4: { /* istanbul ignore else */ if (null != ctx_0) { const itm_6 = ctx_0.runtime; /* istanbul ignore else */ if (null != itm_6) { /* istanbul ignore else */ if (itm_6[`in`] === true) { const arg_8 = identifier_0(`_in_`); const arg_10 = identifier_0(`_in_`); const item_0 = importSpecifier_0(arg_8, arg_10); ret_4 = [item_0]; /* istanbul ignore next */ break ret_4; } } } ret_4 = []; /* istanbul ignore next */ break ret_4; } const specs_0 = ret_4; /* istanbul ignore else */ if (null != specs_0) { /* istanbul ignore else */ if (undefined !== specs_0[0]) { const arg_13 = stringLiteral_0(`@fink/js-interop/runtime.js`); const item_1 = importDeclaration_0(specs_0, arg_13); return [item_1]; } } return []; }; const transform_import_0 = (expr_0, ctx_1) => { const val_5 = expr_0[0].args; const hdm_0 = val_5[1]; const key_id_0 = undefined === hdm_0 ? false : hdm_0; const res_id_0 = expr_0[1][0]; const drec_2 = get_js_literal_0(val_5[0], ctx_1); const arg_17 = resolve_ext_0(drec_2.quasis[0].value.raw, ctx_1); const uri_5 = stringLiteral_0(arg_17); const value_6 = [res_id_0, key_id_0]; let ret_7; ret_7: { /* istanbul ignore else */ if (null != value_6) { const right_3 = get_refs_0(res_id_0, ctx_1); /* istanbul ignore else */ if (true === (0 === right_3)) { ret_7 = []; /* istanbul ignore next */ break ret_7; } } /* istanbul ignore else */ if (null != value_6) { /* istanbul ignore else */ if (key_id_0 === false) { const arg_27 = ident_0(res_id_0); const item_5 = types_0.importDefaultSpecifier(arg_27); ret_7 = [item_5]; /* istanbul ignore next */ break ret_7; } } const drec_4 = get_js_0(key_id_0, ctx_1); const arg_22 = ident_0(res_id_0); const arg_25 = escape_ident_0(drec_4.quasis[0].value.raw); const arg_24 = identifier_0(arg_25); const item_4 = importSpecifier_0(arg_22, arg_24); ret_7 = [item_4]; /* istanbul ignore next */ break ret_7; } const specs_1 = ret_7; const js_0 = importDeclaration_0(specs_1, uri_5); return [[js_0], ctx_1]; }; const transform_dyn_import_0 = (expr_1, ctx_2) => { const uri_6 = get_js_0(expr_1[0].args[0], ctx_2); const arg_34 = types_0[`import`](); const js_1 = types_0.callExpression(arg_34, [uri_6]); const result_29 = set_js2_0(expr_1, js_1, ctx_2); return result_29; }; const add_import_0 = ctx_3 => { const pfn_0 = add_0(`imp`, transform_import_0); const ppr_0 = pfn_0(ctx_3); const pfn_1 = add_0(`impd`, transform_dyn_import_0); const ppr_1 = pfn_1(ppr_0); return ppr_1; }; exports.resolve_ext = resolve_ext_0; exports.get_runtime_imports = get_runtime_imports_0; exports.transform_import = transform_import_0; exports.transform_dyn_import = transform_dyn_import_0; exports.add_import = add_import_0;