Skip to content

Commit

Permalink
allow sourceURL to match original implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
marco-ippolito committed Oct 14, 2024
1 parent 02f1d25 commit ede9ffe
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
22 changes: 20 additions & 2 deletions doc/api/parser.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ added: REPLACEME
* `'transform'` Strip type annotations and transform TypeScript features to JavaScript.
* `sourceMap` {boolean} **Default:** `false`. Only when `mode` is `'transform'`, if `true`, a source map
will be generated for the transformed code.
* `sourceUrl` {string} Only when `mode` is `'transform'`, specifies the source url used in the source map.
* `sourceUrl` {string} Specifies the source url used in the source map.
* Returns: {string} The code with type annotations stripped.
`parser.stripTypeScriptTypes()` removes type annotations from TypeScript code. It
can be used to strip type annotations from TypeScript code before running it
Expand All @@ -42,7 +42,7 @@ added: REPLACEME
When mode is `'transform'`, it also transforms TypeScript features to JavaScript,
see [transform TypeScript features][] for more information.
When mode is `'strip-only'`, source maps are not generated, because locations are preserved.
If `sourceMap` or `sourceUrl` is provided, when mode is `'strip-only'`, an error will be thrown.
If `sourceMap` is provided, when mode is `'strip-only'`, an error will be thrown.

_WARNING_: The output of this function should not be considered stable across Node.js versions,
due to changes in the TypeScript parser.
Expand All @@ -63,6 +63,24 @@ console.log(strippedCode);
// Prints: const a = 1;
```

If `sourceUrl` is provided, it will be used appended as a comment at the end of the output:

```mjs
import parser from 'node:parser';
const code = 'const a: number = 1;';
const strippedCode = parser.stripTypeScriptTypes(code, { mode: 'strip-only', sourceUrl: 'source.ts' });
console.log(strippedCode);
// Prints: const a = 1\n\n//# sourceURL=source.ts;
```

```cjs
const parser = require('node:parser');
const code = 'const a: number = 1;';
const strippedCode = parser.stripTypeScriptTypes(code, { mode: 'strip-only', sourceUrl: 'source.ts' });
console.log(strippedCode);
// Prints: const a = 1\n\n//# sourceURL=source.ts;
```

When `mode` is `'transform'`, the code is transformed to JavaScript:

```mjs
Expand Down
6 changes: 5 additions & 1 deletion lib/internal/parser/typescript.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ function stripTypeScriptTypes(code, options = kEmptyObject) {
validateOneOf(mode, 'options.mode', ['strip-only', 'transform']);
if (mode === 'strip-only') {
validateOneOf(sourceMap, 'options.sourceMap', [false, undefined]);
validateOneOf(sourceUrl, 'options.sourceUrl', ['', undefined]);
}
validateBoolean(sourceMap, 'options.sourceMap');
validateString(sourceUrl, 'options.sourceUrl');
Expand All @@ -82,6 +81,11 @@ function stripTypeScriptTypes(code, options = kEmptyObject) {
if (map) {
return addSourceMap(transformed, map);
}

if (sourceUrl) {
return `${transformed}\n\n//# sourceURL=${sourceUrl}`;
}

return transformed;
}

Expand Down
5 changes: 2 additions & 3 deletions test/parallel/test-parser-strip-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ test('stripTypeScriptTypes sourceMap throws when mode is strip-only', () => {

test('stripTypeScriptTypes sourceUrl throws when mode is strip-only', () => {
const source = 'const x: number = 1;';
assert.throws(() => stripTypeScriptTypes(source,
{ mode: 'strip-only', sourceUrl: 'foo.ts' }),
{ code: 'ERR_INVALID_ARG_VALUE' });
const result = stripTypeScriptTypes(source, { mode: 'strip-only', sourceUrl: 'foo.ts' });
assert.strictEqual(result, 'const x = 1;\n\n//# sourceURL=foo.ts');
});

test('stripTypeScriptTypes source map when mode is transform', () => {
Expand Down

0 comments on commit ede9ffe

Please sign in to comment.