diff --git a/.eslintrc b/.eslintrc index fc8dbf2a8262162f5a4394d8957ee5ab30e0c5c0..cd4cc07eeee5dbc84064e6814ed3e3463e36ba80 100644 --- a/.eslintrc +++ b/.eslintrc @@ -23,24 +23,44 @@ "ecmaVersion": 12, "sourceType": "module" }, - "plugins": ["react", "babel", "jest", "react-hooks", "@typescript-eslint"], + "plugins": [ + "react", + "babel", + "jest", + "react-hooks", + "@typescript-eslint" + ], "settings": { "react": { "version": "16.13.1" }, - "polyfills": ["Promise"] + "polyfills": [ + "Promise" + ] }, "rules": { "react/jsx-props-no-spreading": 0, "no-use-before-define": "off", "react/display-name": 0, "import/extensions": 0, - "comma-dangle": ["error", "never"], - "space-before-function-paren": [0, "always"], + "import/no-cycle": 1, + "comma-dangle": [ + "error", + "never" + ], + "space-before-function-paren": [ + 0, + "always" + ], "react/jsx-filename-extension": [ 2, { - "extensions": [".js", ".jsx", ".ts", ".tsx"] + "extensions": [ + ".js", + ".jsx", + ".ts", + ".tsx" + ] } ] } diff --git a/src/util/condition.ts b/src/util/condition.ts index 2fe70fd6af9ebbc56e3321b9304e7e8cbb7a91dd..7de2fc5c7500b7b3e4513ce11e71f319684683ca 100644 --- a/src/util/condition.ts +++ b/src/util/condition.ts @@ -1,5 +1,5 @@ import { template } from 'lodash' -import { set } from '../util/produce' +import { set } from './produce' import { ParamConfig } from '../interface' import ParamHelper from './param' import { Field } from '../components/formFields/common' @@ -24,79 +24,98 @@ export interface ConditionConfig { debug?: boolean } -export default function ConditionHelper (condition: ConditionConfig | undefined, datas: { record?: object, data: object[], step: { [field: string]: any }, extraContainerPath?: string }, _this?: Field): boolean { +export default function ConditionHelper( + condition: ConditionConfig | undefined, + datas: { record?: object; data: object[]; step: { [field: string]: any }; extraContainerPath?: string }, + _this?: Field +): boolean { // 条件语句模版 let conditionTemplate = '' // 条件语句模版入参 let statementParams = {} - if (condition === undefined || ((condition.statement === undefined || condition.statement === '') && (condition.template === undefined || condition.template === ''))) { + if ( + condition === undefined || + ((condition.statement === undefined || condition.statement === '') && + (condition.template === undefined || condition.template === '')) + ) { return true - } else { - if (condition.template) { - conditionTemplate = condition.template - if (condition.params) { - condition.params.forEach((param) => { - if (param.field !== undefined && param.data !== undefined) { - const value = ParamHelper(param.data, datas, _this) - if (param.field === '') { - statementParams = value === undefined ? 'undefined' : JSON.stringify(value) - } else { - statementParams = set(statementParams, param.field, value === undefined ? 'undefined' : JSON.stringify(value)) - } + } + if (condition.template) { + conditionTemplate = condition.template + if (condition.params) { + condition.params.forEach((param) => { + if (param.field !== undefined && param.data !== undefined) { + const value = ParamHelper(param.data, datas, _this) + if (param.field === '') { + statementParams = value === undefined ? 'undefined' : JSON.stringify(value) + } else { + statementParams = set( + statementParams, + param.field, + value === undefined ? 'undefined' : JSON.stringify(value) + ) } - }) - } - } else { - // 用于兼容旧版本中的通配符 - // V2新增逻辑段 - 开始 - // const statementTemplate = template(condition.statement) - // V2新增逻辑段 - 结束 - // V2移除逻辑段 - 开始 - conditionTemplate = condition.statement?.replace(/([^\$])\{/g, '$1${') || '' - // V2移除逻辑段 - 结束 + } + }) + } + } else { + // 用于兼容旧版本中的通配符 + // V2新增逻辑段 - 开始 + // const statementTemplate = template(condition.statement) + // V2新增逻辑段 - 结束 + // V2移除逻辑段 - 开始 + conditionTemplate = condition.statement?.replace(/([^$])\{/g, '$1${') || '' + // V2移除逻辑段 - 结束 - if (condition.params) { - condition.params.forEach((param) => { - if (param.field !== undefined && param.data !== undefined) { - if (param.field === '') { - statementParams = ParamHelper(param.data, datas, _this) - } else { - statementParams = set(statementParams, param.field, ParamHelper(param.data, datas, _this)) - } + if (condition.params) { + condition.params.forEach((param) => { + if (param.field !== undefined && param.data !== undefined) { + if (param.field === '') { + statementParams = ParamHelper(param.data, datas, _this) + } else { + statementParams = set(statementParams, param.field, ParamHelper(param.data, datas, _this)) } - }) - } + } + }) } - - return execConditionHandler(condition, conditionTemplate, statementParams) } + + return execConditionHandler(condition, conditionTemplate, statementParams) +} + +const evil = (fn) => { + const Fn = Function // 一个变量指向Function,防止有些前端编译工具报错 + return new Fn(`return ${fn}`)()() } // 执行条件语句,返回结果 -const execConditionHandler = (condition: ConditionConfig | undefined, conditionTemplate: string, statementParams: object): boolean => { +const execConditionHandler = ( + condition: ConditionConfig | undefined, + conditionTemplate: string, + statementParams: object +): boolean => { try { if (Object.values(statementParams).some((param) => param === undefined)) { if (condition?.debug) { console.info(`CCMS debug: condition ${conditionTemplate} => false`) } return false - } else { - const statement = template(conditionTemplate)(statementParams) + } + const statement = template(conditionTemplate)(statementParams) - try { - const result = eval(statement) - if (condition?.debug) { - console.info(`CCMS debug: condition ${statement} => ${result}`) - } - return result - } catch (e) { - console.error('表单项展示条件语句执行错误。', conditionTemplate, statement) - return false + try { + const result = evil(statement) + if (condition?.debug) { + console.info(`CCMS debug: condition ${statement} => ${result}`) } + return result + } catch (e) { + console.error('表单项展示条件语句执行错误。', conditionTemplate, statement) + return false } } catch (e) { if (condition?.debug) { - console.info('CCMS debug: condition - `' + conditionTemplate + '` => error') + console.info(`CCMS debug: condition - \`${conditionTemplate}\` => error`) } console.error('表单项展示条件语句执行错误。', conditionTemplate) return false