From 8afa853a736aa9558f064f298a20afe7d11a7e46 Mon Sep 17 00:00:00 2001 From: ItsShivam05 Date: Sat, 21 Nov 2026 17:56:49 +0530 Subject: [PATCH] fix: keep template-literal default values in defaultPropsHandler --- .changeset/brave-ticks-save.md | 8 ++ .../__tests__/defaultPropsHandler-test.ts | 110 +++++++++++++++++- .../src/handlers/defaultPropsHandler.ts | 4 + 3 files changed, 121 insertions(+), 1 deletion(-) create mode 100644 .changeset/brave-ticks-save.md diff --git a/.changeset/brave-ticks-save.md b/.changeset/brave-ticks-save.md new file mode 100644 index 00000000000..28f6cbf09d3 --- /dev/null +++ b/.changeset/brave-ticks-save.md @@ -0,0 +1,8 @@ +--- +'react-docgen': patch +--- + +Fix `defaultPropsHandler` dropping default values that are template literals, +both in destructured parameters and in `defaultProps`. They are now reported +with their source text as the value and `computed: false` +([#1203](https://github.com/reactjs/react-docgen/issues/1203)). diff --git a/packages/react-docgen/src/handlers/__tests__/defaultPropsHandler-test.ts b/packages/react-docgen/src/handlers/__tests__/defaultPropsHandler-test.ts index 5675a16494e..97eb2d36fed 100644 --- a/packages/react-docgen/src/handlers/__tests__/defaultPropsHandler-test.ts +++ b/packages/react-docgen/src/handlers/__tests__/defaultPropsHandler-test.ts @@ -7,7 +7,7 @@ import type { ObjectExpression, VariableDeclaration, } from '@babel/types'; -import { parse, makeMockImporter } from '../../../tests/utils'; +import { parse, parseTypescript, makeMockImporter } from '../../../tests/utils'; import DocumentationBuilder from '../../Documentation'; import type DocumentationMock from '../../__mocks__/Documentation'; import defaultPropsHandler from '../defaultPropsHandler.js'; @@ -699,4 +699,112 @@ describe('defaultPropsHandler', () => { expect(documentation.descriptors).toMatchSnapshot(); }); }); + + describe('template literals', () => { + test('finds template literal defaults in destructured params', () => { + const src = ` + function A({ a = 1, tpl = \`Save\` }) { + return
; + } + `; + + defaultPropsHandler(documentation, parse.statement(src)); + expect(documentation.descriptors).toEqual({ + a: { defaultValue: { value: '1', computed: false } }, + tpl: { defaultValue: { value: '`Save`', computed: false } }, + }); + }); + + test('finds template literal defaults for typed props', () => { + const src = ` + type Props = { tpl?: string; n?: number }; + function A({ tpl = \`Save\`, n = 1 }: Props) { + return null; + } + `; + + defaultPropsHandler(documentation, parseTypescript.statementLast(src)); + expect(documentation.descriptors).toEqual({ + tpl: { defaultValue: { value: '`Save`', computed: false } }, + n: { defaultValue: { value: '1', computed: false } }, + }); + }); + + test('finds template literal defaults in class defaultProps', () => { + const src = ` + class Foo { + static defaultProps = { + tpl: \`Save\`, + }; + } + `; + + defaultPropsHandler( + documentation, + parse.statement(src), + ); + expect(documentation.descriptors).toEqual({ + tpl: { defaultValue: { value: '`Save`', computed: false } }, + }); + }); + + test('finds template literal defaults in function component defaultProps', () => { + const src = ` + var Foo = (props) =>
+ Foo.defaultProps = { tpl: \`Save\` }; + `; + + defaultPropsHandler( + documentation, + parse + .statement(src) + .get('declarations.0.init') as NodePath, + ); + expect(documentation.descriptors).toEqual({ + tpl: { defaultValue: { value: '`Save`', computed: false } }, + }); + }); + + test('treats template literals with expressions like other composite expressions (not computed)', () => { + const src = ` + ({ + tpl = \`Hi \${name}\`, + concat = 'Hi ' + name, + }) =>
+ `; + + defaultPropsHandler( + documentation, + parse.expressionLast(src), + ); + expect(documentation.descriptors).toEqual({ + tpl: { defaultValue: { value: '`Hi ${name}`', computed: false } }, + concat: { defaultValue: { value: "'Hi ' + name", computed: false } }, + }); + }); + + test('preserves whitespace in multi-line template literal defaults', () => { + // Built line by line so the significant indentation is explicit + const src = [ + '({', + ' tpl = `line1', + ' line2', + ' line3`,', + '}) =>
', + ].join('\n'); + + defaultPropsHandler( + documentation, + parse.expressionLast(src), + ); + expect(documentation.descriptors).toEqual({ + tpl: { + defaultValue: { + value: '`line1\n line2\n line3`', + computed: false, + }, + }, + }); + }); + }); }); diff --git a/packages/react-docgen/src/handlers/defaultPropsHandler.ts b/packages/react-docgen/src/handlers/defaultPropsHandler.ts index ccf515b9d02..564c473ffab 100644 --- a/packages/react-docgen/src/handlers/defaultPropsHandler.ts +++ b/packages/react-docgen/src/handlers/defaultPropsHandler.ts @@ -29,6 +29,10 @@ function getDefaultValue(path: NodePath): DefaultValueDescriptor | null { defaultValue = `${path.node.value}`; } else if (path.isNullLiteral()) { defaultValue = 'null'; + } else if (path.isTemplateLiteral()) { + // TemplateLiteral is part of Babel's Literal alias but has no extra.raw. + // Use the exact source: whitespace inside it is part of the string value. + defaultValue = path.getSource(); } else if (path.isLiteral()) { defaultValue = path.node.extra?.raw as string; } else {