From 97774311d2de9b6540a7f2602cb66172c979ab49 Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Mon, 28 Oct 2024 10:52:45 -0500 Subject: [PATCH] module: add module.mapCallSite() --- doc/api/module.md | 32 ++++++++++++++ lib/internal/source_map/source_map_cache.js | 46 +++++++++++++++++++- lib/module.js | 4 +- test/es-module/test-module-get-callsite.mjs | 30 +++++++++++++ test/fixtures/typescript/ts/test-callsite.ts | 9 ++++ 5 files changed, 118 insertions(+), 3 deletions(-) create mode 100644 test/es-module/test-module-get-callsite.mjs create mode 100644 test/fixtures/typescript/ts/test-callsite.ts diff --git a/doc/api/module.md b/doc/api/module.md index 1d9e0d6aa11829..a7688b56ea62f0 100644 --- a/doc/api/module.md +++ b/doc/api/module.md @@ -317,6 +317,37 @@ isBuiltin('fs'); // true isBuiltin('wss'); // false ``` +### `module.mapCallSite(callSite)` + + + +> Stability: 1.1 - Active development + +* `callSite` {Object | Array} A [CallSite][] object or an array of CallSite objects. +* Returns: {Object | Array} The original source code location(s) for the given CallSite object(s). + +Reconstructs the original source code location from a [CallSite][] object through the source map. + +```mjs +import { mapCallSite } from 'node:module'; +import { getCallSite } from 'node:util'; + +mapCallSite(getCallSite()); // Reconstructs the original source code location for the whole stack + +mapCallSite(getCallSite()[0]); // Reconstructs the original source code location for the first frame +``` + +```cjs +const { mapCallSite } = require('node:module'); +const { getCallSite } = require('node:util'); + +mapCallSite(getCallSite()); // Reconstructs the original source code location for the whole stack + +mapCallSite(getCallSite()[0]); // Reconstructs the original source code location for the first frame +``` + ### `module.register(specifier[, parentURL][, options])`