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

Consider methods when typeofKeyword is true #247

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions test/programs/typeof-keyword-methods/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
interface MyObject {
foo: () => string;
bar(): string;
}
17 changes: 17 additions & 0 deletions test/programs/typeof-keyword-methods/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"properties": {
"foo": {
"typeof": "function"
},
"bar": {
"typeof": "function"
}
},
"required": [
"bar",
"foo"
],
"type": "object"
}

2 changes: 1 addition & 1 deletion test/schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export function assertSchema(group: string, type: string, settings: TJS.PartialA

const files = [resolve(BASE + group + "/main.ts")];
const actual = TJS.generateSchema(TJS.getProgramFromFiles(files, compilerOptions), type, settings, files);

// writeFileSync(BASE + group + "/schema.json", stringify(actual, {space: 4}) + "\n\n");

const file = readFileSync(BASE + group + "/schema.json", "utf8");
Expand Down Expand Up @@ -190,6 +189,7 @@ describe("schema", () => {
assertSchema("annotation-id", "MyObject");

assertSchema("typeof-keyword", "MyObject", {typeOfKeyword: true});
assertSchema("typeof-keyword-methods", "MyObject", {typeOfKeyword: true});

assertSchema("user-validation-keywords", "MyObject", {
validationKeywords: [ "chance", "important" ]
Expand Down
7 changes: 4 additions & 3 deletions typescript-json-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,9 +450,10 @@ export class JsonSchemaGenerator {
}

private getDefinitionForProperty(prop: ts.Symbol, node: ts.Node) {
if (prop.flags & ts.SymbolFlags.Method) {
if ((prop.flags & ts.SymbolFlags.Method) && !this.args.typeOfKeyword) {
return null;
}

const propertyName = prop.getName();
const propertyType = this.tc.getTypeOfSymbolAtLocation(prop, node);

Expand Down Expand Up @@ -785,7 +786,7 @@ export class JsonSchemaGenerator {
const requiredProps = props.reduce((required: string[], prop: ts.Symbol) => {
const def = {};
this.parseCommentsIntoDefinition(prop, def, {});
if (!(prop.flags & ts.SymbolFlags.Optional) && !(prop.flags & ts.SymbolFlags.Method) && !(<any>prop).mayBeUndefined && !def.hasOwnProperty("ignore")) {
if (!(prop.flags & ts.SymbolFlags.Optional) && (this.args.typeOfKeyword || !(prop.flags & ts.SymbolFlags.Method)) && !(<any>prop).mayBeUndefined && !def.hasOwnProperty("ignore")) {
required.push(prop.getName());
}
return required;
Expand Down Expand Up @@ -833,7 +834,7 @@ export class JsonSchemaGenerator {
reffedType = undefined;
}

if (this.args.typeOfKeyword && (typ.flags & ts.TypeFlags.Object) && ((<ts.ObjectType>typ).objectFlags & ts.ObjectFlags.Anonymous)) {
if (this.args.typeOfKeyword && ((typ.flags & ts.TypeFlags.Object) || typ.symbol.flags & ts.SymbolFlags.Method) && ((<ts.ObjectType>typ).objectFlags & ts.ObjectFlags.Anonymous)) {
definition.typeof = "function";
return definition;
}
Expand Down