slightning-coco-widget
Version:
SLIGHTNING 的 CoCo 控件框架。
211 lines (210 loc) • 7.28 kB
JavaScript
import * as stringify from "@slightning/anything-to-string";
import { betterToString, XMLEscape } from "../../utils";
import { standardizeMethodBlock } from "../convert/standardize-types";
import { TypeValidateError } from "./type-validate-error";
import { inlineTypeToString, typeToString } from "./utils";
import { VoidType } from "./void-type";
export class FunctionType {
/**
* @param block 函数对应的积木,类似于控件方法类型定义中的 `block` 属性。
* @param returns 返回值类型。
* @param throws 抛出异常类型。
* @param raw 是否保持原始,若为时,则不会被装饰器转换。
*/
constructor({ block, returns, throws, defaultValue, raw }) {
this.block = standardizeMethodBlock(block);
this.returns = returns;
this.throws = throws;
this.defaultValue = defaultValue;
this.raw = raw !== null && raw !== void 0 ? raw : false;
}
validate(value) {
if (typeof value != "function") {
throw new TypeValidateError(`不能将 ${betterToString(value)} 分配给 ${typeToString(this)}`, value, this);
}
return true;
}
getSameDirectionChildren() {
const result = [];
if (this.returns != null) {
result.push({
key: "__slightning_coco_widget_function_return_value__",
label: "函数返回值",
type: this.returns
});
}
if (this.throws != null) {
result.push({
key: "__slightning_coco_widget_function_throw_value__",
label: "函数抛出值",
type: this.throws
});
}
return result;
}
getReverseDirectionChildren() {
const result = [];
for (const part of this.block) {
if (typeof part != "object") {
continue;
}
result.push({
key: `__slightning_coco_widget_function_param__${part.key}`,
label: `函数参数·${part.label}`,
type: part.type
});
}
return result;
}
isVoid() {
return false;
}
typeToStringPrepare(config, context) {
if (this.block != null) {
for (const part of this.block) {
if (typeof part != "object") {
continue;
}
new stringify.AnythingRule().prepare(part.type, config, context);
}
}
if (this.returns != null) {
new stringify.AnythingRule().prepare(this.returns, config, context);
}
if (this.throws != null) {
new stringify.AnythingRule().prepare(this.throws, config, context);
}
}
typeToString(config, context) {
var _a;
let result = "(";
let isFirst = true;
for (const part of this.block) {
if (typeof part != "object") {
continue;
}
if (isFirst) {
isFirst = false;
}
else {
result += ", ";
}
result += `${part.label}: ${new stringify.AnythingRule().toString(part.type, config, context)}`;
}
result += `) => ${new stringify.AnythingRule().toString((_a = this.returns) !== null && _a !== void 0 ? _a : new VoidType(), config, context)}`;
if (this.throws != null) {
result += ` 抛出 ${new stringify.AnythingRule().toString(this.throws, config, context)}`;
}
return result;
}
inlineTypeToStringPrepare(config, context) {
this.typeToStringPrepare(config, context);
}
inlineTypeToString(config, context) {
return this.typeToString(config, context);
}
toCoCoPropertyValueTypes() {
var _a;
return {
valueType: ["string", "number", "boolean", "array", "object"],
checkType: "string",
defaultValue: XMLEscape((_a = this.defaultValue) !== null && _a !== void 0 ? _a : inlineTypeToString(this))
};
}
toCoCoMethodParamValueTypes() {
var _a;
return {
valueType: ["string", "number", "boolean", "array", "object"],
checkType: "string",
defaultValue: XMLEscape((_a = this.defaultValue) !== null && _a !== void 0 ? _a : inlineTypeToString(this))
};
}
toCoCoMethodValueTypes() {
return {
valueType: ["string", "number", "boolean", "array", "object"]
};
}
toCoCoEventParamValueTypes() {
return {
valueType: ["string", "number", "boolean", "array", "object"]
};
}
toCreationProject1PropValueTypes() {
var _a;
return {
valueType: "object",
defaultValue: (_a = this.defaultValue) !== null && _a !== void 0 ? _a : inlineTypeToString(this)
};
}
toCreationProject1MethodParamValueTypes() {
var _a;
if (!this.raw &&
(this.returns == null || this.returns.isVoid()) &&
(this.throws == null || this.throws.isVoid())) {
const codeParams = [];
for (const part of this.block) {
if (typeof part != "object") {
continue;
}
codeParams.push(Object.assign({ key: part.key, label: part.label }, part.type.toCreationProject1EmitParamValueTypes()));
}
return {
valueType: "code",
codeParams
};
}
return {
valueType: "object",
defaultValue: (_a = this.defaultValue) !== null && _a !== void 0 ? _a : inlineTypeToString(this)
};
}
toCreationProject1MethodValueTypes() {
return {
valueType: "object"
};
}
toCreationProject1EmitParamValueTypes() {
return {
valueType: "object"
};
}
toCreationProject2PropValueTypes() {
var _a;
return {
valueType: "object",
defaultValue: (_a = this.defaultValue) !== null && _a !== void 0 ? _a : inlineTypeToString(this)
};
}
toCreationProject2MethodParamValueTypes() {
var _a;
if (!this.raw &&
(this.returns == null || this.returns.isVoid()) &&
(this.throws == null || this.throws.isVoid())) {
const codeParams = [];
for (const part of this.block) {
if (typeof part != "object") {
continue;
}
codeParams.push(Object.assign({ key: part.key, label: part.label }, part.type.toCreationProject2EmitParamValueTypes()));
}
return {
valueType: "code",
codeParams
};
}
return {
valueType: "object",
defaultValue: (_a = this.defaultValue) !== null && _a !== void 0 ? _a : inlineTypeToString(this)
};
}
toCreationProject2MethodValueTypes() {
return {
valueType: "object"
};
}
toCreationProject2EmitParamValueTypes() {
return {
valueType: "object"
};
}
}