Skip to content

Commit

Permalink
fix: more robust generic arrow handling (#30)
Browse files Browse the repository at this point in the history
fixes #29

Given:

```ts
return<T>
(v: T) => v
```

We need to ensure the arrow function is still returned after erasing the
type argument. This is achieved by moving the opening parentheses up:

```js
return(
 v: T) => v
```

**Testing performed**

Additional test fixtures have been added.

---------

Signed-off-by: Ashley Claymore <[email protected]>
  • Loading branch information
acutmore authored Jan 15, 2025
1 parent fa37b5e commit 14c8181
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
12 changes: 2 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,6 @@ function visitModifiers(modifiers: ArrayLike<ts.ModifierLike>, addSemi: boolean)
}
}

function isAsync(modifiers: ArrayLike<ts.ModifierLike> | 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
*/
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,23 @@ const c = async <
// ^^^ ^^^^^^^^^^
T
> => v;

// https://github.com/bloomberg/ts-blank-space/issues/29
(function () {
return<T>
(v: T) => v
});
(function () {
return/**/<
T
>/**/(v: T)/**/:
T/**/=> v
});
(function* () {
yield<T>
(v: T)=>v;
});
(function* () {
throw<T>
(v: T)=>v;
});

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 14c8181

Please sign in to comment.