Skip to content

Commit

Permalink
refactor: standardize evaluate function name
Browse files Browse the repository at this point in the history
  • Loading branch information
tinymins committed Aug 29, 2024
1 parent 720cc53 commit cfa6272
Show file tree
Hide file tree
Showing 30 changed files with 134 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { CloseCircleOutlined, MinusCircleOutlined, PlusCircleOutlined, PlusOutli
import { Alert, Button, Col, Popover, Row } from 'antd';
import React from 'react';

import { safeExecute } from '@/utils/sandbox';
import { safeEvaluate } from '@/utils/sandbox';
import RichText from '@/components/RichText';
import { DTGComponentPropertySchema } from '@/typing';

Expand Down Expand Up @@ -92,7 +92,7 @@ export default class ArrayComponent extends React.PureComponent<Props> {
if (typeof schema.visible === 'function') {
return schema.visible(currentValue[schema.name], currentValue, index, parentIndex);
} if (typeof schema.visible === 'string') {
return safeExecute(schema.visible, {
return safeEvaluate(schema.visible, {
currentValue: currentValue[schema.name],
formData: currentValue,
index,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { TabsPosition, TabsProps } from 'antd/lib/tabs';
import { DripTableProps, DripTableRecordTypeBase } from 'drip-table';
import React, { Component } from 'react';

import { safeExecute } from '@/utils/sandbox';
import { safeEvaluate } from '@/utils/sandbox';
import RichText from '@/components/RichText';
import { DTGComponentPropertySchema } from '@/typing';

Expand Down Expand Up @@ -139,7 +139,7 @@ export default class CustomForm<T> extends Component<Props<T>, State> {
if (typeof config.visible === 'function') {
return config.visible(formValues[config.name], formValues);
} if (typeof config.visible === 'string') {
return safeExecute(config.visible, { formData: formValues }, false);
return safeEvaluate(config.visible, { formData: formValues }, false);
} if (typeof config.visible === 'boolean') {
return config.visible;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as DOMHandler from 'domhandler';
import React from 'react';
import ViewerJS from 'viewerjs';

import { execute } from '@/utils/sandbox';
import { evaluate } from '@/utils/sandbox';
import Highlight, { HighlightProps } from '@/components/Highlight';

type UppercaseLetter = 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' | 'P' | 'Q' | 'R' | 'S' | 'T' | 'U' | 'V' | 'W' | 'X' | 'Y' | 'Z';
Expand Down Expand Up @@ -391,7 +391,7 @@ export default class RichText extends React.PureComponent<RichTextProps> {
Object.entries(attribs)
.map(([k, v]) => [domEvents[k.toLowerCase()], v])
.filter(([k, v]) => k)
.map(([k, v]) => [k, execute(v)]),
.map(([k, v]) => [k, evaluate(v)]),
),
// static props 静态属性
key,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Pagination, PaginationProps } from 'antd';
import { DripTableColumnSchema, DripTableExtraOptions, DripTableSchema } from 'drip-table';
import React from 'react';

import { safeExecute } from '@/utils/sandbox';
import { safeEvaluate } from '@/utils/sandbox';
import { DataSourceTypeAbbr, DripTableGeneratorProps } from '@/typing';

import { parseReactCSS } from './dom';
Expand Down Expand Up @@ -69,22 +69,22 @@ SubtableDataSourceKey extends React.Key = never,
pageSizeOptions={props.pageSizeOptions}
style={parseReactCSS(
typeof props?.style === 'string'
? safeExecute(props?.style, { props: { ext: props.ext } })
? safeEvaluate(props?.style, { props: { ext: props.ext } })
: props?.style,
)}
pageNumberStyle={parseReactCSS(
typeof props?.pageNumberStyle === 'string'
? safeExecute(props.pageNumberStyle, { props: { ext: props.ext } })
? safeEvaluate(props.pageNumberStyle, { props: { ext: props.ext } })
: props?.pageNumberStyle,
)}
pageStepperStyle={parseReactCSS(
typeof props?.pageStepperStyle === 'string'
? safeExecute(props.pageStepperStyle, { props: { ext: props.ext } })
? safeEvaluate(props.pageStepperStyle, { props: { ext: props.ext } })
: props?.pageStepperStyle,
)}
pageSelectorStyle={parseReactCSS(
typeof props?.pageSelectorStyle === 'string'
? safeExecute(props.pageSelectorStyle, { props: { ext: props.ext } })
? safeEvaluate(props.pageSelectorStyle, { props: { ext: props.ext } })
: props?.pageSelectorStyle,
)}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
import React from 'react';

import { filterAttributes } from '@/utils';
import { createExecutor, execute, finalizeString, safeExecute } from '@/utils/sandbox';
import { createEvaluator, evaluate, finalizeString, safeEvaluate } from '@/utils/sandbox';
import { DataSourceTypeAbbr, DripTableGeneratorProps } from '@/typing';

export interface CommonCellProps<
Expand Down Expand Up @@ -73,9 +73,9 @@ ExtraOptions extends Partial<DripTableExtraOptions> = never,
icons: props.icons,
defaultComponentLib: props.defaultComponentLib,
onEvent: props.preview === false ? props.onEvent : void 0,
createExecutor,
execute,
safeExecute,
createEvaluator,
evaluate,
safeEvaluate,
finalizeString,
schemaFunctionPreprocessor: void 0,
},
Expand Down
34 changes: 17 additions & 17 deletions packages/drip-table-generator/src/utils/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ import get from 'lodash/get';
/**
* 执行器缓存,优化性能
*/
const executorCache = new Map<string, ReturnType<FunctionConstructor>>();
let timerExecutorGC = 0;
const executorGC = () => { executorCache.clear(); };
const resetExecutorGC = () => {
if (timerExecutorGC) {
window.clearTimeout(timerExecutorGC);
const evaluatorCache = new Map<string, ReturnType<FunctionConstructor>>();
let timerEvaluatorGC = 0;
const executorGC = () => { evaluatorCache.clear(); };
const resetEvaluatorGC = () => {
if (timerEvaluatorGC) {
window.clearTimeout(timerEvaluatorGC);
}
timerExecutorGC = window.setTimeout(executorGC, 2000);
timerEvaluatorGC = window.setTimeout(executorGC, 2000);
};

/**
Expand All @@ -30,16 +30,16 @@ const resetExecutorGC = () => {
* @returns 创建的函数
* @throws Error 创建异常
*/
export const createExecutor = (script: string, contextKeys: string[] = []) => {
export const createEvaluator = (script: string, contextKeys: string[] = []) => {
const key = script + JSON.stringify(contextKeys);
let executor = executorCache.has(key)
? executorCache.get(key)
let executor = evaluatorCache.has(key)
? evaluatorCache.get(key)
: void 0;
if (!executor) {
executor = new Function(...contextKeys, script);
executorCache.set(key, executor);
evaluatorCache.set(key, executor);
}
resetExecutorGC();
resetEvaluatorGC();
return executor;
};

Expand All @@ -51,7 +51,7 @@ export const createExecutor = (script: string, contextKeys: string[] = []) => {
* @returns 代码段返回结果
* @throws Error 代码执行异常
*/
export const execute = (script: string, context: Record<string, unknown> = {}) => createExecutor(script, Object.keys(context))(...Object.values(context));
export const evaluate = (script: string, context: Record<string, unknown> = {}) => createEvaluator(script, Object.keys(context))(...Object.values(context));

/**
* 指定上下文,执行 JavaScript 代码段,抑制错误
Expand All @@ -61,9 +61,9 @@ export const execute = (script: string, context: Record<string, unknown> = {}) =
* @param defaultValue 异常时的默认返回值
* @returns 代码段返回结果,异常时返回默认结果
*/
export const safeExecute = (script: string, context: Record<string, unknown> = {}, defaultValue: unknown = void 0) => {
export const safeEvaluate = (script: string, context: Record<string, unknown> = {}, defaultValue: unknown = void 0) => {
try {
return execute(script, context);
return evaluate(script, context);
} catch (error) {
console.warn(error);
}
Expand Down Expand Up @@ -106,7 +106,7 @@ export const finalizeString = (mode: 'plain' | 'key' | 'pattern' | 'script', tex
value = stringify(text)
.replace(/\{\{(.+?)\}\}/guis, (s, s1) => {
try {
return execute(`return ${s1}`, {
return evaluate(`return ${s1}`, {
props: {
record,
recordIndex,
Expand All @@ -122,7 +122,7 @@ export const finalizeString = (mode: 'plain' | 'key' | 'pattern' | 'script', tex
});
} else if (mode === 'script') {
try {
value = stringify(execute(text, {
value = stringify(evaluate(text, {
props: {
record,
recordIndex,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export default class DTCButton<RecordType extends DripTableRecordTypeBase> exten
if (!visibleFunc) {
return true;
}
return !!dataProcessValue(this.props.execute, record, dataIndex, visibleFunc);
return !!dataProcessValue(this.props.evaluate, record, dataIndex, visibleFunc);
}

private getDisabled(disableFunc?: string): boolean {
Expand All @@ -279,13 +279,13 @@ export default class DTCButton<RecordType extends DripTableRecordTypeBase> exten
if (!disableFunc) {
return this.props.disable ?? false;
}
return !!dataProcessValue(this.props.execute, record, dataIndex, disableFunc);
return !!dataProcessValue(this.props.evaluate, record, dataIndex, disableFunc);
}

private parseReactCSS(style?: string | Record<string, string>) {
const { record, recordIndex, ext } = this.props;
const styleObject = typeof style === 'string'
? this.props.safeExecute(style, { props: { record, recordIndex, ext } })
? this.props.safeEvaluate(style, { props: { record, recordIndex, ext } })
: style;
return parseReactCSS(styleObject);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export default class DTCCheckbox<
private get disabled(): boolean {
const disable = this.props.schema.disable;
if (typeof disable === 'string') {
return !!this.props.safeExecute(
return !!this.props.safeEvaluate(
`return ${disable}`,
{
props: {
Expand Down Expand Up @@ -160,7 +160,7 @@ export default class DTCCheckbox<
checked={this.state.checkedValues?.includes(option.value)}
disabled={
typeof option.disabled === 'string'
? this.props.safeExecute(option.disabled, {
? this.props.safeEvaluate(option.disabled, {
props: { record, recordIndex, ext },
})
: option.disabled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import React from 'react';

import type { SandboxCreateExecutor, SandboxExecute, SandboxSafeExecute } from '@/utils/sandbox';
import type { SandboxCreateEvaluator, SandboxEvaluate, SandboxSafeEvaluate } from '@/utils/sandbox';
import type { DripTableColumnSchema, DripTableExtraOptions, DripTableProps, DripTableRecordTypeBase, DripTableRecordTypeWithSubtable, EventLike } from '@/types';

import type { DripTableBuiltInComponentEvent } from '.';
Expand Down Expand Up @@ -52,15 +52,15 @@ export interface DripTableComponentProps<
/**
* 沙箱生成器
*/
createExecutor: SandboxCreateExecutor;
createEvaluator: SandboxCreateEvaluator;
/**
* 沙箱执行器
*/
execute: SandboxExecute;
evaluate: SandboxEvaluate;
/**
* 安全沙箱执行器
*/
safeExecute: SandboxSafeExecute;
safeEvaluate: SandboxSafeEvaluate;
/**
* 格式化模板字符串
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ ExtraOptions extends Partial<DripTableExtraOptions> = never,
private parseReactCSS(style?: string | Record<string, string>) {
const { record, recordIndex, ext } = this.props;
const styleObject = typeof style === 'string'
? this.props.safeExecute(style, { props: { record, recordIndex, ext } })
? this.props.safeEvaluate(style, { props: { record, recordIndex, ext } })
: style;
return parseReactCSS(styleObject);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class DTCIcon<RecordType extends DripTableRecordTypeBase> extends
private get disabled(): boolean {
const disable = this.props.schema.disable;
if (typeof disable === 'string') {
return !!this.props.safeExecute(`return ${disable}`, {
return !!this.props.safeEvaluate(`return ${disable}`, {
props: {
value: this.value,
record: this.props.record,
Expand Down Expand Up @@ -127,7 +127,7 @@ export default class DTCIcon<RecordType extends DripTableRecordTypeBase> extends
return <RichText html={options.icon.html || ''} />;
}
if (options.icon.render) {
const html = this.props.safeExecute(options.icon.render, {
const html = this.props.safeEvaluate(options.icon.render, {
props: {
value: this.value,
record: this.props.record,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export default class DTCLink<RecordType extends DripTableRecordTypeBase> extends

private finalizeDisabled(disabled?: boolean | string): boolean {
if (typeof disabled === 'string') {
return !!this.props.safeExecute(`return ${disabled}`, {
return !!this.props.safeEvaluate(`return ${disabled}`, {
props: {
value: this.props.value,
record: this.props.record,
Expand All @@ -179,7 +179,7 @@ export default class DTCLink<RecordType extends DripTableRecordTypeBase> extends
const { dataIndex, options } = schema;
const { mode, visibleFunc } = options;
if (mode === 'single' && visibleFunc) {
return !!dataProcessValue(this.props.execute, record, dataIndex, visibleFunc);
return !!dataProcessValue(this.props.evaluate, record, dataIndex, visibleFunc);
}
return true;
}
Expand Down Expand Up @@ -314,7 +314,7 @@ export default class DTCLink<RecordType extends DripTableRecordTypeBase> extends
);
}
return (
<div key={index} style={{ display: dataProcessValue(this.props.execute, this.props.record, this.props.schema.dataIndex, config.visibleFunc) || !config.visibleFunc ? 'inline' : 'none' }}>
<div key={index} style={{ display: dataProcessValue(this.props.evaluate, this.props.record, this.props.schema.dataIndex, config.visibleFunc) || !config.visibleFunc ? 'inline' : 'none' }}>
<a
className={classNames(`${prefixCls}-link`, `${prefixCls}-link-break`, {
[`${prefixCls}-link-disabled`]: disabled,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ ExtraOptions extends Partial<DripTableExtraOptions> = never,
private parseReactCSS(style?: string | Record<string, string>) {
const { record, recordIndex, ext } = this.props;
const styleObject = typeof style === 'string'
? this.props.safeExecute(style, { props: { record, recordIndex, ext } })
? this.props.safeEvaluate(style, { props: { record, recordIndex, ext } })
: style;
return parseReactCSS(styleObject);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class DTCRenderHTMLRemote<RecordType extends DripTableRecordTypeB
return <Spin tip="Loading" />;
}
try {
const html = this.props.execute(this.state.render, {
const html = this.props.evaluate(this.state.render, {
props: {
value: this.props.value,
record: this.props.record,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default class DTCRenderHTML<RecordType extends DripTableRecordTypeBase> e
public render(): JSX.Element {
const { record, recordIndex, schema: { options }, ext } = this.props;
try {
const html = this.props.execute(options.render, {
const html = this.props.evaluate(options.render, {
props: {
value: this.props.value,
record,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ export default class DTCSelect<RecordType extends DripTableRecordTypeBase> exten
private get disabled(): boolean {
const options = this.props.schema.options;
if (typeof options.disabled === 'string') {
return !!this.props.safeExecute(`return ${options.disabled}`, {
return !!this.props.safeEvaluate(`return ${options.disabled}`, {
props: {
value: this.props.value,
record: this.props.record,
Expand Down Expand Up @@ -238,7 +238,7 @@ export default class DTCSelect<RecordType extends DripTableRecordTypeBase> exten

private finalizeOptionDisabled(disabled?: boolean | string, value?: unknown): boolean {
if (typeof disabled === 'string') {
return !!this.props.safeExecute(`return ${disabled}`, {
return !!this.props.safeEvaluate(`return ${disabled}`, {
props: {
value,
record: this.props.record,
Expand All @@ -256,7 +256,7 @@ export default class DTCSelect<RecordType extends DripTableRecordTypeBase> exten
if (typeof body === 'string') {
let finalBodyString: string | undefined = void 0;
try {
finalBodyString = this.props.execute(`return ${body}`, {
finalBodyString = this.props.evaluate(`return ${body}`, {
props: {
record: this.props.record,
recordIndex: this.props.recordIndex,
Expand Down Expand Up @@ -292,7 +292,7 @@ export default class DTCSelect<RecordType extends DripTableRecordTypeBase> exten
return response;
}
if (options.response?.mapper) {
return this.props.safeExecute(`return ${options.response?.mapper || 'response'}`, {
return this.props.safeEvaluate(`return ${options.response?.mapper || 'response'}`, {
props: {
response,
ext: this.props.ext,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class DTCSwitch<RecordType extends DripTableRecordTypeBase> exten
private get disabled(): boolean {
const disable = this.props.schema.disable;
if (typeof disable === 'string') {
return !!this.props.safeExecute(`return ${disable}`, {
return !!this.props.safeEvaluate(`return ${disable}`, {
props: {
value: this.props.value,
record: this.props.record,
Expand Down
Loading

0 comments on commit cfa6272

Please sign in to comment.