GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — module: refactor ts parser loading · nodejs/node@1ba2000

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • lib/internal/modules
    • helpers.js
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/internal/modules/helpers.js‎

Copy file name to clipboardExpand all lines: lib/internal/modules/helpers.js
+25-6Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,29 @@ function getBuiltinModule(id) {
300300
return normalizedId ? require(normalizedId) : undefined;
301301
}
302302

303-
let parseTS;
303+
let typeScriptParser;
304304

305-
function lazyLoadTSParser() {
306-
parseTS ??= require('internal/deps/amaro/dist/index').transformSync;
307-
return parseTS;
305+
/**
306+
* Load the TypeScript parser.
307+
* @param {Function} parser - A function that takes a string of TypeScript code
308+
* and returns an object with a `code` property.
309+
* @returns {Function} The TypeScript parser function.
310+
*/
311+
function loadTypeScriptParser(parser) {
312+
if (typeScriptParser) {
313+
return typeScriptParser;
314+
}
315+
316+
if (parser) {
317+
typeScriptParser = parser;
318+
} else {
319+
const amaro = require('internal/deps/amaro/dist/index');
320+
// Default option for Amaro is to perform Type Stripping only.
321+
const defaultOptions = { __proto__: null, mode: 'strip-only' };
322+
// Curry the transformSync function with the default options.
323+
typeScriptParser = (source) => amaro.transformSync(source, defaultOptions);
324+
}
325+
return typeScriptParser;
308326
}
309327

310328
/**
@@ -313,9 +331,10 @@ function lazyLoadTSParser() {
313331
* @returns {string} JavaScript code.
314332
*/
315333
function tsParse(source) {
334+
// TODO(@marco-ippolito) Checking empty string or non string input should be handled in Amaro.
316335
if (!source || typeof source !== 'string') { return ''; }
317-
const transformSync = lazyLoadTSParser();
318-
const { code } = transformSync(source, { __proto__: null, mode: 'strip-only' });
336+
const parse = loadTypeScriptParser();
337+
const { code } = parse(source);
319338
return code;
320339
}
321340

0 commit comments

Comments
0 (0)
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.