Inspector
Extends:
Constructor Summary
| Public Constructor | ||
| public | 
       constructor(filePaths: string[], opts: object) Creates a new Inspector, which extends EventEmitter.  | 
    |
Method Summary
| Public Methods | ||
| public | 
       run() Runs the inspector on the given file paths, as provided in the constructor.  | 
    |
| Private Methods | ||
| private | 
       _analyze() Traverses the keys at which the various nodes are stored.  | 
    |
| private | 
       _expand(nodeArrays: Node[][]) Expands each instance of a match to the largest common sequence of nodes with the same type, and optionally identifiers.  | 
    |
| private | 
       _getMapKey(nodes: Node[]): string Generates a key based on the type of each of the passed nodes, returned as a base64-encoded sha1 hash.  | 
    |
| private | 
      
       Accepts a multi-dimensional array of nodes and groups them based on the supplied function, which is expected to return a string.  | 
    |
| private | 
       _groupByMatchingIdentifiers(groups: Node[][][]): Node[][][] Iterates over the multi-dimensional array of nodes, and returns a new array grouping them based on matching identifiers.  | 
    |
| private | 
       _groupByMatchingLiterals(groups: Node[][][]): Node[][][] Iterates over the multi-dimensional array of nodes, and returns a new array grouping them based on matching literals.  | 
    |
| private | 
       _insert(nodes: Node[]) Generates a key based on the combined types of each of the supplied nodes.  | 
    |
| private | 
       _omitOverlappingInstances(nodeArrays: Node[][]) Removes overlapping instances from a group of node arrays.  | 
    |
| private | 
       _prune(nodeArrays: Node[][]) Removes the nodes from consideration in any additional matches.  | 
    |
| private | 
       _removeNode(node: Node) Removes all occurrences of a given node.  | 
    |
| private | 
      
       Walks a given node's AST, building up arrays of nodes that meet the inspector's threshold.  | 
    |
| private | 
       _walkSiblings(node: Node, fn: function) Walks sibling nodes under a parent, grouping their DFS traversals, and invoking the callback for those that wouldn't otherwise meet the threshold. Helpful for nodes like BlockStatements that hold a sequence. Note that this will generate overlapping instances, and so _omitOverlappingInstances helps cleanup the results.  | 
    |
Public Constructors
public constructor(filePaths: string[], opts: object) source
Creates a new Inspector, which extends EventEmitter. filePaths is expected to be an array of string paths. Also accepts an options object with any combination of the following: threshold, identifiers literals, and minInstances. Threshold indicates the minimum number of nodes to analyze. Identifiers indicates whether or not the nodes in a match should also have matching identifiers, and literals whether or not literal values should match. minInstances specifies the min number of instances for a match. An instance of Inspector emits the following events: start, match and end.
Public Methods
public run() source
Runs the inspector on the given file paths, as provided in the constructor. Emits a start event, followed by a series of match events for any detected similarities, and an end event on completion.
Private Methods
private _analyze() source
Traverses the keys at which the various nodes are stored. A key containing an array of more than a single entry indicates a potential match. The nodes are then grouped if identifier matching is enabled. A match results in the relevant nodes being removed from any future results. This pruning ensures that we only include the greatest common parent in a set of matches.
private _expand(nodeArrays: Node[][]) source
Expands each instance of a match to the largest common sequence of nodes with the same type, and optionally identifiers. Each array of nodes is modified in place.
Params:
| Name | Type | Attribute | Description | 
| nodeArrays | Node[][] | 
private _getMapKey(nodes: Node[]): string source
Generates a key based on the type of each of the passed nodes, returned as a base64-encoded sha1 hash.
Params:
| Name | Type | Attribute | Description | 
| nodes | Node[] | The nodes for which to generate the key  | 
    
private _group(groups: Node[][][], fn: function): Node[][][] source
Accepts a multi-dimensional array of nodes and groups them based on the supplied function, which is expected to return a string.
Params:
| Name | Type | Attribute | Description | 
| groups | Node[][][] | The groups of nodes to further group  | 
    |
| fn | function | Synchronous function for generating group ids  | 
    
Return:
| Node[][][] | 
private _groupByMatchingIdentifiers(groups: Node[][][]): Node[][][] source
Iterates over the multi-dimensional array of nodes, and returns a new array grouping them based on matching identifiers.
Params:
| Name | Type | Attribute | Description | 
| groups | Node[][][] | 
Return:
| Node[][][] | 
private _groupByMatchingLiterals(groups: Node[][][]): Node[][][] source
Iterates over the multi-dimensional array of nodes, and returns a new array grouping them based on matching literals.
Params:
| Name | Type | Attribute | Description | 
| groups | Node[][][] | 
Return:
| Node[][][] | 
private _insert(nodes: Node[]) source
Generates a key based on the combined types of each of the supplied nodes. Pushes the array to another array at the generated key in _map. Nodes are updated to keep a reference to all their occurrences in _map.
Params:
| Name | Type | Attribute | Description | 
| nodes | Node[] | 
private _omitOverlappingInstances(nodeArrays: Node[][]) source
Removes overlapping instances from a group of node arrays. That is, if one instance has nodes abcd, and another has bcde, then bcde will be removed from the array.
Params:
| Name | Type | Attribute | Description | 
| nodeArrays | Node[][] | 
private _prune(nodeArrays: Node[][]) source
Removes the nodes from consideration in any additional matches.
Params:
| Name | Type | Attribute | Description | 
| nodeArrays | Node[][] | 
private _removeNode(node: Node) source
Removes all occurrences of a given node.
Params:
| Name | Type | Attribute | Description | 
| node | Node | The node to remove  | 
    
private _walk(node: Node, fn: function) source
Walks a given node's AST, building up arrays of nodes that meet the inspector's threshold. When found, the callback is invoked and passed the array of nodes.
Params:
| Name | Type | Attribute | Description | 
| node | Node | The node to traverse  | 
    |
| fn | function | The callback to invoke  | 
    
private _walkSiblings(node: Node, fn: function) source
Walks sibling nodes under a parent, grouping their DFS traversals, and invoking the callback for those that wouldn't otherwise meet the threshold. Helpful for nodes like BlockStatements that hold a sequence. Note that this will generate overlapping instances, and so _omitOverlappingInstances helps cleanup the results.
Params:
| Name | Type | Attribute | Description | 
| node | Node | The node to traverse  | 
    |
| fn | function | The callback to invoke  | 
    
    
  