Skip to content

fix: return an empty string for unnamed functions - #111

Open
Zhuyuyangyy wants to merge 1 commit into
sinonjs:mainfrom
Zhuyuyangyy:fix/function-name-empty-fallback
Open

Zhuyuyangyy wants to merge 1 commit into
sinonjs:mainfrom
Zhuyuyangyy:fix/function-name-empty-fallback

Conversation

@Zhuyuyangyy

Copy link
Copy Markdown

Problem

functionName documents and generates a string return type, and already returns "" for falsy inputs and stringification errors. However, when neither displayName, name, nor the string-parsing fallback finds a name, it returns undefined.

For example, on the current main:

const functionName = require("./lib/function-name");
functionName(function () { return null; }); // undefined
functionName(() => null);                  // undefined
functionName(class {});                    // undefined

Change

Fall back to "" when string parsing finds no name, without changing the existing displayName → name → string-parsing precedence. Strengthen the existing unrecognizable-name test to assert the return value, and cover real anonymous functions, arrow functions, and classes.

Validation

Local Node.js v24.19.0:

  • The final regression tests fail against the unmodified base in four cases (undefined instead of "").
  • npm run test-check-coverage: 61 passing, 100% statements, branches, functions, and lines.
  • npm run lint: exit 0; the same 14 pre-existing JSDoc warnings as the unmodified base, no errors.
  • npm run prettier:check: passed.
  • npm run build: passed; declaration remains Function → string.
  • The changed module's regressions also pass with the real Node flag --disable-proto=throw, using Node's built-in assertions.

Extra-check limitation: running the entire suite with the real Node --disable-proto=throw flag fails while loading the lockfile's nested @sinonjs/commons@1.8.3 through @sinonjs/referee-sinon, before this module or its tests load. Importing that test dependency alone reproduces the failure. The workflow's literal npm test --disable-proto=throw completes, but npm 11 warns that it treats the flag as unknown configuration, so that is not counted as real flag validation. Dependency/CI changes are intentionally out of scope.

This focused change and its tests were prepared and checked with AI assistance; no independent human review is being claimed.

Keep the string return contract when name inference finds no match. Strengthen the existing no-name test and cover anonymous functions, arrow functions, and classes without changing name precedence.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant