@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.cjs CHANGED
@@ -1,7 +1,9 @@
1
1
  "use strict";
2
+ var __create = Object.create;
2
3
  var __defProp = Object.defineProperty;
3
4
  var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
5
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
6
8
  var __export = (target, all) => {
7
9
  for (var name in all)
@@ -15,6 +17,14 @@ var __copyProps = (to, from, except, desc) => {
15
17
  }
16
18
  return to;
17
19
  };
20
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
21
+ // If the importer is in node compatibility mode or this is not an ESM
22
+ // file that has been converted to a CommonJS file using a Babel-
23
+ // compatible transform (i.e. "__esModule" has not been set), then set
24
+ // "default" to the CommonJS "module.exports" for node compatibility.
25
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
26
+ mod
27
+ ));
18
28
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
29
 
20
30
  // src/index.ts
@@ -2627,6 +2637,7 @@ var no_secret_in_log_default = import_utils24.ESLintUtils.RuleCreator(
2627
2637
 
2628
2638
  // src/rules/prefer-string-literal-union.ts
2629
2639
  var import_utils25 = require("@typescript-eslint/utils");
2640
+ var ts = __toESM(require("typescript"), 1);
2630
2641
  var CHOICE_TOKENS = /* @__PURE__ */ new Set([
2631
2642
  "status",
2632
2643
  "state",
@@ -2685,80 +2696,9 @@ function isStringLiteralUnion(node) {
2685
2696
  }
2686
2697
  return node.types.filter(isStringLiteralMember).length >= MIN_CLUSTER_SIZE;
2687
2698
  }
2688
- function isLikelyClosedUnion(node) {
2689
- return node?.type === import_utils25.AST_NODE_TYPES.TSUnionType && node.types.some(isStringLiteralMember);
2690
- }
2691
- function unionMemberNames(typeNode, out) {
2692
- if (typeNode === void 0) {
2693
- return;
2694
- }
2695
- if (typeNode.type === import_utils25.AST_NODE_TYPES.TSIntersectionType || typeNode.type === import_utils25.AST_NODE_TYPES.TSUnionType) {
2696
- for (const t of typeNode.types) {
2697
- unionMemberNames(t, out);
2698
- }
2699
- return;
2700
- }
2701
- if (typeNode.type !== import_utils25.AST_NODE_TYPES.TSTypeLiteral) {
2702
- return;
2703
- }
2704
- for (const member of typeNode.members) {
2705
- if (member.type === import_utils25.AST_NODE_TYPES.TSPropertySignature && isLikelyClosedUnion(member.typeAnnotation?.typeAnnotation)) {
2706
- const propName2 = keyName(member.key);
2707
- if (propName2 !== null) {
2708
- out.add(propName2);
2709
- }
2710
- }
2711
- }
2712
- }
2713
- function bindingName(node) {
2714
- const id = node.type === import_utils25.AST_NODE_TYPES.AssignmentPattern ? node.left : node;
2715
- return id.type === import_utils25.AST_NODE_TYPES.Identifier ? id.name : null;
2716
- }
2717
- function addUnionRefsFromBinding(param, out) {
2718
- const binding = param.type === import_utils25.AST_NODE_TYPES.AssignmentPattern ? param.left : param;
2719
- if (binding.type === import_utils25.AST_NODE_TYPES.ObjectPattern) {
2720
- const members = /* @__PURE__ */ new Set();
2721
- unionMemberNames(binding.typeAnnotation?.typeAnnotation, members);
2722
- if (members.size === 0) {
2723
- return;
2724
- }
2725
- for (const prop of binding.properties) {
2726
- if (prop.type !== import_utils25.AST_NODE_TYPES.Property) {
2727
- continue;
2728
- }
2729
- const propKey = keyName(prop.key);
2730
- const local = bindingName(prop.value);
2731
- if (propKey !== null && local !== null && members.has(propKey)) {
2732
- out.add(local);
2733
- }
2734
- }
2735
- return;
2736
- }
2737
- if (binding.type !== import_utils25.AST_NODE_TYPES.Identifier) {
2738
- return;
2739
- }
2740
- const typeNode = binding.typeAnnotation?.typeAnnotation;
2741
- if (isLikelyClosedUnion(typeNode)) {
2742
- out.add(binding.name);
2743
- return;
2744
- }
2745
- if (typeNode?.type === import_utils25.AST_NODE_TYPES.TSTypeLiteral) {
2746
- for (const member of typeNode.members) {
2747
- if (member.type === import_utils25.AST_NODE_TYPES.TSPropertySignature && isLikelyClosedUnion(member.typeAnnotation?.typeAnnotation)) {
2748
- const propName2 = keyName(member.key);
2749
- if (propName2 !== null) {
2750
- out.add(`${binding.name}.${propName2}`);
2751
- }
2752
- }
2753
- }
2754
- }
2755
- }
2756
- function unionRefsFromParams(fn) {
2757
- const out = /* @__PURE__ */ new Set();
2758
- for (const param of fn.params) {
2759
- addUnionRefsFromBinding(param, out);
2760
- }
2761
- return out;
2699
+ function typeHasRawString(type) {
2700
+ const parts = type.isUnion() ? type.types : [type];
2701
+ return parts.some((t) => (t.flags & ts.TypeFlags.String) !== 0);
2762
2702
  }
2763
2703
  function refKey(node) {
2764
2704
  if (node.type === import_utils25.AST_NODE_TYPES.Identifier) {
@@ -2801,29 +2741,32 @@ var prefer_string_literal_union_default = import_utils25.ESLintUtils.RuleCreator
2801
2741
  if (isIgnoredFile(filename, sourceText)) {
2802
2742
  return {};
2803
2743
  }
2744
+ let services;
2745
+ try {
2746
+ services = import_utils25.ESLintUtils.getParserServices(context);
2747
+ } catch {
2748
+ services = null;
2749
+ }
2804
2750
  const scopeStack = [];
2805
2751
  const validClusters = [];
2806
2752
  const bareChoiceProps = [];
2807
2753
  const containersWithUnion = /* @__PURE__ */ new Set();
2808
- function pushScope(fn) {
2809
- scopeStack.push({
2810
- clusters: /* @__PURE__ */ new Map(),
2811
- unionRefs: unionRefsFromParams(fn)
2812
- });
2813
- }
2814
- function isUnionTypedRef(key, current) {
2815
- if (current.has(key)) {
2816
- return true;
2754
+ function operandIsRawString(node) {
2755
+ if (services === null) {
2756
+ return false;
2817
2757
  }
2818
- return scopeStack.some((s) => s.unionRefs.has(key));
2758
+ return typeHasRawString(services.getTypeAtLocation(node));
2759
+ }
2760
+ function pushScope() {
2761
+ scopeStack.push({ clusters: /* @__PURE__ */ new Map() });
2819
2762
  }
2820
2763
  function popScope() {
2821
2764
  const scope = scopeStack.pop();
2822
2765
  if (scope === void 0) {
2823
2766
  return;
2824
2767
  }
2825
- for (const [key, entry] of scope.clusters) {
2826
- if (entry.allTokens && entry.literals.size >= MIN_CLUSTER_SIZE && !isUnionTypedRef(key, scope.unionRefs)) {
2768
+ for (const entry of scope.clusters.values()) {
2769
+ if (entry.allTokens && entry.literals.size >= MIN_CLUSTER_SIZE) {
2827
2770
  validClusters.push(entry.node);
2828
2771
  }
2829
2772
  }
@@ -2869,15 +2812,6 @@ var prefer_string_literal_union_default = import_utils25.ESLintUtils.RuleCreator
2869
2812
  "FunctionExpression:exit": popScope,
2870
2813
  ArrowFunctionExpression: pushScope,
2871
2814
  "ArrowFunctionExpression:exit": popScope,
2872
- VariableDeclarator(node) {
2873
- const scope = scopeStack[scopeStack.length - 1];
2874
- if (scope === void 0 || node.id.type !== import_utils25.AST_NODE_TYPES.Identifier) {
2875
- return;
2876
- }
2877
- if (isLikelyClosedUnion(node.id.typeAnnotation?.typeAnnotation)) {
2878
- scope.unionRefs.add(node.id.name);
2879
- }
2880
- },
2881
2815
  BinaryExpression(node) {
2882
2816
  if (node.operator !== "===" && node.operator !== "!==" && node.operator !== "==" && node.operator !== "!=") {
2883
2817
  return;
@@ -2887,14 +2821,18 @@ var prefer_string_literal_union_default = import_utils25.ESLintUtils.RuleCreator
2887
2821
  const rightKey = refKey(node.right);
2888
2822
  const leftLit = strLiteral(node.left);
2889
2823
  if (leftKey !== null && rightLit !== null) {
2890
- accumulate(leftKey, [rightLit], node);
2824
+ if (operandIsRawString(node.left)) {
2825
+ accumulate(leftKey, [rightLit], node);
2826
+ }
2891
2827
  } else if (rightKey !== null && leftLit !== null) {
2892
- accumulate(rightKey, [leftLit], node);
2828
+ if (operandIsRawString(node.right)) {
2829
+ accumulate(rightKey, [leftLit], node);
2830
+ }
2893
2831
  }
2894
2832
  },
2895
2833
  SwitchStatement(node) {
2896
2834
  const key = refKey(node.discriminant);
2897
- if (key === null) {
2835
+ if (key === null || !operandIsRawString(node.discriminant)) {
2898
2836
  return;
2899
2837
  }
2900
2838
  const literals = [];
@@ -3133,7 +3071,7 @@ var rules = {
3133
3071
  var plugin = {
3134
3072
  meta: {
3135
3073
  name: "@sarj/eslint-plugin",
3136
- version: "2.3.2"
3074
+ version: "2.3.3"
3137
3075
  },
3138
3076
  rules,
3139
3077
  configs: {