1 (function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3 typeof define === 'function' && define.amd ? define(factory) :
4 (global = global || self, global.esquery = factory());
5 }(this, (function () { 'use strict';
7 function _typeof(obj) {
8 "@babel/helpers - typeof";
10 if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
11 _typeof = function (obj) {
15 _typeof = function (obj) {
16 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
23 function _slicedToArray(arr, i) {
24 return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
27 function _toConsumableArray(arr) {
28 return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
31 function _arrayWithoutHoles(arr) {
32 if (Array.isArray(arr)) return _arrayLikeToArray(arr);
35 function _arrayWithHoles(arr) {
36 if (Array.isArray(arr)) return arr;
39 function _iterableToArray(iter) {
40 if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
43 function _iterableToArrayLimit(arr, i) {
44 if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
51 for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
54 if (i && _arr.length === i) break;
61 if (!_n && _i["return"] != null) _i["return"]();
70 function _unsupportedIterableToArray(o, minLen) {
72 if (typeof o === "string") return _arrayLikeToArray(o, minLen);
73 var n = Object.prototype.toString.call(o).slice(8, -1);
74 if (n === "Object" && o.constructor) n = o.constructor.name;
75 if (n === "Map" || n === "Set") return Array.from(o);
76 if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
79 function _arrayLikeToArray(arr, len) {
80 if (len == null || len > arr.length) len = arr.length;
82 for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
87 function _nonIterableSpread() {
88 throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
91 function _nonIterableRest() {
92 throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
95 function _createForOfIteratorHelper(o, allowArrayLike) {
98 if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
99 if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
103 var F = function () {};
108 if (i >= o.length) return {
123 throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
126 var normalCompletion = true,
131 it = o[Symbol.iterator]();
134 var step = it.next();
135 normalCompletion = step.done;
144 if (!normalCompletion && it.return != null) it.return();
146 if (didErr) throw err;
152 var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
154 function createCommonjsModule(fn, module) {
155 return module = { exports: {} }, fn(module, module.exports), module.exports;
158 var estraverse = createCommonjsModule(function (module, exports) {
160 Copyright (C) 2012-2013 Yusuke Suzuki <utatane.tea@gmail.com>
161 Copyright (C) 2012 Ariya Hidayat <ariya.hidayat@gmail.com>
163 Redistribution and use in source and binary forms, with or without
164 modification, are permitted provided that the following conditions are met:
166 * Redistributions of source code must retain the above copyright
167 notice, this list of conditions and the following disclaimer.
168 * Redistributions in binary form must reproduce the above copyright
169 notice, this list of conditions and the following disclaimer in the
170 documentation and/or other materials provided with the distribution.
172 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
173 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
174 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
175 ARE DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
176 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
177 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
178 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
179 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
180 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
181 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
184 /*jslint vars:false, bitwise:true*/
188 /*global exports:true*/
189 (function clone(exports) {
191 var Syntax, VisitorOption, VisitorKeys, BREAK, SKIP, REMOVE;
193 function deepCopy(obj) {
199 if (obj.hasOwnProperty(key)) {
202 if (typeof val === 'object' && val !== null) {
203 ret[key] = deepCopy(val);
211 } // based on LLVM libc++ upper_bound / lower_bound
215 function upperBound(array, func) {
216 var diff, len, i, current;
224 if (func(array[current])) {
236 AssignmentExpression: 'AssignmentExpression',
237 AssignmentPattern: 'AssignmentPattern',
238 ArrayExpression: 'ArrayExpression',
239 ArrayPattern: 'ArrayPattern',
240 ArrowFunctionExpression: 'ArrowFunctionExpression',
241 AwaitExpression: 'AwaitExpression',
242 // CAUTION: It's deferred to ES7.
243 BlockStatement: 'BlockStatement',
244 BinaryExpression: 'BinaryExpression',
245 BreakStatement: 'BreakStatement',
246 CallExpression: 'CallExpression',
247 CatchClause: 'CatchClause',
248 ChainExpression: 'ChainExpression',
249 ClassBody: 'ClassBody',
250 ClassDeclaration: 'ClassDeclaration',
251 ClassExpression: 'ClassExpression',
252 ComprehensionBlock: 'ComprehensionBlock',
253 // CAUTION: It's deferred to ES7.
254 ComprehensionExpression: 'ComprehensionExpression',
255 // CAUTION: It's deferred to ES7.
256 ConditionalExpression: 'ConditionalExpression',
257 ContinueStatement: 'ContinueStatement',
258 DebuggerStatement: 'DebuggerStatement',
259 DirectiveStatement: 'DirectiveStatement',
260 DoWhileStatement: 'DoWhileStatement',
261 EmptyStatement: 'EmptyStatement',
262 ExportAllDeclaration: 'ExportAllDeclaration',
263 ExportDefaultDeclaration: 'ExportDefaultDeclaration',
264 ExportNamedDeclaration: 'ExportNamedDeclaration',
265 ExportSpecifier: 'ExportSpecifier',
266 ExpressionStatement: 'ExpressionStatement',
267 ForStatement: 'ForStatement',
268 ForInStatement: 'ForInStatement',
269 ForOfStatement: 'ForOfStatement',
270 FunctionDeclaration: 'FunctionDeclaration',
271 FunctionExpression: 'FunctionExpression',
272 GeneratorExpression: 'GeneratorExpression',
273 // CAUTION: It's deferred to ES7.
274 Identifier: 'Identifier',
275 IfStatement: 'IfStatement',
276 ImportExpression: 'ImportExpression',
277 ImportDeclaration: 'ImportDeclaration',
278 ImportDefaultSpecifier: 'ImportDefaultSpecifier',
279 ImportNamespaceSpecifier: 'ImportNamespaceSpecifier',
280 ImportSpecifier: 'ImportSpecifier',
282 LabeledStatement: 'LabeledStatement',
283 LogicalExpression: 'LogicalExpression',
284 MemberExpression: 'MemberExpression',
285 MetaProperty: 'MetaProperty',
286 MethodDefinition: 'MethodDefinition',
287 ModuleSpecifier: 'ModuleSpecifier',
288 NewExpression: 'NewExpression',
289 ObjectExpression: 'ObjectExpression',
290 ObjectPattern: 'ObjectPattern',
292 Property: 'Property',
293 RestElement: 'RestElement',
294 ReturnStatement: 'ReturnStatement',
295 SequenceExpression: 'SequenceExpression',
296 SpreadElement: 'SpreadElement',
298 SwitchStatement: 'SwitchStatement',
299 SwitchCase: 'SwitchCase',
300 TaggedTemplateExpression: 'TaggedTemplateExpression',
301 TemplateElement: 'TemplateElement',
302 TemplateLiteral: 'TemplateLiteral',
303 ThisExpression: 'ThisExpression',
304 ThrowStatement: 'ThrowStatement',
305 TryStatement: 'TryStatement',
306 UnaryExpression: 'UnaryExpression',
307 UpdateExpression: 'UpdateExpression',
308 VariableDeclaration: 'VariableDeclaration',
309 VariableDeclarator: 'VariableDeclarator',
310 WhileStatement: 'WhileStatement',
311 WithStatement: 'WithStatement',
312 YieldExpression: 'YieldExpression'
315 AssignmentExpression: ['left', 'right'],
316 AssignmentPattern: ['left', 'right'],
317 ArrayExpression: ['elements'],
318 ArrayPattern: ['elements'],
319 ArrowFunctionExpression: ['params', 'body'],
320 AwaitExpression: ['argument'],
321 // CAUTION: It's deferred to ES7.
322 BlockStatement: ['body'],
323 BinaryExpression: ['left', 'right'],
324 BreakStatement: ['label'],
325 CallExpression: ['callee', 'arguments'],
326 CatchClause: ['param', 'body'],
327 ChainExpression: ['expression'],
329 ClassDeclaration: ['id', 'superClass', 'body'],
330 ClassExpression: ['id', 'superClass', 'body'],
331 ComprehensionBlock: ['left', 'right'],
332 // CAUTION: It's deferred to ES7.
333 ComprehensionExpression: ['blocks', 'filter', 'body'],
334 // CAUTION: It's deferred to ES7.
335 ConditionalExpression: ['test', 'consequent', 'alternate'],
336 ContinueStatement: ['label'],
337 DebuggerStatement: [],
338 DirectiveStatement: [],
339 DoWhileStatement: ['body', 'test'],
341 ExportAllDeclaration: ['source'],
342 ExportDefaultDeclaration: ['declaration'],
343 ExportNamedDeclaration: ['declaration', 'specifiers', 'source'],
344 ExportSpecifier: ['exported', 'local'],
345 ExpressionStatement: ['expression'],
346 ForStatement: ['init', 'test', 'update', 'body'],
347 ForInStatement: ['left', 'right', 'body'],
348 ForOfStatement: ['left', 'right', 'body'],
349 FunctionDeclaration: ['id', 'params', 'body'],
350 FunctionExpression: ['id', 'params', 'body'],
351 GeneratorExpression: ['blocks', 'filter', 'body'],
352 // CAUTION: It's deferred to ES7.
354 IfStatement: ['test', 'consequent', 'alternate'],
355 ImportExpression: ['source'],
356 ImportDeclaration: ['specifiers', 'source'],
357 ImportDefaultSpecifier: ['local'],
358 ImportNamespaceSpecifier: ['local'],
359 ImportSpecifier: ['imported', 'local'],
361 LabeledStatement: ['label', 'body'],
362 LogicalExpression: ['left', 'right'],
363 MemberExpression: ['object', 'property'],
364 MetaProperty: ['meta', 'property'],
365 MethodDefinition: ['key', 'value'],
367 NewExpression: ['callee', 'arguments'],
368 ObjectExpression: ['properties'],
369 ObjectPattern: ['properties'],
371 Property: ['key', 'value'],
372 RestElement: ['argument'],
373 ReturnStatement: ['argument'],
374 SequenceExpression: ['expressions'],
375 SpreadElement: ['argument'],
377 SwitchStatement: ['discriminant', 'cases'],
378 SwitchCase: ['test', 'consequent'],
379 TaggedTemplateExpression: ['tag', 'quasi'],
381 TemplateLiteral: ['quasis', 'expressions'],
383 ThrowStatement: ['argument'],
384 TryStatement: ['block', 'handler', 'finalizer'],
385 UnaryExpression: ['argument'],
386 UpdateExpression: ['argument'],
387 VariableDeclaration: ['declarations'],
388 VariableDeclarator: ['id', 'init'],
389 WhileStatement: ['test', 'body'],
390 WithStatement: ['object', 'body'],
391 YieldExpression: ['argument']
403 function Reference(parent, key) {
404 this.parent = parent;
408 Reference.prototype.replace = function replace(node) {
409 this.parent[this.key] = node;
412 Reference.prototype.remove = function remove() {
413 if (Array.isArray(this.parent)) {
414 this.parent.splice(this.key, 1);
422 function Element(node, path, wrap, ref) {
429 function Controller() {} // API:
430 // return property path array from root to current node
433 Controller.prototype.path = function path() {
434 var i, iz, j, jz, result, element;
436 function addToPath(result, path) {
437 if (Array.isArray(path)) {
438 for (j = 0, jz = path.length; j < jz; ++j) {
439 result.push(path[j]);
447 if (!this.__current.path) {
449 } // first node is sentinel, second node is root element
454 for (i = 2, iz = this.__leavelist.length; i < iz; ++i) {
455 element = this.__leavelist[i];
456 addToPath(result, element.path);
459 addToPath(result, this.__current.path);
462 // return type of current node
465 Controller.prototype.type = function () {
466 var node = this.current();
467 return node.type || this.__current.wrap;
469 // return array of parent elements
472 Controller.prototype.parents = function parents() {
473 var i, iz, result; // first node is sentinel
477 for (i = 1, iz = this.__leavelist.length; i < iz; ++i) {
478 result.push(this.__leavelist[i].node);
483 // return current node
486 Controller.prototype.current = function current() {
487 return this.__current.node;
490 Controller.prototype.__execute = function __execute(callback, element) {
491 var previous, result;
493 previous = this.__current;
494 this.__current = element;
498 result = callback.call(this, element.node, this.__leavelist[this.__leavelist.length - 1].node);
501 this.__current = previous;
504 // notify control skip / break
507 Controller.prototype.notify = function notify(flag) {
510 // skip child nodes of current node
513 Controller.prototype.skip = function () {
519 Controller.prototype['break'] = function () {
525 Controller.prototype.remove = function () {
529 Controller.prototype.__initialize = function (root, visitor) {
530 this.visitor = visitor;
532 this.__worklist = [];
533 this.__leavelist = [];
534 this.__current = null;
536 this.__fallback = null;
538 if (visitor.fallback === 'iteration') {
539 this.__fallback = Object.keys;
540 } else if (typeof visitor.fallback === 'function') {
541 this.__fallback = visitor.fallback;
544 this.__keys = VisitorKeys;
547 this.__keys = Object.assign(Object.create(this.__keys), visitor.keys);
551 function isNode(node) {
556 return typeof node === 'object' && typeof node.type === 'string';
559 function isProperty(nodeType, key) {
560 return (nodeType === Syntax.ObjectExpression || nodeType === Syntax.ObjectPattern) && 'properties' === key;
563 function candidateExistsInLeaveList(leavelist, candidate) {
564 for (var i = leavelist.length - 1; i >= 0; --i) {
565 if (leavelist[i].node === candidate) {
573 Controller.prototype.traverse = function traverse(root, visitor) {
574 var worklist, leavelist, element, node, nodeType, ret, key, current, current2, candidates, candidate, sentinel;
576 this.__initialize(root, visitor);
578 sentinel = {}; // reference
580 worklist = this.__worklist;
581 leavelist = this.__leavelist; // initialize
583 worklist.push(new Element(root, null, null, null));
584 leavelist.push(new Element(null, null, null, null));
586 while (worklist.length) {
587 element = worklist.pop();
589 if (element === sentinel) {
590 element = leavelist.pop();
591 ret = this.__execute(visitor.leave, element);
593 if (this.__state === BREAK || ret === BREAK) {
601 ret = this.__execute(visitor.enter, element);
603 if (this.__state === BREAK || ret === BREAK) {
607 worklist.push(sentinel);
608 leavelist.push(element);
610 if (this.__state === SKIP || ret === SKIP) {
615 nodeType = node.type || element.wrap;
616 candidates = this.__keys[nodeType];
619 if (this.__fallback) {
620 candidates = this.__fallback(node);
622 throw new Error('Unknown node type ' + nodeType + '.');
626 current = candidates.length;
628 while ((current -= 1) >= 0) {
629 key = candidates[current];
630 candidate = node[key];
636 if (Array.isArray(candidate)) {
637 current2 = candidate.length;
639 while ((current2 -= 1) >= 0) {
640 if (!candidate[current2]) {
644 if (candidateExistsInLeaveList(leavelist, candidate[current2])) {
648 if (isProperty(nodeType, candidates[current])) {
649 element = new Element(candidate[current2], [key, current2], 'Property', null);
650 } else if (isNode(candidate[current2])) {
651 element = new Element(candidate[current2], [key, current2], null, null);
656 worklist.push(element);
658 } else if (isNode(candidate)) {
659 if (candidateExistsInLeaveList(leavelist, candidate)) {
663 worklist.push(new Element(candidate, key, null, null));
670 Controller.prototype.replace = function replace(root, visitor) {
671 var worklist, leavelist, node, nodeType, target, element, current, current2, candidates, candidate, sentinel, outer, key;
673 function removeElem(element) {
674 var i, key, nextElem, parent;
676 if (element.ref.remove()) {
677 // When the reference is an element of an array.
678 key = element.ref.key;
679 parent = element.ref.parent; // If removed from array, then decrease following items' keys.
684 nextElem = worklist[i];
686 if (nextElem.ref && nextElem.ref.parent === parent) {
687 if (nextElem.ref.key < key) {
697 this.__initialize(root, visitor);
699 sentinel = {}; // reference
701 worklist = this.__worklist;
702 leavelist = this.__leavelist; // initialize
707 element = new Element(root, null, null, new Reference(outer, 'root'));
708 worklist.push(element);
709 leavelist.push(element);
711 while (worklist.length) {
712 element = worklist.pop();
714 if (element === sentinel) {
715 element = leavelist.pop();
716 target = this.__execute(visitor.leave, element); // node may be replaced with null,
717 // so distinguish between undefined and null in this place
719 if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) {
721 element.ref.replace(target);
724 if (this.__state === REMOVE || target === REMOVE) {
728 if (this.__state === BREAK || target === BREAK) {
735 target = this.__execute(visitor.enter, element); // node may be replaced with null,
736 // so distinguish between undefined and null in this place
738 if (target !== undefined && target !== BREAK && target !== SKIP && target !== REMOVE) {
740 element.ref.replace(target);
741 element.node = target;
744 if (this.__state === REMOVE || target === REMOVE) {
749 if (this.__state === BREAK || target === BREAK) {
751 } // node may be null
760 worklist.push(sentinel);
761 leavelist.push(element);
763 if (this.__state === SKIP || target === SKIP) {
767 nodeType = node.type || element.wrap;
768 candidates = this.__keys[nodeType];
771 if (this.__fallback) {
772 candidates = this.__fallback(node);
774 throw new Error('Unknown node type ' + nodeType + '.');
778 current = candidates.length;
780 while ((current -= 1) >= 0) {
781 key = candidates[current];
782 candidate = node[key];
788 if (Array.isArray(candidate)) {
789 current2 = candidate.length;
791 while ((current2 -= 1) >= 0) {
792 if (!candidate[current2]) {
796 if (isProperty(nodeType, candidates[current])) {
797 element = new Element(candidate[current2], [key, current2], 'Property', new Reference(candidate, current2));
798 } else if (isNode(candidate[current2])) {
799 element = new Element(candidate[current2], [key, current2], null, new Reference(candidate, current2));
804 worklist.push(element);
806 } else if (isNode(candidate)) {
807 worklist.push(new Element(candidate, key, null, new Reference(node, key)));
815 function traverse(root, visitor) {
816 var controller = new Controller();
817 return controller.traverse(root, visitor);
820 function replace(root, visitor) {
821 var controller = new Controller();
822 return controller.replace(root, visitor);
825 function extendCommentRange(comment, tokens) {
827 target = upperBound(tokens, function search(token) {
828 return token.range[0] > comment.range[0];
830 comment.extendedRange = [comment.range[0], comment.range[1]];
832 if (target !== tokens.length) {
833 comment.extendedRange[1] = tokens[target].range[0];
839 comment.extendedRange[0] = tokens[target].range[1];
845 function attachComments(tree, providedComments, tokens) {
846 // At first, we should calculate extended comment ranges.
854 throw new Error('attachComments needs range information');
855 } // tokens array is empty, we attach comments to tree as 'leadingComments'
858 if (!tokens.length) {
859 if (providedComments.length) {
860 for (i = 0, len = providedComments.length; i < len; i += 1) {
861 comment = deepCopy(providedComments[i]);
862 comment.extendedRange = [0, tree.range[0]];
863 comments.push(comment);
866 tree.leadingComments = comments;
872 for (i = 0, len = providedComments.length; i < len; i += 1) {
873 comments.push(extendCommentRange(deepCopy(providedComments[i]), tokens));
874 } // This is based on John Freeman's implementation.
879 enter: function (node) {
882 while (cursor < comments.length) {
883 comment = comments[cursor];
885 if (comment.extendedRange[1] > node.range[0]) {
889 if (comment.extendedRange[1] === node.range[0]) {
890 if (!node.leadingComments) {
891 node.leadingComments = [];
894 node.leadingComments.push(comment);
895 comments.splice(cursor, 1);
899 } // already out of owned node
902 if (cursor === comments.length) {
903 return VisitorOption.Break;
906 if (comments[cursor].extendedRange[0] > node.range[1]) {
907 return VisitorOption.Skip;
913 leave: function (node) {
916 while (cursor < comments.length) {
917 comment = comments[cursor];
919 if (node.range[1] < comment.extendedRange[0]) {
923 if (node.range[1] === comment.extendedRange[0]) {
924 if (!node.trailingComments) {
925 node.trailingComments = [];
928 node.trailingComments.push(comment);
929 comments.splice(cursor, 1);
933 } // already out of owned node
936 if (cursor === comments.length) {
937 return VisitorOption.Break;
940 if (comments[cursor].extendedRange[0] > node.range[1]) {
941 return VisitorOption.Skip;
948 exports.Syntax = Syntax;
949 exports.traverse = traverse;
950 exports.replace = replace;
951 exports.attachComments = attachComments;
952 exports.VisitorKeys = VisitorKeys;
953 exports.VisitorOption = VisitorOption;
954 exports.Controller = Controller;
956 exports.cloneEnvironment = function () {
962 /* vim: set sw=4 ts=4 et tw=80 : */
966 var parser = createCommonjsModule(function (module) {
968 * Generated by PEG.js 0.10.0.
972 (function (root, factory) {
973 if ( module.exports) {
974 module.exports = factory();
976 })(commonjsGlobal, function () {
978 function peg$subclass(child, parent) {
980 this.constructor = child;
983 ctor.prototype = parent.prototype;
984 child.prototype = new ctor();
987 function peg$SyntaxError(message, expected, found, location) {
988 this.message = message;
989 this.expected = expected;
991 this.location = location;
992 this.name = "SyntaxError";
994 if (typeof Error.captureStackTrace === "function") {
995 Error.captureStackTrace(this, peg$SyntaxError);
999 peg$subclass(peg$SyntaxError, Error);
1001 peg$SyntaxError.buildMessage = function (expected, found) {
1002 var DESCRIBE_EXPECTATION_FNS = {
1003 literal: function literal(expectation) {
1004 return "\"" + literalEscape(expectation.text) + "\"";
1006 "class": function _class(expectation) {
1007 var escapedParts = "",
1010 for (i = 0; i < expectation.parts.length; i++) {
1011 escapedParts += expectation.parts[i] instanceof Array ? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1]) : classEscape(expectation.parts[i]);
1014 return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]";
1016 any: function any(expectation) {
1017 return "any character";
1019 end: function end(expectation) {
1020 return "end of input";
1022 other: function other(expectation) {
1023 return expectation.description;
1028 return ch.charCodeAt(0).toString(16).toUpperCase();
1031 function literalEscape(s) {
1032 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) {
1033 return '\\x0' + hex(ch);
1034 }).replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) {
1035 return '\\x' + hex(ch);
1039 function classEscape(s) {
1040 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) {
1041 return '\\x0' + hex(ch);
1042 }).replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) {
1043 return '\\x' + hex(ch);
1047 function describeExpectation(expectation) {
1048 return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);
1051 function describeExpected(expected) {
1052 var descriptions = new Array(expected.length),
1056 for (i = 0; i < expected.length; i++) {
1057 descriptions[i] = describeExpectation(expected[i]);
1060 descriptions.sort();
1062 if (descriptions.length > 0) {
1063 for (i = 1, j = 1; i < descriptions.length; i++) {
1064 if (descriptions[i - 1] !== descriptions[i]) {
1065 descriptions[j] = descriptions[i];
1070 descriptions.length = j;
1073 switch (descriptions.length) {
1075 return descriptions[0];
1078 return descriptions[0] + " or " + descriptions[1];
1081 return descriptions.slice(0, -1).join(", ") + ", or " + descriptions[descriptions.length - 1];
1085 function describeFound(found) {
1086 return found ? "\"" + literalEscape(found) + "\"" : "end of input";
1089 return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found.";
1092 function peg$parse(input, options) {
1093 options = options !== void 0 ? options : {};
1095 var peg$FAILED = {},
1096 peg$startRuleFunctions = {
1097 start: peg$parsestart
1099 peg$startRuleFunction = peg$parsestart,
1100 peg$c0 = function peg$c0(ss) {
1101 return ss.length === 1 ? ss[0] : {
1106 peg$c1 = function peg$c1() {
1110 peg$c3 = peg$literalExpectation(" ", false),
1111 peg$c4 = /^[^ [\],():#!=><~+.]/,
1112 peg$c5 = peg$classExpectation([" ", "[", "]", ",", "(", ")", ":", "#", "!", "=", ">", "<", "~", "+", "."], true, false),
1113 peg$c6 = function peg$c6(i) {
1117 peg$c8 = peg$literalExpectation(">", false),
1118 peg$c9 = function peg$c9() {
1122 peg$c11 = peg$literalExpectation("~", false),
1123 peg$c12 = function peg$c12() {
1127 peg$c14 = peg$literalExpectation("+", false),
1128 peg$c15 = function peg$c15() {
1131 peg$c16 = function peg$c16() {
1132 return 'descendant';
1135 peg$c18 = peg$literalExpectation(",", false),
1136 peg$c19 = function peg$c19(s, ss) {
1137 return [s].concat(ss.map(function (s) {
1141 peg$c20 = function peg$c20(a, ops) {
1142 return ops.reduce(function (memo, rhs) {
1151 peg$c22 = peg$literalExpectation("!", false),
1152 peg$c23 = function peg$c23(subject, as) {
1153 var b = as.length === 1 ? as[0] : {
1157 if (subject) b.subject = true;
1161 peg$c25 = peg$literalExpectation("*", false),
1162 peg$c26 = function peg$c26(a) {
1169 peg$c28 = peg$literalExpectation("#", false),
1170 peg$c29 = function peg$c29(i) {
1177 peg$c31 = peg$literalExpectation("[", false),
1179 peg$c33 = peg$literalExpectation("]", false),
1180 peg$c34 = function peg$c34(v) {
1184 peg$c36 = peg$classExpectation([">", "<", "!"], false, false),
1186 peg$c38 = peg$literalExpectation("=", false),
1187 peg$c39 = function peg$c39(a) {
1188 return (a || '') + '=';
1191 peg$c41 = peg$classExpectation([">", "<"], false, false),
1193 peg$c43 = peg$literalExpectation(".", false),
1194 peg$c44 = function peg$c44(a, as) {
1195 return [].concat.apply([a], as).join('');
1197 peg$c45 = function peg$c45(name, op, value) {
1205 peg$c46 = function peg$c46(name) {
1212 peg$c48 = peg$literalExpectation("\"", false),
1213 peg$c49 = /^[^\\"]/,
1214 peg$c50 = peg$classExpectation(["\\", "\""], true, false),
1216 peg$c52 = peg$literalExpectation("\\", false),
1217 peg$c53 = peg$anyExpectation(),
1218 peg$c54 = function peg$c54(a, b) {
1221 peg$c55 = function peg$c55(d) {
1224 value: strUnescape(d.join(''))
1228 peg$c57 = peg$literalExpectation("'", false),
1229 peg$c58 = /^[^\\']/,
1230 peg$c59 = peg$classExpectation(["\\", "'"], true, false),
1232 peg$c61 = peg$classExpectation([["0", "9"]], false, false),
1233 peg$c62 = function peg$c62(a, b) {
1234 // Can use `a.flat().join('')` once supported
1235 var leadingDecimals = a ? [].concat.apply([], a).join('') : '';
1238 value: parseFloat(leadingDecimals + b.join(''))
1241 peg$c63 = function peg$c63(i) {
1248 peg$c65 = peg$literalExpectation("type(", false),
1250 peg$c67 = peg$classExpectation([" ", ")"], true, false),
1252 peg$c69 = peg$literalExpectation(")", false),
1253 peg$c70 = function peg$c70(t) {
1259 peg$c71 = /^[imsu]/,
1260 peg$c72 = peg$classExpectation(["i", "m", "s", "u"], false, false),
1262 peg$c74 = peg$literalExpectation("/", false),
1264 peg$c76 = peg$classExpectation(["/"], true, false),
1265 peg$c77 = function peg$c77(d, flgs) {
1268 value: new RegExp(d.join(''), flgs ? flgs.join('') : '')
1271 peg$c78 = function peg$c78(i, is) {
1274 name: is.reduce(function (memo, p) {
1275 return memo + p[0] + p[1];
1280 peg$c80 = peg$literalExpectation(":not(", false),
1281 peg$c81 = function peg$c81(ss) {
1287 peg$c82 = ":matches(",
1288 peg$c83 = peg$literalExpectation(":matches(", false),
1289 peg$c84 = function peg$c84(ss) {
1296 peg$c86 = peg$literalExpectation(":has(", false),
1297 peg$c87 = function peg$c87(ss) {
1303 peg$c88 = ":first-child",
1304 peg$c89 = peg$literalExpectation(":first-child", false),
1305 peg$c90 = function peg$c90() {
1308 peg$c91 = ":last-child",
1309 peg$c92 = peg$literalExpectation(":last-child", false),
1310 peg$c93 = function peg$c93() {
1313 peg$c94 = ":nth-child(",
1314 peg$c95 = peg$literalExpectation(":nth-child(", false),
1315 peg$c96 = function peg$c96(n) {
1316 return nth(parseInt(n.join(''), 10));
1318 peg$c97 = ":nth-last-child(",
1319 peg$c98 = peg$literalExpectation(":nth-last-child(", false),
1320 peg$c99 = function peg$c99(n) {
1321 return nthLast(parseInt(n.join(''), 10));
1324 peg$c101 = peg$literalExpectation(":", false),
1325 peg$c102 = "statement",
1326 peg$c103 = peg$literalExpectation("statement", true),
1327 peg$c104 = "expression",
1328 peg$c105 = peg$literalExpectation("expression", true),
1329 peg$c106 = "declaration",
1330 peg$c107 = peg$literalExpectation("declaration", true),
1331 peg$c108 = "function",
1332 peg$c109 = peg$literalExpectation("function", true),
1333 peg$c110 = "pattern",
1334 peg$c111 = peg$literalExpectation("pattern", true),
1335 peg$c112 = function peg$c112(c) {
1342 peg$posDetailsCache = [{
1347 peg$maxFailExpected = [],
1348 peg$resultsCache = {},
1351 if ("startRule" in options) {
1352 if (!(options.startRule in peg$startRuleFunctions)) {
1353 throw new Error("Can't start parsing from rule \"" + options.startRule + "\".");
1356 peg$startRuleFunction = peg$startRuleFunctions[options.startRule];
1359 function peg$literalExpectation(text, ignoreCase) {
1363 ignoreCase: ignoreCase
1367 function peg$classExpectation(parts, inverted, ignoreCase) {
1372 ignoreCase: ignoreCase
1376 function peg$anyExpectation() {
1382 function peg$endExpectation() {
1388 function peg$computePosDetails(pos) {
1389 var details = peg$posDetailsCache[pos],
1397 while (!peg$posDetailsCache[p]) {
1401 details = peg$posDetailsCache[p];
1404 column: details.column
1408 if (input.charCodeAt(p) === 10) {
1418 peg$posDetailsCache[pos] = details;
1423 function peg$computeLocation(startPos, endPos) {
1424 var startPosDetails = peg$computePosDetails(startPos),
1425 endPosDetails = peg$computePosDetails(endPos);
1429 line: startPosDetails.line,
1430 column: startPosDetails.column
1434 line: endPosDetails.line,
1435 column: endPosDetails.column
1440 function peg$fail(expected) {
1441 if (peg$currPos < peg$maxFailPos) {
1445 if (peg$currPos > peg$maxFailPos) {
1446 peg$maxFailPos = peg$currPos;
1447 peg$maxFailExpected = [];
1450 peg$maxFailExpected.push(expected);
1453 function peg$buildStructuredError(expected, found, location) {
1454 return new peg$SyntaxError(peg$SyntaxError.buildMessage(expected, found), expected, found, location);
1457 function peg$parsestart() {
1459 var key = peg$currPos * 30 + 0,
1460 cached = peg$resultsCache[key];
1463 peg$currPos = cached.nextPos;
1464 return cached.result;
1470 if (s1 !== peg$FAILED) {
1471 s2 = peg$parseselectors();
1473 if (s2 !== peg$FAILED) {
1476 if (s3 !== peg$FAILED) {
1492 if (s0 === peg$FAILED) {
1496 if (s1 !== peg$FAILED) {
1503 peg$resultsCache[key] = {
1504 nextPos: peg$currPos,
1510 function peg$parse_() {
1512 var key = peg$currPos * 30 + 1,
1513 cached = peg$resultsCache[key];
1516 peg$currPos = cached.nextPos;
1517 return cached.result;
1522 if (input.charCodeAt(peg$currPos) === 32) {
1533 while (s1 !== peg$FAILED) {
1536 if (input.charCodeAt(peg$currPos) === 32) {
1548 peg$resultsCache[key] = {
1549 nextPos: peg$currPos,
1555 function peg$parseidentifierName() {
1557 var key = peg$currPos * 30 + 2,
1558 cached = peg$resultsCache[key];
1561 peg$currPos = cached.nextPos;
1562 return cached.result;
1568 if (peg$c4.test(input.charAt(peg$currPos))) {
1569 s2 = input.charAt(peg$currPos);
1579 if (s2 !== peg$FAILED) {
1580 while (s2 !== peg$FAILED) {
1583 if (peg$c4.test(input.charAt(peg$currPos))) {
1584 s2 = input.charAt(peg$currPos);
1598 if (s1 !== peg$FAILED) {
1603 peg$resultsCache[key] = {
1604 nextPos: peg$currPos,
1610 function peg$parsebinaryOp() {
1612 var key = peg$currPos * 30 + 3,
1613 cached = peg$resultsCache[key];
1616 peg$currPos = cached.nextPos;
1617 return cached.result;
1623 if (s1 !== peg$FAILED) {
1624 if (input.charCodeAt(peg$currPos) === 62) {
1635 if (s2 !== peg$FAILED) {
1638 if (s3 !== peg$FAILED) {
1654 if (s0 === peg$FAILED) {
1658 if (s1 !== peg$FAILED) {
1659 if (input.charCodeAt(peg$currPos) === 126) {
1670 if (s2 !== peg$FAILED) {
1673 if (s3 !== peg$FAILED) {
1689 if (s0 === peg$FAILED) {
1693 if (s1 !== peg$FAILED) {
1694 if (input.charCodeAt(peg$currPos) === 43) {
1705 if (s2 !== peg$FAILED) {
1708 if (s3 !== peg$FAILED) {
1724 if (s0 === peg$FAILED) {
1727 if (input.charCodeAt(peg$currPos) === 32) {
1738 if (s1 !== peg$FAILED) {
1741 if (s2 !== peg$FAILED) {
1756 peg$resultsCache[key] = {
1757 nextPos: peg$currPos,
1763 function peg$parseselectors() {
1764 var s0, s1, s2, s3, s4, s5, s6, s7;
1765 var key = peg$currPos * 30 + 4,
1766 cached = peg$resultsCache[key];
1769 peg$currPos = cached.nextPos;
1770 return cached.result;
1774 s1 = peg$parseselector();
1776 if (s1 !== peg$FAILED) {
1781 if (s4 !== peg$FAILED) {
1782 if (input.charCodeAt(peg$currPos) === 44) {
1793 if (s5 !== peg$FAILED) {
1796 if (s6 !== peg$FAILED) {
1797 s7 = peg$parseselector();
1799 if (s7 !== peg$FAILED) {
1800 s4 = [s4, s5, s6, s7];
1819 while (s3 !== peg$FAILED) {
1824 if (s4 !== peg$FAILED) {
1825 if (input.charCodeAt(peg$currPos) === 44) {
1836 if (s5 !== peg$FAILED) {
1839 if (s6 !== peg$FAILED) {
1840 s7 = peg$parseselector();
1842 if (s7 !== peg$FAILED) {
1843 s4 = [s4, s5, s6, s7];
1863 if (s2 !== peg$FAILED) {
1864 s1 = peg$c19(s1, s2);
1875 peg$resultsCache[key] = {
1876 nextPos: peg$currPos,
1882 function peg$parseselector() {
1883 var s0, s1, s2, s3, s4, s5;
1884 var key = peg$currPos * 30 + 5,
1885 cached = peg$resultsCache[key];
1888 peg$currPos = cached.nextPos;
1889 return cached.result;
1893 s1 = peg$parsesequence();
1895 if (s1 !== peg$FAILED) {
1898 s4 = peg$parsebinaryOp();
1900 if (s4 !== peg$FAILED) {
1901 s5 = peg$parsesequence();
1903 if (s5 !== peg$FAILED) {
1915 while (s3 !== peg$FAILED) {
1918 s4 = peg$parsebinaryOp();
1920 if (s4 !== peg$FAILED) {
1921 s5 = peg$parsesequence();
1923 if (s5 !== peg$FAILED) {
1936 if (s2 !== peg$FAILED) {
1937 s1 = peg$c20(s1, s2);
1948 peg$resultsCache[key] = {
1949 nextPos: peg$currPos,
1955 function peg$parsesequence() {
1957 var key = peg$currPos * 30 + 6,
1958 cached = peg$resultsCache[key];
1961 peg$currPos = cached.nextPos;
1962 return cached.result;
1967 if (input.charCodeAt(peg$currPos) === 33) {
1978 if (s1 === peg$FAILED) {
1982 if (s1 !== peg$FAILED) {
1984 s3 = peg$parseatom();
1986 if (s3 !== peg$FAILED) {
1987 while (s3 !== peg$FAILED) {
1989 s3 = peg$parseatom();
1995 if (s2 !== peg$FAILED) {
1996 s1 = peg$c23(s1, s2);
2007 peg$resultsCache[key] = {
2008 nextPos: peg$currPos,
2014 function peg$parseatom() {
2016 var key = peg$currPos * 30 + 7,
2017 cached = peg$resultsCache[key];
2020 peg$currPos = cached.nextPos;
2021 return cached.result;
2024 s0 = peg$parsewildcard();
2026 if (s0 === peg$FAILED) {
2027 s0 = peg$parseidentifier();
2029 if (s0 === peg$FAILED) {
2030 s0 = peg$parseattr();
2032 if (s0 === peg$FAILED) {
2033 s0 = peg$parsefield();
2035 if (s0 === peg$FAILED) {
2036 s0 = peg$parsenegation();
2038 if (s0 === peg$FAILED) {
2039 s0 = peg$parsematches();
2041 if (s0 === peg$FAILED) {
2042 s0 = peg$parsehas();
2044 if (s0 === peg$FAILED) {
2045 s0 = peg$parsefirstChild();
2047 if (s0 === peg$FAILED) {
2048 s0 = peg$parselastChild();
2050 if (s0 === peg$FAILED) {
2051 s0 = peg$parsenthChild();
2053 if (s0 === peg$FAILED) {
2054 s0 = peg$parsenthLastChild();
2056 if (s0 === peg$FAILED) {
2057 s0 = peg$parseclass();
2070 peg$resultsCache[key] = {
2071 nextPos: peg$currPos,
2077 function peg$parsewildcard() {
2079 var key = peg$currPos * 30 + 8,
2080 cached = peg$resultsCache[key];
2083 peg$currPos = cached.nextPos;
2084 return cached.result;
2089 if (input.charCodeAt(peg$currPos) === 42) {
2100 if (s1 !== peg$FAILED) {
2105 peg$resultsCache[key] = {
2106 nextPos: peg$currPos,
2112 function peg$parseidentifier() {
2114 var key = peg$currPos * 30 + 9,
2115 cached = peg$resultsCache[key];
2118 peg$currPos = cached.nextPos;
2119 return cached.result;
2124 if (input.charCodeAt(peg$currPos) === 35) {
2135 if (s1 === peg$FAILED) {
2139 if (s1 !== peg$FAILED) {
2140 s2 = peg$parseidentifierName();
2142 if (s2 !== peg$FAILED) {
2154 peg$resultsCache[key] = {
2155 nextPos: peg$currPos,
2161 function peg$parseattr() {
2162 var s0, s1, s2, s3, s4, s5;
2163 var key = peg$currPos * 30 + 10,
2164 cached = peg$resultsCache[key];
2167 peg$currPos = cached.nextPos;
2168 return cached.result;
2173 if (input.charCodeAt(peg$currPos) === 91) {
2184 if (s1 !== peg$FAILED) {
2187 if (s2 !== peg$FAILED) {
2188 s3 = peg$parseattrValue();
2190 if (s3 !== peg$FAILED) {
2193 if (s4 !== peg$FAILED) {
2194 if (input.charCodeAt(peg$currPos) === 93) {
2205 if (s5 !== peg$FAILED) {
2229 peg$resultsCache[key] = {
2230 nextPos: peg$currPos,
2236 function peg$parseattrOps() {
2238 var key = peg$currPos * 30 + 11,
2239 cached = peg$resultsCache[key];
2242 peg$currPos = cached.nextPos;
2243 return cached.result;
2248 if (peg$c35.test(input.charAt(peg$currPos))) {
2249 s1 = input.charAt(peg$currPos);
2259 if (s1 === peg$FAILED) {
2263 if (s1 !== peg$FAILED) {
2264 if (input.charCodeAt(peg$currPos) === 61) {
2275 if (s2 !== peg$FAILED) {
2287 if (s0 === peg$FAILED) {
2288 if (peg$c40.test(input.charAt(peg$currPos))) {
2289 s0 = input.charAt(peg$currPos);
2300 peg$resultsCache[key] = {
2301 nextPos: peg$currPos,
2307 function peg$parseattrEqOps() {
2309 var key = peg$currPos * 30 + 12,
2310 cached = peg$resultsCache[key];
2313 peg$currPos = cached.nextPos;
2314 return cached.result;
2319 if (input.charCodeAt(peg$currPos) === 33) {
2330 if (s1 === peg$FAILED) {
2334 if (s1 !== peg$FAILED) {
2335 if (input.charCodeAt(peg$currPos) === 61) {
2346 if (s2 !== peg$FAILED) {
2358 peg$resultsCache[key] = {
2359 nextPos: peg$currPos,
2365 function peg$parseattrName() {
2366 var s0, s1, s2, s3, s4, s5;
2367 var key = peg$currPos * 30 + 13,
2368 cached = peg$resultsCache[key];
2371 peg$currPos = cached.nextPos;
2372 return cached.result;
2376 s1 = peg$parseidentifierName();
2378 if (s1 !== peg$FAILED) {
2382 if (input.charCodeAt(peg$currPos) === 46) {
2393 if (s4 !== peg$FAILED) {
2394 s5 = peg$parseidentifierName();
2396 if (s5 !== peg$FAILED) {
2408 while (s3 !== peg$FAILED) {
2412 if (input.charCodeAt(peg$currPos) === 46) {
2423 if (s4 !== peg$FAILED) {
2424 s5 = peg$parseidentifierName();
2426 if (s5 !== peg$FAILED) {
2439 if (s2 !== peg$FAILED) {
2440 s1 = peg$c44(s1, s2);
2451 peg$resultsCache[key] = {
2452 nextPos: peg$currPos,
2458 function peg$parseattrValue() {
2459 var s0, s1, s2, s3, s4, s5;
2460 var key = peg$currPos * 30 + 14,
2461 cached = peg$resultsCache[key];
2464 peg$currPos = cached.nextPos;
2465 return cached.result;
2469 s1 = peg$parseattrName();
2471 if (s1 !== peg$FAILED) {
2474 if (s2 !== peg$FAILED) {
2475 s3 = peg$parseattrEqOps();
2477 if (s3 !== peg$FAILED) {
2480 if (s4 !== peg$FAILED) {
2481 s5 = peg$parsetype();
2483 if (s5 === peg$FAILED) {
2484 s5 = peg$parseregex();
2487 if (s5 !== peg$FAILED) {
2488 s1 = peg$c45(s1, s3, s5);
2511 if (s0 === peg$FAILED) {
2513 s1 = peg$parseattrName();
2515 if (s1 !== peg$FAILED) {
2518 if (s2 !== peg$FAILED) {
2519 s3 = peg$parseattrOps();
2521 if (s3 !== peg$FAILED) {
2524 if (s4 !== peg$FAILED) {
2525 s5 = peg$parsestring();
2527 if (s5 === peg$FAILED) {
2528 s5 = peg$parsenumber();
2530 if (s5 === peg$FAILED) {
2531 s5 = peg$parsepath();
2535 if (s5 !== peg$FAILED) {
2536 s1 = peg$c45(s1, s3, s5);
2559 if (s0 === peg$FAILED) {
2561 s1 = peg$parseattrName();
2563 if (s1 !== peg$FAILED) {
2571 peg$resultsCache[key] = {
2572 nextPos: peg$currPos,
2578 function peg$parsestring() {
2579 var s0, s1, s2, s3, s4, s5;
2580 var key = peg$currPos * 30 + 15,
2581 cached = peg$resultsCache[key];
2584 peg$currPos = cached.nextPos;
2585 return cached.result;
2590 if (input.charCodeAt(peg$currPos) === 34) {
2601 if (s1 !== peg$FAILED) {
2604 if (peg$c49.test(input.charAt(peg$currPos))) {
2605 s3 = input.charAt(peg$currPos);
2615 if (s3 === peg$FAILED) {
2618 if (input.charCodeAt(peg$currPos) === 92) {
2629 if (s4 !== peg$FAILED) {
2630 if (input.length > peg$currPos) {
2631 s5 = input.charAt(peg$currPos);
2641 if (s5 !== peg$FAILED) {
2642 s4 = peg$c54(s4, s5);
2654 while (s3 !== peg$FAILED) {
2657 if (peg$c49.test(input.charAt(peg$currPos))) {
2658 s3 = input.charAt(peg$currPos);
2668 if (s3 === peg$FAILED) {
2671 if (input.charCodeAt(peg$currPos) === 92) {
2682 if (s4 !== peg$FAILED) {
2683 if (input.length > peg$currPos) {
2684 s5 = input.charAt(peg$currPos);
2694 if (s5 !== peg$FAILED) {
2695 s4 = peg$c54(s4, s5);
2708 if (s2 !== peg$FAILED) {
2709 if (input.charCodeAt(peg$currPos) === 34) {
2720 if (s3 !== peg$FAILED) {
2736 if (s0 === peg$FAILED) {
2739 if (input.charCodeAt(peg$currPos) === 39) {
2750 if (s1 !== peg$FAILED) {
2753 if (peg$c58.test(input.charAt(peg$currPos))) {
2754 s3 = input.charAt(peg$currPos);
2764 if (s3 === peg$FAILED) {
2767 if (input.charCodeAt(peg$currPos) === 92) {
2778 if (s4 !== peg$FAILED) {
2779 if (input.length > peg$currPos) {
2780 s5 = input.charAt(peg$currPos);
2790 if (s5 !== peg$FAILED) {
2791 s4 = peg$c54(s4, s5);
2803 while (s3 !== peg$FAILED) {
2806 if (peg$c58.test(input.charAt(peg$currPos))) {
2807 s3 = input.charAt(peg$currPos);
2817 if (s3 === peg$FAILED) {
2820 if (input.charCodeAt(peg$currPos) === 92) {
2831 if (s4 !== peg$FAILED) {
2832 if (input.length > peg$currPos) {
2833 s5 = input.charAt(peg$currPos);
2843 if (s5 !== peg$FAILED) {
2844 s4 = peg$c54(s4, s5);
2857 if (s2 !== peg$FAILED) {
2858 if (input.charCodeAt(peg$currPos) === 39) {
2869 if (s3 !== peg$FAILED) {
2886 peg$resultsCache[key] = {
2887 nextPos: peg$currPos,
2893 function peg$parsenumber() {
2895 var key = peg$currPos * 30 + 16,
2896 cached = peg$resultsCache[key];
2899 peg$currPos = cached.nextPos;
2900 return cached.result;
2907 if (peg$c60.test(input.charAt(peg$currPos))) {
2908 s3 = input.charAt(peg$currPos);
2918 while (s3 !== peg$FAILED) {
2921 if (peg$c60.test(input.charAt(peg$currPos))) {
2922 s3 = input.charAt(peg$currPos);
2933 if (s2 !== peg$FAILED) {
2934 if (input.charCodeAt(peg$currPos) === 46) {
2945 if (s3 !== peg$FAILED) {
2957 if (s1 === peg$FAILED) {
2961 if (s1 !== peg$FAILED) {
2964 if (peg$c60.test(input.charAt(peg$currPos))) {
2965 s3 = input.charAt(peg$currPos);
2975 if (s3 !== peg$FAILED) {
2976 while (s3 !== peg$FAILED) {
2979 if (peg$c60.test(input.charAt(peg$currPos))) {
2980 s3 = input.charAt(peg$currPos);
2994 if (s2 !== peg$FAILED) {
2995 s1 = peg$c62(s1, s2);
3006 peg$resultsCache[key] = {
3007 nextPos: peg$currPos,
3013 function peg$parsepath() {
3015 var key = peg$currPos * 30 + 17,
3016 cached = peg$resultsCache[key];
3019 peg$currPos = cached.nextPos;
3020 return cached.result;
3024 s1 = peg$parseidentifierName();
3026 if (s1 !== peg$FAILED) {
3031 peg$resultsCache[key] = {
3032 nextPos: peg$currPos,
3038 function peg$parsetype() {
3039 var s0, s1, s2, s3, s4, s5;
3040 var key = peg$currPos * 30 + 18,
3041 cached = peg$resultsCache[key];
3044 peg$currPos = cached.nextPos;
3045 return cached.result;
3050 if (input.substr(peg$currPos, 5) === peg$c64) {
3061 if (s1 !== peg$FAILED) {
3064 if (s2 !== peg$FAILED) {
3067 if (peg$c66.test(input.charAt(peg$currPos))) {
3068 s4 = input.charAt(peg$currPos);
3078 if (s4 !== peg$FAILED) {
3079 while (s4 !== peg$FAILED) {
3082 if (peg$c66.test(input.charAt(peg$currPos))) {
3083 s4 = input.charAt(peg$currPos);
3097 if (s3 !== peg$FAILED) {
3100 if (s4 !== peg$FAILED) {
3101 if (input.charCodeAt(peg$currPos) === 41) {
3112 if (s5 !== peg$FAILED) {
3136 peg$resultsCache[key] = {
3137 nextPos: peg$currPos,
3143 function peg$parseflags() {
3145 var key = peg$currPos * 30 + 19,
3146 cached = peg$resultsCache[key];
3149 peg$currPos = cached.nextPos;
3150 return cached.result;
3155 if (peg$c71.test(input.charAt(peg$currPos))) {
3156 s1 = input.charAt(peg$currPos);
3166 if (s1 !== peg$FAILED) {
3167 while (s1 !== peg$FAILED) {
3170 if (peg$c71.test(input.charAt(peg$currPos))) {
3171 s1 = input.charAt(peg$currPos);
3185 peg$resultsCache[key] = {
3186 nextPos: peg$currPos,
3192 function peg$parseregex() {
3193 var s0, s1, s2, s3, s4;
3194 var key = peg$currPos * 30 + 20,
3195 cached = peg$resultsCache[key];
3198 peg$currPos = cached.nextPos;
3199 return cached.result;
3204 if (input.charCodeAt(peg$currPos) === 47) {
3215 if (s1 !== peg$FAILED) {
3218 if (peg$c75.test(input.charAt(peg$currPos))) {
3219 s3 = input.charAt(peg$currPos);
3229 if (s3 !== peg$FAILED) {
3230 while (s3 !== peg$FAILED) {
3233 if (peg$c75.test(input.charAt(peg$currPos))) {
3234 s3 = input.charAt(peg$currPos);
3248 if (s2 !== peg$FAILED) {
3249 if (input.charCodeAt(peg$currPos) === 47) {
3260 if (s3 !== peg$FAILED) {
3261 s4 = peg$parseflags();
3263 if (s4 === peg$FAILED) {
3267 if (s4 !== peg$FAILED) {
3268 s1 = peg$c77(s2, s4);
3287 peg$resultsCache[key] = {
3288 nextPos: peg$currPos,
3294 function peg$parsefield() {
3295 var s0, s1, s2, s3, s4, s5, s6;
3296 var key = peg$currPos * 30 + 21,
3297 cached = peg$resultsCache[key];
3300 peg$currPos = cached.nextPos;
3301 return cached.result;
3306 if (input.charCodeAt(peg$currPos) === 46) {
3317 if (s1 !== peg$FAILED) {
3318 s2 = peg$parseidentifierName();
3320 if (s2 !== peg$FAILED) {
3324 if (input.charCodeAt(peg$currPos) === 46) {
3335 if (s5 !== peg$FAILED) {
3336 s6 = peg$parseidentifierName();
3338 if (s6 !== peg$FAILED) {
3350 while (s4 !== peg$FAILED) {
3354 if (input.charCodeAt(peg$currPos) === 46) {
3365 if (s5 !== peg$FAILED) {
3366 s6 = peg$parseidentifierName();
3368 if (s6 !== peg$FAILED) {
3381 if (s3 !== peg$FAILED) {
3382 s1 = peg$c78(s2, s3);
3397 peg$resultsCache[key] = {
3398 nextPos: peg$currPos,
3404 function peg$parsenegation() {
3405 var s0, s1, s2, s3, s4, s5;
3406 var key = peg$currPos * 30 + 22,
3407 cached = peg$resultsCache[key];
3410 peg$currPos = cached.nextPos;
3411 return cached.result;
3416 if (input.substr(peg$currPos, 5) === peg$c79) {
3427 if (s1 !== peg$FAILED) {
3430 if (s2 !== peg$FAILED) {
3431 s3 = peg$parseselectors();
3433 if (s3 !== peg$FAILED) {
3436 if (s4 !== peg$FAILED) {
3437 if (input.charCodeAt(peg$currPos) === 41) {
3448 if (s5 !== peg$FAILED) {
3472 peg$resultsCache[key] = {
3473 nextPos: peg$currPos,
3479 function peg$parsematches() {
3480 var s0, s1, s2, s3, s4, s5;
3481 var key = peg$currPos * 30 + 23,
3482 cached = peg$resultsCache[key];
3485 peg$currPos = cached.nextPos;
3486 return cached.result;
3491 if (input.substr(peg$currPos, 9) === peg$c82) {
3502 if (s1 !== peg$FAILED) {
3505 if (s2 !== peg$FAILED) {
3506 s3 = peg$parseselectors();
3508 if (s3 !== peg$FAILED) {
3511 if (s4 !== peg$FAILED) {
3512 if (input.charCodeAt(peg$currPos) === 41) {
3523 if (s5 !== peg$FAILED) {
3547 peg$resultsCache[key] = {
3548 nextPos: peg$currPos,
3554 function peg$parsehas() {
3555 var s0, s1, s2, s3, s4, s5;
3556 var key = peg$currPos * 30 + 24,
3557 cached = peg$resultsCache[key];
3560 peg$currPos = cached.nextPos;
3561 return cached.result;
3566 if (input.substr(peg$currPos, 5) === peg$c85) {
3577 if (s1 !== peg$FAILED) {
3580 if (s2 !== peg$FAILED) {
3581 s3 = peg$parseselectors();
3583 if (s3 !== peg$FAILED) {
3586 if (s4 !== peg$FAILED) {
3587 if (input.charCodeAt(peg$currPos) === 41) {
3598 if (s5 !== peg$FAILED) {
3622 peg$resultsCache[key] = {
3623 nextPos: peg$currPos,
3629 function peg$parsefirstChild() {
3631 var key = peg$currPos * 30 + 25,
3632 cached = peg$resultsCache[key];
3635 peg$currPos = cached.nextPos;
3636 return cached.result;
3641 if (input.substr(peg$currPos, 12) === peg$c88) {
3652 if (s1 !== peg$FAILED) {
3657 peg$resultsCache[key] = {
3658 nextPos: peg$currPos,
3664 function peg$parselastChild() {
3666 var key = peg$currPos * 30 + 26,
3667 cached = peg$resultsCache[key];
3670 peg$currPos = cached.nextPos;
3671 return cached.result;
3676 if (input.substr(peg$currPos, 11) === peg$c91) {
3687 if (s1 !== peg$FAILED) {
3692 peg$resultsCache[key] = {
3693 nextPos: peg$currPos,
3699 function peg$parsenthChild() {
3700 var s0, s1, s2, s3, s4, s5;
3701 var key = peg$currPos * 30 + 27,
3702 cached = peg$resultsCache[key];
3705 peg$currPos = cached.nextPos;
3706 return cached.result;
3711 if (input.substr(peg$currPos, 11) === peg$c94) {
3722 if (s1 !== peg$FAILED) {
3725 if (s2 !== peg$FAILED) {
3728 if (peg$c60.test(input.charAt(peg$currPos))) {
3729 s4 = input.charAt(peg$currPos);
3739 if (s4 !== peg$FAILED) {
3740 while (s4 !== peg$FAILED) {
3743 if (peg$c60.test(input.charAt(peg$currPos))) {
3744 s4 = input.charAt(peg$currPos);
3758 if (s3 !== peg$FAILED) {
3761 if (s4 !== peg$FAILED) {
3762 if (input.charCodeAt(peg$currPos) === 41) {
3773 if (s5 !== peg$FAILED) {
3797 peg$resultsCache[key] = {
3798 nextPos: peg$currPos,
3804 function peg$parsenthLastChild() {
3805 var s0, s1, s2, s3, s4, s5;
3806 var key = peg$currPos * 30 + 28,
3807 cached = peg$resultsCache[key];
3810 peg$currPos = cached.nextPos;
3811 return cached.result;
3816 if (input.substr(peg$currPos, 16) === peg$c97) {
3827 if (s1 !== peg$FAILED) {
3830 if (s2 !== peg$FAILED) {
3833 if (peg$c60.test(input.charAt(peg$currPos))) {
3834 s4 = input.charAt(peg$currPos);
3844 if (s4 !== peg$FAILED) {
3845 while (s4 !== peg$FAILED) {
3848 if (peg$c60.test(input.charAt(peg$currPos))) {
3849 s4 = input.charAt(peg$currPos);
3863 if (s3 !== peg$FAILED) {
3866 if (s4 !== peg$FAILED) {
3867 if (input.charCodeAt(peg$currPos) === 41) {
3878 if (s5 !== peg$FAILED) {
3902 peg$resultsCache[key] = {
3903 nextPos: peg$currPos,
3909 function peg$parseclass() {
3911 var key = peg$currPos * 30 + 29,
3912 cached = peg$resultsCache[key];
3915 peg$currPos = cached.nextPos;
3916 return cached.result;
3921 if (input.charCodeAt(peg$currPos) === 58) {
3932 if (s1 !== peg$FAILED) {
3933 if (input.substr(peg$currPos, 9).toLowerCase() === peg$c102) {
3934 s2 = input.substr(peg$currPos, 9);
3944 if (s2 === peg$FAILED) {
3945 if (input.substr(peg$currPos, 10).toLowerCase() === peg$c104) {
3946 s2 = input.substr(peg$currPos, 10);
3956 if (s2 === peg$FAILED) {
3957 if (input.substr(peg$currPos, 11).toLowerCase() === peg$c106) {
3958 s2 = input.substr(peg$currPos, 11);
3968 if (s2 === peg$FAILED) {
3969 if (input.substr(peg$currPos, 8).toLowerCase() === peg$c108) {
3970 s2 = input.substr(peg$currPos, 8);
3980 if (s2 === peg$FAILED) {
3981 if (input.substr(peg$currPos, 7).toLowerCase() === peg$c110) {
3982 s2 = input.substr(peg$currPos, 7);
3996 if (s2 !== peg$FAILED) {
4008 peg$resultsCache[key] = {
4009 nextPos: peg$currPos,
4025 function nthLast(n) {
4027 type: 'nth-last-child',
4035 function strUnescape(s) {
4036 return s.replace(/\\(.)/g, function (match, ch) {
4062 peg$result = peg$startRuleFunction();
4064 if (peg$result !== peg$FAILED && peg$currPos === input.length) {
4067 if (peg$result !== peg$FAILED && peg$currPos < input.length) {
4068 peg$fail(peg$endExpectation());
4071 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));
4076 SyntaxError: peg$SyntaxError,
4082 function _objectEntries(obj) {
4084 var keys = Object.keys(obj);
4086 for (var k = 0; k < keys.length; k++) entries.push([keys[k], obj[keys[k]]]);
4091 * @typedef {"LEFT_SIDE"|"RIGHT_SIDE"} Side
4094 var LEFT_SIDE = 'LEFT_SIDE';
4095 var RIGHT_SIDE = 'RIGHT_SIDE';
4098 * @see https://esprima.readthedocs.io/en/latest/syntax-tree-format.html
4102 * One of the rules of `grammar.pegjs`
4103 * @typedef {PlainObject} SelectorAST
4104 * @see grammar.pegjs
4108 * The `sequence` production of `grammar.pegjs`
4109 * @typedef {PlainObject} SelectorSequenceAST
4113 * Get the value of a property which may be multiple levels down
4115 * @param {?PlainObject} obj
4116 * @param {string} key
4117 * @returns {undefined|boolean|string|number|external:AST}
4120 function getPath(obj, key) {
4121 var keys = key.split('.');
4123 var _iterator = _createForOfIteratorHelper(keys),
4127 for (_iterator.s(); !(_step = _iterator.n()).done;) {
4128 var _key = _step.value;
4145 * Determine whether `node` can be reached by following `path`,
4146 * starting at `ancestor`.
4147 * @param {?external:AST} node
4148 * @param {?external:AST} ancestor
4149 * @param {string[]} path
4150 * @returns {boolean}
4154 function inPath(node, ancestor, path) {
4155 if (path.length === 0) {
4156 return node === ancestor;
4159 if (ancestor == null) {
4163 var field = ancestor[path[0]];
4164 var remainingPath = path.slice(1);
4166 if (Array.isArray(field)) {
4167 var _iterator2 = _createForOfIteratorHelper(field),
4171 for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
4172 var component = _step2.value;
4174 if (inPath(node, component, remainingPath)) {
4186 return inPath(node, field, remainingPath);
4190 * @callback TraverseOptionFallback
4191 * @param {external:AST} node The given node.
4192 * @returns {string[]} An array of visitor keys for the given node.
4196 * @typedef {object} ESQueryOptions
4197 * @property { { [nodeType: string]: string[] } } [visitorKeys] By passing `visitorKeys` mapping, we can extend the properties of the nodes that traverse the node.
4198 * @property {TraverseOptionFallback} [fallback] By passing `fallback` option, we can control the properties of traversing nodes when encountering unknown nodes.
4202 * Given a `node` and its ancestors, determine if `node` is matched
4204 * @param {?external:AST} node
4205 * @param {?SelectorAST} selector
4206 * @param {external:AST[]} [ancestry=[]]
4207 * @param {ESQueryOptions} [options]
4208 * @throws {Error} Unknowns (operator, class name, selector type, or
4209 * selector value type)
4210 * @returns {boolean}
4214 function matches(node, selector, ancestry, options) {
4227 switch (selector.type) {
4232 return selector.value.toLowerCase() === node.type.toLowerCase();
4236 var path = selector.name.split('.');
4237 var ancestor = ancestry[path.length - 1];
4238 return inPath(node, ancestor, path);
4242 var _iterator3 = _createForOfIteratorHelper(selector.selectors),
4246 for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
4247 var sel = _step3.value;
4249 if (matches(node, sel, ancestry, options)) {
4262 var _iterator4 = _createForOfIteratorHelper(selector.selectors),
4266 for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
4267 var _sel = _step4.value;
4269 if (!matches(node, _sel, ancestry, options)) {
4282 var _iterator5 = _createForOfIteratorHelper(selector.selectors),
4286 for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
4287 var _sel2 = _step5.value;
4289 if (matches(node, _sel2, ancestry, options)) {
4303 var _ret = function () {
4306 var _iterator6 = _createForOfIteratorHelper(selector.selectors),
4310 var _loop = function _loop() {
4311 var sel = _step6.value;
4313 estraverse.traverse(node, {
4314 enter: function enter(node, parent) {
4315 if (parent != null) {
4319 if (matches(node, sel, a, options)) {
4320 collector.push(node);
4323 leave: function leave() {
4326 keys: options && options.visitorKeys,
4327 fallback: options && options.fallback || 'iteration'
4331 for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
4341 v: collector.length !== 0
4345 if (_typeof(_ret) === "object") return _ret.v;
4349 if (matches(node, selector.right, ancestry, options)) {
4350 return matches(ancestry[0], selector.left, ancestry.slice(1), options);
4356 if (matches(node, selector.right, ancestry, options)) {
4357 for (var i = 0, l = ancestry.length; i < l; ++i) {
4358 if (matches(ancestry[i], selector.left, ancestry.slice(i + 1), options)) {
4368 var p = getPath(node, selector.name);
4370 switch (selector.operator) {
4375 switch (selector.value.type) {
4377 return typeof p === 'string' && selector.value.value.test(p);
4380 return "".concat(selector.value.value) === "".concat(p);
4383 return selector.value.value === _typeof(p);
4386 throw new Error("Unknown selector value type: ".concat(selector.value.type));
4389 switch (selector.value.type) {
4391 return !selector.value.value.test(p);
4394 return "".concat(selector.value.value) !== "".concat(p);
4397 return selector.value.value !== _typeof(p);
4400 throw new Error("Unknown selector value type: ".concat(selector.value.type));
4403 return p <= selector.value.value;
4406 return p < selector.value.value;
4409 return p > selector.value.value;
4412 return p >= selector.value.value;
4415 throw new Error("Unknown operator: ".concat(selector.operator));
4419 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);
4422 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);
4425 return matches(node, selector.right, ancestry, options) && nthChild(node, ancestry, function () {
4426 return selector.index.value - 1;
4429 case 'nth-last-child':
4430 return matches(node, selector.right, ancestry, options) && nthChild(node, ancestry, function (length) {
4431 return length - selector.index.value;
4435 switch (selector.name.toLowerCase()) {
4437 if (node.type.slice(-9) === 'Statement') return true;
4438 // fallthrough: interface Declaration <: Statement { }
4441 return node.type.slice(-11) === 'Declaration';
4444 if (node.type.slice(-7) === 'Pattern') return true;
4445 // fallthrough: interface Expression <: Node, Pattern { }
4448 return node.type.slice(-10) === 'Expression' || node.type.slice(-7) === 'Literal' || node.type === 'Identifier' && (ancestry.length === 0 || ancestry[0].type !== 'MetaProperty') || node.type === 'MetaProperty';
4451 return node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression';
4454 throw new Error("Unknown class name: ".concat(selector.name));
4457 throw new Error("Unknown selector type: ".concat(selector.type));
4460 * Get visitor keys of a given node.
4461 * @param {external:AST} node The AST node to get keys.
4462 * @param {ESQueryOptions|undefined} options
4463 * @returns {string[]} Visitor keys of the node.
4467 function getVisitorKeys(node, options) {
4468 var nodeType = node.type;
4470 if (options && options.visitorKeys && options.visitorKeys[nodeType]) {
4471 return options.visitorKeys[nodeType];
4474 if (estraverse.VisitorKeys[nodeType]) {
4475 return estraverse.VisitorKeys[nodeType];
4478 if (options && typeof options.fallback === 'function') {
4479 return options.fallback(node);
4480 } // 'iteration' fallback
4483 return Object.keys(node).filter(function (key) {
4484 return key !== 'type';
4488 * Check whether the given value is an ASTNode or not.
4489 * @param {any} node The value to check.
4490 * @returns {boolean} `true` if the value is an ASTNode.
4494 function isNode(node) {
4495 return node !== null && _typeof(node) === 'object' && typeof node.type === 'string';
4498 * Determines if the given node has a sibling that matches the
4500 * @param {external:AST} node
4501 * @param {SelectorSequenceAST} selector
4502 * @param {external:AST[]} ancestry
4503 * @param {Side} side
4504 * @param {ESQueryOptions|undefined} options
4505 * @returns {boolean}
4509 function sibling(node, selector, ancestry, side, options) {
4510 var _ancestry = _slicedToArray(ancestry, 1),
4511 parent = _ancestry[0];
4517 var keys = getVisitorKeys(parent, options);
4519 var _iterator7 = _createForOfIteratorHelper(keys),
4523 for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
4524 var key = _step7.value;
4525 var listProp = parent[key];
4527 if (Array.isArray(listProp)) {
4528 var startIndex = listProp.indexOf(node);
4530 if (startIndex < 0) {
4534 var lowerBound = void 0,
4535 upperBound = void 0;
4537 if (side === LEFT_SIDE) {
4539 upperBound = startIndex;
4541 lowerBound = startIndex + 1;
4542 upperBound = listProp.length;
4545 for (var k = lowerBound; k < upperBound; ++k) {
4546 if (isNode(listProp[k]) && matches(listProp[k], selector, ancestry, options)) {
4561 * Determines if the given node has an adjacent sibling that matches
4562 * the given selector.
4563 * @param {external:AST} node
4564 * @param {SelectorSequenceAST} selector
4565 * @param {external:AST[]} ancestry
4566 * @param {Side} side
4567 * @param {ESQueryOptions|undefined} options
4568 * @returns {boolean}
4572 function adjacent(node, selector, ancestry, side, options) {
4573 var _ancestry2 = _slicedToArray(ancestry, 1),
4574 parent = _ancestry2[0];
4580 var keys = getVisitorKeys(parent, options);
4582 var _iterator8 = _createForOfIteratorHelper(keys),
4586 for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
4587 var key = _step8.value;
4588 var listProp = parent[key];
4590 if (Array.isArray(listProp)) {
4591 var idx = listProp.indexOf(node);
4597 if (side === LEFT_SIDE && idx > 0 && isNode(listProp[idx - 1]) && matches(listProp[idx - 1], selector, ancestry, options)) {
4601 if (side === RIGHT_SIDE && idx < listProp.length - 1 && isNode(listProp[idx + 1]) && matches(listProp[idx + 1], selector, ancestry, options)) {
4615 * @callback IndexFunction
4616 * @param {Integer} len Containing list's length
4617 * @returns {Integer}
4621 * Determines if the given node is the nth child, determined by
4622 * `idxFn`, which is given the containing list's length.
4623 * @param {external:AST} node
4624 * @param {external:AST[]} ancestry
4625 * @param {IndexFunction} idxFn
4626 * @param {ESQueryOptions|undefined} options
4627 * @returns {boolean}
4631 function nthChild(node, ancestry, idxFn, options) {
4632 var _ancestry3 = _slicedToArray(ancestry, 1),
4633 parent = _ancestry3[0];
4639 var keys = getVisitorKeys(parent, options);
4641 var _iterator9 = _createForOfIteratorHelper(keys),
4645 for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
4646 var key = _step9.value;
4647 var listProp = parent[key];
4649 if (Array.isArray(listProp)) {
4650 var idx = listProp.indexOf(node);
4652 if (idx >= 0 && idx === idxFn(listProp.length)) {
4666 * For each selector node marked as a subject, find the portion of the
4667 * selector that the subject must match.
4668 * @param {SelectorAST} selector
4669 * @param {SelectorAST} [ancestor] Defaults to `selector`
4670 * @returns {SelectorAST[]}
4674 function subjects(selector, ancestor) {
4675 if (selector == null || _typeof(selector) != 'object') {
4679 if (ancestor == null) {
4680 ancestor = selector;
4683 var results = selector.subject ? [ancestor] : [];
4685 for (var _i = 0, _Object$entries = _objectEntries(selector); _i < _Object$entries.length; _i++) {
4686 var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
4687 p = _Object$entries$_i[0],
4688 sel = _Object$entries$_i[1];
4690 results.push.apply(results, _toConsumableArray(subjects(sel, p === 'left' ? sel : ancestor)));
4696 * @callback TraverseVisitor
4697 * @param {?external:AST} node
4698 * @param {?external:AST} parent
4699 * @param {external:AST[]} ancestry
4703 * From a JS AST and a selector AST, collect all JS AST nodes that
4704 * match the selector.
4705 * @param {external:AST} ast
4706 * @param {?SelectorAST} selector
4707 * @param {TraverseVisitor} visitor
4708 * @param {ESQueryOptions} [options]
4709 * @returns {external:AST[]}
4713 function traverse(ast, selector, visitor, options) {
4719 var altSubjects = subjects(selector);
4720 estraverse.traverse(ast, {
4721 enter: function enter(node, parent) {
4722 if (parent != null) {
4723 ancestry.unshift(parent);
4726 if (matches(node, selector, ancestry, options)) {
4727 if (altSubjects.length) {
4728 for (var i = 0, l = altSubjects.length; i < l; ++i) {
4729 if (matches(node, altSubjects[i], ancestry, options)) {
4730 visitor(node, parent, ancestry);
4733 for (var k = 0, m = ancestry.length; k < m; ++k) {
4734 var succeedingAncestry = ancestry.slice(k + 1);
4736 if (matches(ancestry[k], altSubjects[i], succeedingAncestry, options)) {
4737 visitor(ancestry[k], parent, succeedingAncestry);
4742 visitor(node, parent, ancestry);
4746 leave: function leave() {
4749 keys: options && options.visitorKeys,
4750 fallback: options && options.fallback || 'iteration'
4754 * From a JS AST and a selector AST, collect all JS AST nodes that
4755 * match the selector.
4756 * @param {external:AST} ast
4757 * @param {?SelectorAST} selector
4758 * @param {ESQueryOptions} [options]
4759 * @returns {external:AST[]}
4763 function match(ast, selector, options) {
4765 traverse(ast, selector, function (node) {
4771 * Parse a selector string and return its AST.
4772 * @param {string} selector
4773 * @returns {SelectorAST}
4777 function parse(selector) {
4778 return parser.parse(selector);
4781 * Query the code AST using the selector string.
4782 * @param {external:AST} ast
4783 * @param {string} selector
4784 * @param {ESQueryOptions} [options]
4785 * @returns {external:AST[]}
4789 function query(ast, selector, options) {
4790 return match(ast, parse(selector), options);
4793 query.parse = parse;
4794 query.match = match;
4795 query.traverse = traverse;
4796 query.matches = matches;
4797 query.query = query;