177 lines
28 KiB
JavaScript
177 lines
28 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
Object.defineProperty(exports, "__esModule", {
|
||
|
value: true
|
||
|
});
|
||
|
|
||
|
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
|
||
|
|
||
|
var _estraverse = require('estraverse');
|
||
|
|
||
|
var _esrecurse = require('esrecurse');
|
||
|
|
||
|
var _esrecurse2 = _interopRequireDefault(_esrecurse);
|
||
|
|
||
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||
|
|
||
|
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
|
||
|
|
||
|
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
|
||
|
|
||
|
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /*
|
||
|
Copyright (C) 2015 Yusuke Suzuki <utatane.tea@gmail.com>
|
||
|
|
||
|
Redistribution and use in source and binary forms, with or without
|
||
|
modification, are permitted provided that the following conditions are met:
|
||
|
|
||
|
* Redistributions of source code must retain the above copyright
|
||
|
notice, this list of conditions and the following disclaimer.
|
||
|
* Redistributions in binary form must reproduce the above copyright
|
||
|
notice, this list of conditions and the following disclaimer in the
|
||
|
documentation and/or other materials provided with the distribution.
|
||
|
|
||
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||
|
ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
|
||
|
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||
|
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||
|
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||
|
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||
|
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
*/
|
||
|
|
||
|
function getLast(xs) {
|
||
|
return xs[xs.length - 1] || null;
|
||
|
}
|
||
|
|
||
|
var PatternVisitor = function (_esrecurse$Visitor) {
|
||
|
_inherits(PatternVisitor, _esrecurse$Visitor);
|
||
|
|
||
|
_createClass(PatternVisitor, null, [{
|
||
|
key: 'isPattern',
|
||
|
value: function isPattern(node) {
|
||
|
var nodeType = node.type;
|
||
|
return nodeType === _estraverse.Syntax.Identifier || nodeType === _estraverse.Syntax.ObjectPattern || nodeType === _estraverse.Syntax.ArrayPattern || nodeType === _estraverse.Syntax.SpreadElement || nodeType === _estraverse.Syntax.RestElement || nodeType === _estraverse.Syntax.AssignmentPattern;
|
||
|
}
|
||
|
}]);
|
||
|
|
||
|
function PatternVisitor(options, rootPattern, callback) {
|
||
|
_classCallCheck(this, PatternVisitor);
|
||
|
|
||
|
var _this = _possibleConstructorReturn(this, Object.getPrototypeOf(PatternVisitor).call(this, null, options));
|
||
|
|
||
|
_this.rootPattern = rootPattern;
|
||
|
_this.callback = callback;
|
||
|
_this.assignments = [];
|
||
|
_this.rightHandNodes = [];
|
||
|
_this.restElements = [];
|
||
|
return _this;
|
||
|
}
|
||
|
|
||
|
_createClass(PatternVisitor, [{
|
||
|
key: 'Identifier',
|
||
|
value: function Identifier(pattern) {
|
||
|
var lastRestElement = getLast(this.restElements);
|
||
|
this.callback(pattern, {
|
||
|
topLevel: pattern === this.rootPattern,
|
||
|
rest: lastRestElement != null && lastRestElement.argument === pattern,
|
||
|
assignments: this.assignments
|
||
|
});
|
||
|
}
|
||
|
}, {
|
||
|
key: 'Property',
|
||
|
value: function Property(property) {
|
||
|
// Computed property's key is a right hand node.
|
||
|
if (property.computed) {
|
||
|
this.rightHandNodes.push(property.key);
|
||
|
}
|
||
|
|
||
|
// If it's shorthand, its key is same as its value.
|
||
|
// If it's shorthand and has its default value, its key is same as its value.left (the value is AssignmentPattern).
|
||
|
// If it's not shorthand, the name of new variable is its value's.
|
||
|
this.visit(property.value);
|
||
|
}
|
||
|
}, {
|
||
|
key: 'ArrayPattern',
|
||
|
value: function ArrayPattern(pattern) {
|
||
|
var i, iz, element;
|
||
|
for (i = 0, iz = pattern.elements.length; i < iz; ++i) {
|
||
|
element = pattern.elements[i];
|
||
|
this.visit(element);
|
||
|
}
|
||
|
}
|
||
|
}, {
|
||
|
key: 'AssignmentPattern',
|
||
|
value: function AssignmentPattern(pattern) {
|
||
|
this.assignments.push(pattern);
|
||
|
this.visit(pattern.left);
|
||
|
this.rightHandNodes.push(pattern.right);
|
||
|
this.assignments.pop();
|
||
|
}
|
||
|
}, {
|
||
|
key: 'RestElement',
|
||
|
value: function RestElement(pattern) {
|
||
|
this.restElements.push(pattern);
|
||
|
this.visit(pattern.argument);
|
||
|
this.restElements.pop();
|
||
|
}
|
||
|
}, {
|
||
|
key: 'MemberExpression',
|
||
|
value: function MemberExpression(node) {
|
||
|
// Computed property's key is a right hand node.
|
||
|
if (node.computed) {
|
||
|
this.rightHandNodes.push(node.property);
|
||
|
}
|
||
|
// the object is only read, write to its property.
|
||
|
this.rightHandNodes.push(node.object);
|
||
|
}
|
||
|
|
||
|
//
|
||
|
// ForInStatement.left and AssignmentExpression.left are LeftHandSideExpression.
|
||
|
// By spec, LeftHandSideExpression is Pattern or MemberExpression.
|
||
|
// (see also: https://github.com/estree/estree/pull/20#issuecomment-74584758)
|
||
|
// But espree 2.0 and esprima 2.0 parse to ArrayExpression, ObjectExpression, etc...
|
||
|
//
|
||
|
|
||
|
}, {
|
||
|
key: 'SpreadElement',
|
||
|
value: function SpreadElement(node) {
|
||
|
this.visit(node.argument);
|
||
|
}
|
||
|
}, {
|
||
|
key: 'ArrayExpression',
|
||
|
value: function ArrayExpression(node) {
|
||
|
node.elements.forEach(this.visit, this);
|
||
|
}
|
||
|
}, {
|
||
|
key: 'AssignmentExpression',
|
||
|
value: function AssignmentExpression(node) {
|
||
|
this.assignments.push(node);
|
||
|
this.visit(node.left);
|
||
|
this.rightHandNodes.push(node.right);
|
||
|
this.assignments.pop();
|
||
|
}
|
||
|
}, {
|
||
|
key: 'CallExpression',
|
||
|
value: function CallExpression(node) {
|
||
|
var _this2 = this;
|
||
|
|
||
|
// arguments are right hand nodes.
|
||
|
node.arguments.forEach(function (a) {
|
||
|
_this2.rightHandNodes.push(a);
|
||
|
});
|
||
|
this.visit(node.callee);
|
||
|
}
|
||
|
}]);
|
||
|
|
||
|
return PatternVisitor;
|
||
|
}(_esrecurse2.default.Visitor);
|
||
|
|
||
|
/* vim: set sw=4 ts=4 et tw=80 : */
|
||
|
|
||
|
|
||
|
exports.default = PatternVisitor;
|
||
|
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbInBhdHRlcm4tdmlzaXRvci5qcyJdLCJuYW1lcyI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