Skip to content

Commit

Permalink
fix: Crash when converting @types/ws
Browse files Browse the repository at this point in the history
Closes #1463.
  • Loading branch information
Gerrit0 committed Jan 16, 2021
1 parent 4f4d85c commit b8071c6
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 35 deletions.
59 changes: 40 additions & 19 deletions src/lib/converter/converter.ts
Expand Up @@ -303,7 +303,7 @@ export class Converter extends ChildableComponent<
entryPoints: readonly string[],
entryName: string
) {
const symbol = context.checker.getSymbolAtLocation(node) ?? node.symbol;
const symbol = getSymbolForModuleLike(context, node);
let moduleContext: Context;

if (entryPoints.length === 1) {
Expand All @@ -320,7 +320,7 @@ export class Converter extends ChildableComponent<
moduleContext = context.withScope(reflection);
}

for (const exp of getExports(context, node).filter((exp) =>
for (const exp of getExports(context, node, symbol).filter((exp) =>
isDirectExport(context.resolveAliasedSymbol(exp), node)
)) {
convertSymbol(moduleContext, exp);
Expand All @@ -330,7 +330,11 @@ export class Converter extends ChildableComponent<
}

private convertReExports(moduleContext: Context, node: ts.SourceFile) {
for (const exp of getExports(moduleContext, node).filter(
for (const exp of getExports(
moduleContext,
node,
moduleContext.project.getSymbolFromReflection(moduleContext.scope)
).filter(
(exp) =>
!isDirectExport(moduleContext.resolveAliasedSymbol(exp), node)
)) {
Expand Down Expand Up @@ -399,24 +403,14 @@ function getModuleName(fileName: string, baseDir: string) {
);
}

function getExports(
function getSymbolForModuleLike(
context: Context,
node: ts.SourceFile | ts.ModuleBlock
): ts.Symbol[] {
let symbol = context.checker.getSymbolAtLocation(node) ?? node.symbol;
if (!symbol && ts.isModuleBlock(node)) {
symbol = context.checker.getSymbolAtLocation(node.parent.name);
}

// The generated docs aren't great, but you really ought not be using
// this in the first place... so it's better than nothing.
const exportEq = symbol?.exports?.get("export=" as ts.__String);
if (exportEq) {
return [exportEq];
}
) {
const symbol = context.checker.getSymbolAtLocation(node) ?? node.symbol;

if (symbol) {
return context.checker.getExportsOfModule(symbol);
return symbol;
}

// This is a global file, get all symbols declared in this file...
Expand All @@ -430,7 +424,7 @@ function getExports(
);

// Detect declaration files with declare module "foo" as their only export
// and lift that up one level
// and lift that up one level as the source file symbol
if (
globalSymbols.length === 1 &&
globalSymbols[0]
Expand All @@ -441,8 +435,35 @@ function getExports(
ts.isStringLiteral(declaration.name)
)
) {
return context.checker.getExportsOfModule(globalSymbols[0]);
return globalSymbols[0];
}
}

function getExports(
context: Context,
node: ts.SourceFile | ts.ModuleBlock,
symbol: ts.Symbol | undefined
): ts.Symbol[] {
// The generated docs aren't great, but you really ought not be using
// this in the first place... so it's better than nothing.
const exportEq = symbol?.exports?.get("export=" as ts.__String);
if (exportEq) {
return [exportEq];
}

if (symbol) {
return context.checker
.getExportsOfModule(symbol)
.filter((s) => !hasFlag(s.flags, ts.SymbolFlags.Prototype));
}

// Global file with no inferred top level symbol, get all symbols declared in this file.
const sourceFile = node.getSourceFile();
const globalSymbols = context.checker
.getSymbolsInScope(node, ts.SymbolFlags.ModuleMember)
.filter((s) =>
s.getDeclarations()?.some((d) => d.getSourceFile() === sourceFile)
);

return globalSymbols;
}
Expand Down
2 changes: 0 additions & 2 deletions src/lib/converter/symbols.ts
Expand Up @@ -478,8 +478,6 @@ function convertProperty(
nameOverride?: string
) {
const declarations = symbol.getDeclarations() ?? [];
const parentSymbol = context.project.getSymbolFromReflection(context.scope);
assert(parentSymbol);

// Don't do anything if we inherited this property and it is private.
if (
Expand Down
14 changes: 7 additions & 7 deletions src/test/converter/class/specs-with-lump-categories.json
Expand Up @@ -906,12 +906,12 @@
],
"categories": [
{
"title": "AnotherTest",
"children": [
32,
30,
28
]
"title": "AnotherTest",
"children": [
32,
30,
28
]
},
{
"title": "Other",
Expand Down Expand Up @@ -3553,4 +3553,4 @@
]
}
]
}
}
14 changes: 7 additions & 7 deletions src/test/converter/class/specs.json
Expand Up @@ -904,12 +904,12 @@
],
"categories": [
{
"title": "AnotherTest",
"children": [
32,
30,
28
]
"title": "AnotherTest",
"children": [
32,
30,
28
]
},
{
"title": "Other",
Expand Down Expand Up @@ -3549,4 +3549,4 @@
]
}
]
}
}

0 comments on commit b8071c6

Please sign in to comment.