1 (function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory(require('estraverse')) :
3 typeof define === 'function' && define.amd ? define(['estraverse'], factory) :
4 (global = global || self, global.esquery = factory(global.estraverse));
5 }(this, (function (estraverse) { 'use strict';
7 estraverse = estraverse && Object.prototype.hasOwnProperty.call(estraverse, 'default') ? estraverse['default'] : estraverse;
9 function _typeof(obj) {
10 "@babel/helpers - typeof";
12 if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") {
13 _typeof = function (obj) {
17 _typeof = function (obj) {
18 return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj;
25 function _slicedToArray(arr, i) {
26 return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest();
29 function _toConsumableArray(arr) {
30 return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
33 function _arrayWithoutHoles(arr) {
34 if (Array.isArray(arr)) return _arrayLikeToArray(arr);
37 function _arrayWithHoles(arr) {
38 if (Array.isArray(arr)) return arr;
41 function _iterableToArray(iter) {
42 if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
45 function _iterableToArrayLimit(arr, i) {
46 if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return;
53 for (var _i = arr[Symbol.iterator](), _s; !(_n = (_s = _i.next()).done); _n = true) {
56 if (i && _arr.length === i) break;
63 if (!_n && _i["return"] != null) _i["return"]();
72 function _unsupportedIterableToArray(o, minLen) {
74 if (typeof o === "string") return _arrayLikeToArray(o, minLen);
75 var n = Object.prototype.toString.call(o).slice(8, -1);
76 if (n === "Object" && o.constructor) n = o.constructor.name;
77 if (n === "Map" || n === "Set") return Array.from(o);
78 if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
81 function _arrayLikeToArray(arr, len) {
82 if (len == null || len > arr.length) len = arr.length;
84 for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
89 function _nonIterableSpread() {
90 throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
93 function _nonIterableRest() {
94 throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
97 function _createForOfIteratorHelper(o, allowArrayLike) {
100 if (typeof Symbol === "undefined" || o[Symbol.iterator] == null) {
101 if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") {
105 var F = function () {};
110 if (i >= o.length) return {
125 throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
128 var normalCompletion = true,
133 it = o[Symbol.iterator]();
136 var step = it.next();
137 normalCompletion = step.done;
146 if (!normalCompletion && it.return != null) it.return();
148 if (didErr) throw err;
154 var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
156 function createCommonjsModule(fn, module) {
157 return module = { exports: {} }, fn(module, module.exports), module.exports;
160 var parser = createCommonjsModule(function (module) {
162 * Generated by PEG.js 0.10.0.
166 (function (root, factory) {
167 if ( module.exports) {
168 module.exports = factory();
170 })(commonjsGlobal, function () {
172 function peg$subclass(child, parent) {
174 this.constructor = child;
177 ctor.prototype = parent.prototype;
178 child.prototype = new ctor();
181 function peg$SyntaxError(message, expected, found, location) {
182 this.message = message;
183 this.expected = expected;
185 this.location = location;
186 this.name = "SyntaxError";
188 if (typeof Error.captureStackTrace === "function") {
189 Error.captureStackTrace(this, peg$SyntaxError);
193 peg$subclass(peg$SyntaxError, Error);
195 peg$SyntaxError.buildMessage = function (expected, found) {
196 var DESCRIBE_EXPECTATION_FNS = {
197 literal: function literal(expectation) {
198 return "\"" + literalEscape(expectation.text) + "\"";
200 "class": function _class(expectation) {
201 var escapedParts = "",
204 for (i = 0; i < expectation.parts.length; i++) {
205 escapedParts += expectation.parts[i] instanceof Array ? classEscape(expectation.parts[i][0]) + "-" + classEscape(expectation.parts[i][1]) : classEscape(expectation.parts[i]);
208 return "[" + (expectation.inverted ? "^" : "") + escapedParts + "]";
210 any: function any(expectation) {
211 return "any character";
213 end: function end(expectation) {
214 return "end of input";
216 other: function other(expectation) {
217 return expectation.description;
222 return ch.charCodeAt(0).toString(16).toUpperCase();
225 function literalEscape(s) {
226 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) {
227 return '\\x0' + hex(ch);
228 }).replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) {
229 return '\\x' + hex(ch);
233 function classEscape(s) {
234 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) {
235 return '\\x0' + hex(ch);
236 }).replace(/[\x10-\x1F\x7F-\x9F]/g, function (ch) {
237 return '\\x' + hex(ch);
241 function describeExpectation(expectation) {
242 return DESCRIBE_EXPECTATION_FNS[expectation.type](expectation);
245 function describeExpected(expected) {
246 var descriptions = new Array(expected.length),
250 for (i = 0; i < expected.length; i++) {
251 descriptions[i] = describeExpectation(expected[i]);
256 if (descriptions.length > 0) {
257 for (i = 1, j = 1; i < descriptions.length; i++) {
258 if (descriptions[i - 1] !== descriptions[i]) {
259 descriptions[j] = descriptions[i];
264 descriptions.length = j;
267 switch (descriptions.length) {
269 return descriptions[0];
272 return descriptions[0] + " or " + descriptions[1];
275 return descriptions.slice(0, -1).join(", ") + ", or " + descriptions[descriptions.length - 1];
279 function describeFound(found) {
280 return found ? "\"" + literalEscape(found) + "\"" : "end of input";
283 return "Expected " + describeExpected(expected) + " but " + describeFound(found) + " found.";
286 function peg$parse(input, options) {
287 options = options !== void 0 ? options : {};
290 peg$startRuleFunctions = {
291 start: peg$parsestart
293 peg$startRuleFunction = peg$parsestart,
294 peg$c0 = function peg$c0(ss) {
295 return ss.length === 1 ? ss[0] : {
300 peg$c1 = function peg$c1() {
304 peg$c3 = peg$literalExpectation(" ", false),
305 peg$c4 = /^[^ [\],():#!=><~+.]/,
306 peg$c5 = peg$classExpectation([" ", "[", "]", ",", "(", ")", ":", "#", "!", "=", ">", "<", "~", "+", "."], true, false),
307 peg$c6 = function peg$c6(i) {
311 peg$c8 = peg$literalExpectation(">", false),
312 peg$c9 = function peg$c9() {
316 peg$c11 = peg$literalExpectation("~", false),
317 peg$c12 = function peg$c12() {
321 peg$c14 = peg$literalExpectation("+", false),
322 peg$c15 = function peg$c15() {
325 peg$c16 = function peg$c16() {
329 peg$c18 = peg$literalExpectation(",", false),
330 peg$c19 = function peg$c19(s, ss) {
331 return [s].concat(ss.map(function (s) {
335 peg$c20 = function peg$c20(a, ops) {
336 return ops.reduce(function (memo, rhs) {
345 peg$c22 = peg$literalExpectation("!", false),
346 peg$c23 = function peg$c23(subject, as) {
347 var b = as.length === 1 ? as[0] : {
351 if (subject) b.subject = true;
355 peg$c25 = peg$literalExpectation("*", false),
356 peg$c26 = function peg$c26(a) {
363 peg$c28 = peg$literalExpectation("#", false),
364 peg$c29 = function peg$c29(i) {
371 peg$c31 = peg$literalExpectation("[", false),
373 peg$c33 = peg$literalExpectation("]", false),
374 peg$c34 = function peg$c34(v) {
378 peg$c36 = peg$classExpectation([">", "<", "!"], false, false),
380 peg$c38 = peg$literalExpectation("=", false),
381 peg$c39 = function peg$c39(a) {
382 return (a || '') + '=';
385 peg$c41 = peg$classExpectation([">", "<"], false, false),
387 peg$c43 = peg$literalExpectation(".", false),
388 peg$c44 = function peg$c44(a, as) {
389 return [].concat.apply([a], as).join('');
391 peg$c45 = function peg$c45(name, op, value) {
399 peg$c46 = function peg$c46(name) {
406 peg$c48 = peg$literalExpectation("\"", false),
408 peg$c50 = peg$classExpectation(["\\", "\""], true, false),
410 peg$c52 = peg$literalExpectation("\\", false),
411 peg$c53 = peg$anyExpectation(),
412 peg$c54 = function peg$c54(a, b) {
415 peg$c55 = function peg$c55(d) {
418 value: strUnescape(d.join(''))
422 peg$c57 = peg$literalExpectation("'", false),
424 peg$c59 = peg$classExpectation(["\\", "'"], true, false),
426 peg$c61 = peg$classExpectation([["0", "9"]], false, false),
427 peg$c62 = function peg$c62(a, b) {
428 // Can use `a.flat().join('')` once supported
429 var leadingDecimals = a ? [].concat.apply([], a).join('') : '';
432 value: parseFloat(leadingDecimals + b.join(''))
435 peg$c63 = function peg$c63(i) {
442 peg$c65 = peg$literalExpectation("type(", false),
444 peg$c67 = peg$classExpectation([" ", ")"], true, false),
446 peg$c69 = peg$literalExpectation(")", false),
447 peg$c70 = function peg$c70(t) {
454 peg$c72 = peg$classExpectation(["i", "m", "s", "u"], false, false),
456 peg$c74 = peg$literalExpectation("/", false),
458 peg$c76 = peg$classExpectation(["/"], true, false),
459 peg$c77 = function peg$c77(d, flgs) {
462 value: new RegExp(d.join(''), flgs ? flgs.join('') : '')
465 peg$c78 = function peg$c78(i, is) {
468 name: is.reduce(function (memo, p) {
469 return memo + p[0] + p[1];
474 peg$c80 = peg$literalExpectation(":not(", false),
475 peg$c81 = function peg$c81(ss) {
481 peg$c82 = ":matches(",
482 peg$c83 = peg$literalExpectation(":matches(", false),
483 peg$c84 = function peg$c84(ss) {
490 peg$c86 = peg$literalExpectation(":has(", false),
491 peg$c87 = function peg$c87(ss) {
497 peg$c88 = ":first-child",
498 peg$c89 = peg$literalExpectation(":first-child", false),
499 peg$c90 = function peg$c90() {
502 peg$c91 = ":last-child",
503 peg$c92 = peg$literalExpectation(":last-child", false),
504 peg$c93 = function peg$c93() {
507 peg$c94 = ":nth-child(",
508 peg$c95 = peg$literalExpectation(":nth-child(", false),
509 peg$c96 = function peg$c96(n) {
510 return nth(parseInt(n.join(''), 10));
512 peg$c97 = ":nth-last-child(",
513 peg$c98 = peg$literalExpectation(":nth-last-child(", false),
514 peg$c99 = function peg$c99(n) {
515 return nthLast(parseInt(n.join(''), 10));
518 peg$c101 = peg$literalExpectation(":", false),
519 peg$c102 = "statement",
520 peg$c103 = peg$literalExpectation("statement", true),
521 peg$c104 = "expression",
522 peg$c105 = peg$literalExpectation("expression", true),
523 peg$c106 = "declaration",
524 peg$c107 = peg$literalExpectation("declaration", true),
525 peg$c108 = "function",
526 peg$c109 = peg$literalExpectation("function", true),
527 peg$c110 = "pattern",
528 peg$c111 = peg$literalExpectation("pattern", true),
529 peg$c112 = function peg$c112(c) {
536 peg$posDetailsCache = [{
541 peg$maxFailExpected = [],
542 peg$resultsCache = {},
545 if ("startRule" in options) {
546 if (!(options.startRule in peg$startRuleFunctions)) {
547 throw new Error("Can't start parsing from rule \"" + options.startRule + "\".");
550 peg$startRuleFunction = peg$startRuleFunctions[options.startRule];
553 function peg$literalExpectation(text, ignoreCase) {
557 ignoreCase: ignoreCase
561 function peg$classExpectation(parts, inverted, ignoreCase) {
566 ignoreCase: ignoreCase
570 function peg$anyExpectation() {
576 function peg$endExpectation() {
582 function peg$computePosDetails(pos) {
583 var details = peg$posDetailsCache[pos],
591 while (!peg$posDetailsCache[p]) {
595 details = peg$posDetailsCache[p];
598 column: details.column
602 if (input.charCodeAt(p) === 10) {
612 peg$posDetailsCache[pos] = details;
617 function peg$computeLocation(startPos, endPos) {
618 var startPosDetails = peg$computePosDetails(startPos),
619 endPosDetails = peg$computePosDetails(endPos);
623 line: startPosDetails.line,
624 column: startPosDetails.column
628 line: endPosDetails.line,
629 column: endPosDetails.column
634 function peg$fail(expected) {
635 if (peg$currPos < peg$maxFailPos) {
639 if (peg$currPos > peg$maxFailPos) {
640 peg$maxFailPos = peg$currPos;
641 peg$maxFailExpected = [];
644 peg$maxFailExpected.push(expected);
647 function peg$buildStructuredError(expected, found, location) {
648 return new peg$SyntaxError(peg$SyntaxError.buildMessage(expected, found), expected, found, location);
651 function peg$parsestart() {
653 var key = peg$currPos * 30 + 0,
654 cached = peg$resultsCache[key];
657 peg$currPos = cached.nextPos;
658 return cached.result;
664 if (s1 !== peg$FAILED) {
665 s2 = peg$parseselectors();
667 if (s2 !== peg$FAILED) {
670 if (s3 !== peg$FAILED) {
686 if (s0 === peg$FAILED) {
690 if (s1 !== peg$FAILED) {
697 peg$resultsCache[key] = {
698 nextPos: peg$currPos,
704 function peg$parse_() {
706 var key = peg$currPos * 30 + 1,
707 cached = peg$resultsCache[key];
710 peg$currPos = cached.nextPos;
711 return cached.result;
716 if (input.charCodeAt(peg$currPos) === 32) {
727 while (s1 !== peg$FAILED) {
730 if (input.charCodeAt(peg$currPos) === 32) {
742 peg$resultsCache[key] = {
743 nextPos: peg$currPos,
749 function peg$parseidentifierName() {
751 var key = peg$currPos * 30 + 2,
752 cached = peg$resultsCache[key];
755 peg$currPos = cached.nextPos;
756 return cached.result;
762 if (peg$c4.test(input.charAt(peg$currPos))) {
763 s2 = input.charAt(peg$currPos);
773 if (s2 !== peg$FAILED) {
774 while (s2 !== peg$FAILED) {
777 if (peg$c4.test(input.charAt(peg$currPos))) {
778 s2 = input.charAt(peg$currPos);
792 if (s1 !== peg$FAILED) {
797 peg$resultsCache[key] = {
798 nextPos: peg$currPos,
804 function peg$parsebinaryOp() {
806 var key = peg$currPos * 30 + 3,
807 cached = peg$resultsCache[key];
810 peg$currPos = cached.nextPos;
811 return cached.result;
817 if (s1 !== peg$FAILED) {
818 if (input.charCodeAt(peg$currPos) === 62) {
829 if (s2 !== peg$FAILED) {
832 if (s3 !== peg$FAILED) {
848 if (s0 === peg$FAILED) {
852 if (s1 !== peg$FAILED) {
853 if (input.charCodeAt(peg$currPos) === 126) {
864 if (s2 !== peg$FAILED) {
867 if (s3 !== peg$FAILED) {
883 if (s0 === peg$FAILED) {
887 if (s1 !== peg$FAILED) {
888 if (input.charCodeAt(peg$currPos) === 43) {
899 if (s2 !== peg$FAILED) {
902 if (s3 !== peg$FAILED) {
918 if (s0 === peg$FAILED) {
921 if (input.charCodeAt(peg$currPos) === 32) {
932 if (s1 !== peg$FAILED) {
935 if (s2 !== peg$FAILED) {
950 peg$resultsCache[key] = {
951 nextPos: peg$currPos,
957 function peg$parseselectors() {
958 var s0, s1, s2, s3, s4, s5, s6, s7;
959 var key = peg$currPos * 30 + 4,
960 cached = peg$resultsCache[key];
963 peg$currPos = cached.nextPos;
964 return cached.result;
968 s1 = peg$parseselector();
970 if (s1 !== peg$FAILED) {
975 if (s4 !== peg$FAILED) {
976 if (input.charCodeAt(peg$currPos) === 44) {
987 if (s5 !== peg$FAILED) {
990 if (s6 !== peg$FAILED) {
991 s7 = peg$parseselector();
993 if (s7 !== peg$FAILED) {
994 s4 = [s4, s5, s6, s7];
1013 while (s3 !== peg$FAILED) {
1018 if (s4 !== peg$FAILED) {
1019 if (input.charCodeAt(peg$currPos) === 44) {
1030 if (s5 !== peg$FAILED) {
1033 if (s6 !== peg$FAILED) {
1034 s7 = peg$parseselector();
1036 if (s7 !== peg$FAILED) {
1037 s4 = [s4, s5, s6, s7];
1057 if (s2 !== peg$FAILED) {
1058 s1 = peg$c19(s1, s2);
1069 peg$resultsCache[key] = {
1070 nextPos: peg$currPos,
1076 function peg$parseselector() {
1077 var s0, s1, s2, s3, s4, s5;
1078 var key = peg$currPos * 30 + 5,
1079 cached = peg$resultsCache[key];
1082 peg$currPos = cached.nextPos;
1083 return cached.result;
1087 s1 = peg$parsesequence();
1089 if (s1 !== peg$FAILED) {
1092 s4 = peg$parsebinaryOp();
1094 if (s4 !== peg$FAILED) {
1095 s5 = peg$parsesequence();
1097 if (s5 !== peg$FAILED) {
1109 while (s3 !== peg$FAILED) {
1112 s4 = peg$parsebinaryOp();
1114 if (s4 !== peg$FAILED) {
1115 s5 = peg$parsesequence();
1117 if (s5 !== peg$FAILED) {
1130 if (s2 !== peg$FAILED) {
1131 s1 = peg$c20(s1, s2);
1142 peg$resultsCache[key] = {
1143 nextPos: peg$currPos,
1149 function peg$parsesequence() {
1151 var key = peg$currPos * 30 + 6,
1152 cached = peg$resultsCache[key];
1155 peg$currPos = cached.nextPos;
1156 return cached.result;
1161 if (input.charCodeAt(peg$currPos) === 33) {
1172 if (s1 === peg$FAILED) {
1176 if (s1 !== peg$FAILED) {
1178 s3 = peg$parseatom();
1180 if (s3 !== peg$FAILED) {
1181 while (s3 !== peg$FAILED) {
1183 s3 = peg$parseatom();
1189 if (s2 !== peg$FAILED) {
1190 s1 = peg$c23(s1, s2);
1201 peg$resultsCache[key] = {
1202 nextPos: peg$currPos,
1208 function peg$parseatom() {
1210 var key = peg$currPos * 30 + 7,
1211 cached = peg$resultsCache[key];
1214 peg$currPos = cached.nextPos;
1215 return cached.result;
1218 s0 = peg$parsewildcard();
1220 if (s0 === peg$FAILED) {
1221 s0 = peg$parseidentifier();
1223 if (s0 === peg$FAILED) {
1224 s0 = peg$parseattr();
1226 if (s0 === peg$FAILED) {
1227 s0 = peg$parsefield();
1229 if (s0 === peg$FAILED) {
1230 s0 = peg$parsenegation();
1232 if (s0 === peg$FAILED) {
1233 s0 = peg$parsematches();
1235 if (s0 === peg$FAILED) {
1236 s0 = peg$parsehas();
1238 if (s0 === peg$FAILED) {
1239 s0 = peg$parsefirstChild();
1241 if (s0 === peg$FAILED) {
1242 s0 = peg$parselastChild();
1244 if (s0 === peg$FAILED) {
1245 s0 = peg$parsenthChild();
1247 if (s0 === peg$FAILED) {
1248 s0 = peg$parsenthLastChild();
1250 if (s0 === peg$FAILED) {
1251 s0 = peg$parseclass();
1264 peg$resultsCache[key] = {
1265 nextPos: peg$currPos,
1271 function peg$parsewildcard() {
1273 var key = peg$currPos * 30 + 8,
1274 cached = peg$resultsCache[key];
1277 peg$currPos = cached.nextPos;
1278 return cached.result;
1283 if (input.charCodeAt(peg$currPos) === 42) {
1294 if (s1 !== peg$FAILED) {
1299 peg$resultsCache[key] = {
1300 nextPos: peg$currPos,
1306 function peg$parseidentifier() {
1308 var key = peg$currPos * 30 + 9,
1309 cached = peg$resultsCache[key];
1312 peg$currPos = cached.nextPos;
1313 return cached.result;
1318 if (input.charCodeAt(peg$currPos) === 35) {
1329 if (s1 === peg$FAILED) {
1333 if (s1 !== peg$FAILED) {
1334 s2 = peg$parseidentifierName();
1336 if (s2 !== peg$FAILED) {
1348 peg$resultsCache[key] = {
1349 nextPos: peg$currPos,
1355 function peg$parseattr() {
1356 var s0, s1, s2, s3, s4, s5;
1357 var key = peg$currPos * 30 + 10,
1358 cached = peg$resultsCache[key];
1361 peg$currPos = cached.nextPos;
1362 return cached.result;
1367 if (input.charCodeAt(peg$currPos) === 91) {
1378 if (s1 !== peg$FAILED) {
1381 if (s2 !== peg$FAILED) {
1382 s3 = peg$parseattrValue();
1384 if (s3 !== peg$FAILED) {
1387 if (s4 !== peg$FAILED) {
1388 if (input.charCodeAt(peg$currPos) === 93) {
1399 if (s5 !== peg$FAILED) {
1423 peg$resultsCache[key] = {
1424 nextPos: peg$currPos,
1430 function peg$parseattrOps() {
1432 var key = peg$currPos * 30 + 11,
1433 cached = peg$resultsCache[key];
1436 peg$currPos = cached.nextPos;
1437 return cached.result;
1442 if (peg$c35.test(input.charAt(peg$currPos))) {
1443 s1 = input.charAt(peg$currPos);
1453 if (s1 === peg$FAILED) {
1457 if (s1 !== peg$FAILED) {
1458 if (input.charCodeAt(peg$currPos) === 61) {
1469 if (s2 !== peg$FAILED) {
1481 if (s0 === peg$FAILED) {
1482 if (peg$c40.test(input.charAt(peg$currPos))) {
1483 s0 = input.charAt(peg$currPos);
1494 peg$resultsCache[key] = {
1495 nextPos: peg$currPos,
1501 function peg$parseattrEqOps() {
1503 var key = peg$currPos * 30 + 12,
1504 cached = peg$resultsCache[key];
1507 peg$currPos = cached.nextPos;
1508 return cached.result;
1513 if (input.charCodeAt(peg$currPos) === 33) {
1524 if (s1 === peg$FAILED) {
1528 if (s1 !== peg$FAILED) {
1529 if (input.charCodeAt(peg$currPos) === 61) {
1540 if (s2 !== peg$FAILED) {
1552 peg$resultsCache[key] = {
1553 nextPos: peg$currPos,
1559 function peg$parseattrName() {
1560 var s0, s1, s2, s3, s4, s5;
1561 var key = peg$currPos * 30 + 13,
1562 cached = peg$resultsCache[key];
1565 peg$currPos = cached.nextPos;
1566 return cached.result;
1570 s1 = peg$parseidentifierName();
1572 if (s1 !== peg$FAILED) {
1576 if (input.charCodeAt(peg$currPos) === 46) {
1587 if (s4 !== peg$FAILED) {
1588 s5 = peg$parseidentifierName();
1590 if (s5 !== peg$FAILED) {
1602 while (s3 !== peg$FAILED) {
1606 if (input.charCodeAt(peg$currPos) === 46) {
1617 if (s4 !== peg$FAILED) {
1618 s5 = peg$parseidentifierName();
1620 if (s5 !== peg$FAILED) {
1633 if (s2 !== peg$FAILED) {
1634 s1 = peg$c44(s1, s2);
1645 peg$resultsCache[key] = {
1646 nextPos: peg$currPos,
1652 function peg$parseattrValue() {
1653 var s0, s1, s2, s3, s4, s5;
1654 var key = peg$currPos * 30 + 14,
1655 cached = peg$resultsCache[key];
1658 peg$currPos = cached.nextPos;
1659 return cached.result;
1663 s1 = peg$parseattrName();
1665 if (s1 !== peg$FAILED) {
1668 if (s2 !== peg$FAILED) {
1669 s3 = peg$parseattrEqOps();
1671 if (s3 !== peg$FAILED) {
1674 if (s4 !== peg$FAILED) {
1675 s5 = peg$parsetype();
1677 if (s5 === peg$FAILED) {
1678 s5 = peg$parseregex();
1681 if (s5 !== peg$FAILED) {
1682 s1 = peg$c45(s1, s3, s5);
1705 if (s0 === peg$FAILED) {
1707 s1 = peg$parseattrName();
1709 if (s1 !== peg$FAILED) {
1712 if (s2 !== peg$FAILED) {
1713 s3 = peg$parseattrOps();
1715 if (s3 !== peg$FAILED) {
1718 if (s4 !== peg$FAILED) {
1719 s5 = peg$parsestring();
1721 if (s5 === peg$FAILED) {
1722 s5 = peg$parsenumber();
1724 if (s5 === peg$FAILED) {
1725 s5 = peg$parsepath();
1729 if (s5 !== peg$FAILED) {
1730 s1 = peg$c45(s1, s3, s5);
1753 if (s0 === peg$FAILED) {
1755 s1 = peg$parseattrName();
1757 if (s1 !== peg$FAILED) {
1765 peg$resultsCache[key] = {
1766 nextPos: peg$currPos,
1772 function peg$parsestring() {
1773 var s0, s1, s2, s3, s4, s5;
1774 var key = peg$currPos * 30 + 15,
1775 cached = peg$resultsCache[key];
1778 peg$currPos = cached.nextPos;
1779 return cached.result;
1784 if (input.charCodeAt(peg$currPos) === 34) {
1795 if (s1 !== peg$FAILED) {
1798 if (peg$c49.test(input.charAt(peg$currPos))) {
1799 s3 = input.charAt(peg$currPos);
1809 if (s3 === peg$FAILED) {
1812 if (input.charCodeAt(peg$currPos) === 92) {
1823 if (s4 !== peg$FAILED) {
1824 if (input.length > peg$currPos) {
1825 s5 = input.charAt(peg$currPos);
1835 if (s5 !== peg$FAILED) {
1836 s4 = peg$c54(s4, s5);
1848 while (s3 !== peg$FAILED) {
1851 if (peg$c49.test(input.charAt(peg$currPos))) {
1852 s3 = input.charAt(peg$currPos);
1862 if (s3 === peg$FAILED) {
1865 if (input.charCodeAt(peg$currPos) === 92) {
1876 if (s4 !== peg$FAILED) {
1877 if (input.length > peg$currPos) {
1878 s5 = input.charAt(peg$currPos);
1888 if (s5 !== peg$FAILED) {
1889 s4 = peg$c54(s4, s5);
1902 if (s2 !== peg$FAILED) {
1903 if (input.charCodeAt(peg$currPos) === 34) {
1914 if (s3 !== peg$FAILED) {
1930 if (s0 === peg$FAILED) {
1933 if (input.charCodeAt(peg$currPos) === 39) {
1944 if (s1 !== peg$FAILED) {
1947 if (peg$c58.test(input.charAt(peg$currPos))) {
1948 s3 = input.charAt(peg$currPos);
1958 if (s3 === peg$FAILED) {
1961 if (input.charCodeAt(peg$currPos) === 92) {
1972 if (s4 !== peg$FAILED) {
1973 if (input.length > peg$currPos) {
1974 s5 = input.charAt(peg$currPos);
1984 if (s5 !== peg$FAILED) {
1985 s4 = peg$c54(s4, s5);
1997 while (s3 !== peg$FAILED) {
2000 if (peg$c58.test(input.charAt(peg$currPos))) {
2001 s3 = input.charAt(peg$currPos);
2011 if (s3 === peg$FAILED) {
2014 if (input.charCodeAt(peg$currPos) === 92) {
2025 if (s4 !== peg$FAILED) {
2026 if (input.length > peg$currPos) {
2027 s5 = input.charAt(peg$currPos);
2037 if (s5 !== peg$FAILED) {
2038 s4 = peg$c54(s4, s5);
2051 if (s2 !== peg$FAILED) {
2052 if (input.charCodeAt(peg$currPos) === 39) {
2063 if (s3 !== peg$FAILED) {
2080 peg$resultsCache[key] = {
2081 nextPos: peg$currPos,
2087 function peg$parsenumber() {
2089 var key = peg$currPos * 30 + 16,
2090 cached = peg$resultsCache[key];
2093 peg$currPos = cached.nextPos;
2094 return cached.result;
2101 if (peg$c60.test(input.charAt(peg$currPos))) {
2102 s3 = input.charAt(peg$currPos);
2112 while (s3 !== peg$FAILED) {
2115 if (peg$c60.test(input.charAt(peg$currPos))) {
2116 s3 = input.charAt(peg$currPos);
2127 if (s2 !== peg$FAILED) {
2128 if (input.charCodeAt(peg$currPos) === 46) {
2139 if (s3 !== peg$FAILED) {
2151 if (s1 === peg$FAILED) {
2155 if (s1 !== peg$FAILED) {
2158 if (peg$c60.test(input.charAt(peg$currPos))) {
2159 s3 = input.charAt(peg$currPos);
2169 if (s3 !== peg$FAILED) {
2170 while (s3 !== peg$FAILED) {
2173 if (peg$c60.test(input.charAt(peg$currPos))) {
2174 s3 = input.charAt(peg$currPos);
2188 if (s2 !== peg$FAILED) {
2189 s1 = peg$c62(s1, s2);
2200 peg$resultsCache[key] = {
2201 nextPos: peg$currPos,
2207 function peg$parsepath() {
2209 var key = peg$currPos * 30 + 17,
2210 cached = peg$resultsCache[key];
2213 peg$currPos = cached.nextPos;
2214 return cached.result;
2218 s1 = peg$parseidentifierName();
2220 if (s1 !== peg$FAILED) {
2225 peg$resultsCache[key] = {
2226 nextPos: peg$currPos,
2232 function peg$parsetype() {
2233 var s0, s1, s2, s3, s4, s5;
2234 var key = peg$currPos * 30 + 18,
2235 cached = peg$resultsCache[key];
2238 peg$currPos = cached.nextPos;
2239 return cached.result;
2244 if (input.substr(peg$currPos, 5) === peg$c64) {
2255 if (s1 !== peg$FAILED) {
2258 if (s2 !== peg$FAILED) {
2261 if (peg$c66.test(input.charAt(peg$currPos))) {
2262 s4 = input.charAt(peg$currPos);
2272 if (s4 !== peg$FAILED) {
2273 while (s4 !== peg$FAILED) {
2276 if (peg$c66.test(input.charAt(peg$currPos))) {
2277 s4 = input.charAt(peg$currPos);
2291 if (s3 !== peg$FAILED) {
2294 if (s4 !== peg$FAILED) {
2295 if (input.charCodeAt(peg$currPos) === 41) {
2306 if (s5 !== peg$FAILED) {
2330 peg$resultsCache[key] = {
2331 nextPos: peg$currPos,
2337 function peg$parseflags() {
2339 var key = peg$currPos * 30 + 19,
2340 cached = peg$resultsCache[key];
2343 peg$currPos = cached.nextPos;
2344 return cached.result;
2349 if (peg$c71.test(input.charAt(peg$currPos))) {
2350 s1 = input.charAt(peg$currPos);
2360 if (s1 !== peg$FAILED) {
2361 while (s1 !== peg$FAILED) {
2364 if (peg$c71.test(input.charAt(peg$currPos))) {
2365 s1 = input.charAt(peg$currPos);
2379 peg$resultsCache[key] = {
2380 nextPos: peg$currPos,
2386 function peg$parseregex() {
2387 var s0, s1, s2, s3, s4;
2388 var key = peg$currPos * 30 + 20,
2389 cached = peg$resultsCache[key];
2392 peg$currPos = cached.nextPos;
2393 return cached.result;
2398 if (input.charCodeAt(peg$currPos) === 47) {
2409 if (s1 !== peg$FAILED) {
2412 if (peg$c75.test(input.charAt(peg$currPos))) {
2413 s3 = input.charAt(peg$currPos);
2423 if (s3 !== peg$FAILED) {
2424 while (s3 !== peg$FAILED) {
2427 if (peg$c75.test(input.charAt(peg$currPos))) {
2428 s3 = input.charAt(peg$currPos);
2442 if (s2 !== peg$FAILED) {
2443 if (input.charCodeAt(peg$currPos) === 47) {
2454 if (s3 !== peg$FAILED) {
2455 s4 = peg$parseflags();
2457 if (s4 === peg$FAILED) {
2461 if (s4 !== peg$FAILED) {
2462 s1 = peg$c77(s2, s4);
2481 peg$resultsCache[key] = {
2482 nextPos: peg$currPos,
2488 function peg$parsefield() {
2489 var s0, s1, s2, s3, s4, s5, s6;
2490 var key = peg$currPos * 30 + 21,
2491 cached = peg$resultsCache[key];
2494 peg$currPos = cached.nextPos;
2495 return cached.result;
2500 if (input.charCodeAt(peg$currPos) === 46) {
2511 if (s1 !== peg$FAILED) {
2512 s2 = peg$parseidentifierName();
2514 if (s2 !== peg$FAILED) {
2518 if (input.charCodeAt(peg$currPos) === 46) {
2529 if (s5 !== peg$FAILED) {
2530 s6 = peg$parseidentifierName();
2532 if (s6 !== peg$FAILED) {
2544 while (s4 !== peg$FAILED) {
2548 if (input.charCodeAt(peg$currPos) === 46) {
2559 if (s5 !== peg$FAILED) {
2560 s6 = peg$parseidentifierName();
2562 if (s6 !== peg$FAILED) {
2575 if (s3 !== peg$FAILED) {
2576 s1 = peg$c78(s2, s3);
2591 peg$resultsCache[key] = {
2592 nextPos: peg$currPos,
2598 function peg$parsenegation() {
2599 var s0, s1, s2, s3, s4, s5;
2600 var key = peg$currPos * 30 + 22,
2601 cached = peg$resultsCache[key];
2604 peg$currPos = cached.nextPos;
2605 return cached.result;
2610 if (input.substr(peg$currPos, 5) === peg$c79) {
2621 if (s1 !== peg$FAILED) {
2624 if (s2 !== peg$FAILED) {
2625 s3 = peg$parseselectors();
2627 if (s3 !== peg$FAILED) {
2630 if (s4 !== peg$FAILED) {
2631 if (input.charCodeAt(peg$currPos) === 41) {
2642 if (s5 !== peg$FAILED) {
2666 peg$resultsCache[key] = {
2667 nextPos: peg$currPos,
2673 function peg$parsematches() {
2674 var s0, s1, s2, s3, s4, s5;
2675 var key = peg$currPos * 30 + 23,
2676 cached = peg$resultsCache[key];
2679 peg$currPos = cached.nextPos;
2680 return cached.result;
2685 if (input.substr(peg$currPos, 9) === peg$c82) {
2696 if (s1 !== peg$FAILED) {
2699 if (s2 !== peg$FAILED) {
2700 s3 = peg$parseselectors();
2702 if (s3 !== peg$FAILED) {
2705 if (s4 !== peg$FAILED) {
2706 if (input.charCodeAt(peg$currPos) === 41) {
2717 if (s5 !== peg$FAILED) {
2741 peg$resultsCache[key] = {
2742 nextPos: peg$currPos,
2748 function peg$parsehas() {
2749 var s0, s1, s2, s3, s4, s5;
2750 var key = peg$currPos * 30 + 24,
2751 cached = peg$resultsCache[key];
2754 peg$currPos = cached.nextPos;
2755 return cached.result;
2760 if (input.substr(peg$currPos, 5) === peg$c85) {
2771 if (s1 !== peg$FAILED) {
2774 if (s2 !== peg$FAILED) {
2775 s3 = peg$parseselectors();
2777 if (s3 !== peg$FAILED) {
2780 if (s4 !== peg$FAILED) {
2781 if (input.charCodeAt(peg$currPos) === 41) {
2792 if (s5 !== peg$FAILED) {
2816 peg$resultsCache[key] = {
2817 nextPos: peg$currPos,
2823 function peg$parsefirstChild() {
2825 var key = peg$currPos * 30 + 25,
2826 cached = peg$resultsCache[key];
2829 peg$currPos = cached.nextPos;
2830 return cached.result;
2835 if (input.substr(peg$currPos, 12) === peg$c88) {
2846 if (s1 !== peg$FAILED) {
2851 peg$resultsCache[key] = {
2852 nextPos: peg$currPos,
2858 function peg$parselastChild() {
2860 var key = peg$currPos * 30 + 26,
2861 cached = peg$resultsCache[key];
2864 peg$currPos = cached.nextPos;
2865 return cached.result;
2870 if (input.substr(peg$currPos, 11) === peg$c91) {
2881 if (s1 !== peg$FAILED) {
2886 peg$resultsCache[key] = {
2887 nextPos: peg$currPos,
2893 function peg$parsenthChild() {
2894 var s0, s1, s2, s3, s4, s5;
2895 var key = peg$currPos * 30 + 27,
2896 cached = peg$resultsCache[key];
2899 peg$currPos = cached.nextPos;
2900 return cached.result;
2905 if (input.substr(peg$currPos, 11) === peg$c94) {
2916 if (s1 !== peg$FAILED) {
2919 if (s2 !== peg$FAILED) {
2922 if (peg$c60.test(input.charAt(peg$currPos))) {
2923 s4 = input.charAt(peg$currPos);
2933 if (s4 !== peg$FAILED) {
2934 while (s4 !== peg$FAILED) {
2937 if (peg$c60.test(input.charAt(peg$currPos))) {
2938 s4 = input.charAt(peg$currPos);
2952 if (s3 !== peg$FAILED) {
2955 if (s4 !== peg$FAILED) {
2956 if (input.charCodeAt(peg$currPos) === 41) {
2967 if (s5 !== peg$FAILED) {
2991 peg$resultsCache[key] = {
2992 nextPos: peg$currPos,
2998 function peg$parsenthLastChild() {
2999 var s0, s1, s2, s3, s4, s5;
3000 var key = peg$currPos * 30 + 28,
3001 cached = peg$resultsCache[key];
3004 peg$currPos = cached.nextPos;
3005 return cached.result;
3010 if (input.substr(peg$currPos, 16) === peg$c97) {
3021 if (s1 !== peg$FAILED) {
3024 if (s2 !== peg$FAILED) {
3027 if (peg$c60.test(input.charAt(peg$currPos))) {
3028 s4 = input.charAt(peg$currPos);
3038 if (s4 !== peg$FAILED) {
3039 while (s4 !== peg$FAILED) {
3042 if (peg$c60.test(input.charAt(peg$currPos))) {
3043 s4 = input.charAt(peg$currPos);
3057 if (s3 !== peg$FAILED) {
3060 if (s4 !== peg$FAILED) {
3061 if (input.charCodeAt(peg$currPos) === 41) {
3072 if (s5 !== peg$FAILED) {
3096 peg$resultsCache[key] = {
3097 nextPos: peg$currPos,
3103 function peg$parseclass() {
3105 var key = peg$currPos * 30 + 29,
3106 cached = peg$resultsCache[key];
3109 peg$currPos = cached.nextPos;
3110 return cached.result;
3115 if (input.charCodeAt(peg$currPos) === 58) {
3126 if (s1 !== peg$FAILED) {
3127 if (input.substr(peg$currPos, 9).toLowerCase() === peg$c102) {
3128 s2 = input.substr(peg$currPos, 9);
3138 if (s2 === peg$FAILED) {
3139 if (input.substr(peg$currPos, 10).toLowerCase() === peg$c104) {
3140 s2 = input.substr(peg$currPos, 10);
3150 if (s2 === peg$FAILED) {
3151 if (input.substr(peg$currPos, 11).toLowerCase() === peg$c106) {
3152 s2 = input.substr(peg$currPos, 11);
3162 if (s2 === peg$FAILED) {
3163 if (input.substr(peg$currPos, 8).toLowerCase() === peg$c108) {
3164 s2 = input.substr(peg$currPos, 8);
3174 if (s2 === peg$FAILED) {
3175 if (input.substr(peg$currPos, 7).toLowerCase() === peg$c110) {
3176 s2 = input.substr(peg$currPos, 7);
3190 if (s2 !== peg$FAILED) {
3202 peg$resultsCache[key] = {
3203 nextPos: peg$currPos,
3219 function nthLast(n) {
3221 type: 'nth-last-child',
3229 function strUnescape(s) {
3230 return s.replace(/\\(.)/g, function (match, ch) {
3256 peg$result = peg$startRuleFunction();
3258 if (peg$result !== peg$FAILED && peg$currPos === input.length) {
3261 if (peg$result !== peg$FAILED && peg$currPos < input.length) {
3262 peg$fail(peg$endExpectation());
3265 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));
3270 SyntaxError: peg$SyntaxError,
3276 function _objectEntries(obj) {
3278 var keys = Object.keys(obj);
3280 for (var k = 0; k < keys.length; k++) entries.push([keys[k], obj[keys[k]]]);
3285 * @typedef {"LEFT_SIDE"|"RIGHT_SIDE"} Side
3288 var LEFT_SIDE = 'LEFT_SIDE';
3289 var RIGHT_SIDE = 'RIGHT_SIDE';
3292 * @see https://esprima.readthedocs.io/en/latest/syntax-tree-format.html
3296 * One of the rules of `grammar.pegjs`
3297 * @typedef {PlainObject} SelectorAST
3298 * @see grammar.pegjs
3302 * The `sequence` production of `grammar.pegjs`
3303 * @typedef {PlainObject} SelectorSequenceAST
3307 * Get the value of a property which may be multiple levels down
3309 * @param {?PlainObject} obj
3310 * @param {string} key
3311 * @returns {undefined|boolean|string|number|external:AST}
3314 function getPath(obj, key) {
3315 var keys = key.split('.');
3317 var _iterator = _createForOfIteratorHelper(keys),
3321 for (_iterator.s(); !(_step = _iterator.n()).done;) {
3322 var _key = _step.value;
3339 * Determine whether `node` can be reached by following `path`,
3340 * starting at `ancestor`.
3341 * @param {?external:AST} node
3342 * @param {?external:AST} ancestor
3343 * @param {string[]} path
3344 * @returns {boolean}
3348 function inPath(node, ancestor, path) {
3349 if (path.length === 0) {
3350 return node === ancestor;
3353 if (ancestor == null) {
3357 var field = ancestor[path[0]];
3358 var remainingPath = path.slice(1);
3360 if (Array.isArray(field)) {
3361 var _iterator2 = _createForOfIteratorHelper(field),
3365 for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
3366 var component = _step2.value;
3368 if (inPath(node, component, remainingPath)) {
3380 return inPath(node, field, remainingPath);
3384 * @callback TraverseOptionFallback
3385 * @param {external:AST} node The given node.
3386 * @returns {string[]} An array of visitor keys for the given node.
3390 * @typedef {object} ESQueryOptions
3391 * @property { { [nodeType: string]: string[] } } [visitorKeys] By passing `visitorKeys` mapping, we can extend the properties of the nodes that traverse the node.
3392 * @property {TraverseOptionFallback} [fallback] By passing `fallback` option, we can control the properties of traversing nodes when encountering unknown nodes.
3396 * Given a `node` and its ancestors, determine if `node` is matched
3398 * @param {?external:AST} node
3399 * @param {?SelectorAST} selector
3400 * @param {external:AST[]} [ancestry=[]]
3401 * @param {ESQueryOptions} [options]
3402 * @throws {Error} Unknowns (operator, class name, selector type, or
3403 * selector value type)
3404 * @returns {boolean}
3408 function matches(node, selector, ancestry, options) {
3421 switch (selector.type) {
3426 return selector.value.toLowerCase() === node.type.toLowerCase();
3430 var path = selector.name.split('.');
3431 var ancestor = ancestry[path.length - 1];
3432 return inPath(node, ancestor, path);
3436 var _iterator3 = _createForOfIteratorHelper(selector.selectors),
3440 for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
3441 var sel = _step3.value;
3443 if (matches(node, sel, ancestry, options)) {
3456 var _iterator4 = _createForOfIteratorHelper(selector.selectors),
3460 for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
3461 var _sel = _step4.value;
3463 if (!matches(node, _sel, ancestry, options)) {
3476 var _iterator5 = _createForOfIteratorHelper(selector.selectors),
3480 for (_iterator5.s(); !(_step5 = _iterator5.n()).done;) {
3481 var _sel2 = _step5.value;
3483 if (matches(node, _sel2, ancestry, options)) {
3497 var _ret = function () {
3500 var _iterator6 = _createForOfIteratorHelper(selector.selectors),
3504 var _loop = function _loop() {
3505 var sel = _step6.value;
3507 estraverse.traverse(node, {
3508 enter: function enter(node, parent) {
3509 if (parent != null) {
3513 if (matches(node, sel, a, options)) {
3514 collector.push(node);
3517 leave: function leave() {
3520 keys: options && options.visitorKeys,
3521 fallback: options && options.fallback || 'iteration'
3525 for (_iterator6.s(); !(_step6 = _iterator6.n()).done;) {
3535 v: collector.length !== 0
3539 if (_typeof(_ret) === "object") return _ret.v;
3543 if (matches(node, selector.right, ancestry, options)) {
3544 return matches(ancestry[0], selector.left, ancestry.slice(1), options);
3550 if (matches(node, selector.right, ancestry, options)) {
3551 for (var i = 0, l = ancestry.length; i < l; ++i) {
3552 if (matches(ancestry[i], selector.left, ancestry.slice(i + 1), options)) {
3562 var p = getPath(node, selector.name);
3564 switch (selector.operator) {
3569 switch (selector.value.type) {
3571 return typeof p === 'string' && selector.value.value.test(p);
3574 return "".concat(selector.value.value) === "".concat(p);
3577 return selector.value.value === _typeof(p);
3580 throw new Error("Unknown selector value type: ".concat(selector.value.type));
3583 switch (selector.value.type) {
3585 return !selector.value.value.test(p);
3588 return "".concat(selector.value.value) !== "".concat(p);
3591 return selector.value.value !== _typeof(p);
3594 throw new Error("Unknown selector value type: ".concat(selector.value.type));
3597 return p <= selector.value.value;
3600 return p < selector.value.value;
3603 return p > selector.value.value;
3606 return p >= selector.value.value;
3609 throw new Error("Unknown operator: ".concat(selector.operator));
3613 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);
3616 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);
3619 return matches(node, selector.right, ancestry, options) && nthChild(node, ancestry, function () {
3620 return selector.index.value - 1;
3623 case 'nth-last-child':
3624 return matches(node, selector.right, ancestry, options) && nthChild(node, ancestry, function (length) {
3625 return length - selector.index.value;
3629 switch (selector.name.toLowerCase()) {
3631 if (node.type.slice(-9) === 'Statement') return true;
3632 // fallthrough: interface Declaration <: Statement { }
3635 return node.type.slice(-11) === 'Declaration';
3638 if (node.type.slice(-7) === 'Pattern') return true;
3639 // fallthrough: interface Expression <: Node, Pattern { }
3642 return node.type.slice(-10) === 'Expression' || node.type.slice(-7) === 'Literal' || node.type === 'Identifier' && (ancestry.length === 0 || ancestry[0].type !== 'MetaProperty') || node.type === 'MetaProperty';
3645 return node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' || node.type === 'ArrowFunctionExpression';
3648 throw new Error("Unknown class name: ".concat(selector.name));
3651 throw new Error("Unknown selector type: ".concat(selector.type));
3654 * Get visitor keys of a given node.
3655 * @param {external:AST} node The AST node to get keys.
3656 * @param {ESQueryOptions|undefined} options
3657 * @returns {string[]} Visitor keys of the node.
3661 function getVisitorKeys(node, options) {
3662 var nodeType = node.type;
3664 if (options && options.visitorKeys && options.visitorKeys[nodeType]) {
3665 return options.visitorKeys[nodeType];
3668 if (estraverse.VisitorKeys[nodeType]) {
3669 return estraverse.VisitorKeys[nodeType];
3672 if (options && typeof options.fallback === 'function') {
3673 return options.fallback(node);
3674 } // 'iteration' fallback
3677 return Object.keys(node).filter(function (key) {
3678 return key !== 'type';
3682 * Check whether the given value is an ASTNode or not.
3683 * @param {any} node The value to check.
3684 * @returns {boolean} `true` if the value is an ASTNode.
3688 function isNode(node) {
3689 return node !== null && _typeof(node) === 'object' && typeof node.type === 'string';
3692 * Determines if the given node has a sibling that matches the
3694 * @param {external:AST} node
3695 * @param {SelectorSequenceAST} selector
3696 * @param {external:AST[]} ancestry
3697 * @param {Side} side
3698 * @param {ESQueryOptions|undefined} options
3699 * @returns {boolean}
3703 function sibling(node, selector, ancestry, side, options) {
3704 var _ancestry = _slicedToArray(ancestry, 1),
3705 parent = _ancestry[0];
3711 var keys = getVisitorKeys(parent, options);
3713 var _iterator7 = _createForOfIteratorHelper(keys),
3717 for (_iterator7.s(); !(_step7 = _iterator7.n()).done;) {
3718 var key = _step7.value;
3719 var listProp = parent[key];
3721 if (Array.isArray(listProp)) {
3722 var startIndex = listProp.indexOf(node);
3724 if (startIndex < 0) {
3728 var lowerBound = void 0,
3729 upperBound = void 0;
3731 if (side === LEFT_SIDE) {
3733 upperBound = startIndex;
3735 lowerBound = startIndex + 1;
3736 upperBound = listProp.length;
3739 for (var k = lowerBound; k < upperBound; ++k) {
3740 if (isNode(listProp[k]) && matches(listProp[k], selector, ancestry, options)) {
3755 * Determines if the given node has an adjacent sibling that matches
3756 * the given selector.
3757 * @param {external:AST} node
3758 * @param {SelectorSequenceAST} selector
3759 * @param {external:AST[]} ancestry
3760 * @param {Side} side
3761 * @param {ESQueryOptions|undefined} options
3762 * @returns {boolean}
3766 function adjacent(node, selector, ancestry, side, options) {
3767 var _ancestry2 = _slicedToArray(ancestry, 1),
3768 parent = _ancestry2[0];
3774 var keys = getVisitorKeys(parent, options);
3776 var _iterator8 = _createForOfIteratorHelper(keys),
3780 for (_iterator8.s(); !(_step8 = _iterator8.n()).done;) {
3781 var key = _step8.value;
3782 var listProp = parent[key];
3784 if (Array.isArray(listProp)) {
3785 var idx = listProp.indexOf(node);
3791 if (side === LEFT_SIDE && idx > 0 && isNode(listProp[idx - 1]) && matches(listProp[idx - 1], selector, ancestry, options)) {
3795 if (side === RIGHT_SIDE && idx < listProp.length - 1 && isNode(listProp[idx + 1]) && matches(listProp[idx + 1], selector, ancestry, options)) {
3809 * @callback IndexFunction
3810 * @param {Integer} len Containing list's length
3811 * @returns {Integer}
3815 * Determines if the given node is the nth child, determined by
3816 * `idxFn`, which is given the containing list's length.
3817 * @param {external:AST} node
3818 * @param {external:AST[]} ancestry
3819 * @param {IndexFunction} idxFn
3820 * @param {ESQueryOptions|undefined} options
3821 * @returns {boolean}
3825 function nthChild(node, ancestry, idxFn, options) {
3826 var _ancestry3 = _slicedToArray(ancestry, 1),
3827 parent = _ancestry3[0];
3833 var keys = getVisitorKeys(parent, options);
3835 var _iterator9 = _createForOfIteratorHelper(keys),
3839 for (_iterator9.s(); !(_step9 = _iterator9.n()).done;) {
3840 var key = _step9.value;
3841 var listProp = parent[key];
3843 if (Array.isArray(listProp)) {
3844 var idx = listProp.indexOf(node);
3846 if (idx >= 0 && idx === idxFn(listProp.length)) {
3860 * For each selector node marked as a subject, find the portion of the
3861 * selector that the subject must match.
3862 * @param {SelectorAST} selector
3863 * @param {SelectorAST} [ancestor] Defaults to `selector`
3864 * @returns {SelectorAST[]}
3868 function subjects(selector, ancestor) {
3869 if (selector == null || _typeof(selector) != 'object') {
3873 if (ancestor == null) {
3874 ancestor = selector;
3877 var results = selector.subject ? [ancestor] : [];
3879 for (var _i = 0, _Object$entries = _objectEntries(selector); _i < _Object$entries.length; _i++) {
3880 var _Object$entries$_i = _slicedToArray(_Object$entries[_i], 2),
3881 p = _Object$entries$_i[0],
3882 sel = _Object$entries$_i[1];
3884 results.push.apply(results, _toConsumableArray(subjects(sel, p === 'left' ? sel : ancestor)));
3890 * @callback TraverseVisitor
3891 * @param {?external:AST} node
3892 * @param {?external:AST} parent
3893 * @param {external:AST[]} ancestry
3897 * From a JS AST and a selector AST, collect all JS AST nodes that
3898 * match the selector.
3899 * @param {external:AST} ast
3900 * @param {?SelectorAST} selector
3901 * @param {TraverseVisitor} visitor
3902 * @param {ESQueryOptions} [options]
3903 * @returns {external:AST[]}
3907 function traverse(ast, selector, visitor, options) {
3913 var altSubjects = subjects(selector);
3914 estraverse.traverse(ast, {
3915 enter: function enter(node, parent) {
3916 if (parent != null) {
3917 ancestry.unshift(parent);
3920 if (matches(node, selector, ancestry, options)) {
3921 if (altSubjects.length) {
3922 for (var i = 0, l = altSubjects.length; i < l; ++i) {
3923 if (matches(node, altSubjects[i], ancestry, options)) {
3924 visitor(node, parent, ancestry);
3927 for (var k = 0, m = ancestry.length; k < m; ++k) {
3928 var succeedingAncestry = ancestry.slice(k + 1);
3930 if (matches(ancestry[k], altSubjects[i], succeedingAncestry, options)) {
3931 visitor(ancestry[k], parent, succeedingAncestry);
3936 visitor(node, parent, ancestry);
3940 leave: function leave() {
3943 keys: options && options.visitorKeys,
3944 fallback: options && options.fallback || 'iteration'
3948 * From a JS AST and a selector AST, collect all JS AST nodes that
3949 * match the selector.
3950 * @param {external:AST} ast
3951 * @param {?SelectorAST} selector
3952 * @param {ESQueryOptions} [options]
3953 * @returns {external:AST[]}
3957 function match(ast, selector, options) {
3959 traverse(ast, selector, function (node) {
3965 * Parse a selector string and return its AST.
3966 * @param {string} selector
3967 * @returns {SelectorAST}
3971 function parse(selector) {
3972 return parser.parse(selector);
3975 * Query the code AST using the selector string.
3976 * @param {external:AST} ast
3977 * @param {string} selector
3978 * @param {ESQueryOptions} [options]
3979 * @returns {external:AST[]}
3983 function query(ast, selector, options) {
3984 return match(ast, parse(selector), options);
3987 query.parse = parse;
3988 query.match = match;
3989 query.traverse = traverse;
3990 query.matches = matches;
3991 query.query = query;