From 6b1000f8ca67ef32192774eaf45e73c3b060c59b Mon Sep 17 00:00:00 2001 From: relative Date: Mon, 2 Oct 2023 16:51:18 -0400 Subject: [PATCH] fix(desequence): fix lost nodes from desequence --- src/transformers/desequence.ts | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/transformers/desequence.ts b/src/transformers/desequence.ts index 4e6e8de..9e3c094 100644 --- a/src/transformers/desequence.ts +++ b/src/transformers/desequence.ts @@ -1,4 +1,4 @@ -import { Program, BlockStatement, sp } from '../util/types' +import { Program, BlockStatement, sp, ExpressionStatement } from '../util/types' import { Transformer, TransformerOptions } from './transformer' import { walk } from '../util/walk' import * as Guard from '../util/guard' @@ -14,22 +14,23 @@ export default class Desequence extends Transformer { function visitor(node: BlockStatement | Program) { // find expstmt > seqexp in node.body - for (let stmt of node.body) { + let length = node.body.length + for (let i = 0; i < length; ++i) { + const stmt = node.body[i] if ( Guard.isExpressionStatement(stmt) && Guard.isSequenceExpression(stmt.expression) ) { - let i = node.body.findIndex( - (s) => s.start === stmt.start && s.end === stmt.end - ) + node.body[i].type = 'EmptyStatement' let expr = stmt.expression.expressions.map((exp) => ({ type: 'ExpressionStatement', expression: exp, - })) - ;(node.body[i] as any) = expr + })) as ExpressionStatement[] + node.body.splice(i, 0, ...expr) + i += expr.length + length = node.body.length } } - sp(node, { body: node.body.flat() }) } walk(ast, { BlockStatement(node) {