amis2/scripts/NodeParser/IntersectionNodeParser.ts

39 lines
1.2 KiB
TypeScript
Raw Normal View History

2020-09-16 00:41:21 +08:00
import {
BaseType,
Context,
2020-09-16 11:05:33 +08:00
IntersectionNodeParser as BaseIntersectionNodeParser,
IntersectionType,
NodeParser,
ReferenceType
2020-09-16 00:41:21 +08:00
} from 'ts-json-schema-generator';
import ts from 'typescript';
export class IntersectionNodeParser extends BaseIntersectionNodeParser {
2020-09-16 11:05:33 +08:00
protected readonly childParser: NodeParser;
public constructor(typeChecker: ts.TypeChecker, childNodeParser: NodeParser) {
2020-11-30 16:53:04 +08:00
super(typeChecker as any, childNodeParser);
2020-09-16 11:05:33 +08:00
this.childParser = childNodeParser;
}
2020-11-30 16:53:04 +08:00
public createType(node: any, context: Context): BaseType | undefined {
2020-09-16 11:05:33 +08:00
// 这两个只能用 allOf 来了,提取不了。
const shouldBeReference = (type: any) =>
2020-09-16 19:33:47 +08:00
['SchemaObject', 'FormControlSchema'].includes(
type.typeName?.escapedText
);
2020-09-16 11:05:33 +08:00
const matched = node.types.some(shouldBeReference);
// 跟 SchemaObject and 一般都有问题,改成 allOf 不要支持 additional props false 了
if (matched) {
const types: BaseType[] = node.types
2020-11-30 16:53:04 +08:00
.map((type: any) => this.childParser.createType(type as any, context)!)
.filter((item: any) => item);
2020-09-16 11:05:33 +08:00
return new IntersectionType(types);
}
2020-11-30 16:53:04 +08:00
return super.createType(node as any, context);
2020-09-16 00:41:21 +08:00
}
}