Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions factory/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { BooleanLiteralNodeParser } from "../src/NodeParser/BooleanLiteralNodePa
import { BooleanTypeNodeParser } from "../src/NodeParser/BooleanTypeNodeParser.js";
import { CallExpressionParser } from "../src/NodeParser/CallExpressionParser.js";
import { ConditionalTypeNodeParser } from "../src/NodeParser/ConditionalTypeNodeParser.js";
import { NewExpressionParser } from "../src/NodeParser/NewExpressionParser.js";
import { ConstructorNodeParser } from "../src/NodeParser/ConstructorNodeParser.js";
import { EnumNodeParser } from "../src/NodeParser/EnumNodeParser.js";
import { ExpressionWithTypeArgumentsNodeParser } from "../src/NodeParser/ExpressionWithTypeArgumentsNodeParser.js";
Expand Down Expand Up @@ -149,6 +150,7 @@ export function createParser(program: ts.Program, config: CompletedConfig, augme
.addNodeParser(new SpreadElementNodeParser(chainNodeParser))

.addNodeParser(new CallExpressionParser(typeChecker, chainNodeParser))
.addNodeParser(new NewExpressionParser(typeChecker, chainNodeParser))
.addNodeParser(new PropertyAccessExpressionParser(typeChecker, chainNodeParser))

.addNodeParser(withCircular(withExpose(withJsDoc(new TypeAliasNodeParser(typeChecker, chainNodeParser)))))
Expand Down
46 changes: 46 additions & 0 deletions src/NodeParser/NewExpressionParser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import ts from "typescript";
import type { NodeParser } from "../NodeParser.js";
import { Context } from "../NodeParser.js";
import type { SubNodeParser } from "../SubNodeParser.js";
import type { BaseType } from "../Type/BaseType.js";
import { UnknownNodeError } from "../Error/Errors.js";

export class NewExpressionParser implements SubNodeParser {
public constructor(
protected typeChecker: ts.TypeChecker,
protected childNodeParser: NodeParser,
) {}

public supportsNode(node: ts.NewExpression): boolean {
return node.kind === ts.SyntaxKind.NewExpression;
}

public createType(node: ts.NewExpression, context: Context): BaseType {
const type = this.typeChecker.getTypeAtLocation(node);

const symbol = type.symbol || type.aliasSymbol;

const decl =
this.typeChecker.typeToTypeNode(type, node, ts.NodeBuilderFlags.IgnoreErrors) ||
symbol?.valueDeclaration ||
symbol?.declarations?.[0];

if (!decl) {
throw new UnknownNodeError(node);
}

return this.childNodeParser.createType(decl, this.createSubContext(node, context));
}

protected createSubContext(node: ts.NewExpression, parentContext: Context): Context {
const subContext = new Context(node);

if (node.arguments) {
for (const arg of node.arguments) {
const type = this.childNodeParser.createType(arg, parentContext);
subContext.pushArgument(type);
}
}
return subContext;
}
}
1 change: 1 addition & 0 deletions test/valid-data-struct.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ describe("valid-data-struct", () => {
it("class-inheritance", assertValidSchema("class-inheritance", "MyObject"));
it("class-generics", assertValidSchema("class-generics", "MyObject"));
it("class-jsdoc", assertValidSchema("class-jsdoc", "MyObject"));
it("class-new-expression", assertValidSchema("class-new-expression", "MyType"));

it("structure-private", assertValidSchema("structure-private", "MyObject"));
it("structure-anonymous", assertValidSchema("structure-anonymous", "MyObject"));
Expand Down
5 changes: 5 additions & 0 deletions test/valid-data/class-new-expression/main.ts
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mostly copied this from the existing class-single test case.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this can be much simpler and focused on just the new.

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class MyObject {}

const obj = new MyObject();

export type MyType = typeof obj;
10 changes: 10 additions & 0 deletions test/valid-data/class-new-expression/schema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"$ref": "#/definitions/MyType",
"$schema": "http://json-schema.org/draft-07/schema#",
"definitions": {
"MyType": {
"additionalProperties": false,
"type": "object"
}
}
}
Loading