Skip to content

Commit

Permalink
Revert "Revert back to old way of eval (appsmithorg#5851)"
Browse files Browse the repository at this point in the history
This reverts commit a24e0b1.
  • Loading branch information
hetunandu committed Jul 15, 2021
1 parent e751313 commit 3cc145c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 56 deletions.
34 changes: 5 additions & 29 deletions app/client/src/workers/evaluate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
},
],
Expand All @@ -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",
},
],
Expand Down Expand Up @@ -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",
},
],
Expand Down
36 changes: 9 additions & 27 deletions app/client/src/workers/evaluate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -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]}`,
Expand Down

0 comments on commit 3cc145c

Please sign in to comment.