slightning-coco-widget
Version:
SLIGHTNING 的 CoCo 控件框架。
182 lines (181 loc) • 5.79 kB
JavaScript
import * as stringify from "@slightning/anything-to-string";
import { betterToString } from "../../utils";
import { TypeValidateError } from "./type-validate-error";
import { typeToString, validate } from "./utils";
export class UnionType {
constructor(...types) {
var _a;
this.types = types;
for (const type of types) {
if ("defaultValue" in type &&
(typeof type.defaultValue == "string" ||
typeof type.defaultValue == "number" ||
typeof type.defaultValue == "boolean")) {
this.defaultValue = type.defaultValue;
break;
}
}
(_a = this.defaultValue) !== null && _a !== void 0 ? _a : (this.defaultValue = "");
}
validate(value) {
const errors = [];
for (const type of this.types) {
try {
validate(null, value, type);
return true;
}
catch (error) {
if (!(error instanceof TypeValidateError)) {
throw error;
}
errors.push(error);
}
}
throw new TypeValidateError(`不能将 ${betterToString(value)} 分配给 ${typeToString(this)}\n` +
errors.map((error) => error.message
.split("\n")
.map((line) => ` ${line}`)
.join("\n")).join("\n"), value, this);
}
getSameDirectionChildren() {
var _a, _b, _c;
const result = [];
for (let i = 0; i < this.types.length; i++) {
const type = this.types[i];
if (type != undefined) {
result.push({
key: `__slightning_coco_widget_union_type_child__${i}_${(_c = (_b = (_a = Object.getPrototypeOf(type)) === null || _a === void 0 ? void 0 : _a.constructor) === null || _b === void 0 ? void 0 : _b.name) !== null && _c !== void 0 ? _c : "unknown"}`,
label: String(i),
type: type
});
}
}
return result;
}
getReverseDirectionChildren() {
return [];
}
isVoid() {
return this.types.every((type) => type.isVoid());
}
typeToStringPrepare(config, context) {
for (const type of this.types) {
new stringify.AnythingRule().prepare(type, config, context);
}
}
typeToString(config, context) {
return this.types.length == 0 ? "空" : `(${this.types.map((type) => new stringify.AnythingRule().toString(type, config, context)).join(" | ")})`;
}
inlineTypeToStringPrepare(config, context) {
this.typeToStringPrepare(config, context);
}
inlineTypeToString(config, context) {
return this.typeToString(config, context);
}
toCoCoPropertyValueTypes() {
const valueTypes = [];
for (const type of this.types) {
const { valueType } = type.toCoCoMethodValueTypes();
if (Array.isArray(valueType)) {
valueTypes.push(...valueType);
}
else if (valueType != null) {
valueTypes.push(valueType);
}
}
return {
valueType: valueTypes,
defaultValue: this.defaultValue
};
}
toCoCoMethodParamValueTypes() {
const valueTypes = [];
for (const type of this.types) {
const { valueType } = type.toCoCoMethodValueTypes();
if (Array.isArray(valueType)) {
valueTypes.push(...valueType);
}
else if (valueType != null) {
valueTypes.push(valueType);
}
}
return {
valueType: valueTypes,
defaultValue: this.defaultValue
};
}
toCoCoMethodValueTypes() {
const valueTypes = [];
for (const type of this.types) {
const { valueType } = type.toCoCoMethodValueTypes();
if (Array.isArray(valueType)) {
valueTypes.push(...valueType);
}
else if (valueType != null) {
valueTypes.push(valueType);
}
}
return {
valueType: valueTypes
};
}
toCoCoEventParamValueTypes() {
const valueTypes = [];
for (const type of this.types) {
const { valueType } = type.toCoCoMethodValueTypes();
if (Array.isArray(valueType)) {
valueTypes.push(...valueType);
}
else if (valueType != null) {
valueTypes.push(valueType);
}
}
return {
valueType: valueTypes
};
}
toCreationProject1PropValueTypes() {
return {
valueType: "string",
defaultValue: this.defaultValue
};
}
toCreationProject1MethodParamValueTypes() {
return {
valueType: "any",
defaultValue: this.defaultValue
};
}
toCreationProject1MethodValueTypes() {
return {
valueType: "string"
};
}
toCreationProject1EmitParamValueTypes() {
return {
valueType: "string"
};
}
toCreationProject2PropValueTypes() {
return {
valueType: "string",
defaultValue: this.defaultValue
};
}
toCreationProject2MethodParamValueTypes() {
return {
valueType: "any",
defaultValue: this.defaultValue
};
}
toCreationProject2MethodValueTypes() {
return {
valueType: "string"
};
}
toCreationProject2EmitParamValueTypes() {
return {
valueType: "string"
};
}
}