PromucFlow_constructor/app/client/packages/ast/src/utils.ts
Valera Melnikov 9f607d250d
chore: move local dependency to packages (#23395)
## Description
1. Move everything related to client from app folder to client folder
(`.yarn`, `yarn.lock`, package.json, .gitignore)
2. Move `ast` and `rst` to client packages
3. Fix running scripts in packages
4. Add running unit tests in packages in CI

TODO: It is necessary to consider enabling the `nmHoistingLimits:
workspaces` option, since now all packages are hoisted to the root,
there may be issues with dependencies in workspaces. Also, there is a
possibility of implicit use of packages.

https://yarnpkg.com/configuration/yarnrc#nmHoistingLimits

#### PR fixes following issue(s)
Fixes #23333

#### Type of change
- Chore (housekeeping or task changes that don't impact user perception)

## Testing

#### How Has This Been Tested?
- [x] Manual
- [x] Jest
- [x] Cypress

## Checklist:
#### Dev activity
- [x] My code follows the style guidelines of this project
- [x] 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

Co-authored-by: Valera Melnikov <melnikov.vv@greendatasoft.ru>
2023-05-22 15:55:46 +03:00

61 lines
2.0 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;
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;
};