current commit
260813.7 · util: preserve function names without source map names
'use strict';

require('../common');
const assert = require('node:assert');
const { spawnSync } = require('node:child_process');
const { getCallSites } = require('node:util');
const fixtures = require('../common/fixtures');

// mapCallSite falls back to the original call site when no source map is
// registered for the current file.
{
  const withoutMap = getCallSites(1);
  const withMap = getCallSites(1, { sourceMap: true });
  // scriptName must be identical — no source map registered for this test file
  assert.strictEqual(withMap[0].scriptName, withoutMap[0].scriptName);
  // Both calls report the actual line in this file (they differ by one because
  // they are on consecutive source lines, which is expected).
  assert.ok(typeof withMap[0].lineNumber === 'number');
  assert.ok(typeof withMap[0].columnNumber === 'number');
}

// reconstructCallSite remaps call sites to the original source when
// --enable-source-maps is active and a source map is found.
{
  const file = fixtures.path('source-map', 'get-call-sites-mapped.js');
  const { status, stderr, stdout } = spawnSync(
    process.execPath,
    ['--enable-source-maps', file],
  );
  assert.strictEqual(status, 0, stderr.toString());
  const callSite = JSON.parse(stdout.toString());
  // scriptName should point to the original (unmapped) source file
  assert.ok(
    callSite.scriptName.endsWith('get-call-sites-original.js'),
    `expected original source in scriptName, got "${callSite.scriptName}"`,
  );
  // lineNumber and columnNumber should reflect the original source position
  assert.strictEqual(callSite.lineNumber, 11);
  assert.strictEqual(callSite.columnNumber, 1);
}

// A source map may omit names, so preserve the function name from the call site.
{
  const file = fixtures.path('source-map', 'get-call-sites-function-name-mapped.js');
  const { status, stderr, stdout } = spawnSync(
    process.execPath,
    ['--enable-source-maps', file],
  );
  assert.strictEqual(status, 0, stderr.toString());
  const callSite = JSON.parse(stdout.toString());
  assert.strictEqual(callSite.functionName, 'foo');
}

// Without --enable-source-maps the generated file path is preserved.
{
  const file = fixtures.path('source-map', 'get-call-sites-mapped.js');
  const { status, stderr, stdout } = spawnSync(process.execPath, [file]);
  assert.strictEqual(status, 0, stderr.toString());
  const callSite = JSON.parse(stdout.toString());
  assert.ok(
    callSite.scriptName.endsWith('get-call-sites-mapped.js'),
    `expected generated file in scriptName, got "${callSite.scriptName}"`,
  );
}