UNPKG

js-confuser

Version:

JavaScript Obfuscation Tool.

63 lines (55 loc) 2.98 kB
"use strict"; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } Object.defineProperty(exports, "__esModule", { value: true }); exports.isStaticValue = isStaticValue; var t = _interopRequireWildcard(require("@babel/types")); function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function _interopRequireWildcard(e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, "default": e }; if (null === e || "object" != _typeof(e) && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (var _t in e) "default" !== _t && {}.hasOwnProperty.call(e, _t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, _t)) && (i.get || i.set) ? o(f, _t, i) : f[_t] = e[_t]); return f; })(e, t); } // Function to check if a node is a static value function isStaticValue(node) { // Check for literals which are considered static if (t.isStringLiteral(node) || t.isNumericLiteral(node) || t.isBooleanLiteral(node) || t.isNullLiteral(node)) { if (t.isDirectiveLiteral(node)) return false; return true; } // Handle unary expressions like -42 if (t.isUnaryExpression(node)) { // Only consider certain operators as static (e.g., -, +) if (["-", "+", "!", "~", "void"].includes(node.operator)) { return isStaticValue(node.argument); } return false; } // Handle binary expressions with static values only if (t.isBinaryExpression(node)) { return isStaticValue(node.left) && isStaticValue(node.right); } // Handle logical expressions (&&, ||) with static values only if (t.isLogicalExpression(node)) { return isStaticValue(node.left) && isStaticValue(node.right); } // Handle conditional (ternary) expressions with static values if (t.isConditionalExpression(node)) { return isStaticValue(node.test) && isStaticValue(node.consequent) && isStaticValue(node.alternate); } // Handle array expressions where all elements are static if (t.isArrayExpression(node)) { return node.elements.every(function (element) { return element !== null && isStaticValue(element); }); } // Handle object expressions where all properties are static if (t.isObjectExpression(node)) { return node.properties.every(function (prop) { if (t.isObjectProperty(prop)) { return isStaticValue(prop.key) && isStaticValue(prop.value); } else if (t.isSpreadElement(prop)) { return isStaticValue(prop.argument); } return false; }); } // Add more cases as needed, depending on what you consider "static" return false; }