Skip to content

Commit 0b81ab8

Browse files
Aviv Kellerlouwers
Aviv Keller
authored andcommitted
process: add process.features.typescript
PR-URL: nodejs#54295 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Yagiz Nizipli <[email protected]> Reviewed-By: Marco Ippolito <[email protected]> Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]>
1 parent 7dddbad commit 0b81ab8

File tree

4 files changed

+80
-14
lines changed

4 files changed

+80
-14
lines changed

doc/api/process.md

+13
Original file line numberDiff line numberDiff line change
@@ -1976,6 +1976,19 @@ added: v0.5.3
19761976
19771977
A boolean value that is `true` if the current Node.js build includes support for SNI in TLS.
19781978
1979+
## `process.features.typescript`
1980+
1981+
<!-- YAML
1982+
added: REPLACEME
1983+
-->
1984+
1985+
> Stability: 1.0 - Early development
1986+
1987+
* {boolean|string}
1988+
1989+
A value that is `"strip"` if Node.js is run with `--experimental-strip-types`,
1990+
`"transform"` if Node.js is run with `--experimental-transform-types`, and `false` otherwise.
1991+
19791992
## `process.features.uv`
19801993
19811994
<!-- YAML

lib/internal/bootstrap/node.js

+23
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,29 @@ ObjectDefineProperty(process, 'features', {
310310
}
311311

312312
const { emitWarning, emitWarningSync } = require('internal/process/warning');
313+
const { getOptionValue } = require('internal/options');
314+
315+
let kTypeStrippingMode = null;
316+
// This must be a getter, as getOptionValue does not work
317+
// before bootstrapping.
318+
ObjectDefineProperty(process.features, 'typescript', {
319+
__proto__: null,
320+
get() {
321+
if (kTypeStrippingMode === null) {
322+
if (getOptionValue('--experimental-transform-types')) {
323+
kTypeStrippingMode = 'transform';
324+
} else if (getOptionValue('--experimental-strip-types')) {
325+
kTypeStrippingMode = 'strip';
326+
} else {
327+
kTypeStrippingMode = false;
328+
}
329+
}
330+
return kTypeStrippingMode;
331+
},
332+
configurable: true,
333+
enumerable: true,
334+
});
335+
313336
process.emitWarning = emitWarning;
314337
internalBinding('process_methods').setEmitWarningSync(emitWarningSync);
315338

test/es-module/test-typescript.mjs

+28
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,34 @@ test('execute a TypeScript test mocking module', { skip: isWindows && process.ar
353353
strictEqual(result.code, 0);
354354
});
355355

356+
test('expect process.features.typescript to be \'strip\' when --experimental-strip-types', async () => {
357+
const result = await spawnPromisified(process.execPath, [
358+
'--no-warnings',
359+
'--experimental-strip-types',
360+
'-p', 'process.features.typescript',
361+
]);
362+
363+
strictEqual(result.stderr, '');
364+
strictEqual(result.stdout, 'strip\n');
365+
strictEqual(result.code, 0);
366+
});
367+
368+
test('expect process.features.typescript to be \'transform\' when --experimental-transform-types', async () => {
369+
const result = await spawnPromisified(process.execPath, [
370+
'--no-warnings',
371+
'--experimental-transform-types',
372+
'-p', 'process.features.typescript',
373+
]);
374+
375+
strictEqual(result.stderr, '');
376+
strictEqual(result.stdout, 'transform\n');
377+
strictEqual(result.code, 0);
378+
});
379+
380+
test('expect process.features.typescript to be false without type-stripping', async () => {
381+
strictEqual(process.features.typescript, false);
382+
});
383+
356384
test('execute a TypeScript file with union types', async () => {
357385
const result = await spawnPromisified(process.execPath, [
358386
'--experimental-strip-types',

test/parallel/test-process-features.js

+16-14
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,22 @@
33
require('../common');
44
const assert = require('assert');
55

6-
const keys = new Set(Object.keys(process.features));
6+
const actualKeys = new Set(Object.keys(process.features));
7+
const expectedKeys = new Map([
8+
['inspector', ['boolean']],
9+
['debug', ['boolean']],
10+
['uv', ['boolean']],
11+
['ipv6', ['boolean']],
12+
['tls_alpn', ['boolean']],
13+
['tls_sni', ['boolean']],
14+
['tls_ocsp', ['boolean']],
15+
['tls', ['boolean']],
16+
['cached_builtins', ['boolean']],
17+
['typescript', ['boolean', 'string']],
18+
]);
719

8-
assert.deepStrictEqual(keys, new Set([
9-
'inspector',
10-
'debug',
11-
'uv',
12-
'ipv6',
13-
'tls_alpn',
14-
'tls_sni',
15-
'tls_ocsp',
16-
'tls',
17-
'cached_builtins',
18-
]));
20+
assert.deepStrictEqual(actualKeys, new Set(expectedKeys.keys()));
1921

20-
for (const key of keys) {
21-
assert.strictEqual(typeof process.features[key], 'boolean');
22+
for (const [key, expected] of expectedKeys) {
23+
assert.ok(expected.includes(typeof process.features[key]), `typeof process.features.${key} is not one of [${expected.join(', ')}]`);
2224
}

0 commit comments

Comments
 (0)