diff --git a/doc/api/deprecations.md b/doc/api/deprecations.md index 38e1b6efc9aafd..7d2dc63b28b902 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#utilgetcallsitesframecount-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 0b3326e87e209c..d4c437e886b9d1 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(frameCount, 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)`