From cf1e9eaa2e92f64b30c0bc9ac02177215afddc35 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/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 ++++++++++++- 5 files changed, 193 insertions(+), 5 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/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)`