From 38f0910c88c085087747c1715c03f8d26cef331e Mon Sep 17 00:00:00 2001 From: j4k0xb <55899582+j4k0xb@users.noreply.github.com> Date: Mon, 25 Dec 2023 22:30:43 +0100 Subject: [PATCH] fix: merge transform visitors --- packages/webcrack/src/ast-utils/transform.ts | 12 ++++++------ .../src/transpile/test/optional-chaining.test.ts | 6 +++--- .../src/transpile/transforms/optional-chaining.ts | 2 +- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/packages/webcrack/src/ast-utils/transform.ts b/packages/webcrack/src/ast-utils/transform.ts index b2ef0aad..4368951a 100644 --- a/packages/webcrack/src/ast-utils/transform.ts +++ b/packages/webcrack/src/ast-utils/transform.ts @@ -80,12 +80,12 @@ export function mergeTransforms(options: { return { name: options.name, tags: options.tags, - run(ast, state) { - state.changes += applyTransforms(ast, options.transforms, { - name: options.name, - log: false, - }).changes; - }, + scope: options.transforms.some((t) => t.scope), + visitor() { + return visitors.merge( + options.transforms.flatMap((t) => t.visitor?.() ?? []), + ); + } }; } diff --git a/packages/webcrack/src/transpile/test/optional-chaining.test.ts b/packages/webcrack/src/transpile/test/optional-chaining.test.ts index 356f975a..2bab6e5b 100644 --- a/packages/webcrack/src/transpile/test/optional-chaining.test.ts +++ b/packages/webcrack/src/transpile/test/optional-chaining.test.ts @@ -23,16 +23,16 @@ test('member expression (Babel)', () => test.skip('computed member expression (Babel)', () => expectJS(` var _a; - (_a = a) === null || _a === void 0 ? void 0 : _a[0]; + (_a = a) === null || _a === undefined ? undefined : _a[0]; `).toMatchInlineSnapshot(`a?.[0];`)); test.skip('call expression (Babel)', () => expectJS(` var _a; - (_a = a) === null || _a === void 0 ? void 0 : _a(); + (_a = a) === null || _a === undefined ? undefined : _a(); `).toMatchInlineSnapshot(`a?.();`)); test.skip('call expression (TS)', () => expectJS(` - a === null || a === void 0 ? void 0 : a(); + a === null || a === undefined ? undefined : a(); `).toMatchInlineSnapshot(`a?.();`)); diff --git a/packages/webcrack/src/transpile/transforms/optional-chaining.ts b/packages/webcrack/src/transpile/transforms/optional-chaining.ts index 8eb346ff..1776328b 100644 --- a/packages/webcrack/src/transpile/transforms/optional-chaining.ts +++ b/packages/webcrack/src/transpile/transforms/optional-chaining.ts @@ -26,7 +26,7 @@ export default { const tmpVar = m.capture(m.identifier()); const tmpMember = m.capture(m.memberExpression(m.fromCapture(tmpVar))); - // Example (Babel): var _tmp; (_tmp = object) === null || _tmp === void 0 ? void 0 : _tmp.property; + // Example (Babel): var _tmp; (_tmp = object) === null || _tmp === undefined ? undefined : _tmp.property; const tmpMatcher = m.conditionalExpression( m.logicalExpression( '||',