ts-blank-space
Advanced tools
Comparing version 0.4.1 to 0.4.2
@@ -6,2 +6,3 @@ /** Like magic-string but with only one feature */ | ||
constructor(input: string); | ||
blankButStartWithOpenParen(start: number, end: number): void; | ||
blankButEndWithCloseParen(start: number, end: number): void; | ||
@@ -8,0 +9,0 @@ blankButStartWithSemi(start: number, end: number): void; |
// Copyright 2024 Bloomberg Finance L.P. | ||
// Distributed under the terms of the Apache 2.0 license. | ||
const FLAG_REPLACE_WITH_CLOSE_PAREN = 1; | ||
const FLAG_REPLACE_WITH_SEMI = 2; | ||
const FLAG_REPLACE_WITH_OPEN_PAREN = 1; | ||
const FLAG_REPLACE_WITH_CLOSE_PAREN = 2; | ||
const FLAG_REPLACE_WITH_SEMI = 3; | ||
@@ -34,2 +35,6 @@ function getSpace(input , start , end ) { | ||
blankButStartWithOpenParen(start , end ) { | ||
this.__ranges.push(FLAG_REPLACE_WITH_OPEN_PAREN, start, end); | ||
} | ||
blankButEndWithCloseParen(start , end ) { | ||
@@ -73,2 +78,5 @@ this.__ranges.push(0, start, end - 1); | ||
rangeStart += 1; | ||
} else if (flags === FLAG_REPLACE_WITH_OPEN_PAREN) { | ||
out += "("; | ||
rangeStart += 1; | ||
} | ||
@@ -75,0 +83,0 @@ |
@@ -180,3 +180,3 @@ // Copyright 2024 Bloomberg Finance L.P. | ||
if (node.typeArguments) { | ||
blankGenerics(node, node.typeArguments); | ||
blankGenerics(node, node.typeArguments, /*startWithParen*/ false); | ||
} | ||
@@ -198,3 +198,3 @@ if (node.arguments) { | ||
if (node.typeArguments) { | ||
blankGenerics(node, node.typeArguments); | ||
blankGenerics(node, node.typeArguments, /*startWithParen*/ false); | ||
} | ||
@@ -238,3 +238,3 @@ visitor(node.template); | ||
if (node.typeParameters && node.typeParameters.length) { | ||
blankGenerics(node, node.typeParameters); | ||
blankGenerics(node, node.typeParameters, /*startWithParen*/ false); | ||
} | ||
@@ -266,3 +266,3 @@ | ||
if (node.typeArguments) { | ||
blankGenerics(node, node.typeArguments); | ||
blankGenerics(node, node.typeArguments, /*startWithParen*/ false); | ||
} | ||
@@ -318,2 +318,10 @@ return VISITED_JS; | ||
function isAsync(modifiers ) { | ||
if (!modifiers) return false; | ||
for (let i = 0; i < modifiers.length; i++) { | ||
if (modifiers[i].kind === SK.AsyncKeyword) return true; | ||
} | ||
return false; | ||
} | ||
/** | ||
@@ -402,4 +410,6 @@ * prop: T | ||
let moveOpenParen = false; | ||
if (node.typeParameters && node.typeParameters.length) { | ||
blankGenerics(node, node.typeParameters); | ||
moveOpenParen = isAsync(node.modifiers) && spansLines(node.typeParameters.pos, node.typeParameters.end); | ||
blankGenerics(node, node.typeParameters, moveOpenParen); | ||
} | ||
@@ -411,2 +421,5 @@ | ||
const params = node.parameters; | ||
if (moveOpenParen) { | ||
str.blank(params.pos - 1, params.pos); | ||
} | ||
for (let i = 0; i < params.length; i++) { | ||
@@ -604,6 +617,6 @@ const p = params[i]; | ||
*/ | ||
function blankGenerics(node , arr ) { | ||
function blankGenerics(node , arr , startWithParen ) { | ||
const start = arr.pos - 1; | ||
const end = scanRange(arr.end, node.end, getGreaterThanToken); | ||
str.blank(start, end); | ||
startWithParen ? str.blankButStartWithOpenParen(start, end) : str.blank(start, end); | ||
} | ||
@@ -610,0 +623,0 @@ |
{ | ||
"name": "ts-blank-space", | ||
"description": "A small, fast, pure JavaScript type-stripper that uses the official TypeScript parser.", | ||
"version": "0.4.1", | ||
"version": "0.4.2", | ||
"license": "Apache-2.0", | ||
@@ -6,0 +6,0 @@ "homepage": "https://bloomberg.github.io/ts-blank-space", |
@@ -58,3 +58,3 @@ <a href="https://bloomberg.github.io/ts-blank-space"> | ||
- It is small | ||
- Less than 700 lines of code and one dependency ([`TypeScript`](https://www.npmjs.com/package/typescript)) | ||
- Around 700 lines of code and one dependency ([`TypeScript`](https://www.npmjs.com/package/typescript)) | ||
- By doing so little, the code should be relatively easy to maintain | ||
@@ -163,10 +163,8 @@ - Delegates the parsing to the [official TypeScript parser](https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API) | ||
### Arrow function return types that introduce a new line | ||
### Arrow function type annotations that introduce a new line | ||
If the annotation marking the return type of an arrow function introduces a new line before the `=>`, then only replacing it with blank space would be incorrect. | ||
If the type annotations around an arrow function's parameters introduce a new line then only replacing them with blank space can be incorrect. Therefore, in addition to removing the type annotation, the `(` or `)` surrounding the function parameters may also be moved. | ||
Therefore, in addition to removing the type annotation, the `)` is moved down to the end of the type annotation. | ||
#### Example one - multi-line return type: | ||
Example input: | ||
<!-- prettier-ignore --> | ||
@@ -188,2 +186,20 @@ ```typescript | ||
#### Example two - `async` with multi-line type arguments: | ||
<!-- prettier-ignore --> | ||
```typescript | ||
let f = async < | ||
T | ||
>(v: T) => {}; | ||
``` | ||
becomes: | ||
<!-- prettier-ignore --> | ||
```javascript | ||
let f = async ( | ||
v ) => {}; | ||
``` | ||
## Unsupported Syntax | ||
@@ -203,3 +219,3 @@ | ||
// to 'define' semantics in the ECMAScript specification | ||
"useDefineAsClassFields": true, | ||
"useDefineForClassFields": true, | ||
// Because imports and exports are preserved as written, only removing the | ||
@@ -213,6 +229,16 @@ // parts which are explicitly annotated with the `type` keyword | ||
`.tsx` input will be `.jsx` output because the JSX parts are not transformed, but instead preserved in the output. | ||
`.tsx` input will generate `.jsx` output. JSX parts are not transformed, but instead preserved in the output. | ||
By default, `ts-blank-space` will parse the file assuming `.ts`. If the original file contains JSX syntax, then the [parsing should be done manually](#bring-your-own-ast). There is a TSX example in [`valid.test.ts`](./tests/valid.test.ts). | ||
```typescript | ||
import ts from "typescript"; | ||
import { blankSourceFile } from "ts-blank-space"; | ||
... | ||
const tsxSource = ts.createSourceFile("input.tsx", tsxInput, ts.ScriptTarget.ESNext, false, ts.ScriptKind.TSX); | ||
const jsxOutput = blankSourceFile(tsxSource, onError); | ||
``` | ||
## Ensuring ESM output | ||
@@ -224,2 +250,6 @@ | ||
## 3rd party ecosystem plugins | ||
- Webpack/Rspack: [ts-blank-loader](https://github.com/leimonio/ts-blank-loader) | ||
## Contributions | ||
@@ -226,0 +256,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46715
686
273