Skip to content

Commit

Permalink
rts updates for the edge cases
Browse files Browse the repository at this point in the history
  • Loading branch information
ChandanBalajiBP committed Nov 28, 2022
1 parent 6b714a4 commit aa68da7
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 49 deletions.
1 change: 0 additions & 1 deletion app/rts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
"dependencies": {
"express-validator": "^6.14.2",
"http-status-codes": "^2.2.0",
"morgan": "^1.10.0",
"supertest": "^6.2.4",
"tsc-alias": "^1.7.0"
}
Expand Down
3 changes: 0 additions & 3 deletions app/rts/src/server.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import http from "http";
import path from "path";
import express from "express";
import morgan from "morgan";
import { Server } from "socket.io";
import log, { LogLevelDesc } from "loglevel";
import { VERSION as buildVersion } from "./version"; // release version of the api
Expand Down Expand Up @@ -49,8 +48,6 @@ const io = new Server(server, {
// Initializing Sockets
initializeSockets(io);

//Track perf metrics for each call
app.use(morgan('tiny'));
// parse incoming json requests
app.use(express.json({ limit: "5mb" }));
// Initializing Routes
Expand Down
28 changes: 0 additions & 28 deletions app/rts/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -923,13 +923,6 @@ [email protected], base64id@~2.0.0:
resolved "https://registry.yarnpkg.com/base64id/-/base64id-2.0.0.tgz#2770ac6bc47d312af97a8bf9a634342e0cd25cb6"
integrity sha512-lGe34o6EHj9y3Kts9R4ZYs/Gr+6N7MCaMlIFA3F1R2O5/m7K06AxfSeO5530PEERE6/WyEg3lsuyw4GHlPZHog==

basic-auth@~2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/basic-auth/-/basic-auth-2.0.1.tgz#b998279bf47ce38344b4f3cf916d4679bbf51e3a"
integrity sha512-NF+epuEdnUYVlGuhaxbbq+dvJttwLnGY+YixlXlME5KpQ5W3CnXA5cVTneY3SPbPDRkcjMbifrwmFYcClgOZeg==
dependencies:
safe-buffer "5.1.2"

binary-extensions@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d"
Expand Down Expand Up @@ -1260,11 +1253,6 @@ depd@~1.1.2:
resolved "https://registry.yarnpkg.com/depd/-/depd-1.1.2.tgz#9bcd52e14c097763e749b274c4346ed2e560b5a9"
integrity sha1-m81S4UwJd2PnSbJ0xDRu0uVgtak=

depd@~2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/depd/-/depd-2.0.0.tgz#b696163cc757560d09cf22cc8fad1571b79e76df"
integrity sha512-g7nH6P6dyDioJogAAGprGpCtVImJhpPk/roCzdb3fIh61/s/nPsfR6onyMwkCAR/OlC3yBC0lESvUoQEAssIrw==

destroy@~1.0.4:
version "1.0.4"
resolved "https://registry.yarnpkg.com/destroy/-/destroy-1.0.4.tgz#978857442c44749e4206613e37946205826abd80"
Expand Down Expand Up @@ -2397,17 +2385,6 @@ mongodb@^3.6.4:
optionalDependencies:
saslprep "^1.0.0"

morgan@^1.10.0:
version "1.10.0"
resolved "https://registry.yarnpkg.com/morgan/-/morgan-1.10.0.tgz#091778abc1fc47cd3509824653dae1faab6b17d7"
integrity sha512-AbegBVI4sh6El+1gNwvD5YIck7nSA36weD7xvIxG4in80j/UoK8AEGaWnnz8v1GxonMCltmlNs5ZKbGvl9b1XQ==
dependencies:
basic-auth "~2.0.1"
debug "2.6.9"
depd "~2.0.0"
on-finished "~2.3.0"
on-headers "~1.0.2"

[email protected]:
version "2.0.0"
resolved "https://registry.yarnpkg.com/ms/-/ms-2.0.0.tgz#5608aeadfc00be6c2901df5f9861788de0d597c8"
Expand Down Expand Up @@ -2477,11 +2454,6 @@ on-finished@~2.3.0:
dependencies:
ee-first "1.1.1"

on-headers@~1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/on-headers/-/on-headers-1.0.2.tgz#772b0ae6aaa525c399e489adfad90c403eb3c28f"
integrity sha512-pZAE+FJLoyITytdqK0U5s+FIpjN0JP3OzFi/u8Rx+EV5/W+JTWGXG8xFzevE7AjBfDqHv/8vL8qQsIhHnqRkrA==

[email protected], once@^1.3.0:
version "1.4.0"
resolved "https://registry.yarnpkg.com/once/-/once-1.4.0.tgz#583b1aa775961d4b113ac17d9c50baef9dd76bd1"
Expand Down
74 changes: 57 additions & 17 deletions app/shared/ast/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ interface IdentifierNode extends Node {
name: string;
}

//Using this to handle the Variable property refactor
interface IdentifierNodeRefactor extends Node {
type: NodeTypes.Identifier;
name: string;
property?: IdentifierNode;
}

// doc: https://github.com/estree/estree/blob/master/es5.md#variabledeclarator
interface VariableDeclaratorNode extends Node {
type: NodeTypes.VariableDeclarator;
Expand Down Expand Up @@ -270,50 +277,75 @@ export const entityRefactorFromCode = (
evaluationVersion: number,
invalidIdentifiers?: Record<string, unknown>
): EntityRefactorResponse => {
//Sanitizing leads to removal of special charater.
//Hence we are not sanatizing the script. Fix(#18492)
//If script is a JSObject then replace export default to decalartion.
if (isJSObject) script = jsObjectToCode(script);
let ast: Node = { end: 0, start: 0, type: "" };
//Copy of script to refactor
let refactorScript = script;
//Difference in length of oldName and newName
let nameLengthDiff: number = newName.length - oldName.length;
const nameLengthDiff: number = newName.length - oldName.length;
//Offset index used for deciding location of oldName.
let refactorOffset: number = 0;
//Count of refactors on the script
let refactorCount: number = 0;
try {
const sanitizedScript = sanitizeScript(script, evaluationVersion);
ast = getAST(sanitizedScript);
ast = getAST(script);
let {
references,
functionalParams,
variableDeclarations,
identifierList,
}: NodeList = ancestorWalk(ast);
let identifierArray = Array.from(identifierList) as Array<IdentifierNode>;
const identifierArray = Array.from(
identifierList
) as Array<IdentifierNodeRefactor>;
Array.from(references).forEach((reference, index) => {
const topLevelIdentifier = toPath(reference)[0];
let shouldUpdateNode = !(
functionalParams.has(topLevelIdentifier) ||
variableDeclarations.has(topLevelIdentifier) ||
has(invalidIdentifiers, topLevelIdentifier)
);
//To handle if oldName has property ("JSObject.myfunc")
const oldNameArr = oldName.split(".");
//check if node should be updated
if (shouldUpdateNode && identifierArray[index].name === oldName) {
if (shouldUpdateNode && identifierArray[index].name === oldNameArr[0]) {
//Replace the oldName by newName
//Get start index from node and get subarray from index 0 till start
//Append above with new name
//Append substring from end index from the node till end of string
//Offset variable is used to alter the position based on `refactorOffset`
refactorScript =
refactorScript.substring(
0,
identifierArray[index].start + refactorOffset
) +
newName +
refactorScript.substring(identifierArray[index].end + refactorOffset);
refactorOffset += nameLengthDiff;
++refactorCount;
//In case of nested JS action get end postion fro the property.
if (oldNameArr.length > 1) {
const propertyNode = identifierArray[index].property;
//Check if the property mataches. If so refactor the script.
if (oldNameArr[1] === propertyNode?.name) {
refactorScript = propertyNode
? refactorScript.substring(
0,
identifierArray[index].start + refactorOffset
) +
newName +
refactorScript.substring(propertyNode.end + refactorOffset)
: refactorScript;
refactorOffset += nameLengthDiff;
++refactorCount;
}
} else {
refactorScript =
refactorScript.substring(
0,
identifierArray[index].start + refactorOffset
) +
newName +
refactorScript.substring(
identifierArray[index].end + refactorOffset
);
refactorOffset += nameLengthDiff;
++refactorCount;
}
}
});
//If script is a JSObject then revert decalartion to export default.
Expand Down Expand Up @@ -507,8 +539,8 @@ export const extractInvalidTopLevelMemberExpressionsFromCode = (
};

const ancestorWalk = (ast: Node): NodeList => {
//List of all Identifier nodes
const identifierList = new Array<IdentifierNode>();
//List of all Identifier nodes with their property(if exists).
const identifierList = new Array<IdentifierNodeRefactor>();
// List of all references found
const references = new Set<string>();
// List of variables declared within the script. All identifiers and member expressions derived from declared variables will be removed
Expand Down Expand Up @@ -554,7 +586,15 @@ const ancestorWalk = (ast: Node): NodeList => {
break;
}
}
identifierList.push(node as IdentifierNode);
//If parent is a Member expression then attach property to the Node.
//else push Identifier Node.
const parentNode = ancestors[ancestors.length - 2];
if (isMemberExpressionNode(parentNode)) {
identifierList.push({
...(node as IdentifierNode),
property: parentNode.property as IdentifierNode,
});
} else identifierList.push(node as IdentifierNodeRefactor);
if (isIdentifierNode(candidateTopLevelNode)) {
// If the node is an Identifier, just save that
references.add(candidateTopLevelNode.name);
Expand Down

0 comments on commit aa68da7

Please sign in to comment.