Skip to content

Commit

Permalink
fix: typeof function (#1182)
Browse files Browse the repository at this point in the history
* fix: typeof function (Close #1181)

* fix: update Function type to UnknownType with a $comment
  • Loading branch information
loopingz committed Jul 31, 2022
1 parent 0677d42 commit aa63c0a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 1 deletion.
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 @@ -64,6 +64,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"
}
}
}

0 comments on commit aa63c0a

Please sign in to comment.