Skip to content

Commit

Permalink
fix(core/reports): evaluateExpression stops messing with predefined i…
Browse files Browse the repository at this point in the history
…dentifiers
  • Loading branch information
robzan8 committed May 2, 2023
1 parent 38a0bb7 commit 7e7306e
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions projects/core/models/src/utils/expression-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,7 @@ export class AjfExpressionUtils {
formatDate: {fn: formatDate},
isoMonth: {fn: isoMonth},
getCoordinate: {fn: getCoordinate},
Math: {fn: Math},
parseInt: {fn: parseInt},
parseFloat: {fn: parseFloat},
parseDate: {fn: dateUtils.parse},
Date: {fn: Date},
plainArray: {fn: plainArray},
COUNT_FORMS: {fn: COUNT_FORMS},
COUNT_FORMS_UNIQUE: {fn: COUNT_FORMS_UNIQUE},
Expand Down Expand Up @@ -208,6 +204,11 @@ export function evaluateExpression(expression: string, context?: AjfContext): an
return createFunction(expression)(context);
}

const globals = [
'this', 'true', 'false', 'null', 'undefined', 'Infinity', 'NaN', 'isNaN', 'isFinite',
'Object', 'String', 'Array', 'Set', 'Map', 'Number', 'Date', 'Math', 'parseInt', 'parseFloat',
];

type Func = (c?: AjfContext) => any;

export function createFunction(expression: string): Func {
Expand All @@ -229,7 +230,11 @@ export function createFunction(expression: string): Func {
return _ => str;
}

const argNames = [...new Set(getCodeIdentifiers(expression, true)).add('execContext')];
const identifiers = new Set(getCodeIdentifiers(expression, true)).add('execContext');
for (const ide of globals) {
identifiers.delete(ide);
}
const argNames = [...identifiers];
let func: Function;
try {
func = new Function(...argNames, 'return ' + expression);
Expand Down

0 comments on commit 7e7306e

Please sign in to comment.