Skip to content

Commit

Permalink
process: add process.features.typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
RedYetiDev committed Sep 20, 2024
1 parent 8b8fc53 commit 4442540
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 14 deletions.
15 changes: 15 additions & 0 deletions doc/api/process.md
Original file line number Diff line number Diff line change
Expand Up @@ -1982,6 +1982,21 @@ A boolean value that is `true` if the current Node.js build includes support for
***
## `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
21 changes: 21 additions & 0 deletions lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,27 @@ ObjectDefineProperty(process, 'features', {
}

const { emitWarning, emitWarningSync } = require('internal/process/warning');
const { getOptionValue } = require('internal/options');

let kTypeStrippingMode = null;
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,3 +353,31 @@ test('execute a TypeScript test mocking module', { skip: isWindows && process.ar
match(result.stdout, /Hello, TypeScript-CommonJS!/);
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);
});
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 4442540

Please sign in to comment.