-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* refactor: split utils to multiple files #208 * Resolve test errors
- Loading branch information
1 parent
f04cfb1
commit 2f7ffaa
Showing
19 changed files
with
134 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// Import Third-party Dependencies | ||
import { | ||
getCallExpressionIdentifier | ||
} from "@nodesecure/estree-ast-utils"; | ||
|
||
export function exportAssignmentHasRequireLeave(expr) { | ||
if (expr.type === "LogicalExpression") { | ||
return atLeastOneBranchHasRequireLeave(expr.left, expr.right); | ||
} | ||
|
||
if (expr.type === "ConditionalExpression") { | ||
return atLeastOneBranchHasRequireLeave(expr.consequent, expr.alternate); | ||
} | ||
|
||
if (expr.type === "CallExpression") { | ||
return getCallExpressionIdentifier(expr) === "require"; | ||
} | ||
|
||
if (expr.type === "MemberExpression") { | ||
let rootMember = expr.object; | ||
while (rootMember.type === "MemberExpression") { | ||
rootMember = rootMember.object; | ||
} | ||
|
||
if (rootMember.type !== "CallExpression") { | ||
return false; | ||
} | ||
|
||
return getCallExpressionIdentifier(rootMember) === "require"; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
function atLeastOneBranchHasRequireLeave(left, right) { | ||
return [ | ||
exportAssignmentHasRequireLeave(left), | ||
exportAssignmentHasRequireLeave(right) | ||
].some((hasRequire) => hasRequire); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Import Internal Dependencies | ||
import { notNullOrUndefined } from "./notNullOrUndefined.js"; | ||
|
||
export function extractNode(expectedType) { | ||
return (callback, nodes) => { | ||
const finalNodes = Array.isArray(nodes) ? nodes : [nodes]; | ||
|
||
for (const node of finalNodes) { | ||
if (notNullOrUndefined(node) && node.type === expectedType) { | ||
callback(node); | ||
} | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export * from "./exportAssignmentHasRequireLeave.js"; | ||
export * from "./extractNode.js"; | ||
export * from "./isOneLineExpressionExport.js"; | ||
export * from "./isUnsafeCallee.js"; | ||
export * from "./notNullOrUndefined.js"; | ||
export * from "./rootLocation.js"; | ||
export * from "./toArrayLocation.js"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Import Internal Dependencies | ||
import { exportAssignmentHasRequireLeave } from "./exportAssignmentHasRequireLeave.js"; | ||
|
||
export function isOneLineExpressionExport(body) { | ||
if (body.length > 1) { | ||
return false; | ||
} | ||
|
||
if (body[0].type !== "ExpressionStatement") { | ||
return false; | ||
} | ||
|
||
if (body[0].expression.type !== "AssignmentExpression") { | ||
return false; | ||
} | ||
|
||
return exportAssignmentHasRequireLeave(body[0].expression.right); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Import Third-party Dependencies | ||
import { getCallExpressionIdentifier } from "@nodesecure/estree-ast-utils"; | ||
|
||
function isEvalCallee(node) { | ||
const identifier = getCallExpressionIdentifier(node, { | ||
resolveCallExpression: false | ||
}); | ||
|
||
return identifier === "eval"; | ||
} | ||
|
||
function isFunctionCallee(node) { | ||
const identifier = getCallExpressionIdentifier(node); | ||
|
||
return identifier === "Function" && node.callee.type === "CallExpression"; | ||
} | ||
|
||
export function isUnsafeCallee(node) { | ||
if (isEvalCallee(node)) { | ||
return [true, "eval"]; | ||
} | ||
|
||
if (isFunctionCallee(node)) { | ||
return [true, "Function"]; | ||
} | ||
|
||
return [false, null]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function notNullOrUndefined(value) { | ||
return value !== null && value !== void 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function rootLocation() { | ||
return { start: { line: 0, column: 0 }, end: { line: 0, column: 0 } }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
// Import Internal Dependencies | ||
import { rootLocation } from "./rootLocation.js"; | ||
|
||
export function toArrayLocation(location = rootLocation()) { | ||
const { start, end = start } = location; | ||
|
||
return [ | ||
[start.line || 0, start.column || 0], | ||
[end.line || 0, end.column || 0] | ||
]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters