2021-12-23 14:17:20 +00:00
|
|
|
/* eslint-disable no-console */
|
chore: upgrade to prettier v2 + enforce import types (#21013)Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
2023-03-16 11:41:47 +00:00
|
|
|
import type { DataTree } from "entities/DataTree/dataTreeFactory";
|
|
|
|
|
import type { EvaluationError } from "utils/DynamicBindingUtils";
|
|
|
|
|
import { PropertyEvaluationErrorType } from "utils/DynamicBindingUtils";
|
2021-03-13 14:12:21 +00:00
|
|
|
import unescapeJS from "unescape-js";
|
2023-02-11 18:33:20 +00:00
|
|
|
import { Severity } from "entities/AppsmithConsole";
|
chore: upgrade to prettier v2 + enforce import types (#21013)Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
2023-03-16 11:41:47 +00:00
|
|
|
import type { EventType } from "constants/AppsmithActionConstants/ActionConstants";
|
|
|
|
|
import type { TriggerMeta } from "@appsmith/sagas/ActionExecution/ActionExecutionSagas";
|
2022-11-30 21:58:58 +00:00
|
|
|
import indirectEval from "./indirectEval";
|
2023-02-11 18:33:20 +00:00
|
|
|
import { jsObjectFunctionFactory } from "./fns/utils/jsObjectFnFactory";
|
2022-12-21 17:14:47 +00:00
|
|
|
import { DOM_APIS } from "./SetupDOM";
|
|
|
|
|
import { JSLibraries, libraryReservedIdentifiers } from "../common/JSLibrary";
|
2022-12-23 10:04:39 +00:00
|
|
|
import { errorModifier, FoundPromiseInSyncEvalError } from "./errorModifier";
|
2023-01-10 00:25:39 +00:00
|
|
|
import { addDataTreeToContext } from "@appsmith/workers/Evaluation/Actions";
|
2021-03-13 14:12:21 +00:00
|
|
|
|
|
|
|
|
export type EvalResult = {
|
|
|
|
|
result: any;
|
2021-06-21 11:09:51 +00:00
|
|
|
errors: EvaluationError[];
|
2021-03-13 14:12:21 +00:00
|
|
|
};
|
|
|
|
|
|
2021-06-21 11:09:51 +00:00
|
|
|
export enum EvaluationScriptType {
|
|
|
|
|
EXPRESSION = "EXPRESSION",
|
|
|
|
|
ANONYMOUS_FUNCTION = "ANONYMOUS_FUNCTION",
|
2022-02-28 09:35:43 +00:00
|
|
|
ASYNC_ANONYMOUS_FUNCTION = "ASYNC_ANONYMOUS_FUNCTION",
|
2021-07-07 09:59:44 +00:00
|
|
|
TRIGGERS = "TRIGGERS",
|
2023-04-03 10:41:15 +00:00
|
|
|
OBJECT_PROPERTY = "OBJECT_PROPERTY",
|
2021-06-21 11:09:51 +00:00
|
|
|
}
|
|
|
|
|
|
2021-10-07 12:33:15 +00:00
|
|
|
export const ScriptTemplate = "<<string>>";
|
|
|
|
|
|
2021-10-05 13:52:27 +00:00
|
|
|
export const EvaluationScripts: Record<EvaluationScriptType, string> = {
|
2021-09-17 10:31:45 +00:00
|
|
|
[EvaluationScriptType.EXPRESSION]: `
|
2023-03-06 11:47:15 +00:00
|
|
|
function $$closedFn () {
|
|
|
|
|
const $$result = ${ScriptTemplate}
|
|
|
|
|
return $$result
|
2021-07-15 04:59:04 +00:00
|
|
|
}
|
2023-03-06 11:47:15 +00:00
|
|
|
$$closedFn.call(THIS_CONTEXT)
|
2021-07-15 04:59:04 +00:00
|
|
|
`,
|
2021-09-17 10:31:45 +00:00
|
|
|
[EvaluationScriptType.ANONYMOUS_FUNCTION]: `
|
2023-03-06 11:47:15 +00:00
|
|
|
function $$closedFn (script) {
|
|
|
|
|
const $$userFunction = script;
|
|
|
|
|
const $$result = $$userFunction?.apply(THIS_CONTEXT, ARGUMENTS);
|
|
|
|
|
return $$result
|
2021-07-15 04:59:04 +00:00
|
|
|
}
|
2023-03-06 11:47:15 +00:00
|
|
|
$$closedFn(${ScriptTemplate})
|
2021-07-15 04:59:04 +00:00
|
|
|
`,
|
2022-02-28 09:35:43 +00:00
|
|
|
[EvaluationScriptType.ASYNC_ANONYMOUS_FUNCTION]: `
|
2023-03-08 18:38:58 +00:00
|
|
|
async function $$closedFn (script) {
|
2023-03-06 11:47:15 +00:00
|
|
|
const $$userFunction = script;
|
|
|
|
|
const $$result = $$userFunction?.apply(THIS_CONTEXT, ARGUMENTS);
|
|
|
|
|
return await $$result;
|
2022-02-28 09:35:43 +00:00
|
|
|
}
|
2023-03-06 11:47:15 +00:00
|
|
|
$$closedFn(${ScriptTemplate})
|
2022-02-28 09:35:43 +00:00
|
|
|
`,
|
2021-09-17 10:31:45 +00:00
|
|
|
[EvaluationScriptType.TRIGGERS]: `
|
2023-03-06 11:47:15 +00:00
|
|
|
async function $$closedFn () {
|
|
|
|
|
const $$result = ${ScriptTemplate};
|
|
|
|
|
return await $$result
|
2021-07-15 04:59:04 +00:00
|
|
|
}
|
2023-03-06 11:47:15 +00:00
|
|
|
$$closedFn.call(THIS_CONTEXT)
|
2021-07-15 04:59:04 +00:00
|
|
|
`,
|
2023-04-03 10:41:15 +00:00
|
|
|
[EvaluationScriptType.OBJECT_PROPERTY]: `
|
|
|
|
|
function $$closedFn () {
|
|
|
|
|
const $$result = {${ScriptTemplate}}
|
|
|
|
|
return $$result
|
|
|
|
|
}
|
|
|
|
|
$$closedFn.call(THIS_CONTEXT)
|
|
|
|
|
`,
|
2021-06-21 11:09:51 +00:00
|
|
|
};
|
|
|
|
|
|
2022-09-30 01:31:05 +00:00
|
|
|
const topLevelWorkerAPIs = Object.keys(self).reduce((acc, key: string) => {
|
|
|
|
|
acc[key] = true;
|
|
|
|
|
return acc;
|
|
|
|
|
}, {} as any);
|
|
|
|
|
|
|
|
|
|
function resetWorkerGlobalScope() {
|
2023-02-11 18:33:20 +00:00
|
|
|
self.$isDataField = false;
|
2022-09-30 01:31:05 +00:00
|
|
|
for (const key of Object.keys(self)) {
|
2022-12-21 17:14:47 +00:00
|
|
|
if (topLevelWorkerAPIs[key] || DOM_APIS[key]) continue;
|
|
|
|
|
//TODO: Remove this once we have a better way to handle this
|
|
|
|
|
if (["evaluationVersion", "window", "document", "location"].includes(key))
|
|
|
|
|
continue;
|
|
|
|
|
if (JSLibraries.find((lib) => lib.accessor.includes(key))) continue;
|
|
|
|
|
if (libraryReservedIdentifiers[key]) continue;
|
|
|
|
|
try {
|
|
|
|
|
// @ts-expect-error: Types are not available
|
|
|
|
|
delete self[key];
|
|
|
|
|
} catch (e) {
|
|
|
|
|
// @ts-expect-error: Types are not available
|
|
|
|
|
self[key] = undefined;
|
|
|
|
|
}
|
2022-09-30 01:31:05 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-22 20:31:08 +00:00
|
|
|
export const getScriptType = (
|
2021-12-23 14:17:20 +00:00
|
|
|
evalArgumentsExist = false,
|
2021-07-07 09:59:44 +00:00
|
|
|
isTriggerBased = false,
|
2021-08-26 04:45:17 +00:00
|
|
|
): EvaluationScriptType => {
|
2021-07-07 09:59:44 +00:00
|
|
|
let scriptType = EvaluationScriptType.EXPRESSION;
|
2022-02-28 09:35:43 +00:00
|
|
|
if (evalArgumentsExist && isTriggerBased) {
|
|
|
|
|
scriptType = EvaluationScriptType.ASYNC_ANONYMOUS_FUNCTION;
|
|
|
|
|
} else if (evalArgumentsExist && !isTriggerBased) {
|
2021-07-07 09:59:44 +00:00
|
|
|
scriptType = EvaluationScriptType.ANONYMOUS_FUNCTION;
|
2022-02-28 09:35:43 +00:00
|
|
|
} else if (isTriggerBased && !evalArgumentsExist) {
|
2021-07-07 09:59:44 +00:00
|
|
|
scriptType = EvaluationScriptType.TRIGGERS;
|
|
|
|
|
}
|
2021-08-26 04:45:17 +00:00
|
|
|
return scriptType;
|
|
|
|
|
};
|
|
|
|
|
|
2022-12-21 17:14:47 +00:00
|
|
|
export const additionalLibrariesNames: string[] = [];
|
|
|
|
|
|
2021-10-05 13:52:27 +00:00
|
|
|
export const getScriptToEval = (
|
2021-08-26 04:45:17 +00:00
|
|
|
userScript: string,
|
2021-09-17 10:31:45 +00:00
|
|
|
type: EvaluationScriptType,
|
2021-08-26 04:45:17 +00:00
|
|
|
): string => {
|
2021-10-07 12:33:15 +00:00
|
|
|
// Using replace here would break scripts with replacement patterns (ex: $&, $$)
|
|
|
|
|
const buffer = EvaluationScripts[type].split(ScriptTemplate);
|
|
|
|
|
return `${buffer[0]}${userScript}${buffer[1]}`;
|
2021-06-21 11:09:51 +00:00
|
|
|
};
|
|
|
|
|
|
2021-10-05 13:52:27 +00:00
|
|
|
const beginsWithLineBreakRegex = /^\s+|\s+$/;
|
2022-12-23 10:04:39 +00:00
|
|
|
|
|
|
|
|
export type EvalContext = Record<string, any>;
|
|
|
|
|
type ResolvedFunctions = Record<string, any>;
|
|
|
|
|
export interface createEvaluationContextArgs {
|
2022-07-22 20:31:08 +00:00
|
|
|
dataTree: DataTree;
|
2022-12-23 10:04:39 +00:00
|
|
|
resolvedFunctions: ResolvedFunctions;
|
2022-07-22 20:31:08 +00:00
|
|
|
context?: EvaluateContext;
|
|
|
|
|
isTriggerBased: boolean;
|
2022-12-23 10:04:39 +00:00
|
|
|
evalArguments?: Array<unknown>;
|
2023-04-03 10:41:15 +00:00
|
|
|
/*
|
|
|
|
|
Whether to remove functions like "run", "clear" from entities in global context
|
|
|
|
|
use case => To show lint warning when Api.run is used in a function bound to a data field (Eg. Button.text)
|
|
|
|
|
*/
|
|
|
|
|
removeEntityFunctions?: boolean;
|
2022-07-22 20:31:08 +00:00
|
|
|
}
|
2022-12-23 10:04:39 +00:00
|
|
|
/**
|
|
|
|
|
* This method created an object with dataTree and appsmith's framework actions that needs to be added to worker global scope for the JS code evaluation to then consume it.
|
|
|
|
|
*
|
|
|
|
|
* Example:
|
|
|
|
|
* - For `eval("Table1.tableData")` code to work as expected, we define Table1.tableData in worker global scope and for that we use `createEvaluationContext` to get the object to set in global scope.
|
|
|
|
|
*/
|
|
|
|
|
export const createEvaluationContext = (args: createEvaluationContextArgs) => {
|
2022-07-22 20:31:08 +00:00
|
|
|
const {
|
|
|
|
|
context,
|
|
|
|
|
dataTree,
|
|
|
|
|
evalArguments,
|
|
|
|
|
isTriggerBased,
|
2023-04-03 10:41:15 +00:00
|
|
|
removeEntityFunctions,
|
2022-07-22 20:31:08 +00:00
|
|
|
resolvedFunctions,
|
|
|
|
|
} = args;
|
|
|
|
|
|
2022-12-23 10:04:39 +00:00
|
|
|
const EVAL_CONTEXT: EvalContext = {};
|
2021-10-05 13:52:27 +00:00
|
|
|
///// Adding callback data
|
2022-12-23 10:04:39 +00:00
|
|
|
EVAL_CONTEXT.ARGUMENTS = evalArguments;
|
2022-02-04 12:28:46 +00:00
|
|
|
//// Adding contextual data not part of data tree
|
2022-12-23 10:04:39 +00:00
|
|
|
EVAL_CONTEXT.THIS_CONTEXT = context?.thisContext || {};
|
|
|
|
|
|
|
|
|
|
if (context?.globalContext) {
|
|
|
|
|
Object.assign(EVAL_CONTEXT, context.globalContext);
|
2022-02-11 10:52:27 +00:00
|
|
|
}
|
2022-12-23 10:04:39 +00:00
|
|
|
|
|
|
|
|
addDataTreeToContext({
|
|
|
|
|
EVAL_CONTEXT,
|
|
|
|
|
dataTree,
|
2023-04-03 10:41:15 +00:00
|
|
|
removeEntityFunctions: !!removeEntityFunctions,
|
2022-12-23 10:04:39 +00:00
|
|
|
isTriggerBased,
|
|
|
|
|
});
|
|
|
|
|
|
2023-02-11 18:33:20 +00:00
|
|
|
assignJSFunctionsToContext(EVAL_CONTEXT, resolvedFunctions, isTriggerBased);
|
2022-12-23 10:04:39 +00:00
|
|
|
|
|
|
|
|
return EVAL_CONTEXT;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const assignJSFunctionsToContext = (
|
|
|
|
|
EVAL_CONTEXT: EvalContext,
|
|
|
|
|
resolvedFunctions: ResolvedFunctions,
|
2023-02-11 18:33:20 +00:00
|
|
|
isTriggerBased: boolean,
|
2022-12-23 10:04:39 +00:00
|
|
|
) => {
|
|
|
|
|
const jsObjectNames = Object.keys(resolvedFunctions || {});
|
|
|
|
|
for (const jsObjectName of jsObjectNames) {
|
|
|
|
|
const resolvedObject = resolvedFunctions[jsObjectName];
|
|
|
|
|
const jsObject = EVAL_CONTEXT[jsObjectName];
|
|
|
|
|
const jsObjectFunction: Record<string, Record<"data", unknown>> = {};
|
|
|
|
|
if (!jsObject) continue;
|
|
|
|
|
for (const fnName of Object.keys(resolvedObject)) {
|
|
|
|
|
const fn = resolvedObject[fnName];
|
|
|
|
|
if (typeof fn !== "function") continue;
|
|
|
|
|
// Investigate promisify of JSObject function confirmation
|
|
|
|
|
// Task: https://github.com/appsmithorg/appsmith/issues/13289
|
|
|
|
|
// Previous implementation commented code: https://github.com/appsmithorg/appsmith/pull/18471
|
|
|
|
|
const data = jsObject[fnName]?.data;
|
2023-02-11 18:33:20 +00:00
|
|
|
jsObjectFunction[fnName] = isTriggerBased
|
|
|
|
|
? jsObjectFunctionFactory(fn, jsObjectName + "." + fnName)
|
2022-12-30 12:31:08 +00:00
|
|
|
: fn;
|
2022-12-23 10:04:39 +00:00
|
|
|
if (!!data) {
|
|
|
|
|
jsObjectFunction[fnName]["data"] = data;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
EVAL_CONTEXT[jsObjectName] = Object.assign({}, jsObject, jsObjectFunction);
|
2021-10-05 13:52:27 +00:00
|
|
|
}
|
2021-06-21 11:09:51 +00:00
|
|
|
};
|
|
|
|
|
|
2021-12-14 08:30:43 +00:00
|
|
|
export function sanitizeScript(js: string) {
|
2021-12-02 10:03:43 +00:00
|
|
|
// We remove any line breaks from the beginning of the script because that
|
|
|
|
|
// makes the final function invalid. We also unescape any escaped characters
|
|
|
|
|
// so that eval can happen
|
|
|
|
|
const trimmedJS = js.replace(beginsWithLineBreakRegex, "");
|
2021-12-14 08:30:43 +00:00
|
|
|
return self.evaluationVersion > 1 ? trimmedJS : unescapeJS(trimmedJS);
|
2021-12-02 10:03:43 +00:00
|
|
|
}
|
|
|
|
|
|
2021-12-14 08:30:43 +00:00
|
|
|
/** Define a context just for this script
|
2022-02-04 12:28:46 +00:00
|
|
|
* thisContext will define it on the `this`
|
|
|
|
|
* globalContext will define it globally
|
2022-01-14 06:20:01 +00:00
|
|
|
* requestId is used for completing promises
|
2021-12-14 08:30:43 +00:00
|
|
|
*/
|
|
|
|
|
export type EvaluateContext = {
|
2022-02-04 12:28:46 +00:00
|
|
|
thisContext?: Record<string, any>;
|
|
|
|
|
globalContext?: Record<string, any>;
|
2021-12-23 14:17:20 +00:00
|
|
|
requestId?: string;
|
2022-10-10 06:39:14 +00:00
|
|
|
eventType?: EventType;
|
2022-10-17 17:10:17 +00:00
|
|
|
triggerMeta?: TriggerMeta;
|
2021-12-14 08:30:43 +00:00
|
|
|
};
|
|
|
|
|
|
2021-12-23 14:17:20 +00:00
|
|
|
export const getUserScriptToEvaluate = (
|
|
|
|
|
userScript: string,
|
|
|
|
|
isTriggerBased: boolean,
|
2021-06-21 11:09:51 +00:00
|
|
|
evalArguments?: Array<any>,
|
2021-12-23 14:17:20 +00:00
|
|
|
) => {
|
|
|
|
|
const unescapedJS = sanitizeScript(userScript);
|
2022-07-22 20:31:08 +00:00
|
|
|
// If nothing is present to evaluate, return
|
2021-12-23 14:17:20 +00:00
|
|
|
if (!unescapedJS.length) {
|
2021-12-21 14:30:19 +00:00
|
|
|
return {
|
2021-12-23 14:17:20 +00:00
|
|
|
script: "",
|
2021-12-21 14:30:19 +00:00
|
|
|
};
|
|
|
|
|
}
|
2021-12-23 14:17:20 +00:00
|
|
|
const scriptType = getScriptType(!!evalArguments, isTriggerBased);
|
|
|
|
|
const script = getScriptToEval(unescapedJS, scriptType);
|
2022-07-22 20:31:08 +00:00
|
|
|
return { script };
|
2021-12-23 14:17:20 +00:00
|
|
|
};
|
2021-12-21 14:30:19 +00:00
|
|
|
|
2021-12-23 14:17:20 +00:00
|
|
|
export default function evaluateSync(
|
|
|
|
|
userScript: string,
|
|
|
|
|
dataTree: DataTree,
|
|
|
|
|
resolvedFunctions: Record<string, any>,
|
2022-02-11 10:52:27 +00:00
|
|
|
isJSCollection: boolean,
|
2022-02-04 12:28:46 +00:00
|
|
|
context?: EvaluateContext,
|
2021-12-23 14:17:20 +00:00
|
|
|
evalArguments?: Array<any>,
|
|
|
|
|
): EvalResult {
|
chore: upgrade to prettier v2 + enforce import types (#21013)Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
2023-03-16 11:41:47 +00:00
|
|
|
return (function () {
|
2022-09-30 01:31:05 +00:00
|
|
|
resetWorkerGlobalScope();
|
2022-07-22 20:31:08 +00:00
|
|
|
const errors: EvaluationError[] = [];
|
2021-06-21 11:09:51 +00:00
|
|
|
let result;
|
2023-01-09 08:04:53 +00:00
|
|
|
|
2022-09-07 06:23:47 +00:00
|
|
|
// skipping log reset if the js collection is being evaluated without run
|
|
|
|
|
// Doing this because the promise execution is losing logs in the process due to resets
|
2021-03-13 14:12:21 +00:00
|
|
|
/**** Setting the eval context ****/
|
2022-12-23 10:04:39 +00:00
|
|
|
const evalContext: EvalContext = createEvaluationContext({
|
2021-12-23 14:17:20 +00:00
|
|
|
dataTree,
|
2021-10-05 13:52:27 +00:00
|
|
|
resolvedFunctions,
|
2022-02-04 12:28:46 +00:00
|
|
|
context,
|
2021-10-05 13:52:27 +00:00
|
|
|
evalArguments,
|
2022-12-23 10:04:39 +00:00
|
|
|
isTriggerBased: isJSCollection,
|
2022-07-22 20:31:08 +00:00
|
|
|
});
|
2022-12-23 10:04:39 +00:00
|
|
|
|
2023-02-11 18:33:20 +00:00
|
|
|
evalContext["$isDataField"] = true;
|
2022-12-23 10:04:39 +00:00
|
|
|
|
2022-07-22 20:31:08 +00:00
|
|
|
const { script } = getUserScriptToEvaluate(
|
2021-12-23 14:17:20 +00:00
|
|
|
userScript,
|
|
|
|
|
false,
|
|
|
|
|
evalArguments,
|
|
|
|
|
);
|
2022-12-23 10:04:39 +00:00
|
|
|
|
2021-12-23 14:17:20 +00:00
|
|
|
// If nothing is present to evaluate, return instead of evaluating
|
|
|
|
|
if (!script.length) {
|
|
|
|
|
return {
|
|
|
|
|
errors: [],
|
|
|
|
|
result: undefined,
|
|
|
|
|
triggers: [],
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-13 14:12:21 +00:00
|
|
|
// Set it to self so that the eval function can have access to it
|
|
|
|
|
// as global data. This is what enables access all appsmith
|
|
|
|
|
// entity properties from the global context
|
2022-12-23 10:04:39 +00:00
|
|
|
Object.assign(self, evalContext);
|
2021-03-13 14:12:21 +00:00
|
|
|
|
2021-06-21 11:09:51 +00:00
|
|
|
try {
|
2022-11-30 21:58:58 +00:00
|
|
|
result = indirectEval(script);
|
2022-12-23 10:04:39 +00:00
|
|
|
if (result instanceof Promise) {
|
|
|
|
|
/**
|
2023-04-03 10:41:15 +00:00
|
|
|
* If a promise is returned in data field then show the error to help understand data field doesn't await to resolve promise.
|
|
|
|
|
* NOTE: Awaiting for promise will make data field evaluation slower.
|
2022-12-23 10:04:39 +00:00
|
|
|
*/
|
|
|
|
|
throw new FoundPromiseInSyncEvalError();
|
|
|
|
|
}
|
2022-06-21 13:57:34 +00:00
|
|
|
} catch (error) {
|
2023-04-03 10:41:15 +00:00
|
|
|
const { errorCategory, errorMessage } = errorModifier.run(error as Error);
|
2021-06-21 11:09:51 +00:00
|
|
|
errors.push({
|
2023-04-03 10:41:15 +00:00
|
|
|
errorMessage,
|
2021-06-21 11:09:51 +00:00
|
|
|
severity: Severity.ERROR,
|
|
|
|
|
raw: script,
|
|
|
|
|
errorType: PropertyEvaluationErrorType.PARSE,
|
2021-12-23 14:17:20 +00:00
|
|
|
originalBinding: userScript,
|
2023-04-03 10:41:15 +00:00
|
|
|
kind: errorCategory && {
|
|
|
|
|
category: errorCategory,
|
|
|
|
|
rootcause: "",
|
|
|
|
|
},
|
2021-06-21 11:09:51 +00:00
|
|
|
});
|
2021-12-23 14:17:20 +00:00
|
|
|
} finally {
|
2022-12-23 10:04:39 +00:00
|
|
|
for (const entityName in evalContext) {
|
|
|
|
|
if (evalContext.hasOwnProperty(entityName)) {
|
|
|
|
|
// @ts-expect-error: Types are not available
|
|
|
|
|
delete self[entityName];
|
|
|
|
|
}
|
2021-12-23 14:17:20 +00:00
|
|
|
}
|
2023-02-11 18:33:20 +00:00
|
|
|
self["$isDataField"] = false;
|
2021-06-21 11:09:51 +00:00
|
|
|
}
|
2023-02-11 18:33:20 +00:00
|
|
|
return { result, errors };
|
2021-12-23 14:17:20 +00:00
|
|
|
})();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function evaluateAsync(
|
|
|
|
|
userScript: string,
|
|
|
|
|
dataTree: DataTree,
|
|
|
|
|
resolvedFunctions: Record<string, any>,
|
|
|
|
|
context?: EvaluateContext,
|
|
|
|
|
evalArguments?: Array<any>,
|
|
|
|
|
) {
|
chore: upgrade to prettier v2 + enforce import types (#21013)Co-authored-by: Satish Gandham <hello@satishgandham.com> Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
https://github.com/appsmithorg/appsmith/commit/7cbb12af886621256224be0c93e6a465dd710ad3,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
2023-03-16 11:41:47 +00:00
|
|
|
return (async function () {
|
2022-09-30 01:31:05 +00:00
|
|
|
resetWorkerGlobalScope();
|
2021-12-23 14:17:20 +00:00
|
|
|
const errors: EvaluationError[] = [];
|
|
|
|
|
let result;
|
2023-01-09 08:04:53 +00:00
|
|
|
|
|
|
|
|
/**** Setting the eval context ****/
|
2022-12-23 10:04:39 +00:00
|
|
|
const evalContext: EvalContext = createEvaluationContext({
|
2021-12-23 14:17:20 +00:00
|
|
|
dataTree,
|
|
|
|
|
resolvedFunctions,
|
2022-12-21 17:14:47 +00:00
|
|
|
context,
|
2021-12-23 14:17:20 +00:00
|
|
|
evalArguments,
|
2022-12-23 10:04:39 +00:00
|
|
|
isTriggerBased: true,
|
2022-07-22 20:31:08 +00:00
|
|
|
});
|
2023-01-09 08:04:53 +00:00
|
|
|
|
2022-07-22 20:31:08 +00:00
|
|
|
const { script } = getUserScriptToEvaluate(userScript, true, evalArguments);
|
2023-02-11 18:33:20 +00:00
|
|
|
evalContext["$isDataField"] = false;
|
2022-12-23 10:04:39 +00:00
|
|
|
|
2021-12-23 14:17:20 +00:00
|
|
|
// Set it to self so that the eval function can have access to it
|
|
|
|
|
// as global data. This is what enables access all appsmith
|
|
|
|
|
// entity properties from the global context
|
2022-12-23 10:04:39 +00:00
|
|
|
Object.assign(self, evalContext);
|
2021-12-23 14:17:20 +00:00
|
|
|
|
|
|
|
|
try {
|
2022-11-30 21:58:58 +00:00
|
|
|
result = await indirectEval(script);
|
2023-01-02 07:40:24 +00:00
|
|
|
} catch (e) {
|
|
|
|
|
const error = e as Error;
|
|
|
|
|
const errorMessage = error.name
|
2023-02-18 12:55:46 +00:00
|
|
|
? { name: error.name, message: error.message }
|
|
|
|
|
: {
|
|
|
|
|
name: "UncaughtPromiseRejection",
|
|
|
|
|
message: `${error.message}`,
|
|
|
|
|
};
|
2021-12-23 14:17:20 +00:00
|
|
|
errors.push({
|
|
|
|
|
errorMessage: errorMessage,
|
|
|
|
|
severity: Severity.ERROR,
|
|
|
|
|
raw: script,
|
|
|
|
|
errorType: PropertyEvaluationErrorType.PARSE,
|
|
|
|
|
originalBinding: userScript,
|
|
|
|
|
});
|
|
|
|
|
} finally {
|
2022-12-21 17:14:47 +00:00
|
|
|
return {
|
|
|
|
|
result,
|
|
|
|
|
errors,
|
|
|
|
|
};
|
2021-09-21 06:02:45 +00:00
|
|
|
}
|
2021-12-23 14:17:20 +00:00
|
|
|
})();
|
|
|
|
|
}
|