11'use strict' ;
22
3+ type Options = import ( '../../../deps/amaro/lib/wasm' ) . Options ;
4+ type TransformOutput = import ( '../../../deps/amaro/lib/wasm' ) . TransformOutput ;
5+ type Mode = 'strip-only' | 'transform' ;
6+
37const {
48 ObjectPrototypeHasOwnProperty,
59} = primordials ;
@@ -9,11 +13,13 @@ const {
913 validateObject,
1014 validateString,
1115} = require ( 'internal/validators' ) ;
12- const { assertTypeScript,
13- emitExperimentalWarning,
14- getLazy,
15- isUnderNodeModules,
16- kEmptyObject } = require ( 'internal/util' ) ;
16+ const {
17+ assertTypeScript,
18+ emitExperimentalWarning,
19+ getLazy,
20+ isUnderNodeModules,
21+ kEmptyObject
22+ } = require ( 'internal/util' ) ;
1723const {
1824 ERR_INVALID_TYPESCRIPT_SYNTAX ,
1925 ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING ,
@@ -30,31 +36,26 @@ const {
3036
3137/**
3238 * The TypeScript parsing mode, either 'strip-only' or 'transform'.
33- * @type {function(): TypeScriptMode }
3439 */
35- const getTypeScriptParsingMode = getLazy ( ( ) =>
36- ( getOptionValue ( '--experimental-transform-types' ) ?
37- ( emitExperimentalWarning ( 'Transform Types' ) , 'transform' ) : 'strip-only' ) ,
40+ const getTypeScriptParsingMode : ( ) => Mode = getLazy ( ( ) =>
41+ ( getOptionValue ( '--experimental-transform-types' ) ?
42+ ( emitExperimentalWarning ( 'Transform Types' ) , 'transform' ) : 'strip-only' ) ,
3843) ;
3944
4045/**
4146 * Load the TypeScript parser.
4247 * and returns an object with a `code` property.
43- * @returns {Function } The TypeScript parser function.
4448 */
45- const loadTypeScriptParser = getLazy ( ( ) => {
49+ const loadTypeScriptParser : ( ) => ( source : string , options : Options ) => TransformOutput = getLazy ( ( ) => {
4650 assertTypeScript ( ) ;
47- const amaro = require ( 'internal/deps/amaro/dist/index' ) ;
51+ const amaro : { transformSync : ( source : string , options : Options ) => TransformOutput } = require ( 'internal/deps/amaro/dist/index' ) ;
4852 return amaro . transformSync ;
4953} ) ;
5054
5155/**
52- *
53- * @param {string } source the source code
54- * @param {object } options the options to pass to the parser
55- * @returns {TransformOutput } an object with a `code` property.
56+ * Parse TypeScript source code.
5657 */
57- function parseTypeScript ( source , options ) {
58+ function parseTypeScript ( source : string , options : Options ) : TransformOutput {
5859 const parse = loadTypeScriptParser ( ) ;
5960 try {
6061 return parse ( source , options ) ;
@@ -82,24 +83,18 @@ function parseTypeScript(source, options) {
8283}
8384
8485/**
85- *
86- * @param {Error } error the error to decorate: ERR_INVALID_TYPESCRIPT_SYNTAX, ERR_UNSUPPORTED_TYPESCRIPT_SYNTAX
87- * @param {object } amaroError the error object from amaro
88- * @returns {Error } the decorated error
86+ * Decorate an error with a code snippet from the Amaro error.
8987 */
90- function decorateErrorWithSnippet ( error , amaroError ) {
88+ function decorateErrorWithSnippet ( error : Error , amaroError : any ) : Error {
9189 const errorHints = `${ amaroError . filename } :${ amaroError . startLine } \n${ amaroError . snippet } ` ;
9290 error . stack = `${ errorHints } \n${ error . stack } ` ;
9391 return error ;
9492}
9593
9694/**
9795 * Performs type-stripping to TypeScript source code.
98- * @param {string } code TypeScript code to parse.
99- * @param {TransformOptions } options The configuration for type stripping.
100- * @returns {string } The stripped TypeScript code.
10196 */
102- function stripTypeScriptTypes ( code , options = kEmptyObject ) {
97+ function stripTypeScriptTypes ( code : string , options = kEmptyObject ) : string {
10398 emitExperimentalWarning ( 'stripTypeScriptTypes' ) ;
10499 validateString ( code , 'code' ) ;
105100 validateObject ( options , 'options' ) ;
@@ -127,22 +122,11 @@ function stripTypeScriptTypes(code, options = kEmptyObject) {
127122 } ) ;
128123}
129124
130- /**
131- * @typedef {'strip-only' | 'transform' } TypeScriptMode
132- * @typedef {object } TypeScriptOptions
133- * @property {TypeScriptMode } mode Mode.
134- * @property {boolean } sourceMap Whether to generate source maps.
135- * @property {string|undefined } filename Filename.
136- */
137-
138125/**
139126 * Processes TypeScript code by stripping types or transforming.
140127 * Handles source maps if needed.
141- * @param {string } code TypeScript code to process.
142- * @param {TypeScriptOptions } options The configuration object.
143- * @returns {string } The processed code.
144128 */
145- function processTypeScriptCode ( code , options ) {
129+ function processTypeScriptCode ( code : string , options : Options ) : string {
146130 const { code : transformedCode , map } = parseTypeScript ( code , options ) ;
147131
148132 if ( map ) {
@@ -158,11 +142,8 @@ function processTypeScriptCode(code, options) {
158142
159143/**
160144 * Get the type enum used for compile cache.
161- * @param {TypeScriptMode } mode Mode of transpilation.
162- * @param {boolean } sourceMap Whether source maps are enabled.
163- * @returns {number }
164145 */
165- function getCachedCodeType ( mode , sourceMap ) {
146+ function getCachedCodeType ( mode : Mode , sourceMap : boolean ) : number {
166147 if ( mode === 'transform' ) {
167148 if ( sourceMap ) { return kTransformedTypeScriptWithSourceMaps ; }
168149 return kTransformedTypeScript ;
@@ -173,11 +154,8 @@ function getCachedCodeType(mode, sourceMap) {
173154/**
174155 * Performs type-stripping to TypeScript source code internally.
175156 * It is used by internal loaders.
176- * @param {string } source TypeScript code to parse.
177- * @param {string } filename The filename of the source code.
178- * @returns {TransformOutput } The stripped TypeScript code.
179157 */
180- function stripTypeScriptModuleTypes ( source , filename ) {
158+ function stripTypeScriptModuleTypes ( source : string , filename : string ) : string {
181159 assert ( typeof source === 'string' ) ;
182160 if ( isUnderNodeModules ( filename ) ) {
183161 throw new ERR_UNSUPPORTED_NODE_MODULES_TYPE_STRIPPING ( filename ) ;
@@ -200,7 +178,7 @@ function stripTypeScriptModuleTypes(source, filename) {
200178 return cached . transpiled ;
201179 }
202180
203- const options = {
181+ const options : Options = {
204182 mode,
205183 sourceMap,
206184 filename,
@@ -219,12 +197,9 @@ function stripTypeScriptModuleTypes(source, filename) {
219197}
220198
221199/**
222- *
223- * @param {string } code The compiled code.
224- * @param {string } sourceMap The source map.
225- * @returns {string } The code with the source map attached.
200+ * Add a source map to compiled code.
226201 */
227- function addSourceMap ( code , sourceMap ) {
202+ function addSourceMap ( code : string , sourceMap : string ) : string {
228203 // The base64 encoding should be https://datatracker.ietf.org/doc/html/rfc4648#section-4,
229204 // not base64url https://datatracker.ietf.org/doc/html/rfc4648#section-5. See data url
230205 // spec https://tools.ietf.org/html/rfc2397#section-2.
0 commit comments