PromucFlow_constructor/app/client/packages/ast/src/utils.ts
Favour Ohanekwu 4b3ef8ebd6
fix: improve autocompletion hints discovery (#28222)
## Description

This PR improves autocompletion hints discovery by

- Taking entities' recency of usage into consideration when sorting
hints
- Showing entity names at the top, before supported functions and
properties
- Deprioritizing the Function constructor and the MainContainer entity

#### PR fixes following issue(s)
Fixes #27870 
Fixes #17684 
Fixes https://github.com/appsmithorg/appsmith/issues/24975


#### Type of change
- New feature (non-breaking change which adds functionality)
 
## Testing
>
#### How Has This Been Tested?
> Please describe the tests that you ran to verify your changes. Also
list any relevant details for your test configuration.
> Delete anything that is not relevant
- [ ] Manual
- [ ] JUnit
- [ ] Jest
- [ ] Cypress
>
>
#### Test Plan
- [x] check for autocomplete results when multiple widgets of the same
type are used
- [x] check for entity renaming behavior
- [x] verify function and mainContainer have been removed
- [x] verify that recent entity changes affects autocomplete ranking
>
>
#### Issues raised during DP testing

https://github.com/appsmithorg/appsmith/pull/28222#issuecomment-1777487126
>
>
>
## 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:
- [x] [Speedbreak
features](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#speedbreakers-)
have been covered
- [x] Test plan covers all impacted features and [areas of
interest](https://github.com/appsmithorg/TestSmith/wiki/Guidelines-for-test-plans#areas-of-interest-)
- [ ] Test plan has been peer reviewed by project stakeholders and other
QA members
- [x] Manually tested functionality on DP
- [ ] We had an implementation alignment call with stakeholders post QA
Round 2
- [ ] Cypress test cases have been added and approved by SDET/manual QA
- [ ] Added `Test Plan Approved` label after Cypress tests were reviewed
- [ ] Added `Test Plan Approved` label after JUnit tests were reviewed
2023-10-25 18:18:45 +01:00

72 lines
2.3 KiB
TypeScript

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;
const trimmedJS = js.replace(beginsWithLineBreakRegex, "");
return evaluationVersion > 1 ? trimmedJS : unescapeJS(trimmedJS);
}
// 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 = (
item: unknown,
): item is Record<string, unknown> => {
return Object.prototype.toString.call(item) === "[object Object]";
};
export const getNameFromPropertyNode = (node: PropertyNode): string =>
isLiteralNode(node.key) ? String(node.key.value) : node.key.name;
interface 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;
};
export const getStringValue = (
inputValue: string | number | boolean | RegExp,
) => {
if (typeof inputValue === "object" || typeof inputValue === "boolean") {
inputValue = JSON.stringify(inputValue);
} else if (typeof inputValue === "number" || typeof inputValue === "string") {
inputValue += "";
}
return inputValue;
};