UNPKG

@effectful/transducers

Version:

JS syntax transformation framework for @effectful/js

125 lines (124 loc) 5.07 kB
"use strict"; exports.__esModule = true; exports.default = void 0; var _core = require("../core"); var Kit = require("../kit"); var T = require("@babel/types"); var Match = require("../match"); 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; } var _default = exports.default = Kit.pipe(Match.run(`for($$ of $$) $$`), function eagerForOfTransform(si) { const s = Kit.auto(si); function* walkDecls(decls, txt) { const c = decls[0]; if (c.type === _core.Tag.VariableDeclaration) { yield s.enter(_core.Tag.push, Kit.Subst); for (const j of decls) { if (j.leave && j.type === _core.Tag.VariableDeclarator) { yield* s.toks(_core.Tag.init, txt); } yield j; } } else { yield s.enter(_core.Tag.push, _core.Tag.ExpressionStatement); yield s.enter(_core.Tag.expression, _core.Tag.AssignmentExpression, { node: { operator: "=" } }); yield s.enter(_core.Tag.left, Kit.Subst); yield* decls; yield* s.leave(); yield* s.toks(_core.Tag.right, txt); yield* s.leave(); } yield* s.leave(); } function* walk(label) { for (const i of s.sub()) { switch (i.type) { case Match.Root: if (i.enter) { Kit.skip(s.till(v => v.enter && v.type === Match.Placeholder)); const decls = _toConsumableArray(s.sub()); Kit.skip(s.till(v => v.enter && v.type === Match.Placeholder)); const init = _toConsumableArray(s.sub()); Kit.skip(s.till(v => v.enter && v.type === Match.Placeholder)); const body = _toConsumableArray(Kit.inBlockBody(s, walk())); Kit.skip(s.till(v => v.leave && v.type === Match.Root)); const lab = s.label(); yield* s.template(_core.Tag.push, ` { const _e = $_; const _arr = e$y$arr(_e); if (_arr != null) { const _len = _arr.length; $_: for(let _i = 0; _i < _len; ++_i) { $_; $_; } } else { const _iter = _e[Symbol.iterator]() $_: for(let _i = _iter.next(); !_i.done; _i = _iter.next()) { $_; $_; } } }`); yield s.enter(_core.Tag.init, Kit.Subst); yield* init; yield* s.leave(); yield* s.refocus(); yield s.tok(_core.Tag.label, label == null ? _core.Tag.Null : T.identifier(label)); yield* s.refocus(); yield* walkDecls(decls, "=_arr[_i]"); yield* s.refocus(); yield* body; yield* s.refocus(); yield s.tok(_core.Tag.label, label == null ? _core.Tag.Null : T.identifier(label)); yield* s.refocus(); yield* walkDecls(_toConsumableArray(Kit.clone(decls)), "=_i.value"); yield* s.refocus(); yield* Kit.clone(body); yield* lab(); } continue; case _core.Tag.LabeledStatement: const lab = s.label(); const buf = [s.peel(i)].concat(_toConsumableArray(s.one())); if (s.cur().type === Match.Root) { yield* walk(i.value.node.label.name); Kit.skip(lab()); } else { yield* buf; yield* walk(); yield* lab(); } continue; } yield i; } } return walk(); }, function removeEmptyLabels(si) { const s = Kit.auto(si); function* walk() { for (const i of s.sub()) { if (i.enter && i.type === _core.Tag.LabeledStatement && s.cur().type === _core.Tag.Null) { s.peel(i); Kit.skip(s.one()); yield s.enter(i.pos, Kit.Subst); yield* walk(); yield* s.leave(); Kit.skip(s.leave()); continue; } yield i; } } return walk(); }, Kit.completeSubst);