diff --git a/doc/api/util.md b/doc/api/util.md index d46092a815774d..620d985b0da836 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 @@ -376,8 +376,11 @@ changes: description: The API is renamed from `util.getCallSite` to `util.getCallSites()`. --> -* `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 @@ -425,6 +428,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)`