Browse Source

refactoring

sanex3339 9 years ago
parent
commit
a4c96c2f0a

+ 8 - 7
src/NodeUtils.js

@@ -1,10 +1,11 @@
 "use strict";
+const Utils_1 = require("./Utils");
 class NodeUtils {
     static getScopeOfNode(node, depth = 0) {
         if (node.parentNode.type === 'Program') {
             return node.parentNode;
         }
-        if (NodeUtils.scopeNodes.indexOf(node.parentNode.type) < 0) {
+        if (!Utils_1.Utils.arrayContains(NodeUtils.scopeNodes, node.parentNode.type)) {
             return NodeUtils.getScopeOfNode(node.parentNode, depth);
         }
         if (depth > 0) {
@@ -15,15 +16,15 @@ class NodeUtils {
         }
         return node;
     }
-    static getParentNodeWithType(node, types, limitNodeTypes = [], deep = 0) {
-        if (node.parentNode.type === 'Program' || limitNodeTypes.indexOf(node.parentNode.type) >= 0) {
+    static getParentNodeWithType(node, types, limitNodeTypes = [], depth = 0) {
+        if (node.parentNode.type === 'Program' || Utils_1.Utils.arrayContains(limitNodeTypes, node.parentNode.type)) {
             return node.parentNode;
         }
-        if (types.indexOf(node.parentNode.type) < 0) {
-            return NodeUtils.getParentNodeWithType(node.parentNode, types, limitNodeTypes, deep);
+        if (!Utils_1.Utils.arrayContains(types, node.parentNode.type)) {
+            return NodeUtils.getParentNodeWithType(node.parentNode, types, limitNodeTypes, depth);
         }
-        if (deep > 0) {
-            return NodeUtils.getParentNodeWithType(node.parentNode, types, limitNodeTypes, --deep);
+        if (depth > 0) {
+            return NodeUtils.getParentNodeWithType(node.parentNode, types, limitNodeTypes, --depth);
         }
         return node.parentNode;
     }

+ 10 - 8
src/NodeUtils.ts

@@ -6,6 +6,8 @@ import { IPropertyNode } from "./interfaces/nodes/IPropertyNode";
 import { ITreeNode } from './interfaces/nodes/ITreeNode';
 import { IVariableDeclaratorNode } from "./interfaces/nodes/IVariableDeclaratorNode";
 
+import { Utils } from "./Utils";
+
 export class NodeUtils {
     /**
      * @type {string[]}
@@ -27,7 +29,7 @@ export class NodeUtils {
             return node.parentNode;
         }
 
-        if (NodeUtils.scopeNodes.indexOf(node.parentNode.type) < 0) {
+        if (!Utils.arrayContains(NodeUtils.scopeNodes, node.parentNode.type)) {
             return NodeUtils.getScopeOfNode(node.parentNode, depth);
         }
 
@@ -46,25 +48,25 @@ export class NodeUtils {
      * @param node
      * @param types
      * @param limitNodeTypes
-     * @param deep
+     * @param depth
      * @returns {ITreeNode}
      */
     public static getParentNodeWithType (
         node: ITreeNode,
         types: string[],
         limitNodeTypes: string[] = [],
-        deep: number = 0
+        depth: number = 0
     ): ITreeNode {
-        if (node.parentNode.type === 'Program' || limitNodeTypes.indexOf(node.parentNode.type) >= 0) {
+        if (node.parentNode.type === 'Program' || Utils.arrayContains(limitNodeTypes, node.parentNode.type)) {
             return node.parentNode;
         }
 
-        if (types.indexOf(node.parentNode.type) < 0) {
-            return NodeUtils.getParentNodeWithType(node.parentNode, types, limitNodeTypes, deep);
+        if (!Utils.arrayContains(types, node.parentNode.type)) {
+            return NodeUtils.getParentNodeWithType(node.parentNode, types, limitNodeTypes, depth);
         }
 
-        if (deep > 0) {
-            return NodeUtils.getParentNodeWithType(node.parentNode, types, limitNodeTypes, --deep);
+        if (depth > 0) {
+            return NodeUtils.getParentNodeWithType(node.parentNode, types, limitNodeTypes, --depth);
         }
 
         return node.parentNode;

+ 3 - 0
src/Utils.js

@@ -1,5 +1,8 @@
 "use strict";
 class Utils {
+    static arrayContains(array, searchElement) {
+        return array.indexOf(searchElement) >= 0;
+    }
     static arrayRotate(array, times, reverse = false) {
         if (times < 0) {
             return;

+ 9 - 0
src/Utils.ts

@@ -1,4 +1,13 @@
 export class Utils {
+    /**
+     * @param array
+     * @param searchElement
+     * @returns {boolean}
+     */
+    public static arrayContains (array: any[], searchElement: any) {
+        return array.indexOf(searchElement) >= 0;
+    }
+
     /**
      * @param array
      * @param times

+ 2 - 1
src/node-obfuscators/MethodDefinitionObfuscator.js

@@ -2,6 +2,7 @@
 const estraverse = require('estraverse');
 const NodeObfuscator_1 = require('./NodeObfuscator');
 const NodeUtils_1 = require("../NodeUtils");
+const Utils_1 = require("../Utils");
 class MethodDefinitionObfuscator extends NodeObfuscator_1.NodeObfuscator {
     constructor(...args) {
         super(...args);
@@ -14,7 +15,7 @@ class MethodDefinitionObfuscator extends NodeObfuscator_1.NodeObfuscator {
         estraverse.replace(methodDefinitionNode.key, {
             leave: (node) => {
                 if (NodeUtils_1.NodeUtils.isIdentifierNode(node) &&
-                    this.ignoredNames.indexOf(node.name) < 0 &&
+                    !Utils_1.Utils.arrayContains(this.ignoredNames, node.name) &&
                     methodDefinitionNode.computed === false) {
                     methodDefinitionNode.computed = true;
                     node.name = this.replaceLiteralStringByArrayElement(node.name);

+ 2 - 1
src/node-obfuscators/MethodDefinitionObfuscator.ts

@@ -5,6 +5,7 @@ import { ITreeNode } from "../interfaces/nodes/ITreeNode";
 
 import { NodeObfuscator } from './NodeObfuscator';
 import { NodeUtils } from "../NodeUtils";
+import { Utils } from "../Utils";
 
 /**
  * replaces:
@@ -37,7 +38,7 @@ export class MethodDefinitionObfuscator extends NodeObfuscator {
             leave: (node: ITreeNode): any => {
                 if (
                     NodeUtils.isIdentifierNode(node) &&
-                    this.ignoredNames.indexOf(node.name) < 0 &&
+                    !Utils.arrayContains(this.ignoredNames, node.name) &&
                     methodDefinitionNode.computed === false
                 ) {
                     methodDefinitionNode.computed = true;

+ 1 - 1
src/node-obfuscators/VariableDeclarationObfuscator.js

@@ -40,7 +40,7 @@ class VariableDeclarationObfuscator extends NodeObfuscator_1.NodeObfuscator {
                     'FunctionDeclaration',
                     'FunctionExpression'
                 ];
-                if (functionNodes.indexOf(node.type) >= 0) {
+                if (Utils_1.Utils.arrayContains(functionNodes, node.type)) {
                     estraverse.replace(node, {
                         enter: (node, parentNode) => {
                             this.replaceNodeIdentifierByNewValue(node, parentNode, this.variableNames);

+ 1 - 1
src/node-obfuscators/VariableDeclarationObfuscator.ts

@@ -79,7 +79,7 @@ export class VariableDeclarationObfuscator extends NodeObfuscator {
                     'FunctionExpression'
                 ];
 
-                if (functionNodes.indexOf(node.type) >= 0) {
+                if (Utils.arrayContains(functionNodes, node.type)) {
                     estraverse.replace(node, {
                         enter: (node: ITreeNode, parentNode: ITreeNode): any => {
                             this.replaceNodeIdentifierByNewValue(node, parentNode, this.variableNames);