PromucFlow_constructor/app/client/packages/ast/src/utils.ts

61 lines
2.0 KiB
TypeScript
Raw Normal View History

feat: show lint errors in async functions bound to sync fields (#21187) ## Description This PR improves the error resolution journey for users. Lint warnings are added to async JS functions which are bound to data fields (sync fields). - JSObjects are "linted" by individual properties (as opposed to being "linted" as a whole) - Only edited jsobject properties get "linted", improving jsObject linting by ~35%.(This largely depends on the size of the JSObject) <img width="500" alt="Screenshot 2023-04-03 at 11 17 45" src="https://user-images.githubusercontent.com/46670083/229482424-233f3950-ffec-46f5-8c42-680dff6a412f.png"> <img width="500" alt="Screenshot 2023-03-14 at 11 26 00" src="https://user-images.githubusercontent.com/46670083/224975572-b2d8d404-aac6-43fb-be14-20edf7c56117.png"> <img width="500" alt="Screenshot 2023-03-14 at 11 41 11" src="https://user-images.githubusercontent.com/46670083/224975952-c40848b1-69d8-489d-9b62-24127ea1a2f1.png"> Fixes #20289 Fixes #20008 ## Type of change - Bug fix (non-breaking change which fixes an issue) ## How Has This Been Tested? - CYPRESS - JEST ### 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 - [ ] 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 - [ ] 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 - [x] Test plan has been peer reviewed by QA - [x] 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
2023-04-03 10:41:15 +00:00
import unescapeJS from "unescape-js";
import type { PropertyNode } from "../index";
import { isLiteralNode } from "../index";
const beginsWithLineBreakRegex = /^\s+|\s+$/;
export function sanitizeScript(js: string, evaluationVersion: number) {
// 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
//default value of evalutaion version is 2
evaluationVersion = evaluationVersion ? evaluationVersion : 2;
feat: show lint errors in async functions bound to sync fields (#21187) ## Description This PR improves the error resolution journey for users. Lint warnings are added to async JS functions which are bound to data fields (sync fields). - JSObjects are "linted" by individual properties (as opposed to being "linted" as a whole) - Only edited jsobject properties get "linted", improving jsObject linting by ~35%.(This largely depends on the size of the JSObject) <img width="500" alt="Screenshot 2023-04-03 at 11 17 45" src="https://user-images.githubusercontent.com/46670083/229482424-233f3950-ffec-46f5-8c42-680dff6a412f.png"> <img width="500" alt="Screenshot 2023-03-14 at 11 26 00" src="https://user-images.githubusercontent.com/46670083/224975572-b2d8d404-aac6-43fb-be14-20edf7c56117.png"> <img width="500" alt="Screenshot 2023-03-14 at 11 41 11" src="https://user-images.githubusercontent.com/46670083/224975952-c40848b1-69d8-489d-9b62-24127ea1a2f1.png"> Fixes #20289 Fixes #20008 ## Type of change - Bug fix (non-breaking change which fixes an issue) ## How Has This Been Tested? - CYPRESS - JEST ### 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 - [ ] 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 - [ ] 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 - [x] Test plan has been peer reviewed by QA - [x] 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
2023-04-03 10:41:15 +00:00
const trimmedJS = js.replace(beginsWithLineBreakRegex, "");
return evaluationVersion > 1 ? trimmedJS : unescapeJS(trimmedJS);
}
feat: Linting in entity properties and methods (#16171) * Initial commit * Remove arrow function params from identifiers * Remove invalid identifiers from extracted identifiers * Remove invalid identifiers which are derived from function params and variable declarations * Fix typo error * Correctly remove invalid identifiers * Remove invalid names from identifier list * fix build failure * Add Promise to list of unacceptable entity name * Keep track of unreferenced identifiers in bindings * Add Global scope object names as unusable entity names * Keep track of unreferenced identifiers * Prevent traversal of data tree for addition of new paths and entities * Sync linting in trigger fields * Support linting of invalid properties * Fix linting reactivity bug in trigger field * Remove unused objects * Fix conflict in merging * Lint jsobject body for function change * Remove unused map from tests * Code cleanup * Modify jest tests * Update jest tests * Fix cypress tests * Code cleanup * Support linting of multiple bindings * Set squiggle line as long as invalid property length * Add jest tests * Minor code refactor * Move ast to shared repo * Rename confusing identifiers * Improve naming of functions and their return values * move shared widget validation utils and constants to shared folder * Add jest test for invalid entity names * Add cypress tests * Modify test comment * Extend list of dedicated worker scope identifiers * Resolve code review comments * Resolve review comments * Annonate code where necessary * Code refactor * Improve worker global scope object * Code refactor * Fix merge conflict * Code refactor * Minor bug fix * Redundant commit to retrigger vercel build * Add null checks to dependecy chain
2022-09-17 17:40:28 +00:00
// For the times when you need to know if something truly an object like { a: 1, b: 2}
// typeof, lodash.isObject and others will return false positives for things like array, null, etc
export const isTrueObject = (
feat: show lint errors in async functions bound to sync fields (#21187) ## Description This PR improves the error resolution journey for users. Lint warnings are added to async JS functions which are bound to data fields (sync fields). - JSObjects are "linted" by individual properties (as opposed to being "linted" as a whole) - Only edited jsobject properties get "linted", improving jsObject linting by ~35%.(This largely depends on the size of the JSObject) <img width="500" alt="Screenshot 2023-04-03 at 11 17 45" src="https://user-images.githubusercontent.com/46670083/229482424-233f3950-ffec-46f5-8c42-680dff6a412f.png"> <img width="500" alt="Screenshot 2023-03-14 at 11 26 00" src="https://user-images.githubusercontent.com/46670083/224975572-b2d8d404-aac6-43fb-be14-20edf7c56117.png"> <img width="500" alt="Screenshot 2023-03-14 at 11 41 11" src="https://user-images.githubusercontent.com/46670083/224975952-c40848b1-69d8-489d-9b62-24127ea1a2f1.png"> Fixes #20289 Fixes #20008 ## Type of change - Bug fix (non-breaking change which fixes an issue) ## How Has This Been Tested? - CYPRESS - JEST ### 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 - [ ] 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 - [ ] 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 - [x] Test plan has been peer reviewed by QA - [x] 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
2023-04-03 10:41:15 +00:00
item: unknown,
feat: Linting in entity properties and methods (#16171) * Initial commit * Remove arrow function params from identifiers * Remove invalid identifiers from extracted identifiers * Remove invalid identifiers which are derived from function params and variable declarations * Fix typo error * Correctly remove invalid identifiers * Remove invalid names from identifier list * fix build failure * Add Promise to list of unacceptable entity name * Keep track of unreferenced identifiers in bindings * Add Global scope object names as unusable entity names * Keep track of unreferenced identifiers * Prevent traversal of data tree for addition of new paths and entities * Sync linting in trigger fields * Support linting of invalid properties * Fix linting reactivity bug in trigger field * Remove unused objects * Fix conflict in merging * Lint jsobject body for function change * Remove unused map from tests * Code cleanup * Modify jest tests * Update jest tests * Fix cypress tests * Code cleanup * Support linting of multiple bindings * Set squiggle line as long as invalid property length * Add jest tests * Minor code refactor * Move ast to shared repo * Rename confusing identifiers * Improve naming of functions and their return values * move shared widget validation utils and constants to shared folder * Add jest test for invalid entity names * Add cypress tests * Modify test comment * Extend list of dedicated worker scope identifiers * Resolve code review comments * Resolve review comments * Annonate code where necessary * Code refactor * Improve worker global scope object * Code refactor * Fix merge conflict * Code refactor * Minor bug fix * Redundant commit to retrigger vercel build * Add null checks to dependecy chain
2022-09-17 17:40:28 +00:00
): item is Record<string, unknown> => {
feat: show lint errors in async functions bound to sync fields (#21187) ## Description This PR improves the error resolution journey for users. Lint warnings are added to async JS functions which are bound to data fields (sync fields). - JSObjects are "linted" by individual properties (as opposed to being "linted" as a whole) - Only edited jsobject properties get "linted", improving jsObject linting by ~35%.(This largely depends on the size of the JSObject) <img width="500" alt="Screenshot 2023-04-03 at 11 17 45" src="https://user-images.githubusercontent.com/46670083/229482424-233f3950-ffec-46f5-8c42-680dff6a412f.png"> <img width="500" alt="Screenshot 2023-03-14 at 11 26 00" src="https://user-images.githubusercontent.com/46670083/224975572-b2d8d404-aac6-43fb-be14-20edf7c56117.png"> <img width="500" alt="Screenshot 2023-03-14 at 11 41 11" src="https://user-images.githubusercontent.com/46670083/224975952-c40848b1-69d8-489d-9b62-24127ea1a2f1.png"> Fixes #20289 Fixes #20008 ## Type of change - Bug fix (non-breaking change which fixes an issue) ## How Has This Been Tested? - CYPRESS - JEST ### 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 - [ ] 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 - [ ] 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 - [x] Test plan has been peer reviewed by QA - [x] 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
2023-04-03 10:41:15 +00:00
return Object.prototype.toString.call(item) === "[object Object]";
};
export const getNameFromPropertyNode = (node: PropertyNode): string =>
isLiteralNode(node.key) ? String(node.key.value) : node.key.name;
type Position = {
line: number;
ch: number;
};
export const extractContentByPosition = (
content: string,
position: { from: Position; to: Position },
) => {
const eachLine = content.split("\n");
let returnedString = "";
for (let i = position.from.line; i <= position.to.line; i++) {
if (i === position.from.line) {
returnedString =
position.from.line !== position.to.line
? eachLine[position.from.line].slice(position.from.ch)
: eachLine[position.from.line].slice(
position.from.ch,
position.to.ch + 1,
);
} else if (i === position.to.line) {
returnedString += eachLine[position.to.line].slice(0, position.to.ch + 1);
} else {
returnedString += eachLine[i];
}
if (i !== position.to.line) {
returnedString += "\n";
}
}
return returnedString;
feat: Linting in entity properties and methods (#16171) * Initial commit * Remove arrow function params from identifiers * Remove invalid identifiers from extracted identifiers * Remove invalid identifiers which are derived from function params and variable declarations * Fix typo error * Correctly remove invalid identifiers * Remove invalid names from identifier list * fix build failure * Add Promise to list of unacceptable entity name * Keep track of unreferenced identifiers in bindings * Add Global scope object names as unusable entity names * Keep track of unreferenced identifiers * Prevent traversal of data tree for addition of new paths and entities * Sync linting in trigger fields * Support linting of invalid properties * Fix linting reactivity bug in trigger field * Remove unused objects * Fix conflict in merging * Lint jsobject body for function change * Remove unused map from tests * Code cleanup * Modify jest tests * Update jest tests * Fix cypress tests * Code cleanup * Support linting of multiple bindings * Set squiggle line as long as invalid property length * Add jest tests * Minor code refactor * Move ast to shared repo * Rename confusing identifiers * Improve naming of functions and their return values * move shared widget validation utils and constants to shared folder * Add jest test for invalid entity names * Add cypress tests * Modify test comment * Extend list of dedicated worker scope identifiers * Resolve code review comments * Resolve review comments * Annonate code where necessary * Code refactor * Improve worker global scope object * Code refactor * Fix merge conflict * Code refactor * Minor bug fix * Redundant commit to retrigger vercel build * Add null checks to dependecy chain
2022-09-17 17:40:28 +00:00
};