1 function _typeof(obj) {
2 "@babel/helpers - typeof";
4 if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
5 _typeof = function (obj) {
9 _typeof = function (obj) {
10 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
17 function _slicedToArray(arr, i) {
18 return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
21 function _toConsumableArray(arr) {
22 return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
25 function _arrayWithoutHoles(arr) {
26 if (Array.isArray(arr)) return _arrayLikeToArray(arr);
29 function _arrayWithHoles(arr) {
30 if (Array.isArray(arr)) return arr;
33 function _iterableToArray(iter) {
34 if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
37 function _iterableToArrayLimit(arr, i) {
38 if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
45 for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
48 if (i && _arr.length === i) break;
55 if (!_n && _i["return"] != null) _i["return"]();
64 function _unsupportedIterableToArray(o, minLen) {
66 if (typeof o === "string") return _arrayLikeToArray(o, minLen);
67 var n = Object.prototype.toString.call(o).slice(8, -1);
68 if (n === "Object" && o.constructor) n = o.constructor.name;
69 if (n === "Map" || n === "Set") return Array.from(o);
70 if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
73 function _arrayLikeToArray(arr, len) {
74 if (len == null || len > arr.length) len = arr.length;
76 for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
81 function _nonIterableSpread() {
82 throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
85 function _nonIterableRest() {
86 throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
89 function _createForOfIteratorHelper(o, allowArrayLike) {
92 if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
93 if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
97 var F = function () {};
102 if (i >= o.length) return {
117 throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
120 var normalCompletion = true,
125 it = o[Symbol.iterator]();
128 var step = it.next();
129 normalCompletion = step.done;
138 if (!normalCompletion && it.return != null) it.return();
140 if (didErr) throw err;
146 var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
148 function createCommonjsModule(fn, module) {
149 return module = { exports: {} }, fn(module, module.exports), module.exports;
152 var estraverse = createCommonjsModule(function (module, exports) {
154 Copyright (C) 2012-2013 Yusuke Suzuki <utatane.tea@gmail.com>
155 Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
157 Redistribution and use in source and binary forms, with or without
158 modification, are permitted provided that the following conditions are met:
160 * Redistributions of source code must retain the above copyright
161 notice, this list of conditions and the following disclaimer.
162 * Redistributions in binary form must reproduce the above copyright
163 notice, this list of conditions and the following disclaimer in the
164 documentation and/or other materials provided with the distribution.
166 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
167 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
168 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
169 ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
170 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
171 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
172 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
173 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
174 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
175 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
178 /*jslint vars:false, bitwise:true*/
182 /*global exports:true*/
183 (function clone(exports) {
185 var Syntax, VisitorOption, VisitorKeys, BREAK, SKIP, REMOVE;
187 function deepCopy(obj) {
193 if (obj.hasOwnProperty(key)) {
196 if (typeof val === 'object' && val !== null) {
197 ret[key] = deepCopy(val);
205 } // based on LLVM libc++ upper_bound / lower_bound
209 function upperBound(array, func) {
210 var diff, len, i, current;
218 if (func(array[current])) {
230 AssignmentExpression: 'AssignmentExpression',
231 AssignmentPattern: 'AssignmentPattern',
232 ArrayExpression: 'ArrayExpression',
233 ArrayPattern: 'ArrayPattern',
234 ArrowFunctionExpression: 'ArrowFunctionExpression',
235 AwaitExpression: 'AwaitExpression',
236 // CAUTION: It's deferred to ES7.
237 BlockStatement: 'BlockStatement',
238 BinaryExpression: 'BinaryExpression',
239 BreakStatement: 'BreakStatement',
240 CallExpression: 'CallExpression',
241 CatchClause: 'CatchClause',
242 ChainExpression: 'ChainExpression',
243 ClassBody: 'ClassBody',
244 ClassDeclaration: 'ClassDeclaration',
245 ClassExpression: 'ClassExpression',
246 ComprehensionBlock: 'ComprehensionBlock',
247 // CAUTION: It's deferred to ES7.
248 ComprehensionExpression: 'ComprehensionExpression',
249 // CAUTION: It's deferred to ES7.
250 ConditionalExpression: 'ConditionalExpression',
251 ContinueStatement: 'ContinueStatement',
252 DebuggerStatement: 'DebuggerStatement',
253 DirectiveStatement: 'DirectiveStatement',
254 DoWhileStatement: 'DoWhileStatement',
255 EmptyStatement: 'EmptyStatement',
256 ExportAllDeclaration: 'ExportAllDeclaration',
257 ExportDefaultDeclaration: 'ExportDefaultDeclaration',
258 ExportNamedDeclaration: 'ExportNamedDeclaration',
259 ExportSpecifier: 'ExportSpecifier',
260 ExpressionStatement: 'ExpressionStatement',
261 ForStatement: 'ForStatement',
262 ForInStatement: 'ForInStatement',
263 ForOfStatement: 'ForOfStatement',
264 FunctionDeclaration: 'FunctionDeclaration',
265 FunctionExpression: 'FunctionExpression',
266 GeneratorExpression: 'GeneratorExpression',
267 // CAUTION: It's deferred to ES7.
268 Identifier: 'Identifier',
269 IfStatement: 'IfStatement',
270 ImportExpression: 'ImportExpression',
271 ImportDeclaration: 'ImportDeclaration',
272 ImportDefaultSpecifier: 'ImportDefaultSpecifier',
273 ImportNamespaceSpecifier: 'ImportNamespaceSpecifier',
274 ImportSpecifier: 'ImportSpecifier',
276 LabeledStatement: 'LabeledStatement',
277 LogicalExpression: 'LogicalExpression',
278 MemberExpression: 'MemberExpression',
279 MetaProperty: 'MetaProperty',
280 MethodDefinition: 'MethodDefinition',
281 ModuleSpecifier: 'ModuleSpecifier',
282 NewExpression: 'NewExpression',
283 ObjectExpression: 'ObjectExpression',
284 ObjectPattern: 'ObjectPattern',
286 Property: 'Property',
287 RestElement: 'RestElement',
288 ReturnStatement: 'ReturnStatement',
289 SequenceExpression: 'SequenceExpression',
290 SpreadElement: 'SpreadElement',
292 SwitchStatement: 'SwitchStatement',
293 SwitchCase: 'SwitchCase',
294 TaggedTemplateExpression: 'TaggedTemplateExpression',
295 TemplateElement: 'TemplateElement',
296 TemplateLiteral: 'TemplateLiteral',
297 ThisExpression: 'ThisExpression',
298 ThrowStatement: 'ThrowStatement',
299 TryStatement: 'TryStatement',
300 UnaryExpression: 'UnaryExpression',
301 UpdateExpression: 'UpdateExpression',
302 VariableDeclaration: 'VariableDeclaration',
303 VariableDeclarator: 'VariableDeclarator',
304 WhileStatement: 'WhileStatement',
305 WithStatement: 'WithStatement',
306 YieldExpression: 'YieldExpression'
309 AssignmentExpression: ['left', 'right'],
310 AssignmentPattern: ['left', 'right'],
311 ArrayExpression: ['elements'],
312 ArrayPattern: ['elements'],
313 ArrowFunctionExpression: ['params', 'body'],
314 AwaitExpression: ['argument'],
315 // CAUTION: It's deferred to ES7.
316 BlockStatement: ['body'],
317 BinaryExpression: ['left', 'right'],
318 BreakStatement: ['label'],
319 CallExpression: ['callee', 'arguments'],
320 CatchClause: ['param', 'body'],
321 ChainExpression: ['expression'],
323 ClassDeclaration: ['id', 'superClass', 'body'],
324 ClassExpression: ['id', 'superClass', 'body'],
325 ComprehensionBlock: ['left', 'right'],
326 // CAUTION: It's deferred to ES7.
327 ComprehensionExpression: ['blocks', 'filter', 'body'],
328 // CAUTION: It's deferred to ES7.
329 ConditionalExpression: ['test', 'consequent', 'alternate'],
330 ContinueStatement: ['label'],
331 DebuggerStatement: [],
332 DirectiveStatement: [],
333 DoWhileStatement: ['body', 'test'],
335 ExportAllDeclaration: ['source'],
336 ExportDefaultDeclaration: ['declaration'],
337 ExportNamedDeclaration: ['declaration', 'specifiers', 'source'],
338 ExportSpecifier: ['exported', 'local'],
339 ExpressionStatement: ['expression'],
340 ForStatement: ['init', 'test', 'update', 'body'],
341 ForInStatement: ['left', 'right', 'body'],
342 ForOfStatement: ['left', 'right', 'body'],
343 FunctionDeclaration: ['id', 'params', 'body'],
344 FunctionExpression: ['id', 'params', 'body'],
345 GeneratorExpression: ['blocks', 'filter', 'body'],
346 // CAUTION: It's deferred to ES7.
348 IfStatement: ['test', 'consequent', 'alternate'],
349 ImportExpression: ['source'],
350 ImportDeclaration: ['specifiers', 'source'],
351 ImportDefaultSpecifier: ['local'],
352 ImportNamespaceSpecifier: ['local'],
353 ImportSpecifier: ['imported', 'local'],
355 LabeledStatement: ['label', 'body'],
356 LogicalExpression: ['left', 'right'],
357 MemberExpression: ['object', 'property'],
358 MetaProperty: ['meta', 'property'],
359 MethodDefinition: ['key', 'value'],
361 NewExpression: ['callee', 'arguments'],
362 ObjectExpression: ['properties'],
363 ObjectPattern: ['properties'],
365 Property: ['key', 'value'],
366 RestElement: ['argument'],
367 ReturnStatement: ['argument'],
368 SequenceExpression: ['expressions'],
369 SpreadElement: ['argument'],
371 SwitchStatement: ['discriminant', 'cases'],
372 SwitchCase: ['test', 'consequent'],
373 TaggedTemplateExpression: ['tag', 'quasi'],
375 TemplateLiteral: ['quasis', 'expressions'],
377 ThrowStatement: ['argument'],
378 TryStatement: ['block', 'handler', 'finalizer'],
379 UnaryExpression: ['argument'],
380 UpdateExpression: ['argument'],
381 VariableDeclaration: ['declarations'],
382 VariableDeclarator: ['id', 'init'],
383 WhileStatement: ['test', 'body'],
384 WithStatement: ['object', 'body'],
385 YieldExpression: ['argument']
397 function Reference(parent, key) {
398 this.parent = parent;
402 Reference.prototype.replace = function replace(node) {
403 this.parent[this.key] = node;
406 Reference.prototype.remove = function remove() {
407 if (Array.isArray(this.parent)) {
408 this.parent.splice(this.key, 1);
416 function Element(node, path, wrap, ref) {
423 function Controller() {} // API:
424 // return property path array from root to current node
427 Controller.prototype.path = function path() {
428 var i, iz, j, jz, result, element;
430 function addToPath(result, path) {
431 if (Array.isArray(path)) {
432 for (j = 0, jz = path.length; j < jz; ++j) {
433 result.push(path[j]);
441 if (!this.__current.path) {
443 } // first node is sentinel, second node is root element
448 for (i = 2, iz = this.__leavelist.length; i < iz; ++i) {
449 element = this.__leavelist[i];
450 addToPath(result, element.path);
453 addToPath(result, this.__current.path);
456 // return type of current node
459 Controller.prototype.type = function () {
460 var node = this.current();
461 return node.type || this.__current.wrap;
463 // return array of parent elements
466 Controller.prototype.parents = function parents() {
467 var i, iz, result; // first node is sentinel
471 for (i = 1, iz = this.__leavelist.length; i < iz; ++i) {
472 result.push(this.__leavelist[i].node);
477 // return current node
480 Controller.prototype.current = function current() {
481 return this.__current.node;
484 Controller.prototype.__execute = function __execute(callback, element) {
485 var previous, result;
487 previous = this.__current;
488 this.__current = element;
492 result = callback.call(this, element.node, this.__leavelist[this.__leavelist.length - 1].node);
495 this.__current = previous;
498 // notify control skip / break
501 Controller.prototype.notify = function notify(flag) {
504 // skip child nodes of current node
507 Controller.prototype.skip = function () {
513 Controller.prototype['break'] = function () {
519 Controller.prototype.remove = function () {
523 Controller.prototype.__initialize = function (root, visitor) {
524 this.visitor = visitor;
526 this.__worklist = [];
527 this.__leavelist = [];
528 this.__current = null;
530 this.__fallback = null;
532 if (visitor.fallback === 'iteration') {
533 this.__fallback = Object.keys;
534 } else if (typeof visitor.fallback === 'function') {
535 this.__fallback = visitor.fallback;
538 this.__keys = VisitorKeys;
541 this.__keys = Object.assign(Object.create(this.__keys), visitor.keys);
545 function isNode(node) {
550 return typeof node === 'object' && typeof node.type === 'string';
553 function isProperty(nodeType, key) {
554 return (nodeType === Syntax.ObjectExpression || nodeType === Syntax.ObjectPattern) && 'properties' === key;
557 function candidateExistsInLeaveList(leavelist, candidate) {
558 for (var i = leavelist.length - 1; i >= 0; --i) {
559 if (leavelist[i].node === candidate) {
567 Controller.prototype.traverse = function traverse(root, visitor) {
568 var worklist, leavelist, element, node, nodeType, ret, key, current, current2, candidates, candidate, sentinel;
570 this.__initialize(root, visitor);
572 sentinel = {}; // reference
574 worklist = this.__worklist;
575 leavelist = this.__leavelist; // initialize
577 worklist.push(new Element(root, null, null, null));
578 leavelist.push(new Element(null, null, null, null));
580 while (worklist.length) {
581 element = worklist.pop();
583 if (element === sentinel) {
584 element = leavelist.pop();
585 ret = this.__execute(visitor.leave, element);
587 if (this.__state === BREAK || ret === BREAK) {
595 ret = this.__execute(visitor.enter, element);
597 if (this.__state === BREAK || ret === BREAK) {
601 worklist.push(sentinel);
602 leavelist.push(element);
604 if (this.__state === SKIP || ret === SKIP) {
609 nodeType = node.type || element.wrap;
610 candidates = this.__keys[nodeType];
613 if (this.__fallback) {
614 candidates = this.__fallback(node);
616 throw new Error('Unknown node type ' + nodeType + '.');
620 current = candidates.length;
622 while ((current -= 1) >= 0) {
623 key = candidates[current];
624 candidate = node[key];
630 if (Array.isArray(candidate)) {
631 current2 = candidate.length;
633 while ((current2 -= 1) >= 0) {
634 if (!candidate[current2]) {
638 if (candidateExistsInLeaveList(leavelist, candidate[current2])) {
642 if (isProperty(nodeType, candidates[current])) {
643 element = new Element(candidate[current2], [key, current2], 'Property', null);
644 } else if (isNode(candidate[current2])) {
645 element = new Element(candidate[current2], [key, current2], null, null);
650 worklist.push(element);
652 } else if (isNode(candidate)) {
653 if (candidateExistsInLeaveList(leavelist, candidate)) {
657 worklist.push(new Element(candidate, key, null, null));
664 Controller.prototype.replace = function replace(root, visitor) {
665 var worklist, leavelist, node, nodeType, target, element, current, current2, candidates, candidate, sentinel, outer, key;
667 function removeElem(element) {
668 var i, key, nextElem, parent;
670 if (element.ref.remove()) {
671 // When the reference is an element of an array.
672 key = element.ref.key;
673 parent = element.ref.parent; // If removed from array, then decrease following items' keys.
678 nextElem = worklist[i];
680 if (nextElem.ref && nextElem.ref.parent === parent) {
681 if (nextElem.ref.key < key) {
691 this.__initialize(root, visitor);
693 sentinel = {}; // reference
695 worklist = this.__worklist;
696 leavelist = this.__leavelist; // initialize
701 element = new Element(root, null, null, new Reference(outer, 'root'));
702 worklist.push(element);
703 leavelist.push(element);
705 while (worklist.length) {
706 element = worklist.pop();
708 if (element === sentinel) {
709 element = leavelist.pop();
710 target = this.__execute(visitor.leave, element); // node may be replaced with null,
711 // so distinguish between undefined and null in this place
713 if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) {
715 element.ref.replace(target);
718 if (this.__state === REMOVE || target === REMOVE) {
722 if (this.__state === BREAK || target === BREAK) {
729 target = this.__execute(visitor.enter, element); // node may be replaced with null,
730 // so distinguish between undefined and null in this place
732 if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) {
734 element.ref.replace(target);
735 element.node = target;
738 if (this.__state === REMOVE || target === REMOVE) {
743 if (this.__state === BREAK || target === BREAK) {
745 } // node may be null
754 worklist.push(sentinel);
755 leavelist.push(element);
757 if (this.__state === SKIP || target === SKIP) {
761 nodeType = node.type || element.wrap;
762 candidates = this.__keys[nodeType];
765 if (this.__fallback) {
766 candidates = this.__fallback(node);
768 throw new Error('Unknown node type ' + nodeType + '.');
772 current = candidates.length;
774 while ((current -= 1) >= 0) {
775 key = candidates[current];
776 candidate = node[key];
782 if (Array.isArray(candidate)) {
783 current2 = candidate.length;
785 while ((current2 -= 1) >= 0) {
786 if (!candidate[current2]) {
790 if (isProperty(nodeType, candidates[current])) {
791 element = new Element(candidate[current2], [key, current2], 'Property', new Reference(candidate, current2));
792 } else if (isNode(candidate[current2])) {
793 element = new Element(candidate[current2], [key, current2], null, new Reference(candidate, current2));
798 worklist.push(element);
800 } else if (isNode(candidate)) {
801 worklist.push(new Element(candidate, key, null, new Reference(node, key)));
809 function traverse(root, visitor) {
810 var controller = new Controller();
811 return controller.traverse(root, visitor);
814 function replace(root, visitor) {
815 var controller = new Controller();
816 return controller.replace(root, visitor);
819 function extendCommentRange(comment, tokens) {
821 target = upperBound(tokens, function search(token) {
822 return token.range[0] > comment.range[0];
824 comment.extendedRange = [comment.range[0], comment.range[1]];
826 if (target !== tokens.length) {
827 comment.extendedRange[1] = tokens[target].range[0];
833 comment.extendedRange[0] = tokens[target].range[1];
839 function attachComments(tree, providedComments, tokens) {
840 // At first, we should calculate extended comment ranges.
848 throw new Error('attachComments needs range information');
849 } // tokens array is empty, we attach comments to tree as 'leadingComments'
852 if (!tokens.length) {
853 if (providedComments.length) {
854 for (i = 0, len = providedComments.length; i < len; i += 1) {
855 comment = deepCopy(providedComments[i]);
856 comment.extendedRange = [0, tree.range[0]];
857 comments.push(comment);
860 tree.leadingComments = comments;
866 for (i = 0, len = providedComments.length; i < len; i += 1) {
867 comments.push(extendCommentRange(deepCopy(providedComments[i]), tokens));
868 } // This is based on John Freeman's implementation.
873 enter: function (node) {
876 while (cursor < comments.length) {
877 comment = comments[cursor];
879 if (comment.extendedRange[1] > node.range[0]) {
883 if (comment.extendedRange[1] === node.range[0]) {
884 if (!node.leadingComments) {
885 node.leadingComments = [];
888 node.leadingComments.push(comment);
889 comments.splice(cursor, 1);
893 } // already out of owned node
896 if (cursor === comments.length) {
897 return VisitorOption.Break;
900 if (comments[cursor].extendedRange[0] > node.range[1]) {
901 return VisitorOption.Skip;
907 leave: function (node) {
910 while (cursor < comments.length) {
911 comment = comments[cursor];
913 if (node.range[1] < comment.extendedRange[0]) {
917 if (node.range[1] === comment.extendedRange[0]) {
918 if (!node.trailingComments) {
919 node.trailingComments = [];
922 node.trailingComments.push(comment);
923 comments.splice(cursor, 1);
927 } // already out of owned node
930 if (cursor === comments.length) {
931 return VisitorOption.Break;
934 if (comments[cursor].extendedRange[0] > node.range[1]) {
935 return VisitorOption.Skip;
942 exports.Syntax = Syntax;
943 exports.traverse = traverse;
944 exports.replace = replace;
945 exports.attachComments = attachComments;
946 exports.VisitorKeys = VisitorKeys;
947 exports.VisitorOption = VisitorOption;
948 exports.Controller = Controller;
950 exports.cloneEnvironment = function () {
956 /* vim: set sw=4 ts=4 et tw=80 : */
960 var parser = createCommonjsModule(function (module) {
962 * Generated by PEG.js 0.10.0.
966 (function (root, factory) {
967 if ( module.exports) {
968 module.exports = factory();
970 })(commonjsGlobal, function () {
972 function peg$subclass(child, parent) {
974 this.constructor = child;
977 ctor.prototype = parent.prototype;
978 child.prototype = new ctor();
981 function peg$SyntaxError(message, expected, found, location) {
982 this.message = message;
983 this.expected = expected;
985 this.location = location;
986 this.name = "SyntaxError";
988 if (typeof Error.captureStackTrace === "function") {
989 Error.captureStackTrace(this, peg$SyntaxError);
993 peg$subclass(peg$SyntaxError, Error);
995 peg$SyntaxError.buildMessage = function (expected, found) {
996 var DESCRIBE_EXPECTATION_FNS = {
997 literal: function literal(expectation) {
998 return "\"" + literalEscape(expectation.text) + "\"";
1000 "class": function _class(expectation) {
1001 var escapedParts = "",
1004 for (i = 0; i < expectation.parts.length; i++) {
1005 escapedParts += expectation.parts[i] instanceof Array ? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1]) : classEscape(expectation.parts[i]);
1008 return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]";
1010 any: function any(expectation) {
1011 return "any character";
1013 end: function end(expectation) {
1014 return "end of input";
1016 other: function other(expectation) {
1017 return expectation.description;
1022 return ch.charCodeAt(0).toString(16).toUpperCase();
1025 function literalEscape(s) {
1026 return s.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\0/g, '\\0').replace(/\t/g, '\\t').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/[\x00-\x0F]/g, function (ch) {
1027 return '\\x0' + hex(ch);
1028 }).replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) {
1029 return '\\x' + hex(ch);
1033 function classEscape(s) {
1034 return s.replace(/\\/g, '\\\\').replace(/\]/g, '\\]').replace(/\^/g, '\\^').replace(/-/g, '\\-').replace(/\0/g, '\\0').replace(/\t/g, '\\t').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/[\x00-\x0F]/g, function (ch) {
1035 return '\\x0' + hex(ch);
1036 }).replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) {
1037 return '\\x' + hex(ch);
1041 function describeExpectation(expectation) {
1042 return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);
1045 function describeExpected(expected) {
1046 var descriptions = new Array(expected.length),
1050 for (i = 0; i < expected.length; i++) {
1051 descriptions[i] = describeExpectation(expected[i]);
1054 descriptions.sort();
1056 if (descriptions.length > 0) {
1057 for (i = 1, j = 1; i < descriptions.length; i++) {
1058 if (descriptions[i - 1] !== descriptions[i]) {
1059 descriptions[j] = descriptions[i];
1064 descriptions.length = j;
1067 switch (descriptions.length) {
1069 return descriptions[0];
1072 return descriptions[0] + " or " + descriptions[1];
1075 return descriptions.slice(0, -1).join(", ") + ", or " + descriptions[descriptions.length - 1];
1079 function describeFound(found) {
1080 return found ? "\"" + literalEscape(found) + "\"" : "end of input";
1083 return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found.";
1086 function peg$parse(input, options) {
1087 options = options !== void 0 ? options : {};
1089 var peg$FAILED = {},
1090 peg$startRuleFunctions = {
1091 start: peg$parsestart
1093 peg$startRuleFunction = peg$parsestart,
1094 peg$c0 = function peg$c0(ss) {
1095 return ss.length === 1 ? ss[0] : {
1100 peg$c1 = function peg$c1() {
1104 peg$c3 = peg$literalExpectation(" ", false),
1105 peg$c4 = /^[^ [\],():#!=><~+.]/,
1106 peg$c5 = peg$classExpectation([" ", "[", "]", ",", "(", ")", ":", "#", "!", "=", ">", "<", "~", "+", "."], true, false),
1107 peg$c6 = function peg$c6(i) {
1111 peg$c8 = peg$literalExpectation(">", false),
1112 peg$c9 = function peg$c9() {
1116 peg$c11 = peg$literalExpectation("~", false),
1117 peg$c12 = function peg$c12() {
1121 peg$c14 = peg$literalExpectation("+", false),
1122 peg$c15 = function peg$c15() {
1125 peg$c16 = function peg$c16() {
1126 return 'descendant';
1129 peg$c18 = peg$literalExpectation(",", false),
1130 peg$c19 = function peg$c19(s, ss) {
1131 return [s].concat(ss.map(function (s) {
1135 peg$c20 = function peg$c20(a, ops) {
1136 return ops.reduce(function (memo, rhs) {
1145 peg$c22 = peg$literalExpectation("!", false),
1146 peg$c23 = function peg$c23(subject, as) {
1147 var b = as.length === 1 ? as[0] : {
1151 if (subject) b.subject = true;
1155 peg$c25 = peg$literalExpectation("*", false),
1156 peg$c26 = function peg$c26(a) {
1163 peg$c28 = peg$literalExpectation("#", false),
1164 peg$c29 = function peg$c29(i) {
1171 peg$c31 = peg$literalExpectation("[", false),
1173 peg$c33 = peg$literalExpectation("]", false),
1174 peg$c34 = function peg$c34(v) {
1178 peg$c36 = peg$classExpectation([">", "<", "!"], false, false),
1180 peg$c38 = peg$literalExpectation("=", false),
1181 peg$c39 = function peg$c39(a) {
1182 return (a || '') + '=';
1185 peg$c41 = peg$classExpectation([">", "<"], false, false),
1187 peg$c43 = peg$literalExpectation(".", false),
1188 peg$c44 = function peg$c44(a, as) {
1189 return [].concat.apply([a], as).join('');
1191 peg$c45 = function peg$c45(name, op, value) {
1199 peg$c46 = function peg$c46(name) {
1206 peg$c48 = peg$literalExpectation("\"", false),
1207 peg$c49 = /^[^\\"]/,
1208 peg$c50 = peg$classExpectation(["\\", "\""], true, false),
1210 peg$c52 = peg$literalExpectation("\\", false),
1211 peg$c53 = peg$anyExpectation(),
1212 peg$c54 = function peg$c54(a, b) {
1215 peg$c55 = function peg$c55(d) {
1218 value: strUnescape(d.join(''))
1222 peg$c57 = peg$literalExpectation("'", false),
1223 peg$c58 = /^[^\\']/,
1224 peg$c59 = peg$classExpectation(["\\", "'"], true, false),
1226 peg$c61 = peg$classExpectation([["0", "9"]], false, false),
1227 peg$c62 = function peg$c62(a, b) {
1228 // Can use `a.flat().join('')` once supported
1229 var leadingDecimals = a ? [].concat.apply([], a).join('') : '';
1232 value: parseFloat(leadingDecimals + b.join(''))
1235 peg$c63 = function peg$c63(i) {
1242 peg$c65 = peg$literalExpectation("type(", false),
1244 peg$c67 = peg$classExpectation([" ", ")"], true, false),
1246 peg$c69 = peg$literalExpectation(")", false),
1247 peg$c70 = function peg$c70(t) {
1253 peg$c71 = /^[imsu]/,
1254 peg$c72 = peg$classExpectation(["i", "m", "s", "u"], false, false),
1256 peg$c74 = peg$literalExpectation("/", false),
1258 peg$c76 = peg$classExpectation(["/"], true, false),
1259 peg$c77 = function peg$c77(d, flgs) {
1262 value: new RegExp(d.join(''), flgs ? flgs.join('') : '')
1265 peg$c78 = function peg$c78(i, is) {
1268 name: is.reduce(function (memo, p) {
1269 return memo + p[0] + p[1];
1274 peg$c80 = peg$literalExpectation(":not(", false),
1275 peg$c81 = function peg$c81(ss) {
1281 peg$c82 = ":matches(",
1282 peg$c83 = peg$literalExpectation(":matches(", false),
1283 peg$c84 = function peg$c84(ss) {
1290 peg$c86 = peg$literalExpectation(":has(", false),
1291 peg$c87 = function peg$c87(ss) {
1297 peg$c88 = ":first-child",
1298 peg$c89 = peg$literalExpectation(":first-child", false),
1299 peg$c90 = function peg$c90() {
1302 peg$c91 = ":last-child",
1303 peg$c92 = peg$literalExpectation(":last-child", false),
1304 peg$c93 = function peg$c93() {
1307 peg$c94 = ":nth-child(",
1308 peg$c95 = peg$literalExpectation(":nth-child(", false),
1309 peg$c96 = function peg$c96(n) {
1310 return nth(parseInt(n.join(''), 10));
1312 peg$c97 = ":nth-last-child(",
1313 peg$c98 = peg$literalExpectation(":nth-last-child(", false),
1314 peg$c99 = function peg$c99(n) {
1315 return nthLast(parseInt(n.join(''), 10));
1318 peg$c101 = peg$literalExpectation(":", false),
1319 peg$c102 = "statement",
1320 peg$c103 = peg$literalExpectation("statement", true),
1321 peg$c104 = "expression",
1322 peg$c105 = peg$literalExpectation("expression", true),
1323 peg$c106 = "declaration",
1324 peg$c107 = peg$literalExpectation("declaration", true),
1325 peg$c108 = "function",
1326 peg$c109 = peg$literalExpectation("function", true),
1327 peg$c110 = "pattern",
1328 peg$c111 = peg$literalExpectation("pattern", true),
1329 peg$c112 = function peg$c112(c) {
1336 peg$posDetailsCache = [{
1341 peg$maxFailExpected = [],
1342 peg$resultsCache = {},
1345 if ("startRule" in options) {
1346 if (!(options.startRule in peg$startRuleFunctions)) {
1347 throw new Error("Can't start parsing from rule \"" + options.startRule + "\".");
1350 peg$startRuleFunction = peg$startRuleFunctions[options.startRule];
1353 function peg$literalExpectation(text, ignoreCase) {
1357 ignoreCase: ignoreCase
1361 function peg$classExpectation(parts, inverted, ignoreCase) {
1366 ignoreCase: ignoreCase
1370 function peg$anyExpectation() {
1376 function peg$endExpectation() {
1382 function peg$computePosDetails(pos) {
1383 var details = peg$posDetailsCache[pos],
1391 while (!peg$posDetailsCache[p]) {
1395 details = peg$posDetailsCache[p];
1398 column: details.column
1402 if (input.charCodeAt(p) === 10) {
1412 peg$posDetailsCache[pos] = details;
1417 function peg$computeLocation(startPos, endPos) {
1418 var startPosDetails = peg$computePosDetails(startPos),
1419 endPosDetails = peg$computePosDetails(endPos);
1423 line: startPosDetails.line,
1424 column: startPosDetails.column
1428 line: endPosDetails.line,
1429 column: endPosDetails.column
1434 function peg$fail(expected) {
1435 if (peg$currPos < peg$maxFailPos) {
1439 if (peg$currPos > peg$maxFailPos) {
1440 peg$maxFailPos = peg$currPos;
1441 peg$maxFailExpected = [];
1444 peg$maxFailExpected.push(expected);
1447 function peg$buildStructuredError(expected, found, location) {
1448 return new peg$SyntaxError(peg$SyntaxError.buildMessage(expected, found), expected, found, location);
1451 function peg$parsestart() {
1453 var key = peg$currPos * 30 + 0,
1454 cached = peg$resultsCache[key];
1457 peg$currPos = cached.nextPos;
1458 return cached.result;
1464 if (s1 !== peg$FAILED) {
1465 s2 = peg$parseselectors();
1467 if (s2 !== peg$FAILED) {
1470 if (s3 !== peg$FAILED) {
1486 if (s0 === peg$FAILED) {
1490 if (s1 !== peg$FAILED) {
1497 peg$resultsCache[key] = {
1498 nextPos: peg$currPos,
1504 function peg$parse_() {
1506 var key = peg$currPos * 30 + 1,
1507 cached = peg$resultsCache[key];
1510 peg$currPos = cached.nextPos;
1511 return cached.result;
1516 if (input.charCodeAt(peg$currPos) === 32) {
1527 while (s1 !== peg$FAILED) {
1530 if (input.charCodeAt(peg$currPos) === 32) {
1542 peg$resultsCache[key] = {
1543 nextPos: peg$currPos,
1549 function peg$parseidentifierName() {
1551 var key = peg$currPos * 30 + 2,
1552 cached = peg$resultsCache[key];
1555 peg$currPos = cached.nextPos;
1556 return cached.result;
1562 if (peg$c4.test(input.charAt(peg$currPos))) {
1563 s2 = input.charAt(peg$currPos);
1573 if (s2 !== peg$FAILED) {
1574 while (s2 !== peg$FAILED) {
1577 if (peg$c4.test(input.charAt(peg$currPos))) {
1578 s2 = input.charAt(peg$currPos);
1592 if (s1 !== peg$FAILED) {
1597 peg$resultsCache[key] = {
1598 nextPos: peg$currPos,
1604 function peg$parsebinaryOp() {
1606 var key = peg$currPos * 30 + 3,
1607 cached = peg$resultsCache[key];
1610 peg$currPos = cached.nextPos;
1611 return cached.result;
1617 if (s1 !== peg$FAILED) {
1618 if (input.charCodeAt(peg$currPos) === 62) {
1629 if (s2 !== peg$FAILED) {
1632 if (s3 !== peg$FAILED) {
1648 if (s0 === peg$FAILED) {
1652 if (s1 !== peg$FAILED) {
1653 if (input.charCodeAt(peg$currPos) === 126) {
1664 if (s2 !== peg$FAILED) {
1667 if (s3 !== peg$FAILED) {
1683 if (s0 === peg$FAILED) {
1687 if (s1 !== peg$FAILED) {
1688 if (input.charCodeAt(peg$currPos) === 43) {
1699 if (s2 !== peg$FAILED) {
1702 if (s3 !== peg$FAILED) {
1718 if (s0 === peg$FAILED) {
1721 if (input.charCodeAt(peg$currPos) === 32) {
1732 if (s1 !== peg$FAILED) {
1735 if (s2 !== peg$FAILED) {
1750 peg$resultsCache[key] = {
1751 nextPos: peg$currPos,
1757 function peg$parseselectors() {
1758 var s0, s1, s2, s3, s4, s5, s6, s7;
1759 var key = peg$currPos * 30 + 4,
1760 cached = peg$resultsCache[key];
1763 peg$currPos = cached.nextPos;
1764 return cached.result;
1768 s1 = peg$parseselector();
1770 if (s1 !== peg$FAILED) {
1775 if (s4 !== peg$FAILED) {
1776 if (input.charCodeAt(peg$currPos) === 44) {
1787 if (s5 !== peg$FAILED) {
1790 if (s6 !== peg$FAILED) {
1791 s7 = peg$parseselector();
1793 if (s7 !== peg$FAILED) {
1794 s4 = [s4, s5, s6, s7];
1813 while (s3 !== peg$FAILED) {
1818 if (s4 !== peg$FAILED) {
1819 if (input.charCodeAt(peg$currPos) === 44) {
1830 if (s5 !== peg$FAILED) {
1833 if (s6 !== peg$FAILED) {
1834 s7 = peg$parseselector();
1836 if (s7 !== peg$FAILED) {
1837 s4 = [s4, s5, s6, s7];
1857 if (s2 !== peg$FAILED) {
1858 s1 = peg$c19(s1, s2);
1869 peg$resultsCache[key] = {
1870 nextPos: peg$currPos,
1876 function peg$parseselector() {
1877 var s0, s1, s2, s3, s4, s5;
1878 var key = peg$currPos * 30 + 5,
1879 cached = peg$resultsCache[key];
1882 peg$currPos = cached.nextPos;
1883 return cached.result;
1887 s1 = peg$parsesequence();
1889 if (s1 !== peg$FAILED) {
1892 s4 = peg$parsebinaryOp();
1894 if (s4 !== peg$FAILED) {
1895 s5 = peg$parsesequence();
1897 if (s5 !== peg$FAILED) {
1909 while (s3 !== peg$FAILED) {
1912 s4 = peg$parsebinaryOp();
1914 if (s4 !== peg$FAILED) {
1915 s5 = peg$parsesequence();
1917 if (s5 !== peg$FAILED) {
1930 if (s2 !== peg$FAILED) {
1931 s1 = peg$c20(s1, s2);
1942 peg$resultsCache[key] = {
1943 nextPos: peg$currPos,
1949 function peg$parsesequence() {
1951 var key = peg$currPos * 30 + 6,
1952 cached = peg$resultsCache[key];
1955 peg$currPos = cached.nextPos;
1956 return cached.result;
1961 if (input.charCodeAt(peg$currPos) === 33) {
1972 if (s1 === peg$FAILED) {
1976 if (s1 !== peg$FAILED) {
1978 s3 = peg$parseatom();
1980 if (s3 !== peg$FAILED) {
1981 while (s3 !== peg$FAILED) {
1983 s3 = peg$parseatom();
1989 if (s2 !== peg$FAILED) {
1990 s1 = peg$c23(s1, s2);
2001 peg$resultsCache[key] = {
2002 nextPos: peg$currPos,
2008 function peg$parseatom() {
2010 var key = peg$currPos * 30 + 7,
2011 cached = peg$resultsCache[key];
2014 peg$currPos = cached.nextPos;
2015 return cached.result;
2018 s0 = peg$parsewildcard();
2020 if (s0 === peg$FAILED) {
2021 s0 = peg$parseidentifier();
2023 if (s0 === peg$FAILED) {
2024 s0 = peg$parseattr();
2026 if (s0 === peg$FAILED) {
2027 s0 = peg$parsefield();
2029 if (s0 === peg$FAILED) {
2030 s0 = peg$parsenegation();
2032 if (s0 === peg$FAILED) {
2033 s0 = peg$parsematches();
2035 if (s0 === peg$FAILED) {
2036 s0 = peg$parsehas();
2038 if (s0 === peg$FAILED) {
2039 s0 = peg$parsefirstChild();
2041 if (s0 === peg$FAILED) {
2042 s0 = peg$parselastChild();
2044 if (s0 === peg$FAILED) {
2045 s0 = peg$parsenthChild();
2047 if (s0 === peg$FAILED) {
2048 s0 = peg$parsenthLastChild();
2050 if (s0 === peg$FAILED) {
2051 s0 = peg$parseclass();
2064 peg$resultsCache[key] = {
2065 nextPos: peg$currPos,
2071 function peg$parsewildcard() {
2073 var key = peg$currPos * 30 + 8,
2074 cached = peg$resultsCache[key];
2077 peg$currPos = cached.nextPos;
2078 return cached.result;
2083 if (input.charCodeAt(peg$currPos) === 42) {
2094 if (s1 !== peg$FAILED) {
2099 peg$resultsCache[key] = {
2100 nextPos: peg$currPos,
2106 function peg$parseidentifier() {
2108 var key = peg$currPos * 30 + 9,
2109 cached = peg$resultsCache[key];
2112 peg$currPos = cached.nextPos;
2113 return cached.result;
2118 if (input.charCodeAt(peg$currPos) === 35) {
2129 if (s1 === peg$FAILED) {
2133 if (s1 !== peg$FAILED) {
2134 s2 = peg$parseidentifierName();
2136 if (s2 !== peg$FAILED) {
2148 peg$resultsCache[key] = {
2149 nextPos: peg$currPos,
2155 function peg$parseattr() {
2156 var s0, s1, s2, s3, s4, s5;
2157 var key = peg$currPos * 30 + 10,
2158 cached = peg$resultsCache[key];
2161 peg$currPos = cached.nextPos;
2162 return cached.result;
2167 if (input.charCodeAt(peg$currPos) === 91) {
2178 if (s1 !== peg$FAILED) {
2181 if (s2 !== peg$FAILED) {
2182 s3 = peg$parseattrValue();
2184 if (s3 !== peg$FAILED) {
2187 if (s4 !== peg$FAILED) {
2188 if (input.charCodeAt(peg$currPos) === 93) {
2199 if (s5 !== peg$FAILED) {
2223 peg$resultsCache[key] = {
2224 nextPos: peg$currPos,
2230 function peg$parseattrOps() {
2232 var key = peg$currPos * 30 + 11,
2233 cached = peg$resultsCache[key];
2236 peg$currPos = cached.nextPos;
2237 return cached.result;
2242 if (peg$c35.test(input.charAt(peg$currPos))) {
2243 s1 = input.charAt(peg$currPos);
2253 if (s1 === peg$FAILED) {
2257 if (s1 !== peg$FAILED) {
2258 if (input.charCodeAt(peg$currPos) === 61) {
2269 if (s2 !== peg$FAILED) {
2281 if (s0 === peg$FAILED) {
2282 if (peg$c40.test(input.charAt(peg$currPos))) {
2283 s0 = input.charAt(peg$currPos);
2294 peg$resultsCache[key] = {
2295 nextPos: peg$currPos,
2301 function peg$parseattrEqOps() {
2303 var key = peg$currPos * 30 + 12,
2304 cached = peg$resultsCache[key];
2307 peg$currPos = cached.nextPos;
2308 return cached.result;
2313 if (input.charCodeAt(peg$currPos) === 33) {
2324 if (s1 === peg$FAILED) {
2328 if (s1 !== peg$FAILED) {
2329 if (input.charCodeAt(peg$currPos) === 61) {
2340 if (s2 !== peg$FAILED) {
2352 peg$resultsCache[key] = {
2353 nextPos: peg$currPos,
2359 function peg$parseattrName() {
2360 var s0, s1, s2, s3, s4, s5;
2361 var key = peg$currPos * 30 + 13,
2362 cached = peg$resultsCache[key];
2365 peg$currPos = cached.nextPos;
2366 return cached.result;
2370 s1 = peg$parseidentifierName();
2372 if (s1 !== peg$FAILED) {
2376 if (input.charCodeAt(peg$currPos) === 46) {
2387 if (s4 !== peg$FAILED) {
2388 s5 = peg$parseidentifierName();
2390 if (s5 !== peg$FAILED) {
2402 while (s3 !== peg$FAILED) {
2406 if (input.charCodeAt(peg$currPos) === 46) {
2417 if (s4 !== peg$FAILED) {
2418 s5 = peg$parseidentifierName();
2420 if (s5 !== peg$FAILED) {
2433 if (s2 !== peg$FAILED) {
2434 s1 = peg$c44(s1, s2);
2445 peg$resultsCache[key] = {
2446 nextPos: peg$currPos,
2452 function peg$parseattrValue() {
2453 var s0, s1, s2, s3, s4, s5;
2454 var key = peg$currPos * 30 + 14,
2455 cached = peg$resultsCache[key];
2458 peg$currPos = cached.nextPos;
2459 return cached.result;
2463 s1 = peg$parseattrName();
2465 if (s1 !== peg$FAILED) {
2468 if (s2 !== peg$FAILED) {
2469 s3 = peg$parseattrEqOps();
2471 if (s3 !== peg$FAILED) {
2474 if (s4 !== peg$FAILED) {
2475 s5 = peg$parsetype();
2477 if (s5 === peg$FAILED) {
2478 s5 = peg$parseregex();
2481 if (s5 !== peg$FAILED) {
2482 s1 = peg$c45(s1, s3, s5);
2505 if (s0 === peg$FAILED) {
2507 s1 = peg$parseattrName();
2509 if (s1 !== peg$FAILED) {
2512 if (s2 !== peg$FAILED) {
2513 s3 = peg$parseattrOps();
2515 if (s3 !== peg$FAILED) {
2518 if (s4 !== peg$FAILED) {
2519 s5 = peg$parsestring();
2521 if (s5 === peg$FAILED) {
2522 s5 = peg$parsenumber();
2524 if (s5 === peg$FAILED) {
2525 s5 = peg$parsepath();
2529 if (s5 !== peg$FAILED) {
2530 s1 = peg$c45(s1, s3, s5);
2553 if (s0 === peg$FAILED) {
2555 s1 = peg$parseattrName();
2557 if (s1 !== peg$FAILED) {
2565 peg$resultsCache[key] = {
2566 nextPos: peg$currPos,
2572 function peg$parsestring() {
2573 var s0, s1, s2, s3, s4, s5;
2574 var key = peg$currPos * 30 + 15,
2575 cached = peg$resultsCache[key];
2578 peg$currPos = cached.nextPos;
2579 return cached.result;
2584 if (input.charCodeAt(peg$currPos) === 34) {
2595 if (s1 !== peg$FAILED) {
2598 if (peg$c49.test(input.charAt(peg$currPos))) {
2599 s3 = input.charAt(peg$currPos);
2609 if (s3 === peg$FAILED) {
2612 if (input.charCodeAt(peg$currPos) === 92) {
2623 if (s4 !== peg$FAILED) {
2624 if (input.length > peg$currPos) {
2625 s5 = input.charAt(peg$currPos);
2635 if (s5 !== peg$FAILED) {
2636 s4 = peg$c54(s4, s5);
2648 while (s3 !== peg$FAILED) {
2651 if (peg$c49.test(input.charAt(peg$currPos))) {
2652 s3 = input.charAt(peg$currPos);
2662 if (s3 === peg$FAILED) {
2665 if (input.charCodeAt(peg$currPos) === 92) {
2676 if (s4 !== peg$FAILED) {
2677 if (input.length > peg$currPos) {
2678 s5 = input.charAt(peg$currPos);
2688 if (s5 !== peg$FAILED) {
2689 s4 = peg$c54(s4, s5);
2702 if (s2 !== peg$FAILED) {
2703 if (input.charCodeAt(peg$currPos) === 34) {
2714 if (s3 !== peg$FAILED) {
2730 if (s0 === peg$FAILED) {
2733 if (input.charCodeAt(peg$currPos) === 39) {
2744 if (s1 !== peg$FAILED) {
2747 if (peg$c58.test(input.charAt(peg$currPos))) {
2748 s3 = input.charAt(peg$currPos);
2758 if (s3 === peg$FAILED) {
2761 if (input.charCodeAt(peg$currPos) === 92) {
2772 if (s4 !== peg$FAILED) {
2773 if (input.length > peg$currPos) {
2774 s5 = input.charAt(peg$currPos);
2784 if (s5 !== peg$FAILED) {
2785 s4 = peg$c54(s4, s5);
2797 while (s3 !== peg$FAILED) {
2800 if (peg$c58.test(input.charAt(peg$currPos))) {
2801 s3 = input.charAt(peg$currPos);
2811 if (s3 === peg$FAILED) {
2814 if (input.charCodeAt(peg$currPos) === 92) {
2825 if (s4 !== peg$FAILED) {
2826 if (input.length > peg$currPos) {
2827 s5 = input.charAt(peg$currPos);
2837 if (s5 !== peg$FAILED) {
2838 s4 = peg$c54(s4, s5);
2851 if (s2 !== peg$FAILED) {
2852 if (input.charCodeAt(peg$currPos) === 39) {
2863 if (s3 !== peg$FAILED) {
2880 peg$resultsCache[key] = {
2881 nextPos: peg$currPos,
2887 function peg$parsenumber() {
2889 var key = peg$currPos * 30 + 16,
2890 cached = peg$resultsCache[key];
2893 peg$currPos = cached.nextPos;
2894 return cached.result;
2901 if (peg$c60.test(input.charAt(peg$currPos))) {
2902 s3 = input.charAt(peg$currPos);
2912 while (s3 !== peg$FAILED) {
2915 if (peg$c60.test(input.charAt(peg$currPos))) {
2916 s3 = input.charAt(peg$currPos);
2927 if (s2 !== peg$FAILED) {
2928 if (input.charCodeAt(peg$currPos) === 46) {
2939 if (s3 !== peg$FAILED) {
2951 if (s1 === peg$FAILED) {
2955 if (s1 !== peg$FAILED) {
2958 if (peg$c60.test(input.charAt(peg$currPos))) {
2959 s3 = input.charAt(peg$currPos);
2969 if (s3 !== peg$FAILED) {
2970 while (s3 !== peg$FAILED) {
2973 if (peg$c60.test(input.charAt(peg$currPos))) {
2974 s3 = input.charAt(peg$currPos);
2988 if (s2 !== peg$FAILED) {
2989 s1 = peg$c62(s1, s2);
3000 peg$resultsCache[key] = {
3001 nextPos: peg$currPos,
3007 function peg$parsepath() {
3009 var key = peg$currPos * 30 + 17,
3010 cached = peg$resultsCache[key];
3013 peg$currPos = cached.nextPos;
3014 return cached.result;
3018 s1 = peg$parseidentifierName();
3020 if (s1 !== peg$FAILED) {
3025 peg$resultsCache[key] = {
3026 nextPos: peg$currPos,
3032 function peg$parsetype() {
3033 var s0, s1, s2, s3, s4, s5;
3034 var key = peg$currPos * 30 + 18,
3035 cached = peg$resultsCache[key];
3038 peg$currPos = cached.nextPos;
3039 return cached.result;
3044 if (input.substr(peg$currPos, 5) === peg$c64) {
3055 if (s1 !== peg$FAILED) {
3058 if (s2 !== peg$FAILED) {
3061 if (peg$c66.test(input.charAt(peg$currPos))) {
3062 s4 = input.charAt(peg$currPos);
3072 if (s4 !== peg$FAILED) {
3073 while (s4 !== peg$FAILED) {
3076 if (peg$c66.test(input.charAt(peg$currPos))) {
3077 s4 = input.charAt(peg$currPos);
3091 if (s3 !== peg$FAILED) {
3094 if (s4 !== peg$FAILED) {
3095 if (input.charCodeAt(peg$currPos) === 41) {
3106 if (s5 !== peg$FAILED) {
3130 peg$resultsCache[key] = {
3131 nextPos: peg$currPos,
3137 function peg$parseflags() {
3139 var key = peg$currPos * 30 + 19,
3140 cached = peg$resultsCache[key];
3143 peg$currPos = cached.nextPos;
3144 return cached.result;
3149 if (peg$c71.test(input.charAt(peg$currPos))) {
3150 s1 = input.charAt(peg$currPos);
3160 if (s1 !== peg$FAILED) {
3161 while (s1 !== peg$FAILED) {
3164 if (peg$c71.test(input.charAt(peg$currPos))) {
3165 s1 = input.charAt(peg$currPos);
3179 peg$resultsCache[key] = {
3180 nextPos: peg$currPos,
3186 function peg$parseregex() {
3187 var s0, s1, s2, s3, s4;
3188 var key = peg$currPos * 30 + 20,
3189 cached = peg$resultsCache[key];
3192 peg$currPos = cached.nextPos;
3193 return cached.result;
3198 if (input.charCodeAt(peg$currPos) === 47) {
3209 if (s1 !== peg$FAILED) {
3212 if (peg$c75.test(input.charAt(peg$currPos))) {
3213 s3 = input.charAt(peg$currPos);
3223 if (s3 !== peg$FAILED) {
3224 while (s3 !== peg$FAILED) {
3227 if (peg$c75.test(input.charAt(peg$currPos))) {
3228 s3 = input.charAt(peg$currPos);
3242 if (s2 !== peg$FAILED) {
3243 if (input.charCodeAt(peg$currPos) === 47) {
3254 if (s3 !== peg$FAILED) {
3255 s4 = peg$parseflags();
3257 if (s4 === peg$FAILED) {
3261 if (s4 !== peg$FAILED) {
3262 s1 = peg$c77(s2, s4);
3281 peg$resultsCache[key] = {
3282 nextPos: peg$currPos,
3288 function peg$parsefield() {
3289 var s0, s1, s2, s3, s4, s5, s6;
3290 var key = peg$currPos * 30 + 21,
3291 cached = peg$resultsCache[key];
3294 peg$currPos = cached.nextPos;
3295 return cached.result;
3300 if (input.charCodeAt(peg$currPos) === 46) {
3311 if (s1 !== peg$FAILED) {
3312 s2 = peg$parseidentifierName();
3314 if (s2 !== peg$FAILED) {
3318 if (input.charCodeAt(peg$currPos) === 46) {
3329 if (s5 !== peg$FAILED) {
3330 s6 = peg$parseidentifierName();
3332 if (s6 !== peg$FAILED) {
3344 while (s4 !== peg$FAILED) {
3348 if (input.charCodeAt(peg$currPos) === 46) {
3359 if (s5 !== peg$FAILED) {
3360 s6 = peg$parseidentifierName();
3362 if (s6 !== peg$FAILED) {
3375 if (s3 !== peg$FAILED) {
3376 s1 = peg$c78(s2, s3);
3391 peg$resultsCache[key] = {
3392 nextPos: peg$currPos,
3398 function peg$parsenegation() {
3399 var s0, s1, s2, s3, s4, s5;
3400 var key = peg$currPos * 30 + 22,
3401 cached = peg$resultsCache[key];
3404 peg$currPos = cached.nextPos;
3405 return cached.result;
3410 if (input.substr(peg$currPos, 5) === peg$c79) {
3421 if (s1 !== peg$FAILED) {
3424 if (s2 !== peg$FAILED) {
3425 s3 = peg$parseselectors();
3427 if (s3 !== peg$FAILED) {
3430 if (s4 !== peg$FAILED) {
3431 if (input.charCodeAt(peg$currPos) === 41) {
3442 if (s5 !== peg$FAILED) {
3466 peg$resultsCache[key] = {
3467 nextPos: peg$currPos,
3473 function peg$parsematches() {
3474 var s0, s1, s2, s3, s4, s5;
3475 var key = peg$currPos * 30 + 23,
3476 cached = peg$resultsCache[key];
3479 peg$currPos = cached.nextPos;
3480 return cached.result;
3485 if (input.substr(peg$currPos, 9) === peg$c82) {
3496 if (s1 !== peg$FAILED) {
3499 if (s2 !== peg$FAILED) {
3500 s3 = peg$parseselectors();
3502 if (s3 !== peg$FAILED) {
3505 if (s4 !== peg$FAILED) {
3506 if (input.charCodeAt(peg$currPos) === 41) {
3517 if (s5 !== peg$FAILED) {
3541 peg$resultsCache[key] = {
3542 nextPos: peg$currPos,
3548 function peg$parsehas() {
3549 var s0, s1, s2, s3, s4, s5;
3550 var key = peg$currPos * 30 + 24,
3551 cached = peg$resultsCache[key];
3554 peg$currPos = cached.nextPos;
3555 return cached.result;
3560 if (input.substr(peg$currPos, 5) === peg$c85) {
3571 if (s1 !== peg$FAILED) {
3574 if (s2 !== peg$FAILED) {
3575 s3 = peg$parseselectors();
3577 if (s3 !== peg$FAILED) {
3580 if (s4 !== peg$FAILED) {
3581 if (input.charCodeAt(peg$currPos) === 41) {
3592 if (s5 !== peg$FAILED) {
3616 peg$resultsCache[key] = {
3617 nextPos: peg$currPos,
3623 function peg$parsefirstChild() {
3625 var key = peg$currPos * 30 + 25,
3626 cached = peg$resultsCache[key];
3629 peg$currPos = cached.nextPos;
3630 return cached.result;
3635 if (input.substr(peg$currPos, 12) === peg$c88) {
3646 if (s1 !== peg$FAILED) {
3651 peg$resultsCache[key] = {
3652 nextPos: peg$currPos,
3658 function peg$parselastChild() {
3660 var key = peg$currPos * 30 + 26,
3661 cached = peg$resultsCache[key];
3664 peg$currPos = cached.nextPos;
3665 return cached.result;
3670 if (input.substr(peg$currPos, 11) === peg$c91) {
3681 if (s1 !== peg$FAILED) {
3686 peg$resultsCache[key] = {
3687 nextPos: peg$currPos,
3693 function peg$parsenthChild() {
3694 var s0, s1, s2, s3, s4, s5;
3695 var key = peg$currPos * 30 + 27,
3696 cached = peg$resultsCache[key];
3699 peg$currPos = cached.nextPos;
3700 return cached.result;
3705 if (input.substr(peg$currPos, 11) === peg$c94) {
3716 if (s1 !== peg$FAILED) {
3719 if (s2 !== peg$FAILED) {
3722 if (peg$c60.test(input.charAt(peg$currPos))) {
3723 s4 = input.charAt(peg$currPos);
3733 if (s4 !== peg$FAILED) {
3734 while (s4 !== peg$FAILED) {
3737 if (peg$c60.test(input.charAt(peg$currPos))) {
3738 s4 = input.charAt(peg$currPos);
3752 if (s3 !== peg$FAILED) {
3755 if (s4 !== peg$FAILED) {
3756 if (input.charCodeAt(peg$currPos) === 41) {
3767 if (s5 !== peg$FAILED) {
3791 peg$resultsCache[key] = {
3792 nextPos: peg$currPos,
3798 function peg$parsenthLastChild() {
3799 var s0, s1, s2, s3, s4, s5;
3800 var key = peg$currPos * 30 + 28,
3801 cached = peg$resultsCache[key];
3804 peg$currPos = cached.nextPos;
3805 return cached.result;
3810 if (input.substr(peg$currPos, 16) === peg$c97) {
3821 if (s1 !== peg$FAILED) {
3824 if (s2 !== peg$FAILED) {
3827 if (peg$c60.test(input.charAt(peg$currPos))) {
3828 s4 = input.charAt(peg$currPos);
3838 if (s4 !== peg$FAILED) {
3839 while (s4 !== peg$FAILED) {
3842 if (peg$c60.test(input.charAt(peg$currPos))) {
3843 s4 = input.charAt(peg$currPos);
3857 if (s3 !== peg$FAILED) {
3860 if (s4 !== peg$FAILED) {
3861 if (input.charCodeAt(peg$currPos) === 41) {
3872 if (s5 !== peg$FAILED) {
3896 peg$resultsCache[key] = {
3897 nextPos: peg$currPos,
3903 function peg$parseclass() {
3905 var key = peg$currPos * 30 + 29,
3906 cached = peg$resultsCache[key];
3909 peg$currPos = cached.nextPos;
3910 return cached.result;
3915 if (input.charCodeAt(peg$currPos) === 58) {
3926 if (s1 !== peg$FAILED) {
3927 if (input.substr(peg$currPos, 9).toLowerCase() === peg$c102) {
3928 s2 = input.substr(peg$currPos, 9);
3938 if (s2 === peg$FAILED) {
3939 if (input.substr(peg$currPos, 10).toLowerCase() === peg$c104) {
3940 s2 = input.substr(peg$currPos, 10);
3950 if (s2 === peg$FAILED) {
3951 if (input.substr(peg$currPos, 11).toLowerCase() === peg$c106) {
3952 s2 = input.substr(peg$currPos, 11);
3962 if (s2 === peg$FAILED) {
3963 if (input.substr(peg$currPos, 8).toLowerCase() === peg$c108) {
3964 s2 = input.substr(peg$currPos, 8);
3974 if (s2 === peg$FAILED) {
3975 if (input.substr(peg$currPos, 7).toLowerCase() === peg$c110) {
3976 s2 = input.substr(peg$currPos, 7);
3990 if (s2 !== peg$FAILED) {
4002 peg$resultsCache[key] = {
4003 nextPos: peg$currPos,
4019 function nthLast(n) {
4021 type: 'nth-last-child',
4029 function strUnescape(s) {
4030 return s.replace(/\\(.)/g, function (match, ch) {
4056 peg$result = peg$startRuleFunction();
4058 if (peg$result !== peg$FAILED && peg$currPos === input.length) {
4061 if (peg$result !== peg$FAILED && peg$currPos < input.length) {
4062 peg$fail(peg$endExpectation());
4065 throw peg$buildStructuredError(peg$maxFailExpected, peg$maxFailPos < input.length ? input.charAt(peg$maxFailPos) : null, peg$maxFailPos < input.length ? peg$computeLocation(peg$maxFailPos, peg$maxFailPos + 1) : peg$computeLocation(peg$maxFailPos, peg$maxFailPos));
4070 SyntaxError: peg$SyntaxError,
4076 function _objectEntries(obj) {
4078 var keys = Object.keys(obj);
4080 for (var k = 0; k < keys.length; k++) entries.push([keys[k], obj[keys[k]]]);
4085 * @typedef {"LEFT_SIDE"|"RIGHT_SIDE"} Side
4088 var LEFT_SIDE = 'LEFT_SIDE';
4089 var RIGHT_SIDE = 'RIGHT_SIDE';
4092 * @see https://esprima.readthedocs.io/en/latest/syntax-tree-format.html
4096 * One of the rules of `grammar.pegjs`
4097 * @typedef {PlainObject} SelectorAST
4098 * @see grammar.pegjs
4102 * The `sequence` production of `grammar.pegjs`
4103 * @typedef {PlainObject} SelectorSequenceAST
4107 * Get the value of a property which may be multiple levels down
4109 * @param {?PlainObject} obj
4110 * @param {string} key
4111 * @returns {undefined|boolean|string|number|external:AST}
4114 function getPath(obj, key) {
4115 var keys = key.split('.');
4117 var _iterator = _createForOfIteratorHelper(keys),
4121 for (_iterator.s(); !(_step = _iterator.n()).done;) {
4122 var _key = _step.value;
4139 * Determine whether `node` can be reached by following `path`,
4140 * starting at `ancestor`.
4141 * @param {?external:AST} node
4142 * @param {?external:AST} ancestor
4143 * @param {string[]} path
4144 * @returns {boolean}
4148 function inPath(node, ancestor, path) {
4149 if (path.length === 0) {
4150 return node === ancestor;
4153 if (ancestor == null) {
4157 var field = ancestor[path[0]];
4158 var remainingPath = path.slice(1);
4160 if (Array.isArray(field)) {
4161 var _iterator2 = _createForOfIteratorHelper(field),
4165 for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
4166 var component = _step2.value;
4168 if (inPath(node, component, remainingPath)) {
4180 return inPath(node, field, remainingPath);
4184 * @callback TraverseOptionFallback
4185 * @param {external:AST} node The given node.
4186 * @returns {string[]} An array of visitor keys for the given node.
4190 * @typedef {object} ESQueryOptions
4191 * @property { { [nodeType: string]: string[] } } [visitorKeys] By passing `visitorKeys` mapping, we can extend the properties of the nodes that traverse the node.
4192 * @property {TraverseOptionFallback} [fallback] By passing `fallback` option, we can control the properties of traversing nodes when encountering unknown nodes.
4196 * Given a `node` and its ancestors, determine if `node` is matched
4198 * @param {?external:AST} node
4199 * @param {?SelectorAST} selector
4200 * @param {external:AST[]} [ancestry=[]]
4201 * @param {ESQueryOptions} [options]
4202 * @throws {Error} Unknowns (operator, class name, selector type, or
4203 * selector value type)
4204 * @returns {boolean}
4208 function matches(node, selector, ancestry, options) {
4221 switch (selector.type) {
4226 return selector.value.toLowerCase() === node.type.toLowerCase();
4230 var path = selector.name.split('.');
4231 var ancestor = ancestry[path.length - 1];
4232 return inPath(node, ancestor, path);
4236 var _iterator3 = _createForOfIteratorHelper(selector.selectors),
4240 for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
4241 var sel = _step3.value;
4243 if (matches(node, sel, ancestry, options)) {
4256 var _iterator4 = _createForOfIteratorHelper(selector.selectors),
4260 for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
4261 var _sel = _step4.value;
4263 if (!matches(node, _sel, ancestry, options)) {
4276 var _iterator5 = _createForOfIteratorHelper(selector.selectors),
4280 for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
4281 var _sel2 = _step5.value;
4283 if (matches(node, _sel2, ancestry, options)) {
4297 var _ret = function () {
4300 var _iterator6 = _createForOfIteratorHelper(selector.selectors),
4304 var _loop = function _loop() {
4305 var sel = _step6.value;
4307 estraverse.traverse(node, {
4308 enter: function enter(node, parent) {
4309 if (parent != null) {
4313 if (matches(node, sel, a, options)) {
4314 collector.push(node);
4317 leave: function leave() {
4320 keys: options && options.visitorKeys,
4321 fallback: options && options.fallback || 'iteration'
4325 for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
4335 v: collector.length !== 0
4339 if (_typeof(_ret) === "object") return _ret.v;
4343 if (matches(node, selector.right, ancestry, options)) {
4344 return matches(ancestry[0], selector.left, ancestry.slice(1), options);
4350 if (matches(node, selector.right, ancestry, options)) {
4351 for (var i = 0, l = ancestry.length; i < l; ++i) {
4352 if (matches(ancestry[i], selector.left, ancestry.slice(i + 1), options)) {
4362 var p = getPath(node, selector.name);
4364 switch (selector.operator) {
4369 switch (selector.value.type) {
4371 return typeof p === 'string' && selector.value.value.test(p);
4374 return "".concat(selector.value.value) === "".concat(p);
4377 return selector.value.value === _typeof(p);
4380 throw new Error("Unknown selector value type: ".concat(selector.value.type));
4383 switch (selector.value.type) {
4385 return !selector.value.value.test(p);
4388 return "".concat(selector.value.value) !== "".concat(p);
4391 return selector.value.value !== _typeof(p);
4394 throw new Error("Unknown selector value type: ".concat(selector.value.type));
4397 return p <= selector.value.value;
4400 return p < selector.value.value;
4403 return p > selector.value.value;
4406 return p >= selector.value.value;
4409 throw new Error("Unknown operator: ".concat(selector.operator));
4413 return matches(node, selector.right, ancestry, options) && sibling(node, selector.left, ancestry, LEFT_SIDE, options) || selector.left.subject && matches(node, selector.left, ancestry, options) && sibling(node, selector.right, ancestry, RIGHT_SIDE, options);
4416 return matches(node, selector.right, ancestry, options) && adjacent(node, selector.left, ancestry, LEFT_SIDE, options) || selector.right.subject && matches(node, selector.left, ancestry, options) && adjacent(node, selector.right, ancestry, RIGHT_SIDE, options);
4419 return matches(node, selector.right, ancestry, options) && nthChild(node, ancestry, function () {
4420 return selector.index.value - 1;
4423 case 'nth-last-child':
4424 return matches(node, selector.right, ancestry, options) && nthChild(node, ancestry, function (length) {
4425 return length - selector.index.value;
4429 switch (selector.name.toLowerCase()) {
4431 if (node.type.slice(-9) === 'Statement') return true;
4432 // fallthrough: interface Declaration <: Statement { }
4435 return node.type.slice(-11) === 'Declaration';
4438 if (node.type.slice(-7) === 'Pattern') return true;
4439 // fallthrough: interface Expression <: Node, Pattern { }
4442 return node.type.slice(-10) === 'Expression' || node.type.slice(-7) === 'Literal' || node.type === 'Identifier' && (ancestry.length === 0 || ancestry[0].type !== 'MetaProperty') || node.type === 'MetaProperty';
4445 return node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression';
4448 throw new Error("Unknown class name: ".concat(selector.name));
4451 throw new Error("Unknown selector type: ".concat(selector.type));
4454 * Get visitor keys of a given node.
4455 * @param {external:AST} node The AST node to get keys.
4456 * @param {ESQueryOptions|undefined} options
4457 * @returns {string[]} Visitor keys of the node.
4461 function getVisitorKeys(node, options) {
4462 var nodeType = node.type;
4464 if (options && options.visitorKeys && options.visitorKeys[nodeType]) {
4465 return options.visitorKeys[nodeType];
4468 if (estraverse.VisitorKeys[nodeType]) {
4469 return estraverse.VisitorKeys[nodeType];
4472 if (options && typeof options.fallback === 'function') {
4473 return options.fallback(node);
4474 } // 'iteration' fallback
4477 return Object.keys(node).filter(function (key) {
4478 return key !== 'type';
4482 * Check whether the given value is an ASTNode or not.
4483 * @param {any} node The value to check.
4484 * @returns {boolean} `true` if the value is an ASTNode.
4488 function isNode(node) {
4489 return node !== null && _typeof(node) === 'object' && typeof node.type === 'string';
4492 * Determines if the given node has a sibling that matches the
4494 * @param {external:AST} node
4495 * @param {SelectorSequenceAST} selector
4496 * @param {external:AST[]} ancestry
4497 * @param {Side} side
4498 * @param {ESQueryOptions|undefined} options
4499 * @returns {boolean}
4503 function sibling(node, selector, ancestry, side, options) {
4504 var _ancestry = _slicedToArray(ancestry, 1),
4505 parent = _ancestry[0];
4511 var keys = getVisitorKeys(parent, options);
4513 var _iterator7 = _createForOfIteratorHelper(keys),
4517 for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
4518 var key = _step7.value;
4519 var listProp = parent[key];
4521 if (Array.isArray(listProp)) {
4522 var startIndex = listProp.indexOf(node);
4524 if (startIndex < 0) {
4528 var lowerBound = void 0,
4529 upperBound = void 0;
4531 if (side === LEFT_SIDE) {
4533 upperBound = startIndex;
4535 lowerBound = startIndex + 1;
4536 upperBound = listProp.length;
4539 for (var k = lowerBound; k < upperBound; ++k) {
4540 if (isNode(listProp[k]) && matches(listProp[k], selector, ancestry, options)) {
4555 * Determines if the given node has an adjacent sibling that matches
4556 * the given selector.
4557 * @param {external:AST} node
4558 * @param {SelectorSequenceAST} selector
4559 * @param {external:AST[]} ancestry
4560 * @param {Side} side
4561 * @param {ESQueryOptions|undefined} options
4562 * @returns {boolean}
4566 function adjacent(node, selector, ancestry, side, options) {
4567 var _ancestry2 = _slicedToArray(ancestry, 1),
4568 parent = _ancestry2[0];
4574 var keys = getVisitorKeys(parent, options);
4576 var _iterator8 = _createForOfIteratorHelper(keys),
4580 for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
4581 var key = _step8.value;
4582 var listProp = parent[key];
4584 if (Array.isArray(listProp)) {
4585 var idx = listProp.indexOf(node);
4591 if (side === LEFT_SIDE && idx > 0 && isNode(listProp[idx - 1]) && matches(listProp[idx - 1], selector, ancestry, options)) {
4595 if (side === RIGHT_SIDE && idx < listProp.length - 1 && isNode(listProp[idx + 1]) && matches(listProp[idx + 1], selector, ancestry, options)) {
4609 * @callback IndexFunction
4610 * @param {Integer} len Containing list's length
4611 * @returns {Integer}
4615 * Determines if the given node is the nth child, determined by
4616 * `idxFn`, which is given the containing list's length.
4617 * @param {external:AST} node
4618 * @param {external:AST[]} ancestry
4619 * @param {IndexFunction} idxFn
4620 * @param {ESQueryOptions|undefined} options
4621 * @returns {boolean}
4625 function nthChild(node, ancestry, idxFn, options) {
4626 var _ancestry3 = _slicedToArray(ancestry, 1),
4627 parent = _ancestry3[0];
4633 var keys = getVisitorKeys(parent, options);
4635 var _iterator9 = _createForOfIteratorHelper(keys),
4639 for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
4640 var key = _step9.value;
4641 var listProp = parent[key];
4643 if (Array.isArray(listProp)) {
4644 var idx = listProp.indexOf(node);
4646 if (idx >= 0 && idx === idxFn(listProp.length)) {
4660 * For each selector node marked as a subject, find the portion of the
4661 * selector that the subject must match.
4662 * @param {SelectorAST} selector
4663 * @param {SelectorAST} [ancestor] Defaults to `selector`
4664 * @returns {SelectorAST[]}
4668 function subjects(selector, ancestor) {
4669 if (selector == null || _typeof(selector) != 'object') {
4673 if (ancestor == null) {
4674 ancestor = selector;
4677 var results = selector.subject ? [ancestor] : [];
4679 for (var _i = 0, _Object$entries = _objectEntries(selector); _i < _Object$entries.length; _i++) {
4680 var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
4681 p = _Object$entries$_i[0],
4682 sel = _Object$entries$_i[1];
4684 results.push.apply(results, _toConsumableArray(subjects(sel, p === 'left' ? sel : ancestor)));
4690 * @callback TraverseVisitor
4691 * @param {?external:AST} node
4692 * @param {?external:AST} parent
4693 * @param {external:AST[]} ancestry
4697 * From a JS AST and a selector AST, collect all JS AST nodes that
4698 * match the selector.
4699 * @param {external:AST} ast
4700 * @param {?SelectorAST} selector
4701 * @param {TraverseVisitor} visitor
4702 * @param {ESQueryOptions} [options]
4703 * @returns {external:AST[]}
4707 function traverse(ast, selector, visitor, options) {
4713 var altSubjects = subjects(selector);
4714 estraverse.traverse(ast, {
4715 enter: function enter(node, parent) {
4716 if (parent != null) {
4717 ancestry.unshift(parent);
4720 if (matches(node, selector, ancestry, options)) {
4721 if (altSubjects.length) {
4722 for (var i = 0, l = altSubjects.length; i < l; ++i) {
4723 if (matches(node, altSubjects[i], ancestry, options)) {
4724 visitor(node, parent, ancestry);
4727 for (var k = 0, m = ancestry.length; k < m; ++k) {
4728 var succeedingAncestry = ancestry.slice(k + 1);
4730 if (matches(ancestry[k], altSubjects[i], succeedingAncestry, options)) {
4731 visitor(ancestry[k], parent, succeedingAncestry);
4736 visitor(node, parent, ancestry);
4740 leave: function leave() {
4743 keys: options && options.visitorKeys,
4744 fallback: options && options.fallback || 'iteration'
4748 * From a JS AST and a selector AST, collect all JS AST nodes that
4749 * match the selector.
4750 * @param {external:AST} ast
4751 * @param {?SelectorAST} selector
4752 * @param {ESQueryOptions} [options]
4753 * @returns {external:AST[]}
4757 function match(ast, selector, options) {
4759 traverse(ast, selector, function (node) {
4765 * Parse a selector string and return its AST.
4766 * @param {string} selector
4767 * @returns {SelectorAST}
4771 function parse(selector) {
4772 return parser.parse(selector);
4775 * Query the code AST using the selector string.
4776 * @param {external:AST} ast
4777 * @param {string} selector
4778 * @param {ESQueryOptions} [options]
4779 * @returns {external:AST[]}
4783 function query(ast, selector, options) {
4784 return match(ast, parse(selector), options);
4787 query.parse = parse;
4788 query.match = match;
4789 query.traverse = traverse;
4790 query.matches = matches;
4791 query.query = query;
4793 export default query;