From 249fb55ebbe9b1ed5c0f5c3bfb4155fdfbcd280d Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Wed, 8 Mar 2023 19:24:28 +0800 Subject: [PATCH 1/3] refactor(nocodb): remove jsepTreeToFormula.ts --- .../utils/common/helpers/jsepTreeToFormula.ts | 67 ------------------- 1 file changed, 67 deletions(-) delete mode 100644 packages/nocodb/src/lib/utils/common/helpers/jsepTreeToFormula.ts diff --git a/packages/nocodb/src/lib/utils/common/helpers/jsepTreeToFormula.ts b/packages/nocodb/src/lib/utils/common/helpers/jsepTreeToFormula.ts deleted file mode 100644 index 5532d229f0..0000000000 --- a/packages/nocodb/src/lib/utils/common/helpers/jsepTreeToFormula.ts +++ /dev/null @@ -1,67 +0,0 @@ -export default function jsepTreeToFormula(node) { - if (node.type === 'BinaryExpression' || node.type === 'LogicalExpression') { - return ( - '(' + - jsepTreeToFormula(node.left) + - ' ' + - node.operator + - ' ' + - jsepTreeToFormula(node.right) + - ')' - ); - } - - if (node.type === 'UnaryExpression') { - return node.operator + jsepTreeToFormula(node.argument); - } - - if (node.type === 'MemberExpression') { - return ( - jsepTreeToFormula(node.object) + - '[' + - jsepTreeToFormula(node.property) + - ']' - ); - } - - if (node.type === 'Identifier') { - return node.name; - } - - if (node.type === 'Literal') { - if (typeof node.value === 'string') { - return '"' + node.value + '"'; - } - - return '' + node.value; - } - - if (node.type === 'CallExpression') { - return ( - jsepTreeToFormula(node.callee) + - '(' + - node.arguments.map(jsepTreeToFormula).join(', ') + - ')' - ); - } - - if (node.type === 'ArrayExpression') { - return '[' + node.elements.map(jsepTreeToFormula).join(', ') + ']'; - } - - if (node.type === 'Compound') { - return node.body.map((e) => jsepTreeToFormula(e)).join(' '); - } - - if (node.type === 'ConditionalExpression') { - return ( - jsepTreeToFormula(node.test) + - ' ? ' + - jsepTreeToFormula(node.consequent) + - ' : ' + - jsepTreeToFormula(node.alternate) - ); - } - - return ''; -} From 41ad7093f89546ead599edf13e59d0f4c42d29a4 Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Wed, 8 Mar 2023 19:24:43 +0800 Subject: [PATCH 2/3] refactor(nocodb): use jsepTreeToFormula from nocodb-sdk --- .../src/lib/utils/common/helpers/updateColumnNameInFormula.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/nocodb/src/lib/utils/common/helpers/updateColumnNameInFormula.ts b/packages/nocodb/src/lib/utils/common/helpers/updateColumnNameInFormula.ts index d8d5834529..7ddd4c2a46 100644 --- a/packages/nocodb/src/lib/utils/common/helpers/updateColumnNameInFormula.ts +++ b/packages/nocodb/src/lib/utils/common/helpers/updateColumnNameInFormula.ts @@ -1,4 +1,4 @@ -import jsepTreeToFormula from './jsepTreeToFormula'; +import { jsepTreeToFormula } from 'nocodb-sdk'; export default function (args: { virtualColumns; From e42f7a7d3bb8ee05134b54a440b5b21add81134a Mon Sep 17 00:00:00 2001 From: Wing-Kam Wong Date: Thu, 9 Mar 2023 14:15:50 +0800 Subject: [PATCH 3/3] fix(nocodb-sdk): escapeDoubleQuotes --- packages/nocodb-sdk/src/lib/formulaHelpers.ts | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/nocodb-sdk/src/lib/formulaHelpers.ts b/packages/nocodb-sdk/src/lib/formulaHelpers.ts index e10e491dea..4d71420952 100644 --- a/packages/nocodb-sdk/src/lib/formulaHelpers.ts +++ b/packages/nocodb-sdk/src/lib/formulaHelpers.ts @@ -179,9 +179,8 @@ export function jsepTreeToFormula(node) { if (node.type === 'Literal') { if (typeof node.value === 'string') { - return '"' + node.value + '"'; + return String.raw`"${escapeDoubleQuotes(node.value)}"`; } - return '' + node.value; } @@ -214,3 +213,7 @@ export function jsepTreeToFormula(node) { return ''; } + +function escapeDoubleQuotes(v: string) { + return v.replace(/"/g, '\\"'); +}