Skip to content

Commit 4050f68

Browse files
Aviv Kellertargos
Aviv Keller
authored andcommitted
process: add process.features.typescript
PR-URL: #54295 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 8eae0d3 commit 4050f68

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
@@ -1968,6 +1968,19 @@ added: v0.5.3
19681968
19691969
A boolean value that is `true` if the current Node.js build includes support for SNI in TLS.
19701970
1971+
## `process.features.typescript`
1972+
1973+
<!-- YAML
1974+
added: REPLACEME
1975+
-->
1976+
1977+
> Stability: 1.0 - Early development
1978+
1979+
* {boolean|string}
1980+
1981+
A value that is `"strip"` if Node.js is run with `--experimental-strip-types`,
1982+
`"transform"` if Node.js is run with `--experimental-transform-types`, and `false` otherwise.
1983+
19711984
## `process.features.uv`
19721985
19731986
<!-- YAML

lib/internal/bootstrap/node.js

+23
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,29 @@ ObjectDefineProperty(process, 'features', {
319319
}
320320

321321
const { emitWarning, emitWarningSync } = require('internal/process/warning');
322+
const { getOptionValue } = require('internal/options');
323+
324+
let kTypeStrippingMode = null;
325+
// This must be a getter, as getOptionValue does not work
326+
// before bootstrapping.
327+
ObjectDefineProperty(process.features, 'typescript', {
328+
__proto__: null,
329+
get() {
330+
if (kTypeStrippingMode === null) {
331+
if (getOptionValue('--experimental-transform-types')) {
332+
kTypeStrippingMode = 'transform';
333+
} else if (getOptionValue('--experimental-strip-types')) {
334+
kTypeStrippingMode = 'strip';
335+
} else {
336+
kTypeStrippingMode = false;
337+
}
338+
}
339+
return kTypeStrippingMode;
340+
},
341+
configurable: true,
342+
enumerable: true,
343+
});
344+
322345
process.emitWarning = emitWarning;
323346
internalBinding('process_methods').setEmitWarningSync(emitWarningSync);
324347

test/es-module/test-typescript.mjs

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

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