Skip to content

Commit c81c818

Browse files
marco-ippolitoruyadorno
authored andcommitted
module: throw ERR_NO_TYPESCRIPT when compiled without amaro
PR-URL: #55332 Reviewed-By: Richard Lau <rlau@redhat.com> Reviewed-By: Jacob Smith <jacob@frende.me> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
1 parent 907484f commit c81c818

File tree

4 files changed

+30
-4
lines changed

4 files changed

+30
-4
lines changed

doc/api/errors.md

+12
Original file line numberDiff line numberDiff line change
@@ -2343,6 +2343,17 @@ OpenSSL crypto support.
23432343
An attempt was made to use features that require [ICU][], but Node.js was not
23442344
compiled with ICU support.
23452345

2346+
<a id="ERR_NO_TYPESCRIPT"></a>
2347+
2348+
### `ERR_NO_TYPESCRIPT`
2349+
2350+
<!-- YAML
2351+
added: REPLACEME
2352+
-->
2353+
2354+
An attempt was made to use features that require [Native TypeScript support][], but Node.js was not
2355+
compiled with TypeScript support.
2356+
23462357
<a id="ERR_OPERATION_FAILED"></a>
23472358

23482359
### `ERR_OPERATION_FAILED`
@@ -4112,6 +4123,7 @@ An error occurred trying to allocate memory. This should never happen.
41124123
[ICU]: intl.md#internationalization-support
41134124
[JSON Web Key Elliptic Curve Registry]: https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
41144125
[JSON Web Key Types Registry]: https://www.iana.org/assignments/jose/jose.xhtml#web-key-types
4126+
[Native TypeScript support]: typescript.md#type-stripping
41154127
[Node.js error codes]: #nodejs-error-codes
41164128
[Permission Model]: permissions.md#permission-model
41174129
[RFC 7230 Section 3]: https://tools.ietf.org/html/rfc7230#section-3

lib/internal/errors.js

+2
Original file line numberDiff line numberDiff line change
@@ -1605,6 +1605,8 @@ E('ERR_NO_CRYPTO',
16051605
'Node.js is not compiled with OpenSSL crypto support', Error);
16061606
E('ERR_NO_ICU',
16071607
'%s is not supported on Node.js compiled without ICU', TypeError);
1608+
E('ERR_NO_TYPESCRIPT',
1609+
'Node.js is not compiled with TypeScript support', Error);
16081610
E('ERR_OPERATION_FAILED', 'Operation failed: %s', Error, TypeError);
16091611
E('ERR_OUT_OF_RANGE',
16101612
(str, range, input, replaceDefaultBoolean = false) => {

lib/internal/modules/helpers.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const assert = require('internal/assert');
2828

2929
const { Buffer } = require('buffer');
3030
const { getOptionValue } = require('internal/options');
31-
const { setOwnProperty, getLazy } = require('internal/util');
31+
const { assertTypeScript, setOwnProperty, getLazy } = require('internal/util');
3232
const { inspect } = require('internal/util/inspect');
3333

3434
const lazyTmpdir = getLazy(() => require('os').tmpdir());
@@ -328,6 +328,7 @@ const getTypeScriptParsingMode = getLazy(() =>
328328
* @returns {Function} The TypeScript parser function.
329329
*/
330330
const loadTypeScriptParser = getLazy(() => {
331+
assertTypeScript();
331332
const amaro = require('internal/deps/amaro/dist/index');
332333
return amaro.transformSync;
333334
});

lib/internal/util.js

+14-3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ const {
4545
const {
4646
codes: {
4747
ERR_NO_CRYPTO,
48+
ERR_NO_TYPESCRIPT,
4849
ERR_UNKNOWN_SIGNAL,
4950
},
5051
isErrorStackTraceLimitWritable,
@@ -65,6 +66,7 @@ const { getOptionValue } = require('internal/options');
6566
const { encodings } = internalBinding('string_decoder');
6667

6768
const noCrypto = !process.versions.openssl;
69+
const noTypeScript = !process.versions.amaro;
6870

6971
const isWindows = process.platform === 'win32';
7072
const isMacOS = process.platform === 'darwin';
@@ -196,9 +198,17 @@ function assertCrypto() {
196198
throw new ERR_NO_CRYPTO();
197199
}
198200

199-
// Return undefined if there is no match.
200-
// Move the "slow cases" to a separate function to make sure this function gets
201-
// inlined properly. That prioritizes the common case.
201+
function assertTypeScript() {
202+
if (noTypeScript)
203+
throw new ERR_NO_TYPESCRIPT();
204+
}
205+
206+
/**
207+
* Move the "slow cases" to a separate function to make sure this function gets
208+
* inlined properly. That prioritizes the common case.
209+
* @param {unknown} enc
210+
* @returns {string | undefined} Returns undefined if there is no match.
211+
*/
202212
function normalizeEncoding(enc) {
203213
if (enc == null || enc === 'utf8' || enc === 'utf-8') return 'utf8';
204214
return slowCases(enc);
@@ -862,6 +872,7 @@ for (let i = 0; i < encodings.length; ++i)
862872
module.exports = {
863873
getLazy,
864874
assertCrypto,
875+
assertTypeScript,
865876
cachedResult,
866877
convertToValidSignal,
867878
createClassWrapper,

0 commit comments

Comments
 (0)