source: imaps-frontend/node_modules/eslint-scope/lib/pattern-visitor.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

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