From 75369e1fe126895359e0044b020d7beb7c940619 Mon Sep 17 00:00:00 2001 From: Ashley Claymore Date: Tue, 14 Jan 2025 19:41:26 +0000 Subject: [PATCH 1/2] fix: more robust generic arrow handling fixes #29 Signed-off-by: Ashley Claymore --- package-lock.json | 4 ++-- package.json | 2 +- src/index.ts | 12 ++---------- .../{async-generic-arrow.ts => arrow-functions.ts} | 13 +++++++++++++ .../{async-generic-arrow.js => arrow-functions.js} | 13 +++++++++++++ 5 files changed, 31 insertions(+), 13 deletions(-) rename tests/fixture/cases/{async-generic-arrow.ts => arrow-functions.ts} (54%) rename tests/fixture/output/{async-generic-arrow.js => arrow-functions.js} (54%) diff --git a/package-lock.json b/package-lock.json index efac839..3267f22 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "ts-blank-space", - "version": "0.4.4", + "version": "0.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "ts-blank-space", - "version": "0.4.4", + "version": "0.5.0", "license": "Apache-2.0", "dependencies": { "typescript": "5.1.6 - 5.7.x" diff --git a/package.json b/package.json index 9d54611..a354b03 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "ts-blank-space", "description": "A small, fast, pure JavaScript type-stripper that uses the official TypeScript parser.", - "version": "0.4.4", + "version": "0.5.0", "license": "Apache-2.0", "homepage": "https://bloomberg.github.io/ts-blank-space", "contributors": [ diff --git a/src/index.ts b/src/index.ts index 7a13408..8f23606 100644 --- a/src/index.ts +++ b/src/index.ts @@ -316,14 +316,6 @@ function visitModifiers(modifiers: ArrayLike, addSemi: boolean) } } -function isAsync(modifiers: ArrayLike | undefined): boolean { - if (!modifiers) return false; - for (let i = 0; i < modifiers.length; i++) { - if (modifiers[i].kind === SK.AsyncKeyword) return true; - } - return false; -} - /** * prop: T */ @@ -409,15 +401,15 @@ function visitFunctionLikeDeclaration(node: ts.FunctionLikeDeclaration, kind: ts } let moveOpenParen = false; + const params = node.parameters; if (node.typeParameters && node.typeParameters.length) { - moveOpenParen = isAsync(node.modifiers) && spansLines(node.typeParameters.pos, node.typeParameters.end); + moveOpenParen = spansLines(node.typeParameters.pos, params.pos); blankGenerics(node, node.typeParameters, moveOpenParen); } // method? node.questionToken && blankExact(node.questionToken); - const params = node.parameters; if (moveOpenParen) { str.blank(params.pos - 1, params.pos); } diff --git a/tests/fixture/cases/async-generic-arrow.ts b/tests/fixture/cases/arrow-functions.ts similarity index 54% rename from tests/fixture/cases/async-generic-arrow.ts rename to tests/fixture/cases/arrow-functions.ts index 7cf63e4..bb66e14 100644 --- a/tests/fixture/cases/async-generic-arrow.ts +++ b/tests/fixture/cases/arrow-functions.ts @@ -16,3 +16,16 @@ const c = async < // ^^^ ^^^^^^^^^^ T > => v; + +(function () { + // https://github.com/bloomberg/ts-blank-space/issues/29 + return + (v: T) => v +}()); +(function () { + // https://github.com/bloomberg/ts-blank-space/issues/29 + return/**/< + T + >/**/(v: T)/**/: + T/**/=> v +}()); diff --git a/tests/fixture/output/async-generic-arrow.js b/tests/fixture/output/arrow-functions.js similarity index 54% rename from tests/fixture/output/async-generic-arrow.js rename to tests/fixture/output/arrow-functions.js index 43a1153..af34e2c 100644 --- a/tests/fixture/output/async-generic-arrow.js +++ b/tests/fixture/output/arrow-functions.js @@ -16,3 +16,16 @@ const c = async ( ) => v; + +(function () { + // https://github.com/bloomberg/ts-blank-space/issues/29 + return( + v ) => v +}()); +(function () { + // https://github.com/bloomberg/ts-blank-space/issues/29 + return/**/( + + /**/ v + )/**/=> v +}()); From c38dd45e6b4f364538fcbddb5a02a9827ef6a5f6 Mon Sep 17 00:00:00 2001 From: Ashley Claymore Date: Tue, 14 Jan 2025 20:01:36 +0000 Subject: [PATCH 2/2] [squash] more tests Signed-off-by: Ashley Claymore --- tests/fixture/cases/arrow-functions.ts | 15 +++++++++++---- tests/fixture/output/arrow-functions.js | 15 +++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/tests/fixture/cases/arrow-functions.ts b/tests/fixture/cases/arrow-functions.ts index bb66e14..16627c6 100644 --- a/tests/fixture/cases/arrow-functions.ts +++ b/tests/fixture/cases/arrow-functions.ts @@ -17,15 +17,22 @@ const c = async < T > => v; +// https://github.com/bloomberg/ts-blank-space/issues/29 (function () { - // https://github.com/bloomberg/ts-blank-space/issues/29 return (v: T) => v -}()); +}); (function () { - // https://github.com/bloomberg/ts-blank-space/issues/29 return/**/< T >/**/(v: T)/**/: T/**/=> v -}()); +}); +(function* () { + yield +(v: T)=>v; +}); +(function* () { + throw +(v: T)=>v; +}); diff --git a/tests/fixture/output/arrow-functions.js b/tests/fixture/output/arrow-functions.js index af34e2c..3ffb2ae 100644 --- a/tests/fixture/output/arrow-functions.js +++ b/tests/fixture/output/arrow-functions.js @@ -17,15 +17,22 @@ const c = async ( ) => v; +// https://github.com/bloomberg/ts-blank-space/issues/29 (function () { - // https://github.com/bloomberg/ts-blank-space/issues/29 return( v ) => v -}()); +}); (function () { - // https://github.com/bloomberg/ts-blank-space/issues/29 return/**/( /**/ v )/**/=> v -}()); +}); +(function* () { + yield( + v )=>v; +}); +(function* () { + throw( + v )=>v; +});