Skip to content

Commit 0696784

Browse files
fit2cloudwxxfit2-zhao
authored andcommitted
fix: formula diagnose
1 parent 71ea1bf commit 0696784

File tree

4 files changed

+51
-4
lines changed

4 files changed

+51
-4
lines changed

frontend/packages/web/src/components/business/crm-formula-editor/diagnose/diagnose-rules/index.ts

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { useI18n } from '@lib/shared/hooks/useI18n';
22

33
import { FormulaErrorCode } from '../../config';
44
import { DiagnoseRule } from '../../types';
5-
import { isIllegalFunctionCall, isValueEnd, isValueStart } from './utils';
5+
import { isIllegalFunctionCall, isInsideFunctionArgs, isValueEnd, isValueStart } from './utils';
66

77
const { t } = useI18n();
88

@@ -125,20 +125,43 @@ const commaPositionRule: DiagnoseRule = {
125125
},
126126
};
127127

128+
export const illegalCommaRule: DiagnoseRule = {
129+
name: 'illegal-comma',
130+
131+
check(ctx) {
132+
const { cur, index, tokens } = ctx;
133+
if (!cur) return;
134+
const inFunctionArgs = isInsideFunctionArgs(tokens, index);
135+
if (cur.type === 'comma' && !inFunctionArgs) {
136+
ctx.push({
137+
type: 'error',
138+
code: FormulaErrorCode.SYNTAX_ERROR,
139+
message: t('formulaEditor.diagnostics.missingOperatorUsedAsSeparator'),
140+
highlight: {
141+
tokenRange: [index, index],
142+
},
143+
});
144+
}
145+
},
146+
};
147+
128148
// 相邻规则
129149
const adjacentValueRule: DiagnoseRule = {
130150
name: 'adjacent-value',
131151

132152
check(ctx) {
133-
const { prev, cur, index } = ctx;
153+
const { prev, cur, index, tokens } = ctx;
134154
if (!prev || !cur) return;
135155
// illegal-function-call 处理
136156
if (isIllegalFunctionCall(prev, cur)) return;
137157
if (isValueEnd(prev) && isValueStart(cur)) {
158+
const inFunctionArgs = isInsideFunctionArgs(tokens, index);
138159
ctx.push({
139160
type: 'error',
140161
code: FormulaErrorCode.SYNTAX_ERROR,
141-
message: t('formulaEditor.diagnostics.missingOperator'),
162+
message: inFunctionArgs
163+
? t('formulaEditor.diagnostics.missingSeparator')
164+
: t('formulaEditor.diagnostics.missingOperator'),
142165
highlight: {
143166
tokenRange: [index - 1, index],
144167
},
@@ -233,7 +256,8 @@ const RULES: DiagnoseRule[] = [
233256
leadingOperatorRule, // 放在最前面
234257
duplicateOperatorRule, // 连续操作符
235258
illegalTextRule, // 非法字符
236-
illegalCommaCharRule, // 非法逗号字符
259+
illegalCommaRule, // 非法逗号
260+
illegalCommaCharRule, // 非法字符
237261
duplicateCommaRule, // 连续逗号
238262
commaPositionRule, // 逗号位置
239263
adjacentValueRule, // 相邻值

frontend/packages/web/src/components/business/crm-formula-editor/diagnose/diagnose-rules/utils.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,24 @@ export function isValueStart(token?: Token): boolean {
5555
export function isIllegalFunctionCall(prev?: Token, cur?: Token): boolean {
5656
return prev?.type === 'paren' && prev.value === ')' && cur?.type === 'paren' && cur.value === '(';
5757
}
58+
59+
// 是否在函数参数内
60+
export function isInsideFunctionArgs(tokens: any[], index: number): boolean {
61+
let depth = 0;
62+
63+
for (let i = index - 1; i >= 0; i--) {
64+
const t = tokens[i];
65+
66+
if (t.type === 'paren' && t.value === ')') {
67+
depth++;
68+
} else if (t.type === 'paren' && t.value === '(') {
69+
if (depth === 0) {
70+
// 看左边是不是 function
71+
return tokens[i - 1]?.type === 'function';
72+
}
73+
depth--;
74+
}
75+
}
76+
77+
return false;
78+
}

frontend/packages/web/src/components/business/crm-formula-editor/locale/en-US.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ export default {
2323
'formulaEditor.diagnostics.missingOperator': 'Missing operator',
2424
'formulaEditor.diagnostics.invalidArgOfDAYS':
2525
'The {index}th parameter type error, parameter must be of number or date type',
26+
'c': 'Commas cannot be used between fields, please use operators',
2627
};

frontend/packages/web/src/components/business/crm-formula-editor/locale/zh-CN.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ export default {
2121
'formulaEditor.diagnostics.invalidFunctionCall': '非法的函数调用:表达式不能被再次调用',
2222
'formulaEditor.diagnostics.missingOperator': '缺少运算符',
2323
'formulaEditor.diagnostics.invalidArgOfDAYS': '第{index}个参数类型错误,必须为数字或日期类型',
24+
'formulaEditor.diagnostics.missingOperatorUsedAsSeparator': '字段之间不能使用逗号,请使用运算符',
2425
};

0 commit comments

Comments
 (0)