From d35cde624f5001e5c09b0bb75b3d34b94a859e4e Mon Sep 17 00:00:00 2001 From: Marco Ippolito Date: Mon, 4 Nov 2024 16:06:47 +0000 Subject: [PATCH] util: add sourcemap support to getCallSites PR-URL: https://github.com/nodejs/node/pull/55589 Fixes: https://github.com/nodejs/node/issues/55109 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Rafael Gonzaga Reviewed-By: Chengzhong Wu --- doc/api/deprecations.md | 2 +- doc/api/util.md | 34 +++++++- lib/util.js | 84 ++++++++++++++++++- .../ts/test-get-callsite-explicit.ts | 10 +++ .../typescript/ts/test-get-callsite.ts | 10 +++ test/parallel/test-util-getcallsites.js | 60 ++++++++++++- 6 files changed, 194 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/typescript/ts/test-get-callsite-explicit.ts create mode 100644 test/fixtures/typescript/ts/test-get-callsite.ts diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 38e1b6efc9a..dcba5f65f61 100644 --- a/doc/api/deprecations.md +++ b/doc/api/deprecations.md @@ -3899,7 +3899,7 @@ The `util.getCallSite` API has been removed. Please use [`util.getCallSites()`][ [`url.parse()`]: url.md#urlparseurlstring-parsequerystring-slashesdenotehost [`url.resolve()`]: url.md#urlresolvefrom-to [`util._extend()`]: util.md#util_extendtarget-source -[`util.getCallSites()`]: util.md#utilgetcallsitesframecount +[`util.getCallSites()`]: util.md#utilgetcallsitesframecountoroptions-options [`util.getSystemErrorName()`]: util.md#utilgetsystemerrornameerr [`util.inspect()`]: util.md#utilinspectobject-options [`util.inspect.custom`]: util.md#utilinspectcustom diff --git a/doc/api/util.md b/doc/api/util.md index 0b3326e87e2..6b7855e47ff 100644 --- a/doc/api/util.md +++ b/doc/api/util.md @@ -364,7 +364,7 @@ util.formatWithOptions({ colors: true }, 'See object %O', { foo: 42 }); // when printed to a terminal. ``` -## `util.getCallSites(frameCount)` +## `util.getCallSites(frameCountOrOptions, [options])` > Stability: 1.1 - Active development @@ -372,8 +372,11 @@ util.formatWithOptions({ colors: true }, 'See object %O', { foo: 42 }); added: v22.9.0 --> -* `frameCount` {number} Number of frames to capture as call site objects. +* `frameCount` {number} Optional number of frames to capture as call site objects. **Default:** `10`. Allowable range is between 1 and 200. +* `options` {Object} Optional + * `sourceMap` {boolean} Reconstruct the original location in the stacktrace from the source-map. + Enabled by default with the flag `--enable-source-maps`. * Returns: {Object\[]} An array of call site objects * `functionName` {string} Returns the name of the function associated with this call site. * `scriptName` {string} Returns the name of the resource that contains the script for the @@ -421,6 +424,33 @@ function anotherFunction() { anotherFunction(); ``` +It is possible to reconstruct the original locations by setting the option `sourceMap` to `true`. +If the source map is not available, the original location will be the same as the current location. +When the `--enable-source-maps` flag is enabled, for example when using `--experimental-transform-types`, +`sourceMap` will be true by default. + +```ts +import util from 'node:util'; + +interface Foo { + foo: string; +} + +const callSites = util.getCallSites({ sourceMap: true }); + +// With sourceMap: +// Function Name: '' +// Script Name: example.js +// Line Number: 7 +// Column Number: 26 + +// Without sourceMap: +// Function Name: '' +// Script Name: example.js +// Line Number: 2 +// Column Number: 26 +``` + ## `util.getSystemErrorName(err)`