Skip to content

Commit

Permalink
process: add process.features.typescript
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
RedYetiDev authored and tpoisseau committed Nov 21, 2024
1 parent 9ebd382 commit 5baff7f
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
13 changes: 13 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
28 changes: 28 additions & 0 deletions test/es-module/test-typescript.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
30 changes: 16 additions & 14 deletions test/parallel/test-process-features.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')}]`);
}

0 comments on commit 5baff7f

Please sign in to comment.