UNPKG

@effectful/transducers

Version:

JS syntax transformation framework for @effectful/js

288 lines (284 loc) 10.4 kB
"use strict"; exports.__esModule = true; exports.all = exports.BROWSER_DEBUG = void 0; exports.ccg = ccg; exports.cg = cg; exports.cleanComments = cleanComments; exports.opts = exports.lazy = exports.default = void 0; exports.setSymComments = setSymComments; exports.verify = verify; var _generator = require("@babel/generator"); var T = require("@babel/types"); var _core = require("./core"); 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 MAX_TRACE_CODE_LEN = 40; const BROWSER_DEBUG = exports.BROWSER_DEBUG = typeof window !== "undefined" && window.chrome; const enterStyles = "background: #222; color: #bada55;font-size:1.5em"; const leaveStyles = "color: #ee5757; background: black"; const newTagStyle = "color:purple;font-size:large"; const positionStyle = "color:gray;text-decoration:underline"; const dirStyle = "font-size:x-large;font-weight:bolder"; const opts = exports.opts = { color: true }; /** * outputs short description of AST node */ function cg(ast, opts = {}) { var res; if (ast == null) { // console.error("<NULL>") return "<NULL>"; } try { if (Array.isArray(ast)) { ast = ast.length > 0 && T.isExpression(ast[0]) ? T.sequenceExpression(ast) : T.blockStatement(ast); } res = (0, _generator.default)(ast, opts, "").code; } catch (e) { if (ast.type != null) console.log("ERROR:", e.stack); } if (res != null) { return res; } return "!!" + opts.compact ? JSON.stringify(ast, defaultReplacer) : JSON.stringify(ast, defaultReplacer, 2); } function defaultReplacer(n, v) { return n === "leadingComments" || n === "trailingComments" ? void 0 : v; } /** * same as `cg` but using compact syntax */ function ccg(ast) { return cg(ast, { compact: true }); } if (BROWSER_DEBUG) { global.cg = cg; global.ccg = ccg; } function* verify(s) { const stack = []; for (const i of (0, _core.resetFieldInfo)(s)) { (0, _core.invariant)(i.enter != null); (0, _core.invariant)(i.leave != null); (0, _core.invariant)(i.pos != null); (0, _core.invariant)(i.type != null); (0, _core.invariant)(i.value != null); const ti = (0, _core.typeInfo)(i); const ctrlPos = (0, _core.symInfo)(i.pos).kind === "ctrl"; if (i.enter && stack.length) { const [f, keys] = stack[stack.length - 1]; if (f.type === _core.Tag.Array) { if (!ctrlPos) (0, _core.invariant)(i.pos === _core.Tag.push); } else if (keys != null && !ctrlPos) { let k; while ((k = keys.shift()) != null) { if (_core.Tag[k] === i.pos) break; } (0, _core.invariant)(k); } } if (i.enter && i.value.fieldInfo != null) { if (i.type === _core.Tag.Array) { (0, _core.invariant)(i.value.fieldInfo.array); } } if (i.enter && !i.leave) { const keys = T.VISITOR_KEYS[(0, _core.symName)(i.type)]; stack.push([i, keys && _toConsumableArray(keys)]); } if (!i.enter && i.leave) { const [f] = stack.pop(); (0, _core.invariant)(f != null); if (ti.kind !== "ctrl") (0, _core.invariant)(f.type === i.type); (0, _core.invariant)(f.pos === i.pos); (0, _core.invariant)(f.value === i.value); } yield i; } (0, _core.invariant)(stack.length === 0); } function pad(s) { if (s.length % 2) s += " "; const sps = Array(Math.max(20 - s.length / 2, 2)).join(" "); return sps + s + sps; } const traceImpl = BROWSER_DEBUG ? browserTraceImpl : traceNodeImpl; function* traceNodeImpl(prefix, s) { let level = 0; let x = 0; const term = require("terminal-kit").terminal; for (const i of s) { if (i.enter) level++; const dir = term.bold(i.leave ? i.enter ? "|" : "/" : "\\"); const clevel = s.level ? `/${s.level}` : ""; const descr = `${term.green((0, _core.symName)(i.pos))}:${i.type.ctrl ? term.green((0, _core.symName)(i.type)) : term.green.bold((0, _core.symName)(i.type))}[${level}${clevel}]`; let n = ""; const { node } = i.value; const comments = []; let commentsTxt = ""; const t = [].concat(i.value.comments || [], i.value.tcomments || []); if (t.length) { if (comments.length) commentsTxt = term.bold("[") + comments.join(" ") + term.bold("]"); } if (node != null && i.type !== _core.Tag.Array && (0, _core.symKind)(i.type) !== "ctrl") { n = ccg(node); if (n.length > MAX_TRACE_CODE_LEN) n = n.substr(0, MAX_TRACE_CODE_LEN) + "..."; n = term.yellow(n); const { loc } = node; if (loc != null) { let { source: f, start: s, end: e } = loc; n += term.blue(` @${f || "?"}-${s.line}:${s.column}..${e.line}:${e.column}`); } else { n += term.bold(" @new"); } } console.log(prefix, Array(level).join(" "), dir, `${descr}@${x}`, commentsTxt, n); yield i; if (i.leave) { level--; } x++; } console.log(prefix, "len:", x); } function* browserTraceImpl(prefix, s) { let level = 0; let x = 0; let first = true; for (const i of s) { var _console; if (first) { first = false; let name = (0, _core.symName)(i.type); if (i.value.funcId) name += ":" + i.value.id; console.log(`%c${pad(prefix + " " + name.match(/[A-Z]/g).join(""))}%c`, `background:#2B81AF;color:#fff;font-size:xx-large; text-shadow:rgba(0, 0, 0, 0.5) 2px 2px 1px`, ""); } const styles = []; if (i.enter) level++; const dir = i.enter && i.leave ? "\u21c4" : i.enter ? "\u2192" : "\u2190"; const clevel = s.level ? `/${s.level}` : ""; const tp = i.pos === i.type ? (0, _core.symName)(i.type) : `${(0, _core.symName)(i.pos)}:${(0, _core.symName)(i.type)}`; const descr = `${prefix}${tp}[${level}${clevel}]`; const idtxt = i.value.id ? `-${i.value.id}` : ""; let ldescr; if (i.pos === i.type) { ldescr = `${prefix}%c${(0, _core.symName)(i.type)}%c[${level}${clevel}]`; styles.push((0, _core.symKind)(i.type) === "ctrl" ? "color:navy;font-size:large" : "color:steelblue", ""); } else { ldescr = `${prefix}%c${(0, _core.symName)(i.pos)}:%c${(0, _core.symName)(i.type)}%c[${level}${clevel}]`; styles.push((0, _core.symKind)(i.pos) === "ctrl" ? "color:olive;font-size:large" : "color:green"); styles.push((0, _core.symKind)(i.type) === "ctrl" ? "color:navy;font-size:large" : "color:steelblue", ""); } if (i.enter && !i.leave && console.group != null) { if (x === 0 && i.value.collapsed !== false || i.value.collapsed) console.groupCollapsed(descr);else console.group(descr); } let n = ""; const { node } = i.value; const comments = []; let commentsTxt = ""; const t = [].concat(i.value.comments || [], i.value.tcomments || []); if (t.length) { const pstyle = "color:green;font-size:large"; styles.push(pstyle); for (const j of t) { comments.push(`%c${j.txt}%c`); const mod = !i.enter ? "font-size:small;font-style:italic" : "font-weight:bolder;font-size:large"; const s = `${j.style || ""}${mod}`; styles.push(s, ""); } if (comments.length) { commentsTxt = "%c[" + comments.join(" ") + "%c]%c"; styles.push(pstyle, ""); } } if (node != null && i.type !== _core.Tag.Array && (0, _core.symKind)(i.type) !== "ctrl") { n = ccg(node); if (n.length > MAX_TRACE_CODE_LEN) n = n.substr(0, MAX_TRACE_CODE_LEN) + "..."; n = `%c ${n} %c`; styles.push(i.enter ? enterStyles : leaveStyles, ""); const { loc } = node; if (loc != null) { let { source: f, start: s, end: e } = loc; n += ` %c@${f || "?"}-${s.line}:${s.column}..${e.line}:${e.column}%c`; styles.push(positionStyle, ""); } else { n += " %c@new%c"; styles.push(newTagStyle, ""); } } (_console = console).log.apply(_console, [`%c${dir}%c ${ldescr}@${x}${idtxt}${commentsTxt} ${n}`, dirStyle, ""].concat(styles, [i.value])); yield i; if (i.leave) { if (!i.enter && console.group != null) console.groupEnd(); level--; } x++; } console.log(`${prefix}: len: ${x}`); } function traceAllImpl(prefix, s) { return _toConsumableArray(prefix[0] === "!" ? traceImpl(prefix, s) : verify(traceImpl(prefix, s))); } function traceArgs(impl) { return function traceImpl(prefix, s) { if (prefix == null || prefix.substr == null) { if (s == null) s = prefix; prefix = ""; } if (prefix.length) prefix += ":"; if (s == null || s[Symbol.iterator] == null) return s => impl(prefix, s); return impl(prefix, s); }; } function* cleanComments(s) { for (const i of s) { if (i.enter) { i.value.comments = null; i.value.tcomments = null; if (i.value.node) { i.value.node.leadingComments = null; i.value.node.trailingComments = null; } } yield i; } } function* setSymComments(s) { for (const i of s) { if (i.enter && i.value.sym && i.value.sym.id) { const c = i.value.comments || (i.value.comments = []); c.push({ txt: i.value.sym.id }); } yield i; } } const lazy = exports.lazy = traceArgs(traceImpl); const all = exports.all = traceArgs(traceAllImpl); var _default = exports.default = lazy;