2 Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com>
4 Redistribution and use in source and binary forms, with or without
5 modification, are permitted provided that the following conditions are met:
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.
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.
26 /* eslint-disable no-undefined */
28 const Syntax = require("estraverse").Syntax;
29 const esrecurse = require("esrecurse");
32 * Get last array element
33 * @param {array} xs - array
34 * @returns {any} Last elment
36 function getLast(xs) {
37 return xs[xs.length - 1] || null;
40 class PatternVisitor extends esrecurse.Visitor {
41 static isPattern(node) {
42 const nodeType = node.type;
45 nodeType === Syntax.Identifier ||
46 nodeType === Syntax.ObjectPattern ||
47 nodeType === Syntax.ArrayPattern ||
48 nodeType === Syntax.SpreadElement ||
49 nodeType === Syntax.RestElement ||
50 nodeType === Syntax.AssignmentPattern
54 constructor(options, rootPattern, callback) {
56 this.rootPattern = rootPattern;
57 this.callback = callback;
58 this.assignments = [];
59 this.rightHandNodes = [];
60 this.restElements = [];
64 const lastRestElement = getLast(this.restElements);
66 this.callback(pattern, {
67 topLevel: pattern === this.rootPattern,
68 rest: lastRestElement !== null && lastRestElement !== undefined && lastRestElement.argument === pattern,
69 assignments: this.assignments
75 // Computed property's key is a right hand node.
76 if (property.computed) {
77 this.rightHandNodes.push(property.key);
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);
86 ArrayPattern(pattern) {
87 for (let i = 0, iz = pattern.elements.length; i < iz; ++i) {
88 const element = pattern.elements[i];
94 AssignmentPattern(pattern) {
95 this.assignments.push(pattern);
96 this.visit(pattern.left);
97 this.rightHandNodes.push(pattern.right);
98 this.assignments.pop();
101 RestElement(pattern) {
102 this.restElements.push(pattern);
103 this.visit(pattern.argument);
104 this.restElements.pop();
107 MemberExpression(node) {
109 // Computed property's key is a right hand node.
111 this.rightHandNodes.push(node.property);
114 // the object is only read, write to its property.
115 this.rightHandNodes.push(node.object);
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...
125 SpreadElement(node) {
126 this.visit(node.argument);
129 ArrayExpression(node) {
130 node.elements.forEach(this.visit, this);
133 AssignmentExpression(node) {
134 this.assignments.push(node);
135 this.visit(node.left);
136 this.rightHandNodes.push(node.right);
137 this.assignments.pop();
140 CallExpression(node) {
142 // arguments are right hand nodes.
143 node.arguments.forEach(a => {
144 this.rightHandNodes.push(a);
146 this.visit(node.callee);
150 module.exports = PatternVisitor;
152 /* vim: set sw=4 ts=4 et tw=80 : */