Skip to content

fix: keep template-literal default values in defaultPropsHandler - #1206

Open
ItsShivam05 wants to merge 1 commit into
reactjs:mainfrom
ItsShivam05:fix/template-literal-default-values
Open

ItsShivam05 wants to merge 1 commit into
reactjs:mainfrom
ItsShivam05:fix/template-literal-default-values

Conversation

@ItsShivam05

Copy link
Copy Markdown

Fixes #1203

Problem

getDefaultValue() in defaultPropsHandler handles literals via path.isLiteral() and reads path.node.extra?.raw. Babel includes TemplateLiteral in the Literal alias, but template literals have no extra.raw, so the value came back undefined and the default was dropped:

  • function A({ a = 1, tpl = \Save` }): tpldisappeared from the output entirely (untyped props with an empty descriptor are skipped byDocumentation.toObject()`)
  • typed props (tpl?: string) lost their defaultValue
  • the same for template literals in class and function defaultProps

Fix

A path.isTemplateLiteral() branch before the generic isLiteral() one, using path.getSource(). I used the exact source rather than printValue, because printValue re-indents lines after the first one, and inside a template literal that whitespace is part of the string value.

computed is false, consistent with how 'Hi ' + name and other composite expressions are already reported (computed only looks at the outermost expression).

Tests

New template literals block in defaultPropsHandler-test.ts (all 6 fail before the fix):

  • finds template literal defaults in destructured params
  • finds template literal defaults for typed props
  • finds template literal defaults in class defaultProps
  • finds template literal defaults in function component defaultProps
  • treats template literals with expressions like other composite expressions (not computed)
  • preserves whitespace in multi-line template literal defaults

They use toEqual rather than new snapshots, so they assert the expected output instead of recording whatever the code produces.

  • pnpm test: all passing (react-docgen 1520, core 23, babel 2, typescript 156, hermes 111, cli 60)
  • pnpm test:coverage: thresholds met
  • pnpm build (tsc): 7/7 packages

Changeset added (patch for react-docgen). This is based on main; happy to open a backport to 8.x if you'd like.

@netlify

netlify Bot commented Nov 21, 2026

Copy link
Copy Markdown

👷 Deploy request for react-docgen pending review.

Visit the deploys page to approve it

Name Link
🔨 Latest commit 8afa853

@changeset-bot

changeset-bot Bot commented Nov 21, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 8afa853

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
react-docgen Patch
@react-docgen/cli Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

This branch has not been deployed

No deployments
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