Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: typeof function #1182

Merged
merged 2 commits into from Jul 31, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/NodeParser/TypeofNodeParser.ts
Expand Up @@ -7,6 +7,7 @@ import { ObjectType, ObjectProperty } from "../Type/ObjectType";
import { ReferenceType } from "../Type/ReferenceType";
import { getKey } from "../Utils/nodeKey";
import { LiteralType } from "../Type/LiteralType";
import { UnknownType } from "../Type/UnknownType";

export class TypeofNodeParser implements SubNodeParser {
public constructor(protected typeChecker: ts.TypeChecker, protected childNodeParser: NodeParser) {}
Expand Down Expand Up @@ -38,6 +39,14 @@ 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) {
// Silently ignoring Function as JSON Schema does not define them
// see https://github.com/vega/ts-json-schema-generator/issues/98
return new UnknownType(
`Function:(${(<ts.FunctionDeclaration>valueDec).parameters.map((p) => p.getFullText()).join(",")}): ${(<
ts.FunctionDeclaration
>valueDec).type?.getFullText()}`
);
}

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

export class UnknownType extends BaseType {
constructor(private comment?: string) {
super();
}
public getId(): string {
return "unknown";
}

public getComment(): string | undefined {
return this.comment;
}
}
4 changes: 3 additions & 1 deletion src/TypeFormatter/UnknownTypeFormatter.ts
Expand Up @@ -8,7 +8,9 @@ export class UnknownTypeFormatter implements SubTypeFormatter {
return type instanceof UnknownType;
}
public getDefinition(type: UnknownType): Definition {
return {};
return {
$comment: type.getComment(),
};
}
public getChildren(type: UnknownType): BaseType[] {
return [];
Expand Down
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
7 changes: 7 additions & 0 deletions test/valid-data/type-typeof-function/main.ts
@@ -0,0 +1,7 @@
function checkServerIdentity(hostname: string): Error | undefined {
return undefined;
}
export interface Test {
checkServerIdentity?: typeof checkServerIdentity | undefined;
otherProp: string;
}
32 changes: 32 additions & 0 deletions test/valid-data/type-typeof-function/schema.json
@@ -0,0 +1,32 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"NamedParameters<typeof checkServerIdentity>": {
"additionalProperties": false,
"properties": {
"hostname": {
"type": "string"
}
},
"required": [
"hostname"
],
"type": "object"
},
"Test": {
"additionalProperties": false,
"properties": {
"checkServerIdentity": {
"$comment": "Function:(hostname: string): Error | undefined"
},
"otherProp": {
"type": "string"
}
},
"required": [
"otherProp"
],
"type": "object"
}
}
}