astro-eslint-parser 1.0.1 → 1.0.2

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.mjs CHANGED
@@ -484,8 +484,15 @@ function isTSESLintParserObject(value) {
484
484
  }
485
485
 
486
486
  // src/parser/script.ts
487
- import { analyze as analyzeForTypeScript } from "@typescript-eslint/scope-manager";
488
- import { analyze as analyzeForEcmaScript } from "eslint-scope";
487
+ import { AST_NODE_TYPES } from "@typescript-eslint/types";
488
+ import {
489
+ analyze as analyzeForTypeScript,
490
+ Reference
491
+ } from "@typescript-eslint/scope-manager";
492
+ import {
493
+ Referencer as BaseReferencer,
494
+ ScopeManager
495
+ } from "eslint-scope";
489
496
 
490
497
  // src/traverse.ts
491
498
  function fallbackKeysFilter(key) {
@@ -528,6 +535,302 @@ function traverseNodes(node, visitor) {
528
535
  traverse(node, null, visitor);
529
536
  }
530
537
 
538
+ // src/parser/scope/index.ts
539
+ import {
540
+ Reference as ReferenceClass,
541
+ Variable as VariableClass
542
+ } from "@typescript-eslint/scope-manager";
543
+
544
+ // src/util/index.ts
545
+ function sortedLastIndex(array, compare) {
546
+ let lower = 0;
547
+ let upper = array.length;
548
+ while (lower < upper) {
549
+ const mid = Math.floor(lower + (upper - lower) / 2);
550
+ const target = compare(array[mid]);
551
+ if (target < 0) {
552
+ lower = mid + 1;
553
+ } else if (target > 0) {
554
+ upper = mid;
555
+ } else {
556
+ return mid + 1;
557
+ }
558
+ }
559
+ return upper;
560
+ }
561
+ function addElementToSortedArray(array, element, compare) {
562
+ const index = sortedLastIndex(array, (target) => compare(target, element));
563
+ array.splice(index, 0, element);
564
+ }
565
+ function addElementsToSortedArray(array, elements, compare) {
566
+ if (!elements.length) {
567
+ return;
568
+ }
569
+ let last = elements[0];
570
+ let index = sortedLastIndex(array, (target) => compare(target, last));
571
+ for (const element of elements) {
572
+ if (compare(last, element) > 0) {
573
+ index = sortedLastIndex(array, (target) => compare(target, element));
574
+ }
575
+ let e = array[index];
576
+ while (e && compare(e, element) <= 0) {
577
+ e = array[++index];
578
+ }
579
+ array.splice(index, 0, element);
580
+ last = element;
581
+ }
582
+ }
583
+
584
+ // src/parser/scope/index.ts
585
+ var READ_FLAG = 1;
586
+ var WRITE_FLAG = 2;
587
+ var READ_WRITE_FLAG = 3;
588
+ var REFERENCE_TYPE_VALUE_FLAG = 1;
589
+ var REFERENCE_TYPE_TYPE_FLAG = 2;
590
+ function getProgramScope(scopeManager) {
591
+ const globalScope = scopeManager.globalScope;
592
+ return globalScope.childScopes.find((s) => s.type === "module") || globalScope;
593
+ }
594
+ function removeAllScopeAndVariableAndReference(target, info) {
595
+ const targetScopes = /* @__PURE__ */ new Set();
596
+ traverseNodes(target, {
597
+ visitorKeys: info.visitorKeys,
598
+ enterNode(node) {
599
+ const scope = info.scopeManager.acquire(node);
600
+ if (scope) {
601
+ targetScopes.add(scope);
602
+ return;
603
+ }
604
+ if (node.type === "Identifier") {
605
+ let scope2 = getInnermostScopeFromNode(info.scopeManager, node);
606
+ while (scope2 && scope2.block.type !== "Program" && target.range[0] <= scope2.block.range[0] && scope2.block.range[1] <= target.range[1]) {
607
+ scope2 = scope2.upper;
608
+ }
609
+ if (targetScopes.has(scope2)) {
610
+ return;
611
+ }
612
+ removeIdentifierVariable(node, scope2);
613
+ removeIdentifierReference(node, scope2);
614
+ }
615
+ },
616
+ leaveNode() {
617
+ }
618
+ });
619
+ for (const scope of targetScopes) {
620
+ removeScope(info.scopeManager, scope);
621
+ }
622
+ }
623
+ function addVirtualReference(node, variable, scope, status) {
624
+ const reference = new ReferenceClass(
625
+ node,
626
+ scope,
627
+ status.write && status.read ? READ_WRITE_FLAG : status.write ? WRITE_FLAG : READ_FLAG,
628
+ void 0,
629
+ // writeExpr
630
+ void 0,
631
+ // maybeImplicitGlobal
632
+ void 0,
633
+ // init
634
+ status.typeRef ? REFERENCE_TYPE_TYPE_FLAG : REFERENCE_TYPE_VALUE_FLAG
635
+ );
636
+ reference.astroVirtualReference = true;
637
+ addReference(variable.references, reference);
638
+ reference.resolved = variable;
639
+ if (status.forceUsed) {
640
+ variable.eslintUsed = true;
641
+ }
642
+ return reference;
643
+ }
644
+ function addGlobalVariable(reference, scopeManager) {
645
+ const globalScope = scopeManager.globalScope;
646
+ const name2 = reference.identifier.name;
647
+ let variable = globalScope.set.get(name2);
648
+ if (!variable) {
649
+ variable = new VariableClass(name2, globalScope);
650
+ globalScope.variables.push(variable);
651
+ globalScope.set.set(name2, variable);
652
+ }
653
+ reference.resolved = variable;
654
+ variable.references.push(reference);
655
+ return variable;
656
+ }
657
+ function removeReferenceFromThrough(reference, baseScope) {
658
+ const variable = reference.resolved;
659
+ const name2 = reference.identifier.name;
660
+ let scope = baseScope;
661
+ while (scope) {
662
+ for (const ref of [...scope.through]) {
663
+ if (reference === ref) {
664
+ scope.through.splice(scope.through.indexOf(ref), 1);
665
+ } else if (ref.identifier.name === name2) {
666
+ ref.resolved = variable;
667
+ if (!variable.references.includes(ref)) {
668
+ addReference(variable.references, ref);
669
+ }
670
+ scope.through.splice(scope.through.indexOf(ref), 1);
671
+ }
672
+ }
673
+ scope = scope.upper;
674
+ }
675
+ }
676
+ function removeScope(scopeManager, scope) {
677
+ for (const childScope of scope.childScopes) {
678
+ removeScope(scopeManager, childScope);
679
+ }
680
+ while (scope.references[0]) {
681
+ removeReference(scope.references[0], scope);
682
+ }
683
+ const upper = scope.upper;
684
+ if (upper) {
685
+ const index2 = upper.childScopes.indexOf(scope);
686
+ if (index2 >= 0) {
687
+ upper.childScopes.splice(index2, 1);
688
+ }
689
+ }
690
+ const index = scopeManager.scopes.indexOf(scope);
691
+ if (index >= 0) {
692
+ scopeManager.scopes.splice(index, 1);
693
+ }
694
+ }
695
+ function removeReference(reference, baseScope) {
696
+ if (reference.resolved) {
697
+ if (reference.resolved.defs.some((d) => d.name === reference.identifier)) {
698
+ const varIndex = baseScope.variables.indexOf(reference.resolved);
699
+ if (varIndex >= 0) {
700
+ baseScope.variables.splice(varIndex, 1);
701
+ }
702
+ const name2 = reference.identifier.name;
703
+ if (reference.resolved === baseScope.set.get(name2)) {
704
+ baseScope.set.delete(name2);
705
+ }
706
+ } else {
707
+ const refIndex = reference.resolved.references.indexOf(reference);
708
+ if (refIndex >= 0) {
709
+ reference.resolved.references.splice(refIndex, 1);
710
+ }
711
+ }
712
+ }
713
+ let scope = baseScope;
714
+ while (scope) {
715
+ const refIndex = scope.references.indexOf(reference);
716
+ if (refIndex >= 0) {
717
+ scope.references.splice(refIndex, 1);
718
+ }
719
+ const throughIndex = scope.through.indexOf(reference);
720
+ if (throughIndex >= 0) {
721
+ scope.through.splice(throughIndex, 1);
722
+ }
723
+ scope = scope.upper;
724
+ }
725
+ }
726
+ function removeIdentifierVariable(node, scope) {
727
+ for (let varIndex = 0; varIndex < scope.variables.length; varIndex++) {
728
+ const variable = scope.variables[varIndex];
729
+ const defIndex = variable.defs.findIndex((def) => def.name === node);
730
+ if (defIndex < 0) {
731
+ continue;
732
+ }
733
+ variable.defs.splice(defIndex, 1);
734
+ if (variable.defs.length === 0) {
735
+ referencesToThrough(variable.references, scope);
736
+ variable.references.forEach((r) => {
737
+ if (r.init)
738
+ r.init = false;
739
+ r.resolved = null;
740
+ });
741
+ scope.variables.splice(varIndex, 1);
742
+ const name2 = node.name;
743
+ if (variable === scope.set.get(name2)) {
744
+ scope.set.delete(name2);
745
+ }
746
+ } else {
747
+ const idIndex = variable.identifiers.indexOf(node);
748
+ if (idIndex >= 0) {
749
+ variable.identifiers.splice(idIndex, 1);
750
+ }
751
+ }
752
+ return;
753
+ }
754
+ }
755
+ function removeIdentifierReference(node, scope) {
756
+ const reference = scope.references.find((ref) => ref.identifier === node);
757
+ if (reference) {
758
+ removeReference(reference, scope);
759
+ return true;
760
+ }
761
+ const location = node.range[0];
762
+ const pendingScopes = [];
763
+ for (const childScope of scope.childScopes) {
764
+ const range = childScope.block.range;
765
+ if (range[0] <= location && location < range[1]) {
766
+ if (removeIdentifierReference(node, childScope)) {
767
+ return true;
768
+ }
769
+ } else {
770
+ pendingScopes.push(childScope);
771
+ }
772
+ }
773
+ for (const childScope of pendingScopes) {
774
+ if (removeIdentifierReference(node, childScope)) {
775
+ return true;
776
+ }
777
+ }
778
+ return false;
779
+ }
780
+ function getInnermostScopeFromNode(scopeManager, currentNode) {
781
+ return getInnermostScope(
782
+ getScopeFromNode(scopeManager, currentNode),
783
+ currentNode
784
+ );
785
+ }
786
+ function getScopeFromNode(scopeManager, currentNode) {
787
+ let node = currentNode;
788
+ for (; node; node = node.parent || null) {
789
+ const scope = scopeManager.acquire(node, false);
790
+ if (scope) {
791
+ if (scope.type === "function-expression-name") {
792
+ return scope.childScopes[0];
793
+ }
794
+ if (scope.type === "global" && node.type === "Program" && node.sourceType === "module") {
795
+ return scope.childScopes.find((s) => s.type === "module") || scope;
796
+ }
797
+ return scope;
798
+ }
799
+ }
800
+ const global = scopeManager.globalScope;
801
+ return global;
802
+ }
803
+ function getInnermostScope(initialScope, node) {
804
+ for (const childScope of initialScope.childScopes) {
805
+ const range = childScope.block.range;
806
+ if (range[0] <= node.range[0] && node.range[1] <= range[1]) {
807
+ return getInnermostScope(childScope, node);
808
+ }
809
+ }
810
+ return initialScope;
811
+ }
812
+ function referencesToThrough(references, baseScope) {
813
+ let scope = baseScope;
814
+ while (scope) {
815
+ addAllReferences(scope.through, references);
816
+ scope = scope.upper;
817
+ }
818
+ }
819
+ function addAllReferences(list, elements) {
820
+ addElementsToSortedArray(
821
+ list,
822
+ elements,
823
+ (a, b) => a.identifier.range[0] - b.identifier.range[0]
824
+ );
825
+ }
826
+ function addReference(list, reference) {
827
+ addElementToSortedArray(
828
+ list,
829
+ reference,
830
+ (a, b) => a.identifier.range[0] - b.identifier.range[0]
831
+ );
832
+ }
833
+
531
834
  // src/parser/script.ts
532
835
  function parseScript(code, ctx, parserOptionsCtx) {
533
836
  const result = parseScriptInternal(code, ctx, parserOptionsCtx);
@@ -586,6 +889,73 @@ ${code}`
586
889
  patchResult?.terminate();
587
890
  }
588
891
  }
892
+ var Referencer = class extends BaseReferencer {
893
+ JSXAttribute(node) {
894
+ this.visit(node.value);
895
+ }
896
+ JSXClosingElement() {
897
+ }
898
+ JSXFragment(node) {
899
+ this.visitChildren(node);
900
+ }
901
+ JSXIdentifier(node) {
902
+ const scope = this.currentScope();
903
+ const ref = new Reference(
904
+ node,
905
+ scope,
906
+ READ_FLAG,
907
+ void 0,
908
+ void 0,
909
+ false,
910
+ REFERENCE_TYPE_VALUE_FLAG
911
+ );
912
+ scope.references.push(ref);
913
+ scope.__left.push(ref);
914
+ }
915
+ JSXMemberExpression(node) {
916
+ if (node.object.type !== AST_NODE_TYPES.JSXIdentifier) {
917
+ this.visit(node.object);
918
+ } else {
919
+ if (node.object.name !== "this") {
920
+ this.visit(node.object);
921
+ }
922
+ }
923
+ }
924
+ JSXOpeningElement(node) {
925
+ if (node.name.type === AST_NODE_TYPES.JSXIdentifier) {
926
+ if (node.name.name[0].toUpperCase() === node.name.name[0] || node.name.name === "this") {
927
+ this.visit(node.name);
928
+ }
929
+ } else {
930
+ this.visit(node.name);
931
+ }
932
+ for (const attr of node.attributes) {
933
+ this.visit(attr);
934
+ }
935
+ }
936
+ };
937
+ function analyzeForEcmaScript(tree, providedOptions) {
938
+ const options = Object.assign(
939
+ {
940
+ optimistic: false,
941
+ nodejsScope: false,
942
+ impliedStrict: false,
943
+ sourceType: "script",
944
+ // one of ['script', 'module', 'commonjs']
945
+ ecmaVersion: 5,
946
+ childVisitorKeys: null,
947
+ fallback: "iteration"
948
+ },
949
+ providedOptions
950
+ );
951
+ const scopeManager = new ScopeManager(
952
+ // @ts-expect-error -- No typings
953
+ options
954
+ );
955
+ const referencer = new Referencer(options, scopeManager);
956
+ referencer.visit(tree);
957
+ return scopeManager;
958
+ }
589
959
 
590
960
  // src/parser/sort.ts
591
961
  function sort(tokens) {
@@ -598,7 +968,7 @@ function sort(tokens) {
598
968
  }
599
969
 
600
970
  // src/parser/process-template.ts
601
- import { AST_TOKEN_TYPES, AST_NODE_TYPES } from "@typescript-eslint/types";
971
+ import { AST_TOKEN_TYPES, AST_NODE_TYPES as AST_NODE_TYPES2 } from "@typescript-eslint/types";
602
972
 
603
973
  // src/astro/index.ts
604
974
  import {
@@ -1246,7 +1616,7 @@ function processTemplate(ctx, resultTemplate) {
1246
1616
  script.appendVirtualScript("<>");
1247
1617
  fragmentOpened = true;
1248
1618
  script.restoreContext.addRestoreNodeProcess((scriptNode, { result }) => {
1249
- if (scriptNode.type === AST_NODE_TYPES.ExpressionStatement && scriptNode.expression.type === AST_NODE_TYPES.JSXFragment && scriptNode.range[0] === startOffset && result.ast.body.includes(scriptNode)) {
1619
+ if (scriptNode.type === AST_NODE_TYPES2.ExpressionStatement && scriptNode.expression.type === AST_NODE_TYPES2.JSXFragment && scriptNode.range[0] === startOffset && result.ast.body.includes(scriptNode)) {
1250
1620
  const index = result.ast.body.indexOf(scriptNode);
1251
1621
  const rootFragment = result.ast.body[index] = scriptNode.expression;
1252
1622
  delete rootFragment.closingFragment;
@@ -1284,7 +1654,7 @@ function processTemplate(ctx, resultTemplate) {
1284
1654
  (_scriptNode, { result }) => {
1285
1655
  for (let index = 0; index < result.ast.body.length; index++) {
1286
1656
  const st = result.ast.body[index];
1287
- if (st.type === AST_NODE_TYPES.EmptyStatement) {
1657
+ if (st.type === AST_NODE_TYPES2.EmptyStatement) {
1288
1658
  if (st.range[0] === scriptEnd && st.range[1] === scriptEnd) {
1289
1659
  result.ast.body.splice(index, 1);
1290
1660
  break;
@@ -1316,7 +1686,7 @@ function processTemplate(ctx, resultTemplate) {
1316
1686
  script.appendOriginal(start2);
1317
1687
  script.appendVirtualScript("<>");
1318
1688
  script.restoreContext.addRestoreNodeProcess((scriptNode) => {
1319
- if (scriptNode.range[0] === start2 && scriptNode.type === AST_NODE_TYPES.JSXFragment) {
1689
+ if (scriptNode.range[0] === start2 && scriptNode.type === AST_NODE_TYPES2.JSXFragment) {
1320
1690
  delete scriptNode.openingFragment;
1321
1691
  delete scriptNode.closingFragment;
1322
1692
  const fragmentNode = scriptNode;
@@ -1358,7 +1728,7 @@ function processTemplate(ctx, resultTemplate) {
1358
1728
  script.appendVirtualScript('"');
1359
1729
  script.restoreContext.addRestoreNodeProcess(
1360
1730
  (scriptNode, context) => {
1361
- if (scriptNode.type === AST_NODE_TYPES.JSXAttribute && scriptNode.range[0] === attrStart) {
1731
+ if (scriptNode.type === AST_NODE_TYPES2.JSXAttribute && scriptNode.range[0] === attrStart) {
1362
1732
  const attrNode = scriptNode;
1363
1733
  if (attrNode.value?.type === "Literal" && typeof attrNode.value.value === "string") {
1364
1734
  const raw = ctx.code.slice(start2, end);
@@ -1377,7 +1747,7 @@ function processTemplate(ctx, resultTemplate) {
1377
1747
  const jsxName = /[\s"'[\]{}]/u.test(attr.name) ? generateUniqueId(attr.name) : attr.name;
1378
1748
  script.appendVirtualScript(`${jsxName}=`);
1379
1749
  script.restoreContext.addRestoreNodeProcess((scriptNode) => {
1380
- if (scriptNode.type === AST_NODE_TYPES.JSXAttribute && scriptNode.range[0] === start2) {
1750
+ if (scriptNode.type === AST_NODE_TYPES2.JSXAttribute && scriptNode.range[0] === start2) {
1381
1751
  const attrNode = scriptNode;
1382
1752
  attrNode.type = "AstroShorthandAttribute";
1383
1753
  const locs = ctx.getLocations(
@@ -1401,7 +1771,7 @@ function processTemplate(ctx, resultTemplate) {
1401
1771
  script.appendOriginal(end);
1402
1772
  script.appendVirtualScript("}");
1403
1773
  script.restoreContext.addRestoreNodeProcess((scriptNode) => {
1404
- if (scriptNode.type === AST_NODE_TYPES.JSXAttribute && scriptNode.range[0] === attrStart) {
1774
+ if (scriptNode.type === AST_NODE_TYPES2.JSXAttribute && scriptNode.range[0] === attrStart) {
1405
1775
  const attrNode = scriptNode;
1406
1776
  attrNode.type = "AstroTemplateLiteralAttribute";
1407
1777
  return true;
@@ -1423,7 +1793,7 @@ function processTemplate(ctx, resultTemplate) {
1423
1793
  script.appendOriginal(start2);
1424
1794
  script.skipOriginalOffset(text.value.length);
1425
1795
  script.restoreContext.addRestoreNodeProcess((scriptNode) => {
1426
- if (scriptNode.type === AST_NODE_TYPES.JSXElement && scriptNode.range[0] === styleNodeStart) {
1796
+ if (scriptNode.type === AST_NODE_TYPES2.JSXElement && scriptNode.range[0] === styleNodeStart) {
1427
1797
  const textNode = {
1428
1798
  type: "AstroRawText",
1429
1799
  value: text.value,
@@ -1455,7 +1825,7 @@ function processTemplate(ctx, resultTemplate) {
1455
1825
  script.skipOriginalOffset(length - 2);
1456
1826
  script.appendOriginal(end);
1457
1827
  script.restoreContext.addRestoreNodeProcess((scriptNode, context) => {
1458
- if (scriptNode.range[0] === start && scriptNode.type === AST_NODE_TYPES.JSXFragment) {
1828
+ if (scriptNode.range[0] === start && scriptNode.type === AST_NODE_TYPES2.JSXFragment) {
1459
1829
  delete scriptNode.children;
1460
1830
  delete scriptNode.openingFragment;
1461
1831
  delete scriptNode.closingFragment;
@@ -1490,7 +1860,7 @@ function processTemplate(ctx, resultTemplate) {
1490
1860
  script.skipOriginalOffset(length - 2);
1491
1861
  script.appendOriginal(end);
1492
1862
  script.restoreContext.addRestoreNodeProcess((scriptNode, context) => {
1493
- if (scriptNode.range[0] === start && scriptNode.type === AST_NODE_TYPES.JSXFragment) {
1863
+ if (scriptNode.range[0] === start && scriptNode.type === AST_NODE_TYPES2.JSXFragment) {
1494
1864
  delete scriptNode.children;
1495
1865
  delete scriptNode.openingFragment;
1496
1866
  delete scriptNode.closingFragment;
@@ -1531,7 +1901,7 @@ function processTemplate(ctx, resultTemplate) {
1531
1901
  script.restoreContext.addRestoreNodeProcess(
1532
1902
  (scriptNode, context) => {
1533
1903
  const parent2 = context.getParent(scriptNode);
1534
- if (scriptNode.range[0] === offset && scriptNode.type === AST_NODE_TYPES.JSXClosingElement && parent2.type === AST_NODE_TYPES.JSXElement) {
1904
+ if (scriptNode.range[0] === offset && scriptNode.type === AST_NODE_TYPES2.JSXClosingElement && parent2.type === AST_NODE_TYPES2.JSXElement) {
1535
1905
  parent2.closingElement = null;
1536
1906
  return true;
1537
1907
  }
@@ -1607,13 +1977,13 @@ function processTemplate(ctx, resultTemplate) {
1607
1977
  ]);
1608
1978
  }
1609
1979
  script.restoreContext.addRestoreNodeProcess((scriptNode, context) => {
1610
- if (scriptNode.type === AST_NODE_TYPES.JSXAttribute && scriptNode.range[0] === targetIndex) {
1980
+ if (scriptNode.type === AST_NODE_TYPES2.JSXAttribute && scriptNode.range[0] === targetIndex) {
1611
1981
  const baseNameNode = scriptNode.name;
1612
1982
  if (colonOffset != null) {
1613
1983
  const nameNode = baseNameNode;
1614
- nameNode.type = AST_NODE_TYPES.JSXNamespacedName;
1984
+ nameNode.type = AST_NODE_TYPES2.JSXNamespacedName;
1615
1985
  nameNode.namespace = {
1616
- type: AST_NODE_TYPES.JSXIdentifier,
1986
+ type: AST_NODE_TYPES2.JSXIdentifier,
1617
1987
  name: attr.name.slice(0, colonOffset),
1618
1988
  ...ctx.getLocations(
1619
1989
  baseNameNode.range[0],
@@ -1621,7 +1991,7 @@ function processTemplate(ctx, resultTemplate) {
1621
1991
  )
1622
1992
  };
1623
1993
  nameNode.name = {
1624
- type: AST_NODE_TYPES.JSXIdentifier,
1994
+ type: AST_NODE_TYPES2.JSXIdentifier,
1625
1995
  name: attr.name.slice(colonOffset + 1),
1626
1996
  ...ctx.getLocations(
1627
1997
  baseNameNode.range[0] + colonOffset + 1,
@@ -1632,7 +2002,7 @@ function processTemplate(ctx, resultTemplate) {
1632
2002
  nameNode.namespace.parent = nameNode;
1633
2003
  nameNode.name.parent = nameNode;
1634
2004
  } else {
1635
- if (baseNameNode.type === AST_NODE_TYPES.JSXIdentifier) {
2005
+ if (baseNameNode.type === AST_NODE_TYPES2.JSXIdentifier) {
1636
2006
  const nameNode = baseNameNode;
1637
2007
  nameNode.name = attr.name;
1638
2008
  scriptNode.name = nameNode;
@@ -1664,48 +2034,8 @@ function processTemplate(ctx, resultTemplate) {
1664
2034
  while (usedUniqueIds.has(candidate) || ctx.code.includes(candidate)) {
1665
2035
  candidate = `$_${base.replace(/\W/g, "_")}${uniqueIdSeq++}`;
1666
2036
  }
1667
- usedUniqueIds.add(candidate);
1668
- return candidate;
1669
- }
1670
- }
1671
-
1672
- // src/util/index.ts
1673
- function sortedLastIndex(array, compare) {
1674
- let lower = 0;
1675
- let upper = array.length;
1676
- while (lower < upper) {
1677
- const mid = Math.floor(lower + (upper - lower) / 2);
1678
- const target = compare(array[mid]);
1679
- if (target < 0) {
1680
- lower = mid + 1;
1681
- } else if (target > 0) {
1682
- upper = mid;
1683
- } else {
1684
- return mid + 1;
1685
- }
1686
- }
1687
- return upper;
1688
- }
1689
- function addElementToSortedArray(array, element, compare) {
1690
- const index = sortedLastIndex(array, (target) => compare(target, element));
1691
- array.splice(index, 0, element);
1692
- }
1693
- function addElementsToSortedArray(array, elements, compare) {
1694
- if (!elements.length) {
1695
- return;
1696
- }
1697
- let last = elements[0];
1698
- let index = sortedLastIndex(array, (target) => compare(target, last));
1699
- for (const element of elements) {
1700
- if (compare(last, element) > 0) {
1701
- index = sortedLastIndex(array, (target) => compare(target, element));
1702
- }
1703
- let e = array[index];
1704
- while (e && compare(e, element) <= 0) {
1705
- e = array[++index];
1706
- }
1707
- array.splice(index, 0, element);
1708
- last = element;
2037
+ usedUniqueIds.add(candidate);
2038
+ return candidate;
1709
2039
  }
1710
2040
  }
1711
2041
 
@@ -2337,260 +2667,6 @@ var ParserOptionsContext = class {
2337
2667
  }
2338
2668
  };
2339
2669
 
2340
- // src/parser/scope/index.ts
2341
- import {
2342
- Reference as ReferenceClass,
2343
- Variable as VariableClass
2344
- } from "@typescript-eslint/scope-manager";
2345
- var READ_FLAG = 1;
2346
- var WRITE_FLAG = 2;
2347
- var READ_WRITE_FLAG = 3;
2348
- var REFERENCE_TYPE_VALUE_FLAG = 1;
2349
- var REFERENCE_TYPE_TYPE_FLAG = 2;
2350
- function getProgramScope(scopeManager) {
2351
- const globalScope = scopeManager.globalScope;
2352
- return globalScope.childScopes.find((s) => s.type === "module") || globalScope;
2353
- }
2354
- function removeAllScopeAndVariableAndReference(target, info) {
2355
- const targetScopes = /* @__PURE__ */ new Set();
2356
- traverseNodes(target, {
2357
- visitorKeys: info.visitorKeys,
2358
- enterNode(node) {
2359
- const scope = info.scopeManager.acquire(node);
2360
- if (scope) {
2361
- targetScopes.add(scope);
2362
- return;
2363
- }
2364
- if (node.type === "Identifier") {
2365
- let scope2 = getInnermostScopeFromNode(info.scopeManager, node);
2366
- while (scope2 && scope2.block.type !== "Program" && target.range[0] <= scope2.block.range[0] && scope2.block.range[1] <= target.range[1]) {
2367
- scope2 = scope2.upper;
2368
- }
2369
- if (targetScopes.has(scope2)) {
2370
- return;
2371
- }
2372
- removeIdentifierVariable(node, scope2);
2373
- removeIdentifierReference(node, scope2);
2374
- }
2375
- },
2376
- leaveNode() {
2377
- }
2378
- });
2379
- for (const scope of targetScopes) {
2380
- removeScope(info.scopeManager, scope);
2381
- }
2382
- }
2383
- function addVirtualReference(node, variable, scope, status) {
2384
- const reference = new ReferenceClass(
2385
- node,
2386
- scope,
2387
- status.write && status.read ? READ_WRITE_FLAG : status.write ? WRITE_FLAG : READ_FLAG,
2388
- void 0,
2389
- // writeExpr
2390
- void 0,
2391
- // maybeImplicitGlobal
2392
- void 0,
2393
- // init
2394
- status.typeRef ? REFERENCE_TYPE_TYPE_FLAG : REFERENCE_TYPE_VALUE_FLAG
2395
- );
2396
- reference.astroVirtualReference = true;
2397
- addReference(variable.references, reference);
2398
- reference.resolved = variable;
2399
- if (status.forceUsed) {
2400
- variable.eslintUsed = true;
2401
- }
2402
- return reference;
2403
- }
2404
- function addGlobalVariable(reference, scopeManager) {
2405
- const globalScope = scopeManager.globalScope;
2406
- const name2 = reference.identifier.name;
2407
- let variable = globalScope.set.get(name2);
2408
- if (!variable) {
2409
- variable = new VariableClass(name2, globalScope);
2410
- globalScope.variables.push(variable);
2411
- globalScope.set.set(name2, variable);
2412
- }
2413
- reference.resolved = variable;
2414
- variable.references.push(reference);
2415
- return variable;
2416
- }
2417
- function removeReferenceFromThrough(reference, baseScope) {
2418
- const variable = reference.resolved;
2419
- const name2 = reference.identifier.name;
2420
- let scope = baseScope;
2421
- while (scope) {
2422
- for (const ref of [...scope.through]) {
2423
- if (reference === ref) {
2424
- scope.through.splice(scope.through.indexOf(ref), 1);
2425
- } else if (ref.identifier.name === name2) {
2426
- ref.resolved = variable;
2427
- if (!variable.references.includes(ref)) {
2428
- addReference(variable.references, ref);
2429
- }
2430
- scope.through.splice(scope.through.indexOf(ref), 1);
2431
- }
2432
- }
2433
- scope = scope.upper;
2434
- }
2435
- }
2436
- function removeScope(scopeManager, scope) {
2437
- for (const childScope of scope.childScopes) {
2438
- removeScope(scopeManager, childScope);
2439
- }
2440
- while (scope.references[0]) {
2441
- removeReference(scope.references[0], scope);
2442
- }
2443
- const upper = scope.upper;
2444
- if (upper) {
2445
- const index2 = upper.childScopes.indexOf(scope);
2446
- if (index2 >= 0) {
2447
- upper.childScopes.splice(index2, 1);
2448
- }
2449
- }
2450
- const index = scopeManager.scopes.indexOf(scope);
2451
- if (index >= 0) {
2452
- scopeManager.scopes.splice(index, 1);
2453
- }
2454
- }
2455
- function removeReference(reference, baseScope) {
2456
- if (reference.resolved) {
2457
- if (reference.resolved.defs.some((d) => d.name === reference.identifier)) {
2458
- const varIndex = baseScope.variables.indexOf(reference.resolved);
2459
- if (varIndex >= 0) {
2460
- baseScope.variables.splice(varIndex, 1);
2461
- }
2462
- const name2 = reference.identifier.name;
2463
- if (reference.resolved === baseScope.set.get(name2)) {
2464
- baseScope.set.delete(name2);
2465
- }
2466
- } else {
2467
- const refIndex = reference.resolved.references.indexOf(reference);
2468
- if (refIndex >= 0) {
2469
- reference.resolved.references.splice(refIndex, 1);
2470
- }
2471
- }
2472
- }
2473
- let scope = baseScope;
2474
- while (scope) {
2475
- const refIndex = scope.references.indexOf(reference);
2476
- if (refIndex >= 0) {
2477
- scope.references.splice(refIndex, 1);
2478
- }
2479
- const throughIndex = scope.through.indexOf(reference);
2480
- if (throughIndex >= 0) {
2481
- scope.through.splice(throughIndex, 1);
2482
- }
2483
- scope = scope.upper;
2484
- }
2485
- }
2486
- function removeIdentifierVariable(node, scope) {
2487
- for (let varIndex = 0; varIndex < scope.variables.length; varIndex++) {
2488
- const variable = scope.variables[varIndex];
2489
- const defIndex = variable.defs.findIndex((def) => def.name === node);
2490
- if (defIndex < 0) {
2491
- continue;
2492
- }
2493
- variable.defs.splice(defIndex, 1);
2494
- if (variable.defs.length === 0) {
2495
- referencesToThrough(variable.references, scope);
2496
- variable.references.forEach((r) => {
2497
- if (r.init)
2498
- r.init = false;
2499
- r.resolved = null;
2500
- });
2501
- scope.variables.splice(varIndex, 1);
2502
- const name2 = node.name;
2503
- if (variable === scope.set.get(name2)) {
2504
- scope.set.delete(name2);
2505
- }
2506
- } else {
2507
- const idIndex = variable.identifiers.indexOf(node);
2508
- if (idIndex >= 0) {
2509
- variable.identifiers.splice(idIndex, 1);
2510
- }
2511
- }
2512
- return;
2513
- }
2514
- }
2515
- function removeIdentifierReference(node, scope) {
2516
- const reference = scope.references.find((ref) => ref.identifier === node);
2517
- if (reference) {
2518
- removeReference(reference, scope);
2519
- return true;
2520
- }
2521
- const location = node.range[0];
2522
- const pendingScopes = [];
2523
- for (const childScope of scope.childScopes) {
2524
- const range = childScope.block.range;
2525
- if (range[0] <= location && location < range[1]) {
2526
- if (removeIdentifierReference(node, childScope)) {
2527
- return true;
2528
- }
2529
- } else {
2530
- pendingScopes.push(childScope);
2531
- }
2532
- }
2533
- for (const childScope of pendingScopes) {
2534
- if (removeIdentifierReference(node, childScope)) {
2535
- return true;
2536
- }
2537
- }
2538
- return false;
2539
- }
2540
- function getInnermostScopeFromNode(scopeManager, currentNode) {
2541
- return getInnermostScope(
2542
- getScopeFromNode(scopeManager, currentNode),
2543
- currentNode
2544
- );
2545
- }
2546
- function getScopeFromNode(scopeManager, currentNode) {
2547
- let node = currentNode;
2548
- for (; node; node = node.parent || null) {
2549
- const scope = scopeManager.acquire(node, false);
2550
- if (scope) {
2551
- if (scope.type === "function-expression-name") {
2552
- return scope.childScopes[0];
2553
- }
2554
- if (scope.type === "global" && node.type === "Program" && node.sourceType === "module") {
2555
- return scope.childScopes.find((s) => s.type === "module") || scope;
2556
- }
2557
- return scope;
2558
- }
2559
- }
2560
- const global = scopeManager.globalScope;
2561
- return global;
2562
- }
2563
- function getInnermostScope(initialScope, node) {
2564
- for (const childScope of initialScope.childScopes) {
2565
- const range = childScope.block.range;
2566
- if (range[0] <= node.range[0] && node.range[1] <= range[1]) {
2567
- return getInnermostScope(childScope, node);
2568
- }
2569
- }
2570
- return initialScope;
2571
- }
2572
- function referencesToThrough(references, baseScope) {
2573
- let scope = baseScope;
2574
- while (scope) {
2575
- addAllReferences(scope.through, references);
2576
- scope = scope.upper;
2577
- }
2578
- }
2579
- function addAllReferences(list, elements) {
2580
- addElementsToSortedArray(
2581
- list,
2582
- elements,
2583
- (a, b) => a.identifier.range[0] - b.identifier.range[0]
2584
- );
2585
- }
2586
- function addReference(list, reference) {
2587
- addElementToSortedArray(
2588
- list,
2589
- reference,
2590
- (a, b) => a.identifier.range[0] - b.identifier.range[0]
2591
- );
2592
- }
2593
-
2594
2670
  // src/parser/index.ts
2595
2671
  function parseForESLint(code, options) {
2596
2672
  const { result: resultTemplate, context: ctx } = parseTemplate(
@@ -2725,7 +2801,7 @@ __export(meta_exports, {
2725
2801
 
2726
2802
  // package.json
2727
2803
  var name = "astro-eslint-parser";
2728
- var version = "1.0.1";
2804
+ var version = "1.0.2";
2729
2805
 
2730
2806
  // src/index.ts
2731
2807
  function parseForESLint2(code, options) {