From 8eb0f3371d5e2897850c9fbde912ea6d09806200 Mon Sep 17 00:00:00 2001 From: Collin Klopfenstein <70838+collink@users.noreply.github.com> Date: Wed, 7 Aug 2024 09:54:13 -0500 Subject: [PATCH] fix: Ensure that `getShortType` regex wildcard is non-greedy (#135) --- src/common.mts | 2 +- tests/common.test.ts | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/common.mts b/src/common.mts index 6182a86..53dae0b 100644 --- a/src/common.mts +++ b/src/common.mts @@ -116,7 +116,7 @@ export function extractPropertiesFromObjectParam(param: ParameterDeclaration) { * TODO: Replace with a more robust solution. */ export function getShortType(type: string) { - return type.replaceAll(/import\(".*"\)\./g, ""); + return type.replaceAll(/import\(".*?"\)\./g, ""); } export function getClassesFromService(node: SourceFile) { diff --git a/tests/common.test.ts b/tests/common.test.ts index f502240..76a09fe 100644 --- a/tests/common.test.ts +++ b/tests/common.test.ts @@ -80,6 +80,15 @@ describe("common", () => { const result = getShortType(type); expect(result).toBe("MyType"); }); + + test("json", () => { + const type = + '{ import1?: import("/path/to/import1").Import1; import2: import("/path/to/import2").Import2; import3: import("/path/to/import3").Import3; }'; + const result = getShortType(type); + expect(result).toBe( + "{ import1?: Import1; import2: Import2; import3: Import3; }", + ); + }); }); test("formatOptions - converts string boolean to boolean (false)", () => {