Files
scriptc/tests/corpus/function-name-const-alias.mjs
T
Chris Tate e506d19cd6 Name destructured builtins, parenthesized functions and util.debuglog as Node does
Reading the name of a function stored in a const followed const aliases to
the definition, but stopped at destructuring: `const { cwd } = process`
gave the binding's name instead of Node's "wrappedCwd". A member
destructured from the process global or a builtin module's namespace now
gives the module table's name for it.

An anonymous function wrapped in parentheses (`const f = (function () {})`)
answered an empty name. As in Node, it takes the name of the binding or
property it initializes through the parentheses.

A builtin read as a property (`util.debuglog.name`) answered the name its
shared adapter was first created under, so after `util.debug` it answered
"debug". The name now comes from the property and the module table, and
util.debug's value is named "debuglog", as in Node.
2026-10-28 21:50:00 -05:00

21 lines
806 B
JavaScript

// The name of a function value read through a const binding: a builtin
// keeps Node's name (including when destructured), an anonymous function or
// arrow takes the binding's name through parentheses, and an alias of a
// named function keeps the function's name.
import * as path from "node:path";
import * as processModule from "node:process";
const { cwd } = process;
const { cwd: renamed } = processModule;
const join = path.join;
const parenthesized = (function () {});
const arrow = (() => 0);
const nested = ((function () {}));
function named() {}
const alias = named;
const aliasOfAlias = alias;
console.log(cwd.name, renamed.name, join.name);
console.log(parenthesized.name, arrow.name, nested.name);
console.log(alias.name, aliasOfAlias.name);
console.log(cwd === process.cwd, typeof cwd());