Revert "Revert back to old way of eval (#5851)"

This reverts commit a24e0b180a.
This commit is contained in:
hetunandu 2021-07-15 12:04:46 +05:30
parent e751313b14
commit 3cc145cd55
2 changed files with 14 additions and 56 deletions

View File

@ -48,27 +48,15 @@ describe("evaluate", () => {
errors: [
{
errorMessage: "'wrongJS' is not defined.",
errorSegment: " const result = wrongJS",
errorSegment: "return wrongJS",
errorType: "LINT",
raw: `
function closedFunction () {
const result = wrongJS
return result;
}
closedFunction()
`,
raw: "return wrongJS",
severity: "warning",
},
{
errorMessage: "ReferenceError: wrongJS is not defined",
errorType: "PARSE",
raw: `
function closedFunction () {
const result = wrongJS
return result;
}
closedFunction()
`,
raw: "return wrongJS",
severity: "error",
},
],
@ -81,13 +69,7 @@ describe("evaluate", () => {
{
errorMessage: "TypeError: {}.map is not a function",
errorType: "PARSE",
raw: `
function closedFunction () {
const result = {}.map()
return result;
}
closedFunction()
`,
raw: "return {}.map()",
severity: "error",
},
],
@ -122,13 +104,7 @@ describe("evaluate", () => {
{
errorMessage: "TypeError: setTimeout is not a function",
errorType: "PARSE",
raw: `
function closedFunction () {
const result = setTimeout(() => {}, 100)
return result;
}
closedFunction()
`,
raw: "return setTimeout(() => {}, 100)",
severity: "error",
},
],

View File

@ -27,27 +27,11 @@ const evaluationScripts: Record<
EvaluationScriptType,
(script: string) => string
> = {
[EvaluationScriptType.EXPRESSION]: (script: string) => `
function closedFunction () {
const result = ${script}
return result;
}
closedFunction()
`,
[EvaluationScriptType.ANONYMOUS_FUNCTION]: (script) => `
function callback (script) {
const userFunction = script;
const result = userFunction.apply(self, ARGUMENTS);
return result;
}
callback(${script})
`,
[EvaluationScriptType.TRIGGERS]: (script) => `
function closedFunction () {
const result = ${script}
}
closedFunction()
`,
[EvaluationScriptType.EXPRESSION]: (script: string) => `return ${script}`,
[EvaluationScriptType.ANONYMOUS_FUNCTION]: (script) =>
`const userFunction = ${script}
return userFunction.apply(self, ARGUMENTS)`,
[EvaluationScriptType.TRIGGERS]: (script) => `(function() { ${script} })()`,
};
const getScriptToEval = (
@ -165,12 +149,10 @@ export default function evaluate(
self[func] = undefined;
});
try {
result = eval(script);
if (isTriggerBased) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
triggers = [...self.triggers];
}
result = Function(script)();
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
triggers = [...self.triggers];
} catch (e) {
errors.push({
errorMessage: `${e.stack.split(`\n`)[0]}`,