diff --git a/src/handlers/union.ts b/src/handlers/union.ts index 963ee2e..8dfe997 100644 --- a/src/handlers/union.ts +++ b/src/handlers/union.ts @@ -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; @@ -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); } diff --git a/src/webpack/index.js b/src/webpack/index.js index 40cd7c1..b9f2772 100644 --- a/src/webpack/index.js +++ b/src/webpack/index.js @@ -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) {