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 |
|
25 | import { Syntax } from 'estraverse';
|
26 | import esrecurse from 'esrecurse';
|
27 |
|
28 | function getLast(xs) {
|
29 | return xs[xs.length - 1] || null;
|
30 | }
|
31 |
|
32 | export default class PatternVisitor extends esrecurse.Visitor {
|
33 | static isPattern(node) {
|
34 | var nodeType = node.type;
|
35 | return (
|
36 | nodeType === Syntax.Identifier ||
|
37 | nodeType === Syntax.ObjectPattern ||
|
38 | nodeType === Syntax.ArrayPattern ||
|
39 | nodeType === Syntax.SpreadElement ||
|
40 | nodeType === Syntax.RestElement ||
|
41 | nodeType === Syntax.AssignmentPattern
|
42 | );
|
43 | }
|
44 |
|
45 | constructor(options, rootPattern, callback) {
|
46 | super(null, options);
|
47 | this.rootPattern = rootPattern;
|
48 | this.callback = callback;
|
49 | this.assignments = [];
|
50 | this.rightHandNodes = [];
|
51 | this.restElements = [];
|
52 | }
|
53 |
|
54 | Identifier(pattern) {
|
55 | const lastRestElement = getLast(this.restElements);
|
56 | this.callback(pattern, {
|
57 | topLevel: pattern === this.rootPattern,
|
58 | rest: lastRestElement != null && lastRestElement.argument === pattern,
|
59 | assignments: this.assignments
|
60 | });
|
61 | }
|
62 |
|
63 | Property(property) {
|
64 | // Computed property's key is a right hand node.
|
65 | if (property.computed) {
|
66 | this.rightHandNodes.push(property.key);
|
67 | }
|
68 |
|
69 | // If it's shorthand, its key is same as its value.
|
70 | // If it's shorthand and has its default value, its key is same as its value.left (the value is AssignmentPattern).
|
71 | // If it's not shorthand, the name of new variable is its value's.
|
72 | this.visit(property.value);
|
73 | }
|
74 |
|
75 | ArrayPattern(pattern) {
|
76 | var i, iz, element;
|
77 | for (i = 0, iz = pattern.elements.length; i < iz; ++i) {
|
78 | element = pattern.elements[i];
|
79 | this.visit(element);
|
80 | }
|
81 | }
|
82 |
|
83 | AssignmentPattern(pattern) {
|
84 | this.assignments.push(pattern);
|
85 | this.visit(pattern.left);
|
86 | this.rightHandNodes.push(pattern.right);
|
87 | this.assignments.pop();
|
88 | }
|
89 |
|
90 | RestElement(pattern) {
|
91 | this.restElements.push(pattern);
|
92 | this.visit(pattern.argument);
|
93 | this.restElements.pop();
|
94 | }
|
95 |
|
96 | MemberExpression(node) {
|
97 | // Computed property's key is a right hand node.
|
98 | if (node.computed) {
|
99 | this.rightHandNodes.push(node.property);
|
100 | }
|
101 | // the object is only read, write to its property.
|
102 | this.rightHandNodes.push(node.object);
|
103 | }
|
104 |
|
105 | //
|
106 | // ForInStatement.left and AssignmentExpression.left are LeftHandSideExpression.
|
107 | // By spec, LeftHandSideExpression is Pattern or MemberExpression.
|
108 | // (see also: https://github.com/estree/estree/pull/20#issuecomment-74584758)
|
109 | // But espree 2.0 and esprima 2.0 parse to ArrayExpression, ObjectExpression, etc...
|
110 | //
|
111 |
|
112 | SpreadElement(node) {
|
113 | this.visit(node.argument);
|
114 | }
|
115 |
|
116 | ArrayExpression(node) {
|
117 | node.elements.forEach(this.visit, this);
|
118 | }
|
119 |
|
120 | AssignmentExpression(node) {
|
121 | this.assignments.push(node);
|
122 | this.visit(node.left);
|
123 | this.rightHandNodes.push(node.right);
|
124 | this.assignments.pop();
|
125 | }
|
126 |
|
127 | CallExpression(node) {
|
128 | // arguments are right hand nodes.
|
129 | node.arguments.forEach(a => { this.rightHandNodes.push(a); });
|
130 | this.visit(node.callee);
|
131 | }
|
132 | }
|
133 |
|
134 | /* vim: set sw=4 ts=4 et tw=80 : */
|