eslint-plugin-ts-data-forge 0.6.1 → 0.7.1
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/README.md +1 -0
- package/dist/index.mjs +2 -2
- package/dist/index.mjs.map +1 -1
- package/dist/plugin.d.mts +2 -0
- package/dist/plugin.d.mts.map +1 -1
- package/dist/plugin.mjs +45 -41
- package/dist/plugin.mjs.map +1 -1
- package/dist/rule-types.d.mts +15 -0
- package/dist/rule-types.d.mts.map +1 -1
- package/dist/rule-types.mjs +6 -2
- package/dist/rule-types.mjs.map +1 -1
- package/dist/rules/ast-utils.mjs +107 -50
- package/dist/rules/ast-utils.mjs.map +1 -1
- package/dist/rules/branded-number-types.mjs +41 -37
- package/dist/rules/branded-number-types.mjs.map +1 -1
- package/dist/rules/constants.mjs +2 -4
- package/dist/rules/constants.mjs.map +1 -1
- package/dist/rules/import-utils.mjs +80 -56
- package/dist/rules/import-utils.mjs.map +1 -1
- package/dist/rules/index.mjs +2 -2
- package/dist/rules/index.mjs.map +1 -1
- package/dist/rules/no-side-effect-import.mjs +46 -34
- package/dist/rules/no-side-effect-import.mjs.map +1 -1
- package/dist/rules/no-unnecessary-type-guard.mjs +376 -265
- package/dist/rules/no-unnecessary-type-guard.mjs.map +1 -1
- package/dist/rules/prefer-arr-is-array.mjs +64 -52
- package/dist/rules/prefer-arr-is-array.mjs.map +1 -1
- package/dist/rules/prefer-arr-is-bounded-length-array.mjs +91 -85
- package/dist/rules/prefer-arr-is-bounded-length-array.mjs.map +1 -1
- package/dist/rules/prefer-arr-is-fixed-length-array.mjs +92 -83
- package/dist/rules/prefer-arr-is-fixed-length-array.mjs.map +1 -1
- package/dist/rules/prefer-arr-is-max-length-array.mjs +92 -80
- package/dist/rules/prefer-arr-is-max-length-array.mjs.map +1 -1
- package/dist/rules/prefer-arr-is-min-length-array.mjs +92 -80
- package/dist/rules/prefer-arr-is-min-length-array.mjs.map +1 -1
- package/dist/rules/prefer-arr-is-non-empty.mjs +85 -77
- package/dist/rules/prefer-arr-is-non-empty.mjs.map +1 -1
- package/dist/rules/prefer-arr-scan.d.mts +96 -0
- package/dist/rules/prefer-arr-scan.d.mts.map +1 -0
- package/dist/rules/prefer-arr-scan.mjs +482 -0
- package/dist/rules/prefer-arr-scan.mjs.map +1 -0
- package/dist/rules/prefer-arr-sum.mjs +176 -132
- package/dist/rules/prefer-arr-sum.mjs.map +1 -1
- package/dist/rules/prefer-as-int.mjs +77 -71
- package/dist/rules/prefer-as-int.mjs.map +1 -1
- package/dist/rules/prefer-canonical-array-slicing.mjs +503 -343
- package/dist/rules/prefer-canonical-array-slicing.mjs.map +1 -1
- package/dist/rules/prefer-canonical-length-cast.mjs +217 -144
- package/dist/rules/prefer-canonical-length-cast.mjs.map +1 -1
- package/dist/rules/prefer-canonical-length-guard.mjs +160 -109
- package/dist/rules/prefer-canonical-length-guard.mjs.map +1 -1
- package/dist/rules/prefer-comparison-over-nullish-guard.mjs +89 -71
- package/dist/rules/prefer-comparison-over-nullish-guard.mjs.map +1 -1
- package/dist/rules/prefer-is-non-null-object.mjs +91 -72
- package/dist/rules/prefer-is-non-null-object.mjs.map +1 -1
- package/dist/rules/prefer-is-record-and-has-key.mjs +148 -105
- package/dist/rules/prefer-is-record-and-has-key.mjs.map +1 -1
- package/dist/rules/prefer-num-safe-parse-float.mjs +101 -77
- package/dist/rules/prefer-num-safe-parse-float.mjs.map +1 -1
- package/dist/rules/prefer-num-safe-parse-int.mjs +103 -81
- package/dist/rules/prefer-num-safe-parse-int.mjs.map +1 -1
- package/dist/rules/prefer-obj-over-entries-round-trip.mjs +299 -194
- package/dist/rules/prefer-obj-over-entries-round-trip.mjs.map +1 -1
- package/dist/rules/prefer-range-for-loop.mjs +121 -99
- package/dist/rules/prefer-range-for-loop.mjs.map +1 -1
- package/dist/rules/rules.d.mts +1 -0
- package/dist/rules/rules.d.mts.map +1 -1
- package/dist/rules/rules.mjs +19 -20
- package/dist/rules/rules.mjs.map +1 -1
- package/dist/types.mjs +2 -2
- package/dist/types.mjs.map +1 -1
- package/package.json +6 -7
- package/src/plugin.mts +1 -0
- package/src/rule-types.mts +16 -0
- package/src/rules/prefer-arr-scan.mts +693 -0
- package/src/rules/prefer-arr-scan.test.mts +295 -0
- package/src/rules/rules.mts +2 -0
|
@@ -1,84 +1,108 @@
|
|
|
1
|
-
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
|
|
1
|
+
import { AST_NODE_TYPES, } from '@typescript-eslint/utils';
|
|
2
2
|
import * as ts from 'typescript';
|
|
3
|
-
import {
|
|
3
|
+
import { buildImportFixes, getNamedImports, getTsDataForgeImport, } from './import-utils.mjs';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
4
10
|
|
|
5
11
|
const isStringType = (type) => {
|
|
6
|
-
|
|
7
|
-
|
|
12
|
+
const parts = type.isUnion() ? type.types : [type];
|
|
13
|
+
return (parts.length > 0 &&
|
|
14
|
+
parts.every((t) => (t.flags & ts.TypeFlags.StringLike) !== 0));
|
|
8
15
|
};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
export const preferNumSafeParseFloat = {
|
|
25
|
+
meta: {
|
|
26
|
+
type: 'suggestion',
|
|
27
|
+
docs: {
|
|
28
|
+
description: 'Replace `parseFloat(x)`, `Number.parseFloat(x)`, or `Number(x)` (when x is a string) with `Result.unwrapOkOr(Num.safeParseFloat(x), Number.NaN)` from ts-data-forge.',
|
|
29
|
+
},
|
|
30
|
+
fixable: 'code',
|
|
31
|
+
schema: [],
|
|
32
|
+
messages: {
|
|
33
|
+
useSafeParseFloat: 'Replace `{{original}}` with `Result.unwrapOkOr(Num.safeParseFloat({{argName}}), Number.NaN)` from ts-data-forge.',
|
|
34
|
+
},
|
|
14
35
|
},
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
node,
|
|
60
|
-
messageId: "useSafeParseFloat",
|
|
61
|
-
data: {
|
|
62
|
-
original: originalText,
|
|
63
|
-
argName: argText
|
|
36
|
+
create: (context) => {
|
|
37
|
+
const sourceCode = context.sourceCode;
|
|
38
|
+
const program = sourceCode.ast;
|
|
39
|
+
const tsDataForgeImport = getTsDataForgeImport(program);
|
|
40
|
+
const services = context.sourceCode.parserServices;
|
|
41
|
+
const mut_nodesToFix = [];
|
|
42
|
+
return {
|
|
43
|
+
CallExpression: (node) => {
|
|
44
|
+
const { callee } = node;
|
|
45
|
+
|
|
46
|
+
const isGlobalParseFloat = callee.type === AST_NODE_TYPES.Identifier &&
|
|
47
|
+
callee.name === 'parseFloat';
|
|
48
|
+
|
|
49
|
+
const isNumberParseFloat = callee.type === AST_NODE_TYPES.MemberExpression &&
|
|
50
|
+
!callee.computed &&
|
|
51
|
+
callee.object.type === AST_NODE_TYPES.Identifier &&
|
|
52
|
+
callee.object.name === 'Number' &&
|
|
53
|
+
callee.property.type === AST_NODE_TYPES.Identifier &&
|
|
54
|
+
callee.property.name === 'parseFloat';
|
|
55
|
+
|
|
56
|
+
const isNumberCall = callee.type === AST_NODE_TYPES.Identifier &&
|
|
57
|
+
callee.name === 'Number';
|
|
58
|
+
if (!isGlobalParseFloat && !isNumberParseFloat && !isNumberCall)
|
|
59
|
+
return;
|
|
60
|
+
const args = node.arguments;
|
|
61
|
+
const firstArg = args[0];
|
|
62
|
+
if (firstArg === undefined)
|
|
63
|
+
return;
|
|
64
|
+
|
|
65
|
+
if (firstArg.type === AST_NODE_TYPES.SpreadElement)
|
|
66
|
+
return;
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
if (services?.program == null)
|
|
71
|
+
return;
|
|
72
|
+
const checker = services.program.getTypeChecker();
|
|
73
|
+
const tsNode = services.esTreeNodeToTSNodeMap?.get(firstArg);
|
|
74
|
+
if (tsNode === undefined)
|
|
75
|
+
return;
|
|
76
|
+
const argType = checker.getTypeAtLocation(tsNode);
|
|
77
|
+
if (!isStringType(argType))
|
|
78
|
+
return;
|
|
79
|
+
mut_nodesToFix.push({ node, argExpression: firstArg });
|
|
64
80
|
},
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
+
'Program:exit': () => {
|
|
82
|
+
const namedImports = getNamedImports(tsDataForgeImport);
|
|
83
|
+
const missingImports = ['Num', 'Result'].filter((name) => !namedImports.includes(name));
|
|
84
|
+
for (const [index, { node, argExpression },] of mut_nodesToFix.entries()) {
|
|
85
|
+
const argText = sourceCode.getText(argExpression);
|
|
86
|
+
const originalText = sourceCode.getText(node);
|
|
87
|
+
context.report({
|
|
88
|
+
node,
|
|
89
|
+
messageId: 'useSafeParseFloat',
|
|
90
|
+
data: {
|
|
91
|
+
original: originalText,
|
|
92
|
+
argName: argText,
|
|
93
|
+
},
|
|
94
|
+
fix: (fixer) => {
|
|
95
|
+
const core = `Result.unwrapOkOr(Num.safeParseFloat(${argText}), Number.NaN)`;
|
|
96
|
+
const importFixes = index === 0 && missingImports.length > 0
|
|
97
|
+
? buildImportFixes(fixer, program, tsDataForgeImport, missingImports)
|
|
98
|
+
: [];
|
|
99
|
+
return [...importFixes, fixer.replaceText(node, core)];
|
|
100
|
+
},
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
},
|
|
104
|
+
};
|
|
105
|
+
},
|
|
106
|
+
defaultOptions: [],
|
|
81
107
|
};
|
|
82
|
-
|
|
83
|
-
export { preferNumSafeParseFloat };
|
|
84
|
-
//# sourceMappingURL=prefer-num-safe-parse-float.mjs.map
|
|
108
|
+
//# sourceMappingURL=prefer-num-safe-parse-float.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-num-safe-parse-float.mjs","sources":["../../src/rules/prefer-num-safe-parse-float.mts"],"
|
|
1
|
+
{"version":3,"file":"prefer-num-safe-parse-float.mjs","sourceRoot":"","sources":["../../src/rules/prefer-num-safe-parse-float.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAGf,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AACjC,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,oBAAoB,GACrB,MAAM,oBAAoB,CAAC;AAM5B;;;;;GAKG;AACH,8EAA8E;AAC9E,MAAM,YAAY,GAAG,CAAC,IAAa,EAAW,EAAE;IAC9C,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAE,CAAC,IAAI,CAAW,CAAC;IAE9D,OAAO,CACL,KAAK,CAAC,MAAM,GAAG,CAAC;QAChB,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,KA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,MAAM,CACT,KAAK,EACL,EAAE,IAAI,EAAE,aAAa,EAAE,EACxB,IAAI,cAAc,CAAC,OAAO,EAAE,EAAE,CAAC;oBAC9B,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,CAAC,aAAa,CAAC,CAAC;oBAElD,MAAM,YAAY,GAAG,UAAU,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;oBAE9C,OAAO,CAAC,MAAM,CAAC;wBACb,IAAI;wBACJ,SAAS,EAAE,mBAAmB;wBAC9B,IAAI,EAAE;4BACJ,QAAQ,EAAE,YAAY;4BACtB,OAAO,EAAE,OAAO;yBACjB;wBACD,GAAG,EAAE,CAAC,KAAK,EAAE,EAAE;4BACb,MAAM,IAAI,GAAG,wCAAwC,OAAO,gBAAgB,CAAC;4BAE7E,MAAM,WAAW,GACf,KAAK,KAAK,CAAC,IAAI,cAAc,CAAC,MAAM,GAAG,CAAC;gCACtC,CAAC,CAAC,gBAAgB,CACd,KAAK,EACL,OAAO,EACP,iBAAiB,EACjB,cAAc,CACf;gCACH,CAAC,CAAC,EAAE,CAAC;4BAET,OAAO,CAAC,GAAG,WAAW,EAAE,KAAK,CAAC,WAAW,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC,CAAC;wBACzD,CAAC;qBACF,CAAC,CAAC;gBACL,CAAC;YACH,CAAC;SACF,CAAC;IACJ,CAAC;IACD,cAAc,EAAE,EAAE;CACV,CAAC"}
|
|
@@ -1,88 +1,110 @@
|
|
|
1
|
-
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
|
|
1
|
+
import { AST_NODE_TYPES, } from '@typescript-eslint/utils';
|
|
2
2
|
import * as ts from 'typescript';
|
|
3
|
-
import {
|
|
3
|
+
import { buildImportFixes, getNamedImports, getTsDataForgeImport, } from './import-utils.mjs';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
4
10
|
|
|
5
11
|
const isStringType = (type) => {
|
|
6
|
-
|
|
7
|
-
|
|
12
|
+
const parts = type.isUnion() ? type.types : [type];
|
|
13
|
+
return (parts.length > 0 &&
|
|
14
|
+
parts.every((t) => (t.flags & ts.TypeFlags.StringLike) !== 0));
|
|
8
15
|
};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
export const preferNumSafeParseInt = {
|
|
25
|
+
meta: {
|
|
26
|
+
type: 'suggestion',
|
|
27
|
+
docs: {
|
|
28
|
+
description: 'Replace `parseInt(x, 10)` with `Result.unwrapOkOr(Num.safeParseInt(x), Number.NaN)` from ts-data-forge.',
|
|
29
|
+
},
|
|
30
|
+
fixable: 'code',
|
|
31
|
+
schema: [],
|
|
32
|
+
messages: {
|
|
33
|
+
useSafeParseInt: 'Replace `{{original}}` with `Result.unwrapOkOr(Num.safeParseInt({{argName}}), Number.NaN)` from ts-data-forge.',
|
|
34
|
+
},
|
|
14
35
|
},
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
36
|
+
create: (context) => {
|
|
37
|
+
const sourceCode = context.sourceCode;
|
|
38
|
+
const program = sourceCode.ast;
|
|
39
|
+
const tsDataForgeImport = getTsDataForgeImport(program);
|
|
40
|
+
const services = context.sourceCode.parserServices;
|
|
41
|
+
const mut_nodesToFix = [];
|
|
42
|
+
return {
|
|
43
|
+
CallExpression: (node) => {
|
|
44
|
+
const { callee } = node;
|
|
45
|
+
|
|
46
|
+
const isGlobalParseInt = callee.type === AST_NODE_TYPES.Identifier &&
|
|
47
|
+
callee.name === 'parseInt';
|
|
48
|
+
const isNumberParseInt = callee.type === AST_NODE_TYPES.MemberExpression &&
|
|
49
|
+
!callee.computed &&
|
|
50
|
+
callee.object.type === AST_NODE_TYPES.Identifier &&
|
|
51
|
+
callee.object.name === 'Number' &&
|
|
52
|
+
callee.property.type === AST_NODE_TYPES.Identifier &&
|
|
53
|
+
callee.property.name === 'parseInt';
|
|
54
|
+
if (!isGlobalParseInt && !isNumberParseInt)
|
|
55
|
+
return;
|
|
56
|
+
const args = node.arguments;
|
|
57
|
+
const firstArg = args[0];
|
|
58
|
+
if (firstArg === undefined)
|
|
59
|
+
return;
|
|
60
|
+
|
|
61
|
+
if (firstArg.type === AST_NODE_TYPES.SpreadElement)
|
|
62
|
+
return;
|
|
63
|
+
|
|
64
|
+
if (args.length >= 2) {
|
|
65
|
+
const radix = args[1];
|
|
66
|
+
if (radix?.type !== AST_NODE_TYPES.Literal || radix.value !== 10) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
if (services?.program == null)
|
|
73
|
+
return;
|
|
74
|
+
const checker = services.program.getTypeChecker();
|
|
75
|
+
const tsNode = services.esTreeNodeToTSNodeMap?.get(firstArg);
|
|
76
|
+
if (tsNode === undefined)
|
|
77
|
+
return;
|
|
78
|
+
const argType = checker.getTypeAtLocation(tsNode);
|
|
79
|
+
if (!isStringType(argType))
|
|
80
|
+
return;
|
|
81
|
+
mut_nodesToFix.push({ node, argExpression: firstArg });
|
|
82
|
+
},
|
|
83
|
+
'Program:exit': () => {
|
|
84
|
+
const namedImports = getNamedImports(tsDataForgeImport);
|
|
85
|
+
const missingImports = ['Num', 'Result'].filter((name) => !namedImports.includes(name));
|
|
86
|
+
for (const [index, { node, argExpression },] of mut_nodesToFix.entries()) {
|
|
87
|
+
const argText = sourceCode.getText(argExpression);
|
|
88
|
+
const originalText = sourceCode.getText(node);
|
|
89
|
+
context.report({
|
|
90
|
+
node,
|
|
91
|
+
messageId: 'useSafeParseInt',
|
|
92
|
+
data: {
|
|
93
|
+
original: originalText,
|
|
94
|
+
argName: argText,
|
|
95
|
+
},
|
|
96
|
+
fix: (fixer) => {
|
|
97
|
+
const core = `Result.unwrapOkOr(Num.safeParseInt(${argText}), Number.NaN)`;
|
|
98
|
+
const importFixes = index === 0 && missingImports.length > 0
|
|
99
|
+
? buildImportFixes(fixer, program, tsDataForgeImport, missingImports)
|
|
100
|
+
: [];
|
|
101
|
+
return [...importFixes, fixer.replaceText(node, core)];
|
|
102
|
+
},
|
|
103
|
+
});
|
|
104
|
+
}
|
|
68
105
|
},
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
fixer,
|
|
73
|
-
program,
|
|
74
|
-
tsDataForgeImport,
|
|
75
|
-
missingImports
|
|
76
|
-
) : [];
|
|
77
|
-
return [...importFixes, fixer.replaceText(node, core)];
|
|
78
|
-
}
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
};
|
|
83
|
-
},
|
|
84
|
-
defaultOptions: []
|
|
106
|
+
};
|
|
107
|
+
},
|
|
108
|
+
defaultOptions: [],
|
|
85
109
|
};
|
|
86
|
-
|
|
87
|
-
export { preferNumSafeParseInt };
|
|
88
|
-
//# sourceMappingURL=prefer-num-safe-parse-int.mjs.map
|
|
110
|
+
//# sourceMappingURL=prefer-num-safe-parse-int.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-num-safe-parse-int.mjs","sources":["../../src/rules/prefer-num-safe-parse-int.mts"],"
|
|
1
|
+
{"version":3,"file":"prefer-num-safe-parse-int.mjs","sourceRoot":"","sources":["../../src/rules/prefer-num-safe-parse-int.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAGf,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AACjC,OAAO,EACL,gBAAgB,EAChB,eAAe,EACf,oBAAoB,GACrB,MAAM,oBAAoB,CAAC;AAM5B;;;;;GAKG;AACH,8EAA8E;AAC9E,MAAM,YAAY,GAAG,CAAC,IAAa,EAAW,EAAE;IAC9C,MAAM,KAAK,GAAG,IAAI,CAAC,OAAO,EAAE,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAE,CAAC,IAAI,CAAW,CAAC;IAE9D,OAAO,CACL,KAAK,CAAC,MAAM,GAAG,CAAC;QAChB,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,KAAK,GAAG,EAAE,CAAC,SAAS,CAAC,UAAU,CAAC,KAAK,CAAC,CAAC,CAC9D,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAA6C;IAC7E,IAAI,EAAE;QACJ,IAAI,EAAE,YAAY;QAClB,IAAI,EAAE;YACJ,WAAW,EACT,yGAAyG;SAC5G;QACD,OAAO,EAAE,MAAM;QACf,MAAM,EAAE,EAAE;QACV,QAAQ,EAAE;YACR,eAAe,EACb,gHAAgH;SACnH;KACF;IAED,MAAM,EAAE,CAAC,OAAO,EAAE,EAAE;QAClB,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;QAEtC,MAAM,OAAO,GAAG,U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