Skip to content

Commit

Permalink
fix: Static properties of Error class incorrectly converted
Browse files Browse the repository at this point in the history
Closes #1541
Closes #572
  • Loading branch information
Gerrit0 committed Mar 22, 2021
1 parent 9a627d8 commit fbc5966
Show file tree
Hide file tree
Showing 4 changed files with 545 additions and 284 deletions.
6 changes: 6 additions & 0 deletions src/lib/converter/context.ts
Expand Up @@ -83,6 +83,9 @@ export class Context {
*/
exportSymbol?: ts.Symbol;

/** @internal */
shouldBeStatic = false;

private convertingTypeNode = false;

/**
Expand Down Expand Up @@ -190,6 +193,9 @@ export class Context {
nameOverride ?? exportSymbol?.name ?? symbol?.name ?? "unknown"
);
const reflection = new DeclarationReflection(name, kind, this.scope);
if (this.shouldBeStatic) {
reflection.setFlag(ReflectionFlag.Static);
}
reflection.escapedName = symbol?.escapedName;

this.addChild(reflection);
Expand Down
15 changes: 4 additions & 11 deletions src/lib/converter/symbols.ts
Expand Up @@ -424,6 +424,7 @@ function convertClassOrInterface(
classDeclaration
);

reflectionContext.shouldBeStatic = true;
for (const prop of context.checker.getPropertiesOfType(staticType)) {
// Don't convert namespace members, or the prototype here.
if (
Expand All @@ -433,6 +434,7 @@ function convertClassOrInterface(
continue;
convertSymbol(reflectionContext, prop);
}
reflectionContext.shouldBeStatic = false;

const constructMember = new DeclarationReflection(
"constructor",
Expand Down Expand Up @@ -918,16 +920,7 @@ function setModifiers(
ReflectionFlag.Abstract,
hasAllFlags(modifiers, ts.ModifierFlags.Abstract)
);
reflection.setFlag(
ReflectionFlag.Static,
hasAllFlags(modifiers, ts.ModifierFlags.Static)
);

// JS users can create "classes" by adding properties to functions. See GH1481.
if (
reflection.parent?.kind == ReflectionKind.Class &&
ts.isPropertyAccessExpression(declaration)
) {
reflection.setFlag(ReflectionFlag.Static);
}
// ReflectionFlag.Static happens when constructing the reflection.
// We don't have sufficient information here to determine if it ought to be static.
}
12 changes: 12 additions & 0 deletions src/test/converter/inheritance/mergable-class.ts
@@ -0,0 +1,12 @@
export interface MyCtor {
staticProp: 1;
new (): My;
}

export interface My {
instanceProp: 1;
}

export declare const My: MyCtor;

export class MySubClass extends My {}

0 comments on commit fbc5966

Please sign in to comment.