Skip to content

Commit 2f7ffaa

Browse files
authored
refactor: split utils (#209)
* refactor: split utils to multiple files #208 * Resolve test errors
1 parent f04cfb1 commit 2f7ffaa

19 files changed

+134
-121
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import isMinified from "is-minified-code";
1010
import { SourceFile } from "./src/SourceFile.js";
1111
import { SourceParser } from "./src/SourceParser.js";
1212
import { warnings } from "./src/warnings.js";
13-
import { isOneLineExpressionExport } from "./src/utils.js";
13+
import { isOneLineExpressionExport } from "./src/utils/index.js";
1414

1515
export function runASTAnalysis(
1616
str,

src/SourceFile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Utils, Literal } from "@nodesecure/sec-literal";
33
import { VariableTracer } from "@nodesecure/estree-ast-utils";
44

55
// Import Internal Dependencies
6-
import { rootLocation, toArrayLocation } from "./utils.js";
6+
import { rootLocation, toArrayLocation } from "./utils/index.js";
77
import { generateWarning } from "./warnings.js";
88
import { isObfuscatedCode, hasTrojanSource } from "./obfuscators/index.js";
99
import { ProbeRunner } from "./ProbeRunner.js";

src/obfuscators/jjencode.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Require Internal Dependencies
2-
import { notNullOrUndefined } from "../utils.js";
2+
import { notNullOrUndefined } from "../utils/index.js";
33

44
// CONSTANTS
55
const kJJRegularSymbols = new Set(["$", "_"]);

src/probes/isArrayExpression.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Import Internal Dependencies
2-
import { extractNode } from "../utils.js";
2+
import { extractNode } from "../utils/index.js";
33

44
// CONSTANTS
55
const kLiteralExtractor = extractNode("Literal");

src/probes/isClassDeclaration.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Import Internal Dependencies
2-
import { extractNode } from "../utils.js";
2+
import { extractNode } from "../utils/index.js";
33

44
// CONSTANTS
55
const kIdExtractor = extractNode("Identifier");

src/probes/isFunction.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Import Internal Dependencies
2-
import { extractNode } from "../utils.js";
2+
import { extractNode } from "../utils/index.js";
33

44
// CONSTANTS
55
const kIdExtractor = extractNode("Identifier");

src/probes/isMethodDefinition.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Import Internal Dependencies
2-
import { extractNode } from "../utils.js";
2+
import { extractNode } from "../utils/index.js";
33

44
// CONSTANTS
55
const kIdExtractor = extractNode("Identifier");

src/probes/isUnsafeCallee.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Import Internal Dependencies
2-
import { isUnsafeCallee } from "../utils.js";
2+
import { isUnsafeCallee } from "../utils/index.js";
33
import { ProbeSignals } from "../ProbeRunner.js";
44

55
/**

src/utils.js

Lines changed: 0 additions & 111 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Import Third-party Dependencies
2+
import {
3+
getCallExpressionIdentifier
4+
} from "@nodesecure/estree-ast-utils";
5+
6+
export function exportAssignmentHasRequireLeave(expr) {
7+
if (expr.type === "LogicalExpression") {
8+
return atLeastOneBranchHasRequireLeave(expr.left, expr.right);
9+
}
10+
11+
if (expr.type === "ConditionalExpression") {
12+
return atLeastOneBranchHasRequireLeave(expr.consequent, expr.alternate);
13+
}
14+
15+
if (expr.type === "CallExpression") {
16+
return getCallExpressionIdentifier(expr) === "require";
17+
}
18+
19+
if (expr.type === "MemberExpression") {
20+
let rootMember = expr.object;
21+
while (rootMember.type === "MemberExpression") {
22+
rootMember = rootMember.object;
23+
}
24+
25+
if (rootMember.type !== "CallExpression") {
26+
return false;
27+
}
28+
29+
return getCallExpressionIdentifier(rootMember) === "require";
30+
}
31+
32+
return false;
33+
}
34+
35+
function atLeastOneBranchHasRequireLeave(left, right) {
36+
return [
37+
exportAssignmentHasRequireLeave(left),
38+
exportAssignmentHasRequireLeave(right)
39+
].some((hasRequire) => hasRequire);
40+
}

src/utils/extractNode.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Import Internal Dependencies
2+
import { notNullOrUndefined } from "./notNullOrUndefined.js";
3+
4+
export function extractNode(expectedType) {
5+
return (callback, nodes) => {
6+
const finalNodes = Array.isArray(nodes) ? nodes : [nodes];
7+
8+
for (const node of finalNodes) {
9+
if (notNullOrUndefined(node) && node.type === expectedType) {
10+
callback(node);
11+
}
12+
}
13+
};
14+
}

src/utils/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
export * from "./exportAssignmentHasRequireLeave.js";
2+
export * from "./extractNode.js";
3+
export * from "./isOneLineExpressionExport.js";
4+
export * from "./isUnsafeCallee.js";
5+
export * from "./notNullOrUndefined.js";
6+
export * from "./rootLocation.js";
7+
export * from "./toArrayLocation.js";
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Import Internal Dependencies
2+
import { exportAssignmentHasRequireLeave } from "./exportAssignmentHasRequireLeave.js";
3+
4+
export function isOneLineExpressionExport(body) {
5+
if (body.length > 1) {
6+
return false;
7+
}
8+
9+
if (body[0].type !== "ExpressionStatement") {
10+
return false;
11+
}
12+
13+
if (body[0].expression.type !== "AssignmentExpression") {
14+
return false;
15+
}
16+
17+
return exportAssignmentHasRequireLeave(body[0].expression.right);
18+
}

src/utils/isUnsafeCallee.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Import Third-party Dependencies
2+
import { getCallExpressionIdentifier } from "@nodesecure/estree-ast-utils";
3+
4+
function isEvalCallee(node) {
5+
const identifier = getCallExpressionIdentifier(node, {
6+
resolveCallExpression: false
7+
});
8+
9+
return identifier === "eval";
10+
}
11+
12+
function isFunctionCallee(node) {
13+
const identifier = getCallExpressionIdentifier(node);
14+
15+
return identifier === "Function" && node.callee.type === "CallExpression";
16+
}
17+
18+
export function isUnsafeCallee(node) {
19+
if (isEvalCallee(node)) {
20+
return [true, "eval"];
21+
}
22+
23+
if (isFunctionCallee(node)) {
24+
return [true, "Function"];
25+
}
26+
27+
return [false, null];
28+
}

src/utils/notNullOrUndefined.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function notNullOrUndefined(value) {
2+
return value !== null && value !== void 0;
3+
}

src/utils/rootLocation.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function rootLocation() {
2+
return { start: { line: 0, column: 0 }, end: { line: 0, column: 0 } };
3+
}

src/utils/toArrayLocation.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Import Internal Dependencies
2+
import { rootLocation } from "./rootLocation.js";
3+
4+
export function toArrayLocation(location = rootLocation()) {
5+
const { start, end = start } = location;
6+
7+
return [
8+
[start.line || 0, start.column || 0],
9+
[end.line || 0, end.column || 0]
10+
];
11+
}

src/warnings.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Import Internal Dependencies
2-
import * as utils from "./utils.js";
2+
import * as utils from "./utils/index.js";
33

44
export const warnings = Object.freeze({
55
"parsing-error": {

test/warnings.spec.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { test } from "node:test";
33
import assert from "node:assert";
44

55
// Import Internal Dependencies
6-
import { rootLocation } from "../src/utils.js";
6+
import { rootLocation } from "../src/utils/index.js";
77
import { generateWarning } from "../src/warnings.js";
88

99
test("Given an encoded-literal kind it should generate a warning with deep location array", () => {

0 commit comments

Comments
 (0)