astronomical 3.0.4 → 3.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -664,6 +664,42 @@ function createQuerier() {
664
664
  result
665
665
  };
666
666
  }
667
+ function activateDescendant(fnode, state) {
668
+ state.descendant[state.depth + 1].push(fnode);
669
+ fnode.seq = state.seqCounter++;
670
+ const value = fnode.node.value;
671
+ if (fnode.node.attribute) {
672
+ state.descendantOther.push(fnode);
673
+ state.descendantAttr.push(fnode);
674
+ } else if (value == "*") {
675
+ state.descendantOther.push(fnode);
676
+ } else if (value != void 0) {
677
+ let bucket = state.descendantByType.get(value);
678
+ if (!bucket) {
679
+ bucket = [];
680
+ state.descendantByType.set(value, bucket);
681
+ }
682
+ bucket.push(fnode);
683
+ }
684
+ state.descendantActiveCount++;
685
+ }
686
+ function removeFromBucket(arr, fnode) {
687
+ const i = arr.lastIndexOf(fnode);
688
+ if (i >= 0) arr.splice(i, 1);
689
+ }
690
+ function deactivateDescendant(fnode, state) {
691
+ const value = fnode.node.value;
692
+ if (fnode.node.attribute) {
693
+ removeFromBucket(state.descendantOther, fnode);
694
+ removeFromBucket(state.descendantAttr, fnode);
695
+ } else if (value == "*") {
696
+ removeFromBucket(state.descendantOther, fnode);
697
+ } else if (value != void 0) {
698
+ const bucket = state.descendantByType.get(value);
699
+ if (bucket) removeFromBucket(bucket, fnode);
700
+ }
701
+ state.descendantActiveCount--;
702
+ }
667
703
  function addFilterChildrenToState(filter, state) {
668
704
  if ("type" in filter && (filter.type == NodeType.AND || filter.type == NodeType.OR || filter.type == NodeType.EQUALS)) {
669
705
  addFilterChildrenToState(filter.left, state);
@@ -675,7 +711,7 @@ function createQuerier() {
675
711
  }
676
712
  if (filter.node.type == NodeType.DESCENDANT) {
677
713
  log2?.debug("ADDING FILTER DESCENDANT", filter.node);
678
- state.descendant[state.depth + 1].push(filter);
714
+ activateDescendant(filter, state);
679
715
  }
680
716
  }
681
717
  }
@@ -685,7 +721,7 @@ function createQuerier() {
685
721
  if (token.type == NodeType.CHILD) {
686
722
  state.child[state.depth + 1].push(fnode);
687
723
  } else if (token.type == NodeType.DESCENDANT) {
688
- state.descendant[state.depth + 1].push(fnode);
724
+ activateDescendant(fnode, state);
689
725
  }
690
726
  return fnode;
691
727
  }
@@ -704,16 +740,24 @@ function createQuerier() {
704
740
  }
705
741
  function addIfTokenMatch(fnode, path, state) {
706
742
  if (!isMatch(fnode, path)) return;
743
+ addMatch(fnode, path, state);
744
+ }
745
+ function addMatch(fnode, path, state) {
707
746
  state.matches[state.depth].push([fnode, path]);
708
747
  if (fnode.node.filter) {
709
748
  const filter = createFilter(fnode.node.filter, []);
710
749
  const filteredResult = [];
711
750
  const f = { filter, qNode: fnode.node, node: path.node, result: filteredResult };
712
751
  state.filters[state.depth].push(f);
713
- let fmap = state.filtersMap[state.depth].get(fnode.node);
752
+ let fmapContainer = state.filtersMap[state.depth];
753
+ if (!fmapContainer) {
754
+ fmapContainer = /* @__PURE__ */ new Map();
755
+ state.filtersMap[state.depth] = fmapContainer;
756
+ }
757
+ let fmap = fmapContainer.get(fnode.node);
714
758
  if (!fmap) {
715
759
  fmap = [];
716
- state.filtersMap[state.depth].set(fnode.node, fmap);
760
+ fmapContainer.set(fnode.node, fmap);
717
761
  }
718
762
  fmap.push(f);
719
763
  addFilterChildrenToState(filter, state);
@@ -919,7 +963,8 @@ function createQuerier() {
919
963
  function addResultIfTokenMatch(fnode, path, state) {
920
964
  const matchingFilters = [];
921
965
  const filters = [];
922
- const nodeFilters = state.filtersMap[state.depth].get(fnode.node);
966
+ const fmapContainer = state.filtersMap[state.depth];
967
+ const nodeFilters = fmapContainer ? fmapContainer.get(fnode.node) : void 0;
923
968
  if (nodeFilters) {
924
969
  for (let i = 0; i < nodeFilters.length; i++) {
925
970
  const f = nodeFilters[i];
@@ -1017,9 +1062,14 @@ function createQuerier() {
1017
1062
  child: [[], []],
1018
1063
  descendant: [[], []],
1019
1064
  filters: [[], []],
1020
- filtersMap: [/* @__PURE__ */ new Map(), /* @__PURE__ */ new Map()],
1065
+ filtersMap: [void 0, void 0],
1021
1066
  matches: [[]],
1022
- functionCalls: [[]]
1067
+ functionCalls: [[]],
1068
+ descendantByType: /* @__PURE__ */ new Map(),
1069
+ descendantOther: [],
1070
+ descendantAttr: [],
1071
+ descendantActiveCount: 0,
1072
+ seqCounter: 0
1023
1073
  };
1024
1074
  for (const [name, node] of Object.entries(queries)) {
1025
1075
  createFNodeAndAddToState(node, results[name], state);
@@ -1028,12 +1078,8 @@ function createQuerier() {
1028
1078
  for (let i = 0; i < childAtDepth.length; i++) {
1029
1079
  addPrimitiveAttributeIfMatch(childAtDepth[i], root);
1030
1080
  }
1031
- const descendantSlice = state.descendant.slice(0, state.depth + 1);
1032
- for (let i = 0; i < descendantSlice.length; i++) {
1033
- const fnodes = descendantSlice[i];
1034
- for (let j = 0; j < fnodes.length; j++) {
1035
- addPrimitiveAttributeIfMatch(fnodes[j], root);
1036
- }
1081
+ for (let i = 0; i < state.descendantAttr.length; i++) {
1082
+ addPrimitiveAttributeIfMatch(state.descendantAttr[i], root);
1037
1083
  }
1038
1084
  traverse(root.node, {
1039
1085
  enter(path, state2) {
@@ -1041,15 +1087,31 @@ function createQuerier() {
1041
1087
  state2.child.push([]);
1042
1088
  state2.descendant.push([]);
1043
1089
  state2.filters.push([]);
1044
- state2.filtersMap.push(/* @__PURE__ */ new Map());
1090
+ state2.filtersMap.push(void 0);
1045
1091
  state2.matches.push([]);
1046
1092
  state2.functionCalls.push([]);
1047
1093
  for (const fnode of state2.child[state2.depth]) {
1048
1094
  addIfTokenMatch(fnode, path, state2);
1049
1095
  }
1050
- for (const fnodes of state2.descendant.slice(0, state2.depth + 1)) {
1051
- for (const fnode of fnodes) {
1052
- addIfTokenMatch(fnode, path, state2);
1096
+ const bucket = state2.descendantByType.get(path.node.type);
1097
+ const other = state2.descendantOther;
1098
+ const bucketLen = bucket ? bucket.length : 0;
1099
+ const otherLen = other.length;
1100
+ if (otherLen == 0) {
1101
+ for (let i = 0; i < bucketLen; i++) {
1102
+ addMatch(bucket[i], path, state2);
1103
+ }
1104
+ } else if (bucketLen == 0) {
1105
+ for (let i = 0; i < otherLen; i++) {
1106
+ addIfTokenMatch(other[i], path, state2);
1107
+ }
1108
+ } else {
1109
+ const cands = [];
1110
+ for (let i = 0; i < bucketLen; i++) cands.push(bucket[i]);
1111
+ for (let i = 0; i < otherLen; i++) cands.push(other[i]);
1112
+ cands.sort((a, b) => a.seq - b.seq);
1113
+ for (let i = 0; i < cands.length; i++) {
1114
+ addIfTokenMatch(cands[i], path, state2);
1053
1115
  }
1054
1116
  }
1055
1117
  },
@@ -1059,16 +1121,17 @@ function createQuerier() {
1059
1121
  for (let i = 0; i < childAtDepthPlusOne.length; i++) {
1060
1122
  addPrimitiveAttributeIfMatch(childAtDepthPlusOne[i], path);
1061
1123
  }
1062
- for (let i = 0; i < state2.descendant.length; i++) {
1063
- const fnodes = state2.descendant[i];
1064
- for (let j = 0; j < fnodes.length; j++) {
1065
- addPrimitiveAttributeIfMatch(fnodes[j], path);
1066
- }
1124
+ for (let i = 0; i < state2.descendantAttr.length; i++) {
1125
+ addPrimitiveAttributeIfMatch(state2.descendantAttr[i], path);
1067
1126
  }
1068
1127
  const matchesAtDepth = state2.matches[state2.depth];
1069
1128
  for (let i = 0; i < matchesAtDepth.length; i++) {
1070
1129
  addResultIfTokenMatch(matchesAtDepth[i][0], matchesAtDepth[i][1], state2);
1071
1130
  }
1131
+ const leavingDescendants = state2.descendant[state2.descendant.length - 1];
1132
+ for (let i = 0; i < leavingDescendants.length; i++) {
1133
+ deactivateDescendant(leavingDescendants[i], state2);
1134
+ }
1072
1135
  state2.depth--;
1073
1136
  state2.child.pop();
1074
1137
  state2.descendant.pop();
@@ -1120,16 +1183,17 @@ function multiQuery(code, namedQueries, returnAST) {
1120
1183
  }
1121
1184
  function parseSource(source, optimize = true) {
1122
1185
  const parsingOptions = optimize ? { loc: false, ranges: false } : { loc: true, ranges: true };
1186
+ const base = { next: true, validateRegex: false, ...parsingOptions };
1123
1187
  try {
1124
- return (0, import_meriyah.parseScript)(source, { module: true, next: true, ...parsingOptions });
1188
+ return (0, import_meriyah.parse)(source, { ...base, sourceType: "module" });
1125
1189
  } catch {
1126
1190
  try {
1127
- return (0, import_meriyah.parseScript)(source, { module: false, next: true, ...parsingOptions, webcompat: true });
1191
+ return (0, import_meriyah.parse)(source, { ...base, sourceType: "script", webcompat: true });
1128
1192
  } catch {
1129
1193
  try {
1130
- return (0, import_meriyah.parseScript)(source, { module: true, next: true, ...parsingOptions, jsx: true });
1194
+ return (0, import_meriyah.parse)(source, { ...base, sourceType: "module", jsx: true });
1131
1195
  } catch {
1132
- return (0, import_meriyah.parseScript)(source, { module: false, next: true, ...parsingOptions, webcompat: true, jsx: true });
1196
+ return (0, import_meriyah.parse)(source, { ...base, sourceType: "script", webcompat: true, jsx: true });
1133
1197
  }
1134
1198
  }
1135
1199
  }
@@ -1325,7 +1389,7 @@ function createTraverser() {
1325
1389
  registerBindings(stack, nodePath.scopeId, nodePath.functionScopeId);
1326
1390
  }
1327
1391
  const stateTyped = state;
1328
- const hasDescendantQueries = stateTyped.descendant && stateTyped.descendant.some((arr) => arr.length > 0);
1392
+ const hasDescendantQueries = stateTyped.descendantActiveCount > 0;
1329
1393
  const hasChildQueriesAtNextDepth = stateTyped.child && stateTyped.child[stateTyped.depth + 1] && stateTyped.child[stateTyped.depth + 1].length > 0;
1330
1394
  if (!hasDescendantQueries && !hasChildQueriesAtNextDepth) {
1331
1395
  return;
@@ -1333,17 +1397,18 @@ function createTraverser() {
1333
1397
  for (let keyIdx = 0; keyIdx < keys.length; keyIdx++) {
1334
1398
  const key = keys[keyIdx];
1335
1399
  const childNodes = node[key];
1336
- const children = Array.isArray(childNodes) ? childNodes : childNodes ? [childNodes] : [];
1337
- const nodePaths = [];
1338
- for (let i = 0; i < children.length; i++) {
1339
- const child = children[i];
1340
- if (isNode(child)) {
1341
- const childPath = createNodePath(child, Array.isArray(childNodes) ? i : key, key, nodePath.scopeId, nodePath.functionScopeId, nodePath);
1342
- nodePaths.push(childPath);
1400
+ if (childNodes == void 0) continue;
1401
+ if (Array.isArray(childNodes)) {
1402
+ for (let i = 0; i < childNodes.length; i++) {
1403
+ const child = childNodes[i];
1404
+ if (!isNode(child)) continue;
1405
+ const childPath = createNodePath(child, i, key, nodePath.scopeId, nodePath.functionScopeId, nodePath);
1406
+ visitor.enter(childPath, state);
1407
+ traverseInner(childPath.node, visitor, nodePath.scopeId, nodePath.functionScopeId, state, childPath);
1408
+ visitor.exit(childPath, state);
1343
1409
  }
1344
- }
1345
- for (let i = 0; i < nodePaths.length; i++) {
1346
- const childPath = nodePaths[i];
1410
+ } else if (isNode(childNodes)) {
1411
+ const childPath = createNodePath(childNodes, key, key, nodePath.scopeId, nodePath.functionScopeId, nodePath);
1347
1412
  visitor.enter(childPath, state);
1348
1413
  traverseInner(childPath.node, visitor, nodePath.scopeId, nodePath.functionScopeId, state, childPath);
1349
1414
  visitor.exit(childPath, state);