@effectful/transducers-loose
Version:
@effectful/transducers built with faster generators
305 lines (302 loc) • 7.82 kB
JavaScript
"use strict";
exports.__esModule = true;
exports.importSyms = importSyms;
exports.init = init;
exports.inline = inline;
var Kit = require("./kit");
var Scope = require("./scope");
var _core = require("./core");
var M = require("@effectful/es-rt/opts/loose");
/**
* injects ES6 `import` or commonjs `require`
* commonjs if option `modules` === 'commonjs'
* expects following fields:
* type Value = Value & {
* rt: { importSyms: {syms:Sym[], ns: Sym, module:string } } }
* type Sym = Sym & { importFrom: String }
*/
function importSyms(si) {
var s,
rt,
commonjs,
esDefault,
ns,
module,
importSyms = M.generator(),
loop,
r,
p;
importSyms.step = importSyms_1;
importSyms.$step = 3;
return importSyms;
function importSyms_1(p) {
var a;
for (;;) {
switch (importSyms.$cur = importSyms.$step) {
case 3:
s = Kit.auto(si);
({
rt
} = s.first.value);
if (!rt.importSyms || !rt.importSyms.length) {
importSyms.$step = 7;
return importSyms.$delegate(s);
} else {
a = Kit.fileBody(s);
importSyms.$step = 4;
return importSyms.$delegate(a);
}
case 4:
commonjs = s.opts.modules === "commonjs" || s.opts.modules === "cjs";
esDefault = s.opts.modules === "esDefault";
loop = M.iterator(rt.importSyms);
importSyms.$step = 5;
importSyms.$step = 5;
continue;
case 5:
if (!(loop = loop.step()).done) {
({
ns,
module
} = loop.value);
a = s.toks(_core.Tag.push, commonjs ? `var $I = require("${module}")` : esDefault ? `import $I from "${module}"` : `import * as $I from "${module}"`, ns);
importSyms.$step = 5;
return importSyms.$delegate(a);
} else {
importSyms.$step = 6;
return importSyms.$delegate(s);
}
case 6:
importSyms.$step = 0;
importSyms.value = void 0;
importSyms.done = true;
return importSyms;
case 0:
importSyms.$step = 0;
importSyms.value = p;
importSyms.done = true;
return importSyms;
case 1:
importSyms.$step = 0;
throw p;
case 7:
importSyms.$step = 0;
importSyms.$step = 0;
p = r;
continue;
case 2:
return importSyms.$redir(p);
default:
throw new Error("invalid state");
}
}
}
}
/**
* inlines runtime values
* expects:
* type Value = Value & { rt: { inline: {syms:Sym[],content:string}[] } }
* inlines JS content from all `inlineSources` looking up for
* symbols in `inlineSym`
*/
function inline(si) {
var s,
rt,
inlines,
getBody = function _getBody(si) {
var s,
getBody = M.generator(),
r,
p;
getBody.step = getBody_1;
getBody.$step = 3;
return getBody;
function getBody_1(p) {
var a;
switch (getBody.$cur = getBody.$step) {
case 3:
s = Kit.auto(si);
Kit.skip(s.till(i => {
return i.pos === _core.Tag.body && i.type === _core.Tag.Array;
}));
a = s.sub();
getBody.$step = 4;
return getBody.$delegate(a);
case 4:
Kit.skip(s);
getBody.$step = 0;
getBody.value = void 0;
getBody.done = true;
return getBody;
case 0:
getBody.$step = 0;
getBody.value = p;
getBody.done = true;
return getBody;
case 1:
getBody.$step = 0;
throw p;
case 2:
return getBody.$redir(p);
default:
throw new Error("invalid state");
}
}
},
buf,
transf,
syms,
content,
symsMap,
i,
p,
_i,
node,
sym,
i1,
inline = M.generator(),
loop,
_loop,
loop1,
loop2,
r,
_p;
inline.step = inline_1;
inline.$step = 3;
return inline;
function inline_1(_p) {
var a;
for (;;) {
switch (inline.$cur = inline.$step) {
case 3:
s = Kit.auto(si);
({
rt
} = s.first.value);
inlines = rt.inlineSyms;
if (!inlines || !inlines.length) {
inline.$step = 7;
return inline.$delegate(s);
} else {
buf = [];
transf = Kit.pipe(Kit.parse, _core.produce, Scope.prepare, getBody, Kit.toArray);
loop = M.iterator(inlines);
for (; !(loop = loop.step()).done;) {
({
syms,
content
} = loop.value);
symsMap = new Map();
_loop = M.iterator(syms);
for (; !(_loop = _loop.step()).done;) {
i = _loop.value;
symsMap.set(i.orig, i);
}
p = transf(content);
buf.push(p);
loop1 = M.iterator(p);
for (; !(loop1 = loop1.step()).done;) {
_i = loop1.value;
if (_i.enter) {
({
node
} = _i.value);
node.loc = node.start = node.end = null;
if (_i.type === _core.Tag.Identifier) {
sym = symsMap.get(node.name);
if (sym) {
_i.value.sym = sym;
} else {
if (_i.value.decl && _i.value.sym) {
_i.value.sym.strict = false;
}
}
}
}
}
}
a = Kit.fileBody(s);
inline.$step = 4;
return inline.$delegate(a);
}
case 4:
loop2 = M.iterator(buf);
inline.$step = 5;
inline.$step = 5;
continue;
case 5:
if (!(loop2 = loop2.step()).done) {
i1 = loop2.value;
inline.$step = 5;
return inline.$delegate(i1);
} else {
inline.$step = 6;
return inline.$delegate(s);
}
case 6:
inline.$step = 0;
inline.value = void 0;
inline.done = true;
return inline;
case 0:
inline.$step = 0;
inline.value = _p;
inline.done = true;
return inline;
case 1:
inline.$step = 0;
throw _p;
case 7:
inline.$step = 0;
inline.$step = 0;
_p = r;
continue;
case 2:
return inline.$redir(_p);
default:
throw new Error("invalid state");
}
}
}
}
/** initializes empty `rt` structure */
function init(si) {
var h,
s,
init = M.generator(),
r,
p;
init.step = init_1;
init.$step = 3;
return init;
function init_1(p) {
switch (init.$cur = init.$step) {
case 3:
[h, s] = Kit.la(si);
h.value.rt = {
importSyms: [],
importNs: [],
inlineSyms: []
};
init.$step = 4;
return init.$delegate(s);
case 4:
init.$step = 0;
init.value = void 0;
init.done = true;
return init;
case 0:
init.$step = 0;
init.value = p;
init.done = true;
return init;
case 1:
init.$step = 0;
throw p;
case 2:
return init.$redir(p);
default:
throw new Error("invalid state");
}
}
}