@sarj/eslint-plugin 2.3.2 → 2.3.3

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/dist/index.js CHANGED
@@ -2618,6 +2618,7 @@ import {
2618
2618
  ESLintUtils as ESLintUtils24,
2619
2619
  AST_NODE_TYPES as AST_NODE_TYPES12
2620
2620
  } from "@typescript-eslint/utils";
2621
+ import * as ts from "typescript";
2621
2622
  var CHOICE_TOKENS = /* @__PURE__ */ new Set([
2622
2623
  "status",
2623
2624
  "state",
@@ -2676,80 +2677,9 @@ function isStringLiteralUnion(node) {
2676
2677
  }
2677
2678
  return node.types.filter(isStringLiteralMember).length >= MIN_CLUSTER_SIZE;
2678
2679
  }
2679
- function isLikelyClosedUnion(node) {
2680
- return node?.type === AST_NODE_TYPES12.TSUnionType && node.types.some(isStringLiteralMember);
2681
- }
2682
- function unionMemberNames(typeNode, out) {
2683
- if (typeNode === void 0) {
2684
- return;
2685
- }
2686
- if (typeNode.type === AST_NODE_TYPES12.TSIntersectionType || typeNode.type === AST_NODE_TYPES12.TSUnionType) {
2687
- for (const t of typeNode.types) {
2688
- unionMemberNames(t, out);
2689
- }
2690
- return;
2691
- }
2692
- if (typeNode.type !== AST_NODE_TYPES12.TSTypeLiteral) {
2693
- return;
2694
- }
2695
- for (const member of typeNode.members) {
2696
- if (member.type === AST_NODE_TYPES12.TSPropertySignature && isLikelyClosedUnion(member.typeAnnotation?.typeAnnotation)) {
2697
- const propName2 = keyName(member.key);
2698
- if (propName2 !== null) {
2699
- out.add(propName2);
2700
- }
2701
- }
2702
- }
2703
- }
2704
- function bindingName(node) {
2705
- const id = node.type === AST_NODE_TYPES12.AssignmentPattern ? node.left : node;
2706
- return id.type === AST_NODE_TYPES12.Identifier ? id.name : null;
2707
- }
2708
- function addUnionRefsFromBinding(param, out) {
2709
- const binding = param.type === AST_NODE_TYPES12.AssignmentPattern ? param.left : param;
2710
- if (binding.type === AST_NODE_TYPES12.ObjectPattern) {
2711
- const members = /* @__PURE__ */ new Set();
2712
- unionMemberNames(binding.typeAnnotation?.typeAnnotation, members);
2713
- if (members.size === 0) {
2714
- return;
2715
- }
2716
- for (const prop of binding.properties) {
2717
- if (prop.type !== AST_NODE_TYPES12.Property) {
2718
- continue;
2719
- }
2720
- const propKey = keyName(prop.key);
2721
- const local = bindingName(prop.value);
2722
- if (propKey !== null && local !== null && members.has(propKey)) {
2723
- out.add(local);
2724
- }
2725
- }
2726
- return;
2727
- }
2728
- if (binding.type !== AST_NODE_TYPES12.Identifier) {
2729
- return;
2730
- }
2731
- const typeNode = binding.typeAnnotation?.typeAnnotation;
2732
- if (isLikelyClosedUnion(typeNode)) {
2733
- out.add(binding.name);
2734
- return;
2735
- }
2736
- if (typeNode?.type === AST_NODE_TYPES12.TSTypeLiteral) {
2737
- for (const member of typeNode.members) {
2738
- if (member.type === AST_NODE_TYPES12.TSPropertySignature && isLikelyClosedUnion(member.typeAnnotation?.typeAnnotation)) {
2739
- const propName2 = keyName(member.key);
2740
- if (propName2 !== null) {
2741
- out.add(`${binding.name}.${propName2}`);
2742
- }
2743
- }
2744
- }
2745
- }
2746
- }
2747
- function unionRefsFromParams(fn) {
2748
- const out = /* @__PURE__ */ new Set();
2749
- for (const param of fn.params) {
2750
- addUnionRefsFromBinding(param, out);
2751
- }
2752
- return out;
2680
+ function typeHasRawString(type) {
2681
+ const parts = type.isUnion() ? type.types : [type];
2682
+ return parts.some((t) => (t.flags & ts.TypeFlags.String) !== 0);
2753
2683
  }
2754
2684
  function refKey(node) {
2755
2685
  if (node.type === AST_NODE_TYPES12.Identifier) {
@@ -2792,29 +2722,32 @@ var prefer_string_literal_union_default = ESLintUtils24.RuleCreator(
2792
2722
  if (isIgnoredFile(filename, sourceText)) {
2793
2723
  return {};
2794
2724
  }
2725
+ let services;
2726
+ try {
2727
+ services = ESLintUtils24.getParserServices(context);
2728
+ } catch {
2729
+ services = null;
2730
+ }
2795
2731
  const scopeStack = [];
2796
2732
  const validClusters = [];
2797
2733
  const bareChoiceProps = [];
2798
2734
  const containersWithUnion = /* @__PURE__ */ new Set();
2799
- function pushScope(fn) {
2800
- scopeStack.push({
2801
- clusters: /* @__PURE__ */ new Map(),
2802
- unionRefs: unionRefsFromParams(fn)
2803
- });
2804
- }
2805
- function isUnionTypedRef(key, current) {
2806
- if (current.has(key)) {
2807
- return true;
2735
+ function operandIsRawString(node) {
2736
+ if (services === null) {
2737
+ return false;
2808
2738
  }
2809
- return scopeStack.some((s) => s.unionRefs.has(key));
2739
+ return typeHasRawString(services.getTypeAtLocation(node));
2740
+ }
2741
+ function pushScope() {
2742
+ scopeStack.push({ clusters: /* @__PURE__ */ new Map() });
2810
2743
  }
2811
2744
  function popScope() {
2812
2745
  const scope = scopeStack.pop();
2813
2746
  if (scope === void 0) {
2814
2747
  return;
2815
2748
  }
2816
- for (const [key, entry] of scope.clusters) {
2817
- if (entry.allTokens && entry.literals.size >= MIN_CLUSTER_SIZE && !isUnionTypedRef(key, scope.unionRefs)) {
2749
+ for (const entry of scope.clusters.values()) {
2750
+ if (entry.allTokens && entry.literals.size >= MIN_CLUSTER_SIZE) {
2818
2751
  validClusters.push(entry.node);
2819
2752
  }
2820
2753
  }
@@ -2860,15 +2793,6 @@ var prefer_string_literal_union_default = ESLintUtils24.RuleCreator(
2860
2793
  "FunctionExpression:exit": popScope,
2861
2794
  ArrowFunctionExpression: pushScope,
2862
2795
  "ArrowFunctionExpression:exit": popScope,
2863
- VariableDeclarator(node) {
2864
- const scope = scopeStack[scopeStack.length - 1];
2865
- if (scope === void 0 || node.id.type !== AST_NODE_TYPES12.Identifier) {
2866
- return;
2867
- }
2868
- if (isLikelyClosedUnion(node.id.typeAnnotation?.typeAnnotation)) {
2869
- scope.unionRefs.add(node.id.name);
2870
- }
2871
- },
2872
2796
  BinaryExpression(node) {
2873
2797
  if (node.operator !== "===" && node.operator !== "!==" && node.operator !== "==" && node.operator !== "!=") {
2874
2798
  return;
@@ -2878,14 +2802,18 @@ var prefer_string_literal_union_default = ESLintUtils24.RuleCreator(
2878
2802
  const rightKey = refKey(node.right);
2879
2803
  const leftLit = strLiteral(node.left);
2880
2804
  if (leftKey !== null && rightLit !== null) {
2881
- accumulate(leftKey, [rightLit], node);
2805
+ if (operandIsRawString(node.left)) {
2806
+ accumulate(leftKey, [rightLit], node);
2807
+ }
2882
2808
  } else if (rightKey !== null && leftLit !== null) {
2883
- accumulate(rightKey, [leftLit], node);
2809
+ if (operandIsRawString(node.right)) {
2810
+ accumulate(rightKey, [leftLit], node);
2811
+ }
2884
2812
  }
2885
2813
  },
2886
2814
  SwitchStatement(node) {
2887
2815
  const key = refKey(node.discriminant);
2888
- if (key === null) {
2816
+ if (key === null || !operandIsRawString(node.discriminant)) {
2889
2817
  return;
2890
2818
  }
2891
2819
  const literals = [];
@@ -3124,7 +3052,7 @@ var rules = {
3124
3052
  var plugin = {
3125
3053
  meta: {
3126
3054
  name: "@sarj/eslint-plugin",
3127
- version: "2.3.2"
3055
+ version: "2.3.3"
3128
3056
  },
3129
3057
  rules,
3130
3058
  configs: {