/** * Module to add labels to breaks and continues according to tagged AST nodes * generated by desugarLoop. * */ import { NodePath, Visitor } from 'babel-traverse'; import * as t from 'babel-types'; import { While, Break } from '@stopify/util'; const labelVisitor : Visitor = { ContinueStatement: function (path: NodePath): void { const { label } = path.node; if (label) { const breakStatement = t.breakStatement(label); path.replaceWith(breakStatement); } else { const loopParent : NodePath> = path.findParent(p => p.isWhileStatement()); const continueLabel = loopParent.node.continue_label; const breakStatement = t.breakStatement(continueLabel); path.replaceWith(breakStatement); } }, BreakStatement: function (path: NodePath>): void { const label = path.node.label; if (label === null) { const labeledParent : NodePath> = path.findParent(p => p.isLoop() || p.isSwitchStatement()); if (labeledParent === null) { return; } path.node.label = labeledParent.node.break_label; } }, }; module.exports = function() { return { visitor: labelVisitor }; };