Browse Source

fix(nocodb-sdk): revise jsepCurlyHook logic to handle column names with special characters

pull/4776/head
Wing-Kam Wong 2 years ago
parent
commit
8eb8d9d5dc
  1. 49
      packages/nocodb-sdk/src/lib/formulaHelpers.ts

49
packages/nocodb-sdk/src/lib/formulaHelpers.ts

@ -8,19 +8,35 @@ export const jsepCurlyHook = {
jsep.hooks.add('gobble-token', function gobbleCurlyLiteral(env) { jsep.hooks.add('gobble-token', function gobbleCurlyLiteral(env) {
const OCURLY_CODE = 123; // { const OCURLY_CODE = 123; // {
const CCURLY_CODE = 125; // } const CCURLY_CODE = 125; // }
let start = -1;
let end = -1;
const { context } = env; const { context } = env;
if ( if (
!jsep.isIdentifierStart(context.code) && !jsep.isIdentifierStart(context.code) &&
context.code === OCURLY_CODE context.code === OCURLY_CODE
) { ) {
if (start == -1) {
start = context.index;
}
context.index += 1; context.index += 1;
const nodes = context.gobbleExpressions(CCURLY_CODE); context.gobbleExpressions(CCURLY_CODE);
if (context.code === CCURLY_CODE) { if (context.code === CCURLY_CODE) {
if (start != -1 && end == -1) {
end = context.index;
}
context.index += 1; context.index += 1;
if (start != -1 && end != -1) {
env.node = { env.node = {
type: jsep.IDENTIFIER, type: jsep.IDENTIFIER,
name: nodes.map((node) => parseIdentifierName(node)).join(''), name: /{{(.*?)}}/.test(context.expr)
? // start would be the position of the first curly bracket
// add 2 to point to the first character for expressions like {{col1}}
context.expr.slice(start + 2, end)
: // start would be the position of the first curly bracket
// add 1 to point to the first character for expressions like {col1}
context.expr.slice(start + 1, end),
}; };
}
return env.node; return env.node;
} else { } else {
context.throwError('Unclosed }'); context.throwError('Unclosed }');
@ -30,23 +46,6 @@ export const jsepCurlyHook = {
}, },
} as jsep.IPlugin; } as jsep.IPlugin;
function parseIdentifierName(node) {
if (node.type === 'Identifier') {
// e.g. col
return node.name;
} else if (node.type === 'BinaryExpression') {
// e.g. col-1 would be considered as col (left), - (operator), 1 (right)
return (
parseIdentifierName(node.left) +
node.operator +
parseIdentifierName(node.right)
);
} else if (node.type === 'Literal') {
// e.g col (identifier) + 123 (literal)
return node.value;
}
}
export async function substituteColumnAliasWithIdInFormula( export async function substituteColumnAliasWithIdInFormula(
formula, formula,
columns: ColumnType[] columns: ColumnType[]
@ -74,7 +73,11 @@ export async function substituteColumnAliasWithIdInFormula(
}; };
// register jsep curly hook // register jsep curly hook
jsep.plugins.register(jsepCurlyHook); jsep.plugins.register(jsepCurlyHook);
const parsedFormula = jsep(formula); const parsedFormula = jsep(
// formula may include double curly brackets in previous version
// convert to single curly bracket here for compatibility
formula.replaceAll('{{', '{').replaceAll('}}', '}')
);
await substituteId(parsedFormula); await substituteId(parsedFormula);
return jsepTreeToFormula(parsedFormula); return jsepTreeToFormula(parsedFormula);
} }
@ -109,7 +112,11 @@ export function substituteColumnIdWithAliasInFormula(
// register jsep curly hook // register jsep curly hook
jsep.plugins.register(jsepCurlyHook); jsep.plugins.register(jsepCurlyHook);
const parsedFormula = jsep(formula); const parsedFormula = jsep(
// formula may include double curly brackets in previous version
// convert to single curly bracket here for compatibility
formula.replaceAll('{{', '{').replaceAll('}}', '}')
);
const parsedRawFormula = rawFormula && jsep(rawFormula); const parsedRawFormula = rawFormula && jsep(rawFormula);
substituteId(parsedFormula, parsedRawFormula); substituteId(parsedFormula, parsedRawFormula);
return jsepTreeToFormula(parsedFormula); return jsepTreeToFormula(parsedFormula);

Loading…
Cancel
Save