Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .changeset/brave-ticks-save.md
Original file line number Diff line number Diff line change
@@ -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)).
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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 <div />;
}
`;

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<ClassDeclaration>(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) => <div />
Foo.defaultProps = { tpl: \`Save\` };
`;

defaultPropsHandler(
documentation,
parse
.statement(src)
.get('declarations.0.init') as NodePath<ArrowFunctionExpression>,
);
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,
}) => <div />
`;

defaultPropsHandler(
documentation,
parse.expressionLast<ArrowFunctionExpression>(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`,',
'}) => <div />',
].join('\n');

defaultPropsHandler(
documentation,
parse.expressionLast<ArrowFunctionExpression>(src),
);
expect(documentation.descriptors).toEqual({
tpl: {
defaultValue: {
value: '`line1\n line2\n line3`',
computed: false,
},
},
});
});
});
});
4 changes: 4 additions & 0 deletions packages/react-docgen/src/handlers/defaultPropsHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down