From d06ced43116c258ca24226ab8c4e991743276069 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Wed, 14 Aug 2024 15:53:49 +0200 Subject: [PATCH] test: add tests for nativeClassProperties --- src/loader.ts | 6 ++++-- src/transform.ts | 4 ++-- test/snapshots/transform.test.js.snapshot | 4 ++++ test/transform.test.js | 19 +++++++++++++++++++ 4 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/loader.ts b/src/loader.ts index 58a1737cd..1e4843dcb 100644 --- a/src/loader.ts +++ b/src/loader.ts @@ -1,5 +1,5 @@ -import assert from "node:assert"; import type { LoadFnOutput, LoadHookContext } from "node:module"; +import type { Options } from "../lib/wasm"; import { transformSync } from "./index.ts"; type NextLoad = ( @@ -22,7 +22,9 @@ export async function load( }); if (source == null) throw new Error("Source code cannot be null or undefined"); - const { code } = transformSync(source.toString(), { mode: "strip-only" }); + const { code } = transformSync(source.toString(), { + mode: "strip-only", + } as Options); return { format: format.replace("-typescript", ""), source: code, diff --git a/src/transform.ts b/src/transform.ts index dabf4b718..1a6145b1d 100644 --- a/src/transform.ts +++ b/src/transform.ts @@ -1,9 +1,9 @@ import type { Options, TransformOutput } from "../lib/wasm"; import swc from "../lib/wasm.js"; -const DEFAULT_OPTIONS: Options = { +const DEFAULT_OPTIONS = { mode: "strip-only", -}; +} as Options; export function transformSync( source: string, diff --git a/test/snapshots/transform.test.js.snapshot b/test/snapshots/transform.test.js.snapshot index 6689f84a5..42a35d87d 100644 --- a/test/snapshots/transform.test.js.snapshot +++ b/test/snapshots/transform.test.js.snapshot @@ -49,3 +49,7 @@ exports[`should transform TypeScript private class fields 1`] = ` exports[`should transform TypeScript type annotations and type guards 1`] = ` "function isString(value) {\\n return typeof value === 'string';\\n}\\nconst check = isString(\\"hello\\");\\noutput = check;\\n" `; + +exports[`test native class properties 1`] = ` +"class Foo {\\n y;\\n x;\\n constructor(y = console.log(2)){\\n this.y = y;\\n this.x = console.log(1);\\n console.log(3);\\n }\\n}\\n" +`; diff --git a/test/transform.test.js b/test/transform.test.js index a1f373d60..b0e04cabf 100644 --- a/test/transform.test.js +++ b/test/transform.test.js @@ -282,3 +282,22 @@ test("should transform TypeScript namespaces with additional functionality", (t) assert.ok(result.validator); assert.strictEqual(result.isValid, true); }); + +test("test native class properties", (t) => { + const inputCode = ` + class Foo { + x = console.log(1) + constructor(public y = console.log(2)) { + console.log(3) + } + }`; + const { code } = transformSync(inputCode, { + mode: "transform", + sourceMap: true, + transform: { + verbatimModuleSyntax: true, + nativeClassProperties: true, + }, + }); + t.assert.snapshot(code); +});