Skip to content

Commit

Permalink
fix: typeof function (Close #1181)
Browse files Browse the repository at this point in the history
  • Loading branch information
loopingz committed Mar 26, 2022
1 parent 32e8948 commit aaed428
Show file tree
Hide file tree
Showing 6 changed files with 600 additions and 1 deletion.
4 changes: 3 additions & 1 deletion factory/formatter.ts
Expand Up @@ -28,6 +28,7 @@ import { UnknownTypeFormatter } from "../src/TypeFormatter/UnknownTypeFormatter"
import { VoidTypeFormatter } from "../src/TypeFormatter/VoidTypeFormatter";
import { MutableTypeFormatter } from "../src/MutableTypeFormatter";
import { NeverTypeFormatter } from "../src/TypeFormatter/NeverTypeFormatter";
import { FunctionTypeFormatter } from "../src/TypeFormatter/FunctionTypeFormatter";

export type FormatterAugmentor = (
formatter: MutableTypeFormatter,
Expand Down Expand Up @@ -74,7 +75,8 @@ export function createFormatter(config: Config, augmentor?: FormatterAugmentor):
.addTypeFormatter(new ArrayTypeFormatter(circularReferenceTypeFormatter))
.addTypeFormatter(new TupleTypeFormatter(circularReferenceTypeFormatter))
.addTypeFormatter(new UnionTypeFormatter(circularReferenceTypeFormatter))
.addTypeFormatter(new IntersectionTypeFormatter(circularReferenceTypeFormatter));
.addTypeFormatter(new IntersectionTypeFormatter(circularReferenceTypeFormatter))
.addTypeFormatter(new FunctionTypeFormatter());

return circularReferenceTypeFormatter;
}
4 changes: 4 additions & 0 deletions src/NodeParser/TypeofNodeParser.ts
Expand Up @@ -38,6 +38,10 @@ export class TypeofNodeParser implements SubNodeParser {
return this.childNodeParser.createType(valueDec, context);
} else if (ts.isPropertyAssignment(valueDec)) {
return this.childNodeParser.createType(valueDec.initializer, context);
} else if (valueDec.kind === ts.SyntaxKind.FunctionDeclaration) {
if ((<ts.FunctionDeclaration>valueDec).type! !== undefined) {
return this.childNodeParser.createType(<ts.Node>(<ts.FunctionDeclaration>valueDec).type, context);
}
}

throw new LogicError(`Invalid type query "${valueDec.getFullText()}" (ts.SyntaxKind = ${valueDec.kind})`);
Expand Down
29 changes: 29 additions & 0 deletions src/TypeFormatter/FunctionTypeFormatter.ts
@@ -0,0 +1,29 @@
import { Definition } from "../Schema/Definition";
import { SubTypeFormatter } from "../SubTypeFormatter";
import { BaseType } from "../Type/BaseType";
import { FunctionType } from "../Type/FunctionType";

export class FunctionTypeFormatter implements SubTypeFormatter {
public supportsType(type: FunctionType): boolean {
// console.log("MyCustomFormatter Type", type);
return type instanceof FunctionType;
}

public getDefinition(type: FunctionType): Definition {
// Return a custom schema for the function property.
return {
type: "object",
properties: {
isFunction: {
type: "boolean",
const: true,
},
},
};
}

// If this type does NOT HAVE children, generally all you need is:
public getChildren(type: FunctionType): BaseType[] {
return [];
}
}
1 change: 1 addition & 0 deletions test/valid-data-type.test.ts
Expand Up @@ -62,6 +62,7 @@ describe("valid-data-type", () => {
it("type-typeof-class-static-property", assertValidSchema("type-typeof-class-static-property", "MyType"));
it("type-typeof-enum", assertValidSchema("type-typeof-enum", "MyObject"));
it("type-typeof-class", assertValidSchema("type-typeof-class", "MyObject"));
it("type-typeof-function", assertValidSchema("type-typeof-function", "*"));
it("type-keys", assertValidSchema("type-typeof-keys", "MyType"));

it("type-indexed-access-tuple-1", assertValidSchema("type-indexed-access-tuple-1", "MyType"));
Expand Down
3 changes: 3 additions & 0 deletions test/valid-data/type-typeof-function/main.ts
@@ -0,0 +1,3 @@
import { ConnectionOptions } from "node:tls";

export interface Test extends ConnectionOptions {}

0 comments on commit aaed428

Please sign in to comment.