Skip to content
Merged
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
21 changes: 19 additions & 2 deletions src/handlers/union.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,19 @@ function findDiscriminant(branches: ts.Type[], checker: ts.TypeChecker): string
const valuesAreAllLiteral = branches.every((branch) => {
const symbol = checker.getPropertyOfType(branch, propName);
if (!symbol) return false;
const propType = checker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration ?? branch.symbol.valueDeclaration!);
// Prefer a location (the property's own declaration, falling back to
// the branch type's) so getTypeOfSymbolAtLocation can resolve
// location-dependent constructs (e.g. `this` types) correctly. But
// a branch that isn't a plain named interface — an intersection or
// a resolved indexed-access/mapped type, both common when a prop's
// fields are typed via `OtherProps['field']` — has no `.symbol` at
// all, and a property reached through it may have no
// `valueDeclaration` either. In that case fall back to the
// location-less lookup rather than crash.
const location = symbol.valueDeclaration ?? branch.symbol?.valueDeclaration;
const propType = location
? checker.getTypeOfSymbolAtLocation(symbol, location)
: checker.getTypeOfSymbol(symbol);
return propType.isLiteral();
});
if (valuesAreAllLiteral) return propName;
Expand All @@ -81,6 +93,11 @@ function findDiscriminant(branches: ts.Type[], checker: ts.TypeChecker): string
function literalPropValue(branch: ts.Type, propName: string, checker: ts.TypeChecker): string {
const symbol = checker.getPropertyOfType(branch, propName);
if (!symbol) return '';
const propType = checker.getTypeOfSymbolAtLocation(symbol, symbol.valueDeclaration ?? branch.symbol.valueDeclaration!);
// See the matching fallback in findDiscriminant above for why a plain
// `branch.symbol.valueDeclaration!` isn't safe here.
const location = symbol.valueDeclaration ?? branch.symbol?.valueDeclaration;
const propType = location
? checker.getTypeOfSymbolAtLocation(symbol, location)
: checker.getTypeOfSymbol(symbol);
return checker.typeToString(propType);
}
21 changes: 16 additions & 5 deletions src/webpack/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,25 @@ export default function webpackLoader(source) {
// their real prop types differ (e.g. an internal-only export vs. its
// public-facing wrapper) — a real per-export fix needs core to
// resolve props per named export, not just once per file.
// Emit the (potentially large, deeply-nested-object-props-heavy)
// payload as a single shared `const` rather than inlining
// JSON.stringify(documentation) once per name — interpolating it into
// each per-name injection would re-embed the full literal N times in
// the emitted source for a file with N plausible exports, which for a
// component with a few levels of nested/union props can add hundreds
// of KB per copy and is enough on its own to push a file over Babel's
// codegen deoptimization threshold. Only `displayName` varies per
// name, so each injection spreads the shared base and overrides just
// that.
const baseVarName = '__reactPropsParserDocgenInfo';
const injections = componentNames
.map((name) => {
const docgenInfo = { ...documentation, displayName: name };
return `try { ${name}.__docgenInfo = ${JSON.stringify(docgenInfo)}; } catch (e) {}`;
})
.map(
(name) =>
`try { ${name}.__docgenInfo = { ...${baseVarName}, displayName: ${JSON.stringify(name)} }; } catch (e) {}`
)
.join('\n');

return `${source}\n${injections}\n`;
return `${source}\nconst ${baseVarName} = ${JSON.stringify(documentation)};\n${injections}\n`;
}

function findComponentNames(code) {
Expand Down
Loading