amis2/scripts/NodeParser/IntersectionNodeParser.ts
2020-09-16 11:05:33 +08:00

42 lines
1.2 KiB
TypeScript

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