process: add process.features.typescript

PR-URL: https://github.com/nodejs/node/pull/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>
This commit is contained in:
Aviv Keller 2024-10-05 03:08:11 -04:00 committed by GitHub
parent 32efeea0c0
commit bbdfeebd9e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 80 additions and 14 deletions

View File

@ -1976,6 +1976,19 @@ added: v0.5.3
A boolean value that is `true` if the current Node.js build includes support for SNI in TLS.
## `process.features.typescript`
<!-- YAML
added: REPLACEME
-->
> Stability: 1.0 - Early development
* {boolean|string}
A value that is `"strip"` if Node.js is run with `--experimental-strip-types`,
`"transform"` if Node.js is run with `--experimental-transform-types`, and `false` otherwise.
## `process.features.uv`
<!-- YAML

View File

@ -310,6 +310,29 @@ ObjectDefineProperty(process, 'features', {
}
const { emitWarning, emitWarningSync } = require('internal/process/warning');
const { getOptionValue } = require('internal/options');
let kTypeStrippingMode = null;
// This must be a getter, as getOptionValue does not work
// before bootstrapping.
ObjectDefineProperty(process.features, 'typescript', {
__proto__: null,
get() {
if (kTypeStrippingMode === null) {
if (getOptionValue('--experimental-transform-types')) {
kTypeStrippingMode = 'transform';
} else if (getOptionValue('--experimental-strip-types')) {
kTypeStrippingMode = 'strip';
} else {
kTypeStrippingMode = false;
}
}
return kTypeStrippingMode;
},
configurable: true,
enumerable: true,
});
process.emitWarning = emitWarning;
internalBinding('process_methods').setEmitWarningSync(emitWarningSync);

View File

@ -353,6 +353,34 @@ test('execute a TypeScript test mocking module', { skip: isWindows && process.ar
strictEqual(result.code, 0);
});
test('expect process.features.typescript to be \'strip\' when --experimental-strip-types', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-strip-types',
'-p', 'process.features.typescript',
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, 'strip\n');
strictEqual(result.code, 0);
});
test('expect process.features.typescript to be \'transform\' when --experimental-transform-types', async () => {
const result = await spawnPromisified(process.execPath, [
'--no-warnings',
'--experimental-transform-types',
'-p', 'process.features.typescript',
]);
strictEqual(result.stderr, '');
strictEqual(result.stdout, 'transform\n');
strictEqual(result.code, 0);
});
test('expect process.features.typescript to be false without type-stripping', async () => {
strictEqual(process.features.typescript, false);
});
test('execute a TypeScript file with union types', async () => {
const result = await spawnPromisified(process.execPath, [
'--experimental-strip-types',

View File

@ -3,20 +3,22 @@
require('../common');
const assert = require('assert');
const keys = new Set(Object.keys(process.features));
const actualKeys = new Set(Object.keys(process.features));
const expectedKeys = new Map([
['inspector', ['boolean']],
['debug', ['boolean']],
['uv', ['boolean']],
['ipv6', ['boolean']],
['tls_alpn', ['boolean']],
['tls_sni', ['boolean']],
['tls_ocsp', ['boolean']],
['tls', ['boolean']],
['cached_builtins', ['boolean']],
['typescript', ['boolean', 'string']],
]);
assert.deepStrictEqual(keys, new Set([
'inspector',
'debug',
'uv',
'ipv6',
'tls_alpn',
'tls_sni',
'tls_ocsp',
'tls',
'cached_builtins',
]));
assert.deepStrictEqual(actualKeys, new Set(expectedKeys.keys()));
for (const key of keys) {
assert.strictEqual(typeof process.features[key], 'boolean');
for (const [key, expected] of expectedKeys) {
assert.ok(expected.includes(typeof process.features[key]), `typeof process.features.${key} is not one of [${expected.join(', ')}]`);
}