1/* 2 Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com> 3 4 Redistribution and use in source and binary forms, with or without 5 modification, are permitted provided that the following conditions are met: 6 7 * Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 * Redistributions in binary form must reproduce the above copyright 10 notice, this list of conditions and the following disclaimer in the 11 documentation and/or other materials provided with the distribution. 12 13 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY 17 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 18 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 20 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 21 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 22 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23*/ 24"use strict"; 25 26/* eslint-disable no-undefined */ 27 28const Syntax = require("estraverse").Syntax; 29const esrecurse = require("esrecurse"); 30 31/** 32 * Get last array element 33 * @param {array} xs - array 34 * @returns {any} Last elment 35 */ 36function getLast(xs) { 37 return xs[xs.length - 1] || null; 38} 39 40class PatternVisitor extends esrecurse.Visitor { 41 static isPattern(node) { 42 const nodeType = node.type; 43 44 return ( 45 nodeType === Syntax.Identifier || 46 nodeType === Syntax.ObjectPattern || 47 nodeType === Syntax.ArrayPattern || 48 nodeType === Syntax.SpreadElement || 49 nodeType === Syntax.RestElement || 50 nodeType === Syntax.AssignmentPattern 51 ); 52 } 53 54 constructor(options, rootPattern, callback) { 55 super(null, options); 56 this.rootPattern = rootPattern; 57 this.callback = callback; 58 this.assignments = []; 59 this.rightHandNodes = []; 60 this.restElements = []; 61 } 62 63 Identifier(pattern) { 64 const lastRestElement = getLast(this.restElements); 65 66 this.callback(pattern, { 67 topLevel: pattern === this.rootPattern, 68 rest: lastRestElement !== null && lastRestElement !== undefined && lastRestElement.argument === pattern, 69 assignments: this.assignments 70 }); 71 } 72 73 Property(property) { 74 75 // Computed property's key is a right hand node. 76 if (property.computed) { 77 this.rightHandNodes.push(property.key); 78 } 79 80 // If it's shorthand, its key is same as its value. 81 // If it's shorthand and has its default value, its key is same as its value.left (the value is AssignmentPattern). 82 // If it's not shorthand, the name of new variable is its value's. 83 this.visit(property.value); 84 } 85 86 ArrayPattern(pattern) { 87 for (let i = 0, iz = pattern.elements.length; i < iz; ++i) { 88 const element = pattern.elements[i]; 89 90 this.visit(element); 91 } 92 } 93 94 AssignmentPattern(pattern) { 95 this.assignments.push(pattern); 96 this.visit(pattern.left); 97 this.rightHandNodes.push(pattern.right); 98 this.assignments.pop(); 99 } 100 101 RestElement(pattern) { 102 this.restElements.push(pattern); 103 this.visit(pattern.argument); 104 this.restElements.pop(); 105 } 106 107 MemberExpression(node) { 108 109 // Computed property's key is a right hand node. 110 if (node.computed) { 111 this.rightHandNodes.push(node.property); 112 } 113 114 // the object is only read, write to its property. 115 this.rightHandNodes.push(node.object); 116 } 117 118 // 119 // ForInStatement.left and AssignmentExpression.left are LeftHandSideExpression. 120 // By spec, LeftHandSideExpression is Pattern or MemberExpression. 121 // (see also: https://github.com/estree/estree/pull/20#issuecomment-74584758) 122 // But espree 2.0 parses to ArrayExpression, ObjectExpression, etc... 123 // 124 125 SpreadElement(node) { 126 this.visit(node.argument); 127 } 128 129 ArrayExpression(node) { 130 node.elements.forEach(this.visit, this); 131 } 132 133 AssignmentExpression(node) { 134 this.assignments.push(node); 135 this.visit(node.left); 136 this.rightHandNodes.push(node.right); 137 this.assignments.pop(); 138 } 139 140 CallExpression(node) { 141 142 // arguments are right hand nodes. 143 node.arguments.forEach(a => { 144 this.rightHandNodes.push(a); 145 }); 146 this.visit(node.callee); 147 } 148} 149 150module.exports = PatternVisitor; 151 152/* vim: set sw=4 ts=4 et tw=80 : */ 153