@sarj/eslint-plugin 2.3.2 → 2.3.4
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 +80 -92
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +70 -92
- package/dist/index.js.map +1 -1
- package/package.json +8 -2
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,28 @@ function isStringLiteralUnion(node) {
|
|
|
2685
2696
|
}
|
|
2686
2697
|
return node.types.filter(isStringLiteralMember).length >= MIN_CLUSTER_SIZE;
|
|
2687
2698
|
}
|
|
2688
|
-
function
|
|
2689
|
-
|
|
2699
|
+
function typeHasRawString(type) {
|
|
2700
|
+
const parts = type.isUnion() ? type.types : [type];
|
|
2701
|
+
return parts.some((t) => (t.flags & ts.TypeFlags.String) !== 0);
|
|
2690
2702
|
}
|
|
2691
|
-
function
|
|
2692
|
-
if (
|
|
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
|
-
}
|
|
2703
|
+
function isExternalSourceFile(sf) {
|
|
2704
|
+
if (sf === void 0) {
|
|
2705
|
+
return false;
|
|
2711
2706
|
}
|
|
2707
|
+
return sf.isDeclarationFile || sf.fileName.includes("/node_modules/");
|
|
2712
2708
|
}
|
|
2713
|
-
function
|
|
2714
|
-
|
|
2715
|
-
return id.type === import_utils25.AST_NODE_TYPES.Identifier ? id.name : null;
|
|
2709
|
+
function symbolIsExternallyDeclared(sym) {
|
|
2710
|
+
return sym?.declarations?.some((d) => isExternalSourceFile(d.getSourceFile())) ?? false;
|
|
2716
2711
|
}
|
|
2717
|
-
function
|
|
2718
|
-
|
|
2719
|
-
|
|
2720
|
-
|
|
2721
|
-
|
|
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
|
-
}
|
|
2712
|
+
function bindingSourceExpression(decl) {
|
|
2713
|
+
let node = decl.parent;
|
|
2714
|
+
while (!ts.isForOfStatement(node) && !(ts.isVariableDeclaration(node) && node.initializer !== void 0)) {
|
|
2715
|
+
if (node.parent === void 0) {
|
|
2716
|
+
return void 0;
|
|
2753
2717
|
}
|
|
2718
|
+
node = node.parent;
|
|
2754
2719
|
}
|
|
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;
|
|
2720
|
+
return ts.isForOfStatement(node) ? node.expression : node.initializer;
|
|
2762
2721
|
}
|
|
2763
2722
|
function refKey(node) {
|
|
2764
2723
|
if (node.type === import_utils25.AST_NODE_TYPES.Identifier) {
|
|
@@ -2801,29 +2760,63 @@ var prefer_string_literal_union_default = import_utils25.ESLintUtils.RuleCreator
|
|
|
2801
2760
|
if (isIgnoredFile(filename, sourceText)) {
|
|
2802
2761
|
return {};
|
|
2803
2762
|
}
|
|
2763
|
+
let services;
|
|
2764
|
+
try {
|
|
2765
|
+
services = import_utils25.ESLintUtils.getParserServices(context);
|
|
2766
|
+
} catch {
|
|
2767
|
+
services = null;
|
|
2768
|
+
}
|
|
2804
2769
|
const scopeStack = [];
|
|
2805
2770
|
const validClusters = [];
|
|
2806
2771
|
const bareChoiceProps = [];
|
|
2807
2772
|
const containersWithUnion = /* @__PURE__ */ new Set();
|
|
2808
|
-
function
|
|
2809
|
-
|
|
2810
|
-
|
|
2811
|
-
|
|
2812
|
-
|
|
2773
|
+
function operandIsRawString(node) {
|
|
2774
|
+
if (services === null) {
|
|
2775
|
+
return false;
|
|
2776
|
+
}
|
|
2777
|
+
return typeHasRawString(services.getTypeAtLocation(node));
|
|
2813
2778
|
}
|
|
2814
|
-
function
|
|
2815
|
-
if (
|
|
2816
|
-
return
|
|
2779
|
+
function originIsExternal(node, depth) {
|
|
2780
|
+
if (node === void 0 || services === null || depth > 6) {
|
|
2781
|
+
return false;
|
|
2782
|
+
}
|
|
2783
|
+
const checker = services.program.getTypeChecker();
|
|
2784
|
+
if (ts.isParenthesizedExpression(node) || ts.isNonNullExpression(node) || ts.isAsExpression(node)) {
|
|
2785
|
+
return originIsExternal(node.expression, depth + 1);
|
|
2786
|
+
}
|
|
2787
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
2788
|
+
return symbolIsExternallyDeclared(checker.getSymbolAtLocation(node.name));
|
|
2789
|
+
}
|
|
2790
|
+
if (ts.isCallExpression(node)) {
|
|
2791
|
+
return originIsExternal(node.expression, depth + 1);
|
|
2792
|
+
}
|
|
2793
|
+
if (ts.isIdentifier(node)) {
|
|
2794
|
+
const decl = checker.getSymbolAtLocation(node)?.valueDeclaration;
|
|
2795
|
+
if (decl === void 0) {
|
|
2796
|
+
return false;
|
|
2797
|
+
}
|
|
2798
|
+
if (ts.isVariableDeclaration(decl) && decl.initializer !== void 0) {
|
|
2799
|
+
return originIsExternal(decl.initializer, depth + 1);
|
|
2800
|
+
}
|
|
2801
|
+
if (ts.isBindingElement(decl)) {
|
|
2802
|
+
return originIsExternal(bindingSourceExpression(decl), depth + 1);
|
|
2803
|
+
}
|
|
2817
2804
|
}
|
|
2818
|
-
return
|
|
2805
|
+
return false;
|
|
2806
|
+
}
|
|
2807
|
+
function operandIsFlaggable(node) {
|
|
2808
|
+
return operandIsRawString(node) && !originIsExternal(services?.esTreeNodeToTSNodeMap.get(node), 0);
|
|
2809
|
+
}
|
|
2810
|
+
function pushScope() {
|
|
2811
|
+
scopeStack.push({ clusters: /* @__PURE__ */ new Map() });
|
|
2819
2812
|
}
|
|
2820
2813
|
function popScope() {
|
|
2821
2814
|
const scope = scopeStack.pop();
|
|
2822
2815
|
if (scope === void 0) {
|
|
2823
2816
|
return;
|
|
2824
2817
|
}
|
|
2825
|
-
for (const
|
|
2826
|
-
if (entry.allTokens && entry.literals.size >= MIN_CLUSTER_SIZE
|
|
2818
|
+
for (const entry of scope.clusters.values()) {
|
|
2819
|
+
if (entry.allTokens && entry.literals.size >= MIN_CLUSTER_SIZE) {
|
|
2827
2820
|
validClusters.push(entry.node);
|
|
2828
2821
|
}
|
|
2829
2822
|
}
|
|
@@ -2869,15 +2862,6 @@ var prefer_string_literal_union_default = import_utils25.ESLintUtils.RuleCreator
|
|
|
2869
2862
|
"FunctionExpression:exit": popScope,
|
|
2870
2863
|
ArrowFunctionExpression: pushScope,
|
|
2871
2864
|
"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
2865
|
BinaryExpression(node) {
|
|
2882
2866
|
if (node.operator !== "===" && node.operator !== "!==" && node.operator !== "==" && node.operator !== "!=") {
|
|
2883
2867
|
return;
|
|
@@ -2887,14 +2871,18 @@ var prefer_string_literal_union_default = import_utils25.ESLintUtils.RuleCreator
|
|
|
2887
2871
|
const rightKey = refKey(node.right);
|
|
2888
2872
|
const leftLit = strLiteral(node.left);
|
|
2889
2873
|
if (leftKey !== null && rightLit !== null) {
|
|
2890
|
-
|
|
2874
|
+
if (operandIsFlaggable(node.left)) {
|
|
2875
|
+
accumulate(leftKey, [rightLit], node);
|
|
2876
|
+
}
|
|
2891
2877
|
} else if (rightKey !== null && leftLit !== null) {
|
|
2892
|
-
|
|
2878
|
+
if (operandIsFlaggable(node.right)) {
|
|
2879
|
+
accumulate(rightKey, [leftLit], node);
|
|
2880
|
+
}
|
|
2893
2881
|
}
|
|
2894
2882
|
},
|
|
2895
2883
|
SwitchStatement(node) {
|
|
2896
2884
|
const key = refKey(node.discriminant);
|
|
2897
|
-
if (key === null) {
|
|
2885
|
+
if (key === null || !operandIsFlaggable(node.discriminant)) {
|
|
2898
2886
|
return;
|
|
2899
2887
|
}
|
|
2900
2888
|
const literals = [];
|
|
@@ -3133,7 +3121,7 @@ var rules = {
|
|
|
3133
3121
|
var plugin = {
|
|
3134
3122
|
meta: {
|
|
3135
3123
|
name: "@sarj/eslint-plugin",
|
|
3136
|
-
version: "2.3.
|
|
3124
|
+
version: "2.3.4"
|
|
3137
3125
|
},
|
|
3138
3126
|
rules,
|
|
3139
3127
|
configs: {
|