UNPKG

@effectful/transducers

Version:

JS syntax transformation framework for @effectful/js

214 lines (213 loc) 7.81 kB
"use strict"; exports.__esModule = true; exports.convertCtrl = exports.color = void 0; exports.copyComment = copyComment; exports.fin = exports.default = void 0; exports.markNodeType = markNodeType; exports.output = exports.opts = void 0; exports.setComment = setComment; exports.setEndComment = setEndComment; exports.toConsole = void 0; exports.toStr = toStr; var Kit = require("./kit"); var _scope = require("./scope"); var Trace = require("./trace"); var _core = require("./core"); var _generator = require("@babel/generator"); function _toConsumableArray(r) { return _arrayWithoutHoles(r) || _iterableToArray(r) || _unsupportedIterableToArray(r) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(r, a) { if (r) { if ("string" == typeof r) return _arrayLikeToArray(r, a); var t = {}.toString.call(r).slice(8, -1); return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0; } } function _iterableToArray(r) { if ("undefined" != typeof Symbol && null != r[Symbol.iterator] || null != r["@@iterator"]) return Array.from(r); } function _arrayWithoutHoles(r) { if (Array.isArray(r)) return _arrayLikeToArray(r); } function _arrayLikeToArray(r, a) { (null == a || a > r.length) && (a = r.length); for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e]; return n; } const BROWSER_DEBUG = Trace.BROWSER_DEBUG; const opts = exports.opts = { color: true }; function* markNodeType(s) { for (const i of s) { if (i.enter) { const ti = (0, _core.typeInfo)(i); if (ti.kind === "ctrl") (i.value.comments || (i.value.comments = [])).unshift({ txt: (0, _core.symName)(i.type), style: styles.nodetype });else if (i.value.comments != null && i.value.comments.length) { (i.value.comments || (i.value.comments = [])).unshift({ txt: (0, _core.symName)(i.type).match(/[A-Z]/g).join(""), style: styles.nodetype }); } } yield i; } } const convertCtrl = exports.convertCtrl = Kit.pipe(_core.resetFieldInfo, markNodeType, Array.from, function convertCtrl(s) { s = Kit.auto(s); function* walk(sw) { for (const i of sw) { if (i.enter) { const fld = i.value.fieldInfo || {}, ti = (0, _core.typeInfo)(i); if (ti.kind === "ctrl") { s.peel(i); const lab = s.label(); const stmt = !fld.expr; yield s.enter(i.pos, stmt ? _core.Tag.BlockStatement : _core.Tag.SequenceExpression, { comments: i.value.comments }); yield s.enter(stmt ? _core.Tag.body : _core.Tag.expressions, _core.Tag.Array); if (!i.leave) { while (s.curLev() != null) { yield s.enter(_core.Tag.push, Kit.Subst); yield* walk(s.one()); yield* s.leave(); } } yield* lab(); Kit.skip(s.leave()); continue; } } yield i; } } return walk(s); }, Kit.completeSubst, Array.from, Kit.adjustFieldType); const color = exports.color = BROWSER_DEBUG ? function* color(s) { if (!opts.color) { yield* s; return; } for (const i of s) { if (i.enter && i.value.comments) { for (const j of i.value.comments) { if (j.style != null && (0, _core.typeInfo)(i).kind === "node") { j.txt = `%c${j.txt}%c`; j.args = [j.style, ""]; } } } if (i.leave && i.value.tcomments) { for (const j of i.value.tcomments) { if (j.style != null && (0, _core.typeInfo)(i).kind === "node") { j.txt = `%c${j.txt}%c`; j.args = [j.style, ""]; } } } yield i; } } : s => s; function* getArgs(s) { for (const i of s) { if (i.enter && i.value.comments) { for (const j of i.value.comments) { if (j.args) for (const k of j.args) yield k; } } if (i.leave && i.value.tcomments) { for (const j of i.value.tcomments) { if (j.args) for (const k of j.args) yield k; } } } } function* applyComments(s) { for (const i of s) { const node = i.value.node; if (i.enter && node != null) { if (i.value.comments != null && i.value.comments.length) { (node.leadingComments || (node.leadingComments = [])).push({ type: "CommentBlock", value: i.value.comments.map(v => v.txt).join("|") }); } if (i.value.tcomments != null && i.value.tcomments.length) { (node.trailingComments || (node.trailingComments = [])).push({ type: "CommentBlock", value: i.value.tcomments.map(v => v.txt).join("|") }); } } yield i; } } const toConsole = exports.toConsole = Kit.curry(function toConsole(tag, s) { var _console; s = Kit.auto(s); const root = s.first.value; let name = (0, _core.symName)(s.first.type).match(/[A-Z]/g).join(""); if (root.funcId) name += ":" + root.funcId.id; if (BROWSER_DEBUG && opts.color) (tag[0] === "+" ? console.group : console.groupCollapsed)(`dump %c${tag} %c${name}`, "color:orange;font-size:large", "color:coral");else console.log(`dump ${tag}`); const col = Kit.pipe(convertCtrl, color, _scope.tempNames, Array.from, applyComments, Array.from)(s); const args = Array.from(getArgs(col)); (0, _core.consume)(Trace.verify(col)); (_console = console).log.apply(_console, [(0, _generator.default)(col[0].value.node).code].concat(args)); if (BROWSER_DEBUG) console.groupEnd(); }); const fin = exports.fin = Kit.pipe(_core.removeNulls, _scope.tempNames, convertCtrl, Array.from, applyComments, Array.from); function toStr(s, opts = {}) { return (0, _generator.default)((0, _core.consume)(Kit.repos(s, _core.Tag.top)).top, opts).code; } const output = exports.output = Kit.curry(function dump(tag, s) { const sa = Kit.toArray(s); const sl = Kit.auto(Kit.clone(sa)); const opts = sl.opts || {}; let dest; if (BROWSER_DEBUG) { dest = "console"; } else { dest = `dump-${tag}.js`; if (opts.dump) { if (opts.dump.substr) { if (opts.dump === "console") { dest = opts.dump; } else { dest = opts.dump + dest; } } else { const s = opts.dump[tag]; if (s == null && s.substr) { dest = s; } } } } if (dest === "console") { toConsole(tag, sl); } else { require("fs").writeFileSync(dest, toStr(sl)); } return sa; }); var _default = exports.default = output; function setComment(i, txt, style = "none") { style = style.substr && styles[style] || style; (i.value.comments || (i.value.comments = [])).push({ txt, style }); return i; } function setEndComment(i, txt, style = "none") { style = style.substr && styles[style] || style; (i.value.tcomments || (i.value.tcomments = [])).push({ txt, style }); return i; } function copyComment(f, t) { var _ref, _ref2; if (f.value.comments != null) (_ref = t.value.comments || (t.value.comments = [])).push.apply(_ref, _toConsumableArray(f.value.comments)); if (f.value.tcomments != null) (_ref2 = t.value.tcomments || (t.value.tcomments = [])).push.apply(_ref2, _toConsumableArray(f.value.tcomments)); return t; } const styles = { large: "font-size:xx-large;color:orange", hl: "font-size:large;color:navy;", small: "font-size:large;color:navy;", smallH: "font-size:large;color:orange;font-weight:bolder", nodetype: "font-size:xx-small;color:green;font-weight:bolder", none: "" };