eslint-plugin-ts-std-forge 0.1.8 → 0.1.10
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/rule-types.d.mts +3 -2
- package/dist/rule-types.d.mts.map +1 -1
- package/dist/rule-types.mjs.map +1 -1
- package/dist/rules/ast-utils.d.mts +8 -5
- package/dist/rules/ast-utils.d.mts.map +1 -1
- package/dist/rules/ast-utils.mjs +14 -13
- package/dist/rules/ast-utils.mjs.map +1 -1
- package/dist/rules/prefer-is-non-null-object.d.mts.map +1 -1
- package/dist/rules/prefer-is-non-null-object.mjs +113 -38
- package/dist/rules/prefer-is-non-null-object.mjs.map +1 -1
- package/dist/rules/prefer-is-record-and-has-key.d.mts.map +1 -1
- package/dist/rules/prefer-is-record-and-has-key.mjs +95 -34
- package/dist/rules/prefer-is-record-and-has-key.mjs.map +1 -1
- package/dist/rules/prefer-safe-array-length-guard.d.mts.map +1 -1
- package/dist/rules/prefer-safe-array-length-guard.mjs +4 -10
- package/dist/rules/prefer-safe-array-length-guard.mjs.map +1 -1
- package/dist/rules/prefer-safe-number-parse-integer.mjs +4 -3
- package/dist/rules/prefer-safe-number-parse-integer.mjs.map +1 -1
- package/dist/rules/prefer-safe-number-parse.mjs +4 -3
- package/dist/rules/prefer-safe-number-parse.mjs.map +1 -1
- package/package.json +10 -10
- package/src/rule-types.mts +3 -2
- package/src/rules/ast-utils.mts +20 -21
- package/src/rules/prefer-is-non-null-object.mts +152 -49
- package/src/rules/prefer-is-non-null-object.test.mts +107 -0
- package/src/rules/prefer-is-record-and-has-key.mts +141 -53
- package/src/rules/prefer-is-record-and-has-key.test.mts +116 -29
- package/src/rules/prefer-safe-array-length-guard.mts +11 -16
- package/src/rules/prefer-safe-array-length-guard.test.mts +19 -5
- package/src/rules/prefer-safe-number-parse-integer.mts +8 -8
- package/src/rules/prefer-safe-number-parse-integer.test.mts +19 -5
- package/src/rules/prefer-safe-number-parse.mts +8 -8
- package/src/rules/prefer-safe-number-parse.test.mts +19 -5
package/dist/rule-types.d.mts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Linter } from 'eslint';
|
|
2
|
+
import type { ReadonlyRecord } from 'ts-type-forge';
|
|
2
3
|
/**
|
|
3
|
-
* @description Replace `typeof u === "object" && u !== null
|
|
4
|
+
* @description Replace `typeof u === "object" && u !== null`, in either order and anywhere in an `&&` chain, with `isNonNullObject(u)` from ts-std-forge.
|
|
4
5
|
*
|
|
5
6
|
* ```md
|
|
6
7
|
* | key | value |
|
|
@@ -91,6 +92,6 @@ export type EslintTsStdForgeRules = Readonly<{
|
|
|
91
92
|
'ts-std-forge/prefer-safe-number-parse': PreferSafeNumberParse.RuleEntry;
|
|
92
93
|
'ts-std-forge/prefer-safe-number-parse-integer': PreferSafeNumberParseInteger.RuleEntry;
|
|
93
94
|
}>;
|
|
94
|
-
export type EslintTsStdForgeRulesOption =
|
|
95
|
+
export type EslintTsStdForgeRulesOption = ReadonlyRecord<never, never>;
|
|
95
96
|
export {};
|
|
96
97
|
//# sourceMappingURL=rule-types.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rule-types.d.mts","sourceRoot":"","sources":["../src/rule-types.mts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"rule-types.d.mts","sourceRoot":"","sources":["../src/rule-types.mts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAErC,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAGpD;;;;;;;;;;GAUG;AACH,kBAAU,qBAAqB,CAAC;IAC9B,KAAY,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC;CAC/C;AAED;;;;;;;;;;GAUG;AACH,kBAAU,uBAAuB,CAAC;IAChC,KAAY,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC;CAC/C;AAED;;;;;;;;;;GAUG;AACH,kBAAU,sBAAsB,CAAC;IAC/B,KAAY,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC;CAC/C;AAED;;;;;;;;;;GAUG;AACH,kBAAU,0BAA0B,CAAC;IACnC,KAAY,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC;CAC/C;AAED;;;;;;;;;;GAUG;AACH,kBAAU,qBAAqB,CAAC;IAC9B,KAAY,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC;CAC/C;AAED;;;;;;;;;;GAUG;AACH,kBAAU,4BAA4B,CAAC;IACrC,KAAY,SAAS,GAAG,MAAM,CAAC,cAAc,CAAC;CAC/C;AAED,MAAM,MAAM,qBAAqB,GAAG,QAAQ,CAAC;IAC3C,wCAAwC,EAAE,qBAAqB,CAAC,SAAS,CAAC;IAC1E,2CAA2C,EAAE,uBAAuB,CAAC,SAAS,CAAC;IAC/E,yCAAyC,EAAE,sBAAsB,CAAC,SAAS,CAAC;IAC5E,6CAA6C,EAAE,0BAA0B,CAAC,SAAS,CAAC;IACpF,uCAAuC,EAAE,qBAAqB,CAAC,SAAS,CAAC;IACzE,+CAA+C,EAAE,4BAA4B,CAAC,SAAS,CAAC;CACzF,CAAC,CAAC;AAEH,MAAM,MAAM,2BAA2B,GAAG,cAAc,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC"}
|
package/dist/rule-types.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"rule-types.mjs","sourceRoot":"","sources":["../src/rule-types.mts"],"names":[],"mappings":"AAAA,oBAAoB;AAMpB,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;
|
|
1
|
+
{"version":3,"file":"rule-types.mjs","sourceRoot":"","sources":["../src/rule-types.mts"],"names":[],"mappings":"AAAA,oBAAoB;AAMpB,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAyG1C,8EAA8E;AAC9E,8DAA8D;AAC9D,UAAU,CAGR,GAAG,CAAC,CAAC"}
|
|
@@ -6,10 +6,13 @@ import type * as ts from 'typescript';
|
|
|
6
6
|
*/
|
|
7
7
|
export declare const isIdentifierNamed: (node: TSESTree.Node, name: string) => boolean;
|
|
8
8
|
/**
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* information. A
|
|
12
|
-
*
|
|
9
|
+
* Whether `holds` is true of the type of every layer of type wrappers around
|
|
10
|
+
* `node` (see {@link typeWrapperLayers}), outermost to innermost; `false`
|
|
11
|
+
* without type information. A widening cast (`s as string | number`) is the
|
|
12
|
+
* author saying the value may be more than it is, and is respected; a
|
|
13
|
+
* narrowing one (`s!`, `v as unknown as string`) cannot vouch for what the
|
|
14
|
+
* value's own type does not, and the fix, which keeps the wrapper, has to
|
|
15
|
+
* type-check at every layer too.
|
|
13
16
|
*/
|
|
14
|
-
export declare const
|
|
17
|
+
export declare const typeHoldsAtEveryLayer: (sourceCode: TSESLint.SourceCode, node: TSESTree.Node, holds: (type: ts.Type, typeChecker: ts.TypeChecker) => boolean) => boolean;
|
|
15
18
|
//# sourceMappingURL=ast-utils.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ast-utils.d.mts","sourceRoot":"","sources":["../../src/rules/ast-utils.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,QAAQ,EACd,MAAM,0BAA0B,CAAC;AAElC,OAAO,KAAK,KAAK,EAAE,MAAM,YAAY,CAAC;AAEtC;;;GAGG;AACH,eAAO,MAAM,iBAAiB,SAEtB,QAAQ,CAAC,IAAI,QACb,MAAM,KACX,OAMF,CAAC;AAEF
|
|
1
|
+
{"version":3,"file":"ast-utils.d.mts","sourceRoot":"","sources":["../../src/rules/ast-utils.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,QAAQ,EACd,MAAM,0BAA0B,CAAC;AAElC,OAAO,KAAK,KAAK,EAAE,MAAM,YAAY,CAAC;AAEtC;;;GAGG;AACH,eAAO,MAAM,iBAAiB,SAEtB,QAAQ,CAAC,IAAI,QACb,MAAM,KACX,OAMF,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,qBAAqB,eAEpB,QAAQ,CAAC,UAAU,QAEzB,QAAQ,CAAC,IAAI,SAEZ,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,EAAE,WAAW,EAAE,EAAE,CAAC,WAAW,KAAK,OAAO,KAC7D,OAgBF,CAAC"}
|
package/dist/rules/ast-utils.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { AST_NODE_TYPES, } from '@typescript-eslint/utils';
|
|
2
|
-
import {
|
|
2
|
+
import { skipTypeWrappers, typeWrapperLayers } from 'eslint-rule-forge';
|
|
3
3
|
|
|
4
4
|
|
|
5
5
|
|
|
@@ -16,23 +16,24 @@ node, name) => {
|
|
|
16
16
|
|
|
17
17
|
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
export const typeHoldsAtEveryLayer = (
|
|
20
23
|
|
|
21
24
|
sourceCode,
|
|
22
25
|
|
|
23
|
-
node
|
|
26
|
+
node,
|
|
27
|
+
|
|
28
|
+
holds) => {
|
|
24
29
|
const services = sourceCode.parserServices;
|
|
25
30
|
const checker = services?.program?.getTypeChecker();
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
return undefined;
|
|
31
|
+
if (checker === undefined) {
|
|
32
|
+
return false;
|
|
29
33
|
}
|
|
30
|
-
|
|
31
|
-
|
|
34
|
+
return typeWrapperLayers(node).every((layer) => {
|
|
35
|
+
const tsNode = services?.esTreeNodeToTSNodeMap?.get(layer);
|
|
36
|
+
return (tsNode !== undefined && holds(checker.getTypeAtLocation(tsNode), checker));
|
|
37
|
+
});
|
|
32
38
|
};
|
|
33
|
-
const hasNonNullAssertion = (
|
|
34
|
-
|
|
35
|
-
node) => isTypeWrapper(node) &&
|
|
36
|
-
(node.type === AST_NODE_TYPES.TSNonNullExpression ||
|
|
37
|
-
hasNonNullAssertion(node.expression));
|
|
38
39
|
//# sourceMappingURL=ast-utils.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ast-utils.mjs","sourceRoot":"","sources":["../../src/rules/ast-utils.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAGf,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"ast-utils.mjs","sourceRoot":"","sources":["../../src/rules/ast-utils.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAGf,MAAM,0BAA0B,CAAC;AAClC,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AAGxE;;;GAGG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG;AAC/B,8EAA8E;AAC9E,IAAmB,EACnB,IAAY,EACH,EAAE;IACX,MAAM,SAAS,GAAG,gBAAgB,CAAC,IAAI,CAAC,CAAC;IAEzC,OAAO,CACL,SAAS,CAAC,IAAI,KAAK,cAAc,CAAC,UAAU,IAAI,SAAS,CAAC,IAAI,KAAK,IAAI,CACxE,CAAC;AACJ,CAAC,CAAC;AAEF;;;;;;;;GAQG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG;AACnC,8EAA8E;AAC9E,UAA+B;AAC/B,8EAA8E;AAC9E,IAAmB;AACnB,8EAA8E;AAC9E,KAA8D,EACrD,EAAE;IACX,MAAM,QAAQ,GAAG,UAAU,CAAC,cAAc,CAAC;IAE3C,MAAM,OAAO,GAAG,QAAQ,EAAE,OAAO,EAAE,cAAc,EAAE,CAAC;IAEpD,IAAI,OAAO,KAAK,SAAS,EAAE,CAAC;QAC1B,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,iBAAiB,CAAC,IAAI,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE;QAC7C,MAAM,MAAM,GAAG,QAAQ,EAAE,qBAAqB,EAAE,GAAG,CAAC,KAAK,CAAC,CAAC;QAE3D,OAAO,CACL,MAAM,KAAK,SAAS,IAAI,KAAK,CAAC,OAAO,CAAC,iBAAiB,CAAC,MAAM,CAAC,EAAE,OAAO,CAAC,CAC1E,CAAC;IACJ,CAAC,CAAC,CAAC;AACL,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-is-non-null-object.d.mts","sourceRoot":"","sources":["../../src/rules/prefer-is-non-null-object.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EAEd,MAAM,0BAA0B,CAAC;
|
|
1
|
+
{"version":3,"file":"prefer-is-non-null-object.d.mts","sourceRoot":"","sources":["../../src/rules/prefer-is-non-null-object.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EAEd,MAAM,0BAA0B,CAAC;AAWlC,KAAK,OAAO,GAAG,SAAS,EAAE,CAAC;AAE3B,KAAK,UAAU,GAAG,oBAAoB,CAAC;AAEvC,eAAO,MAAM,qBAAqB,EAAE,QAAQ,CAAC,UAAU,CAAC,UAAU,EAAE,OAAO,CA4EjE,CAAC"}
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { AST_NODE_TYPES, } from '@typescript-eslint/utils';
|
|
2
|
-
import { buildNamedImportFix, getImportDeclarations, getNamedImports, skipTypeWrappers, } from 'eslint-rule-forge';
|
|
2
|
+
import { buildNamedImportFix, getImportDeclarations, getNamedImports, getRangeWithParens, parenthesesDepth, skipTypeWrappers, } from 'eslint-rule-forge';
|
|
3
3
|
import { TS_STD_FORGE_MODULE } from './constants.mjs';
|
|
4
4
|
export const preferIsNonNullObject = {
|
|
5
5
|
meta: {
|
|
6
6
|
type: 'suggestion',
|
|
7
7
|
docs: {
|
|
8
|
-
description: 'Replace `typeof u === "object" && u !== null
|
|
8
|
+
description: 'Replace `typeof u === "object" && u !== null`, in either order and anywhere in an `&&` chain, with `isNonNullObject(u)` from ts-std-forge.',
|
|
9
9
|
},
|
|
10
10
|
fixable: 'code',
|
|
11
11
|
schema: [],
|
|
@@ -17,14 +17,14 @@ export const preferIsNonNullObject = {
|
|
|
17
17
|
const sourceCode = context.sourceCode;
|
|
18
18
|
const program = sourceCode.ast;
|
|
19
19
|
const tsStdForgeImports = getImportDeclarations(program, TS_STD_FORGE_MODULE);
|
|
20
|
-
const
|
|
20
|
+
const mut_pairsToFix = [];
|
|
21
21
|
return {
|
|
22
22
|
LogicalExpression: (node) => {
|
|
23
|
-
|
|
24
|
-
if (
|
|
23
|
+
|
|
24
|
+
if (node.operator !== '&&' || isChainOperand(sourceCode, node)) {
|
|
25
25
|
return;
|
|
26
26
|
}
|
|
27
|
-
|
|
27
|
+
mut_pairsToFix.push(...findNonNullObjectPairs(sourceCode, node));
|
|
28
28
|
},
|
|
29
29
|
'Program:exit': () => {
|
|
30
30
|
const hasIsNonNullObjectImport = getNamedImports(tsStdForgeImports).includes('isNonNullObject');
|
|
@@ -33,9 +33,12 @@ export const preferIsNonNullObject = {
|
|
|
33
33
|
|
|
34
34
|
|
|
35
35
|
|
|
36
|
-
for (const [index, {
|
|
36
|
+
for (const [index, { range, identifierName },] of mut_pairsToFix.entries()) {
|
|
37
37
|
context.report({
|
|
38
|
-
|
|
38
|
+
loc: {
|
|
39
|
+
start: sourceCode.getLocFromIndex(range[0]),
|
|
40
|
+
end: sourceCode.getLocFromIndex(range[1]),
|
|
41
|
+
},
|
|
39
42
|
messageId: 'useIsNonNullObject',
|
|
40
43
|
fix: (fixer) => {
|
|
41
44
|
const replacement = `isNonNullObject(${identifierName})`;
|
|
@@ -44,7 +47,10 @@ export const preferIsNonNullObject = {
|
|
|
44
47
|
'isNonNullObject',
|
|
45
48
|
])
|
|
46
49
|
: [];
|
|
47
|
-
return [
|
|
50
|
+
return [
|
|
51
|
+
...importFixes,
|
|
52
|
+
fixer.replaceTextRange(range, replacement),
|
|
53
|
+
];
|
|
48
54
|
},
|
|
49
55
|
});
|
|
50
56
|
}
|
|
@@ -55,57 +61,126 @@ export const preferIsNonNullObject = {
|
|
|
55
61
|
};
|
|
56
62
|
|
|
57
63
|
|
|
58
|
-
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
const isChainOperand = (
|
|
67
|
+
|
|
68
|
+
sourceCode,
|
|
69
|
+
|
|
70
|
+
node) => node.parent.type === AST_NODE_TYPES.LogicalExpression &&
|
|
71
|
+
node.parent.operator === '&&' &&
|
|
72
|
+
parenthesesDepth(sourceCode, node) === 0;
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
const findNonNullObjectPairs = (
|
|
80
|
+
|
|
81
|
+
sourceCode,
|
|
59
82
|
|
|
60
83
|
node) => {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
84
|
+
const operands = chainOperands(sourceCode, node);
|
|
85
|
+
const pairsFrom = (index) => {
|
|
86
|
+
const first = operands[index];
|
|
87
|
+
const second = operands[index + 1];
|
|
88
|
+
if (first === undefined || second === undefined) {
|
|
89
|
+
return [];
|
|
90
|
+
}
|
|
91
|
+
const identifierName = pairIdentifierName(first, second) ?? pairIdentifierName(second, first);
|
|
92
|
+
return identifierName === undefined
|
|
93
|
+
? pairsFrom(index + 1)
|
|
94
|
+
: [
|
|
95
|
+
{
|
|
96
|
+
range: [
|
|
97
|
+
getRangeWithParens(sourceCode, first)[0],
|
|
98
|
+
getRangeWithParens(sourceCode, second)[1],
|
|
99
|
+
],
|
|
100
|
+
identifierName,
|
|
101
|
+
},
|
|
102
|
+
...pairsFrom(index + 2),
|
|
103
|
+
];
|
|
104
|
+
};
|
|
105
|
+
return pairsFrom(0);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
const chainOperands = (
|
|
112
|
+
|
|
113
|
+
sourceCode,
|
|
114
|
+
|
|
115
|
+
node) => [node.left, node.right].flatMap((operand) => operand.type === AST_NODE_TYPES.LogicalExpression &&
|
|
116
|
+
operand.operator === '&&' &&
|
|
117
|
+
parenthesesDepth(sourceCode, operand) === 0
|
|
118
|
+
? chainOperands(sourceCode, operand)
|
|
119
|
+
: [operand]);
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
const pairIdentifierName = (
|
|
127
|
+
|
|
128
|
+
typeofCheck,
|
|
129
|
+
|
|
130
|
+
nullCheck) => {
|
|
131
|
+
const identifierName = getTypeofObjectIdentifierName(skipTypeWrappers(typeofCheck));
|
|
132
|
+
return identifierName !== undefined &&
|
|
133
|
+
identifierName ===
|
|
134
|
+
getNonNullCheckIdentifierName(skipTypeWrappers(nullCheck))
|
|
135
|
+
? identifierName
|
|
136
|
+
: undefined;
|
|
71
137
|
};
|
|
138
|
+
|
|
72
139
|
const getTypeofObjectIdentifierName = (
|
|
73
140
|
|
|
74
141
|
node) => {
|
|
75
|
-
if (node.type !== AST_NODE_TYPES.BinaryExpression
|
|
76
|
-
|
|
77
|
-
}
|
|
78
|
-
if (node.operator !== '===') {
|
|
142
|
+
if (node.type !== AST_NODE_TYPES.BinaryExpression ||
|
|
143
|
+
node.operator !== '===') {
|
|
79
144
|
return undefined;
|
|
80
145
|
}
|
|
81
146
|
const left = skipTypeWrappers(node.left);
|
|
82
147
|
const right = skipTypeWrappers(node.right);
|
|
83
|
-
|
|
84
|
-
left
|
|
85
|
-
|
|
86
|
-
|
|
148
|
+
const typeofSide = isLiteralOf(right, 'object')
|
|
149
|
+
? left
|
|
150
|
+
: isLiteralOf(left, 'object')
|
|
151
|
+
? right
|
|
152
|
+
: undefined;
|
|
153
|
+
if (typeofSide?.type !== AST_NODE_TYPES.UnaryExpression ||
|
|
154
|
+
typeofSide.operator !== 'typeof') {
|
|
87
155
|
return undefined;
|
|
88
156
|
}
|
|
89
|
-
const argument = skipTypeWrappers(
|
|
157
|
+
const argument = skipTypeWrappers(typeofSide.argument);
|
|
90
158
|
return argument.type === AST_NODE_TYPES.Identifier
|
|
91
159
|
? argument.name
|
|
92
160
|
: undefined;
|
|
93
161
|
};
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
|
|
94
167
|
const getNonNullCheckIdentifierName = (
|
|
95
168
|
|
|
96
169
|
node) => {
|
|
97
|
-
if (node.type !== AST_NODE_TYPES.BinaryExpression
|
|
98
|
-
|
|
99
|
-
}
|
|
100
|
-
if (node.operator !== '!==') {
|
|
170
|
+
if (node.type !== AST_NODE_TYPES.BinaryExpression ||
|
|
171
|
+
(node.operator !== '!==' && node.operator !== '!=')) {
|
|
101
172
|
return undefined;
|
|
102
173
|
}
|
|
103
174
|
const left = skipTypeWrappers(node.left);
|
|
104
175
|
const right = skipTypeWrappers(node.right);
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
176
|
+
const checked = isLiteralOf(right, null)
|
|
177
|
+
? left
|
|
178
|
+
: isLiteralOf(left, null)
|
|
179
|
+
? right
|
|
180
|
+
: undefined;
|
|
181
|
+
return checked?.type === AST_NODE_TYPES.Identifier ? checked.name : undefined;
|
|
110
182
|
};
|
|
183
|
+
const isLiteralOf = (
|
|
184
|
+
|
|
185
|
+
node, value) => node.type === AST_NODE_TYPES.Literal && node.value === value;
|
|
111
186
|
//# sourceMappingURL=prefer-is-non-null-object.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-is-non-null-object.mjs","sourceRoot":"","sources":["../../src/rules/prefer-is-non-null-object.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAGf,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,mBAAmB,EACnB,qBAAqB,EACrB,eAAe,EACf,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAMtD,MAAM,CAAC,MAAM,qBAAqB,GAA6C;IAC7E,IAAI,EAAE;QACJ,IAAI,EAAE,YAAY;QAClB,IAAI,EAAE;YACJ,WAAW,EACT,
|
|
1
|
+
{"version":3,"file":"prefer-is-non-null-object.mjs","sourceRoot":"","sources":["../../src/rules/prefer-is-non-null-object.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAGf,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,mBAAmB,EACnB,qBAAqB,EACrB,eAAe,EACf,kBAAkB,EAClB,gBAAgB,EAChB,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAMtD,MAAM,CAAC,MAAM,qBAAqB,GAA6C;IAC7E,IAAI,EAAE;QACJ,IAAI,EAAE,YAAY;QAClB,IAAI,EAAE;YACJ,WAAW,EACT,4IAA4I;SAC/I;QACD,OAAO,EAAE,MAAM;QACf,MAAM,EAAE,EAAE;QACV,QAAQ,EAAE;YACR,kBAAkB,EAChB,2EAA2E;SAC9E;KACF;IAED,MAAM,EAAE,CAAC,OAAO,EAAE,EAAE;QAClB,MAAM,UAAU,GAAG,OAAO,CAAC,UAAU,CAAC;QAEtC,MAAM,OAAO,GAAG,UAAU,CAAC,GAAG,CAAC;QAE/B,MAAM,iBAAiB,GAAG,qBAAqB,CAC7C,OAAO,EACP,mBAAmB,CACpB,CAAC;QAEF,MAAM,cAAc,GAAwB,EAAE,CAAC;QAE/C,OAAO;YACL,iBAAiB,EAAE,CAAC,IAAI,EAAE,EAAE;gBAC1B,iDAAiD;gBACjD,IAAI,IAAI,CAAC,QAAQ,KAAK,IAAI,IAAI,cAAc,CAAC,UAAU,EAAE,IAAI,CAAC,EAAE,CAAC;oBAC/D,OAAO;gBACT,CAAC;gBAED,cAAc,CAAC,IAAI,CAAC,GAAG,sBA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KAAoB,EACX,EAAE,CAAC,IAAI,CAAC,IAAI,KAAK,cAAc,CAAC,OAAO,IAAI,IAAI,CAAC,KAAK,KAAK,KAAK,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-is-record-and-has-key.d.mts","sourceRoot":"","sources":["../../src/rules/prefer-is-record-and-has-key.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EAEd,MAAM,0BAA0B,CAAC;AAalC,KAAK,OAAO,GAAG,SAAS,EAAE,CAAC;AAE3B,KAAK,UAAU,GAAG,sBAAsB,GAAG,WAAW,CAAC;AAEvD,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,UAAU,CAAC,UAAU,EAAE,OAAO,
|
|
1
|
+
{"version":3,"file":"prefer-is-record-and-has-key.d.mts","sourceRoot":"","sources":["../../src/rules/prefer-is-record-and-has-key.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EAEd,MAAM,0BAA0B,CAAC;AAalC,KAAK,OAAO,GAAG,SAAS,EAAE,CAAC;AAE3B,KAAK,UAAU,GAAG,sBAAsB,GAAG,WAAW,CAAC;AAEvD,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,UAAU,CAAC,UAAU,EAAE,OAAO,CA4NjE,CAAC"}
|
|
@@ -2,7 +2,7 @@ import { AST_NODE_TYPES, } from '@typescript-eslint/utils';
|
|
|
2
2
|
import { buildNamedImportFix, getArgumentText, getImportDeclarations, getNamedImports, skipTypeWrappers, } from 'eslint-rule-forge';
|
|
3
3
|
import { SafeArray } from 'ts-std-forge';
|
|
4
4
|
import * as ts from 'typescript';
|
|
5
|
-
import {
|
|
5
|
+
import { isIdentifierNamed, typeHoldsAtEveryLayer } from './ast-utils.mjs';
|
|
6
6
|
import { TS_STD_FORGE_MODULE } from './constants.mjs';
|
|
7
7
|
export const preferIsRecordAndHasKey = {
|
|
8
8
|
meta: {
|
|
@@ -21,7 +21,8 @@ export const preferIsRecordAndHasKey = {
|
|
|
21
21
|
const sourceCode = context.sourceCode;
|
|
22
22
|
const program = sourceCode.ast;
|
|
23
23
|
const tsStdForgeImports = getImportDeclarations(program, TS_STD_FORGE_MODULE);
|
|
24
|
-
|
|
24
|
+
|
|
25
|
+
|
|
25
26
|
|
|
26
27
|
|
|
27
28
|
|
|
@@ -31,13 +32,18 @@ export const preferIsRecordAndHasKey = {
|
|
|
31
32
|
|
|
32
33
|
const isKnownRecord = (
|
|
33
34
|
|
|
34
|
-
expression) =>
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
expression) => typeHoldsAtEveryLayer(sourceCode, expression, (type, checker) => isAlreadyRecordType(checker, type));
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
const mayBeArray = (
|
|
44
|
+
|
|
45
|
+
expression) => sourceCode.parserServices?.program != null &&
|
|
46
|
+
!typeHoldsAtEveryLayer(sourceCode, expression, (type, checker) => !mayBeArrayType(checker, type));
|
|
41
47
|
const mut_nodesToFix = [];
|
|
42
48
|
return {
|
|
43
49
|
|
|
@@ -60,7 +66,8 @@ export const preferIsRecordAndHasKey = {
|
|
|
60
66
|
}
|
|
61
67
|
|
|
62
68
|
if (objArg.type === AST_NODE_TYPES.SpreadElement ||
|
|
63
|
-
keyArg.type === AST_NODE_TYPES.SpreadElement
|
|
69
|
+
keyArg.type === AST_NODE_TYPES.SpreadElement ||
|
|
70
|
+
mayBeArray(objArg)) {
|
|
64
71
|
return;
|
|
65
72
|
}
|
|
66
73
|
mut_nodesToFix.push({
|
|
@@ -77,7 +84,8 @@ export const preferIsRecordAndHasKey = {
|
|
|
77
84
|
const keyExpression = node.left;
|
|
78
85
|
const objExpression = node.right;
|
|
79
86
|
|
|
80
|
-
if (keyExpression.type === AST_NODE_TYPES.PrivateIdentifier
|
|
87
|
+
if (keyExpression.type === AST_NODE_TYPES.PrivateIdentifier ||
|
|
88
|
+
mayBeArray(objExpression)) {
|
|
81
89
|
return;
|
|
82
90
|
}
|
|
83
91
|
mut_nodesToFix.push({
|
|
@@ -88,20 +96,29 @@ export const preferIsRecordAndHasKey = {
|
|
|
88
96
|
},
|
|
89
97
|
'Program:exit': () => {
|
|
90
98
|
const namedImports = getNamedImports(tsStdForgeImports);
|
|
91
|
-
const rewrites = mut_nodesToFix.map((entry) =>
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
!
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
99
|
+
const rewrites = mut_nodesToFix.map((entry) => {
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
const needsIsRecord = !isKnownRecord(entry.objExpression);
|
|
103
|
+
return {
|
|
104
|
+
...entry,
|
|
105
|
+
needsIsRecord,
|
|
106
|
+
|
|
107
|
+
fixable: !needsIsRecord || isSideEffectFree(entry.objExpression),
|
|
108
|
+
};
|
|
109
|
+
});
|
|
110
|
+
const fixableRewrites = rewrites.filter(({ fixable }) => fixable);
|
|
111
|
+
const importsToAdd = SafeArray.isEmpty(fixableRewrites)
|
|
112
|
+
? []
|
|
113
|
+
: [
|
|
114
|
+
...(fixableRewrites.some(({ needsIsRecord }) => needsIsRecord) && !namedImports.includes('isRecord')
|
|
115
|
+
? ['isRecord']
|
|
116
|
+
: []),
|
|
117
|
+
...(namedImports.includes('hasKey') ? [] : ['hasKey']),
|
|
118
|
+
];
|
|
119
|
+
const importCarrier = fixableRewrites[0];
|
|
120
|
+
for (const rewrite of rewrites) {
|
|
121
|
+
const { node, objExpression, keyExpression, needsIsRecord } = rewrite;
|
|
105
122
|
const objText = getArgumentText(sourceCode, objExpression);
|
|
106
123
|
const keyText = getArgumentText(sourceCode, keyExpression);
|
|
107
124
|
const originalText = sourceCode.getText(node);
|
|
@@ -113,18 +130,24 @@ export const preferIsRecordAndHasKey = {
|
|
|
113
130
|
objName: objText,
|
|
114
131
|
keyName: keyText,
|
|
115
132
|
},
|
|
116
|
-
fix:
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
133
|
+
fix: !rewrite.fixable
|
|
134
|
+
? undefined
|
|
135
|
+
: (fixer) => {
|
|
136
|
+
const replacement = needsIsRecord
|
|
137
|
+
? `(isRecord(${objText}) && hasKey(${objText}, ${keyText}))`
|
|
138
|
+
: `hasKey(${objText}, ${keyText})`;
|
|
120
139
|
|
|
121
140
|
|
|
122
141
|
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
142
|
+
|
|
143
|
+
const importFixes = rewrite === importCarrier && importsToAdd.length > 0
|
|
144
|
+
? buildNamedImportFix(fixer, program, TS_STD_FORGE_MODULE, importsToAdd)
|
|
145
|
+
: [];
|
|
146
|
+
return [
|
|
147
|
+
...importFixes,
|
|
148
|
+
fixer.replaceText(node, replacement),
|
|
149
|
+
];
|
|
150
|
+
},
|
|
128
151
|
});
|
|
129
152
|
}
|
|
130
153
|
},
|
|
@@ -136,6 +159,44 @@ export const preferIsRecordAndHasKey = {
|
|
|
136
159
|
|
|
137
160
|
|
|
138
161
|
|
|
162
|
+
const isSideEffectFree = (
|
|
163
|
+
|
|
164
|
+
wrapped) => {
|
|
165
|
+
const node = skipTypeWrappers(wrapped);
|
|
166
|
+
return (node.type === AST_NODE_TYPES.Identifier ||
|
|
167
|
+
node.type === AST_NODE_TYPES.ThisExpression ||
|
|
168
|
+
(node.type === AST_NODE_TYPES.MemberExpression &&
|
|
169
|
+
!node.computed &&
|
|
170
|
+
isSideEffectFree(node.object)));
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
const mayBeArrayType = (
|
|
178
|
+
|
|
179
|
+
checker,
|
|
180
|
+
|
|
181
|
+
type) => {
|
|
182
|
+
if (type.isUnionOrIntersection()) {
|
|
183
|
+
return type.types.some((member) => mayBeArrayType(checker, member));
|
|
184
|
+
}
|
|
185
|
+
if ((type.flags &
|
|
186
|
+
(ts.TypeFlags.TypeParameter |
|
|
187
|
+
ts.TypeFlags.IndexedAccess |
|
|
188
|
+
ts.TypeFlags.Conditional |
|
|
189
|
+
ts.TypeFlags.Substitution)) !==
|
|
190
|
+
0) {
|
|
191
|
+
const constraint = checker.getBaseConstraintOfType(type);
|
|
192
|
+
return constraint !== undefined && mayBeArrayType(checker, constraint);
|
|
193
|
+
}
|
|
194
|
+
return checker.isArrayType(type) || checker.isTupleType(type);
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
|
|
199
|
+
|
|
139
200
|
|
|
140
201
|
|
|
141
202
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-is-record-and-has-key.mjs","sourceRoot":"","sources":["../../src/rules/prefer-is-record-and-has-key.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAGf,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,mBAAmB,EACnB,eAAe,EACf,qBAAqB,EACrB,eAAe,EACf,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AACjC,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"prefer-is-record-and-has-key.mjs","sourceRoot":"","sources":["../../src/rules/prefer-is-record-and-has-key.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAGf,MAAM,0BAA0B,CAAC;AAClC,OAAO,EACL,mBAAmB,EACnB,eAAe,EACf,qBAAqB,EACrB,eAAe,EACf,gBAAgB,GACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EAAE,SAAS,EAAE,MAAM,cAAc,CAAC;AACzC,OAAO,KAAK,EAAE,MA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AC,IAAI,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC;AAChE,CAAC,CAAC;AAEF;;;;;;;;;;;;;GAaG;AACH,MAAM,mBAAmB,GAAG;AAC1B,8EAA8E;AAC9E,OAAuB;AACvB,8EAA8E;AAC9E,IAAa,EACJ,EAAE,CACX,IAAI,CAAC,OAAO,EAAE;IACZ,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,mBAAmB,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;IACpE,CAAC,CAAC,OAAO,CAAC,kBAAkB,CAAC,IAAI,EAAE,EAAE,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,SAAS;QACnE,SAAS,CAAC,OAAO,CACf,OAAO,CAAC,mBAAmB,CAAC,IAAI,EAAE,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,CACzD;QACD,SAAS,CAAC,OAAO,CACf,OAAO,CAAC,mBAAmB,CAAC,IAAI,EAAE,EAAE,CAAC,aAAa,CAAC,SAAS,CAAC,CAC9D;QACD,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC;QAC1B,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-safe-array-length-guard.d.mts","sourceRoot":"","sources":["../../src/rules/prefer-safe-array-length-guard.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EAEd,MAAM,0BAA0B,CAAC;AAYlC,KAAK,OAAO,GAAG,SAAS,EAAE,CAAC;AAE3B,KAAK,UAAU,GAAG,YAAY,GAAG,eAAe,CAAC;AAwCjD,eAAO,MAAM,0BAA0B,EAAE,QAAQ,CAAC,UAAU,CAC1D,UAAU,EACV,OAAO,
|
|
1
|
+
{"version":3,"file":"prefer-safe-array-length-guard.d.mts","sourceRoot":"","sources":["../../src/rules/prefer-safe-array-length-guard.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EAEd,MAAM,0BAA0B,CAAC;AAYlC,KAAK,OAAO,GAAG,SAAS,EAAE,CAAC;AAE3B,KAAK,UAAU,GAAG,YAAY,GAAG,eAAe,CAAC;AAwCjD,eAAO,MAAM,0BAA0B,EAAE,QAAQ,CAAC,UAAU,CAC1D,UAAU,EACV,OAAO,CA2GC,CAAC"}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AST_NODE_TYPES, } from '@typescript-eslint/utils';
|
|
2
2
|
import { buildNamedImportFix, getArgumentText, getImportDeclarations, getNamedImports, skipTypeWrappers, } from 'eslint-rule-forge';
|
|
3
|
-
import {
|
|
3
|
+
import { typeHoldsAtEveryLayer } from './ast-utils.mjs';
|
|
4
4
|
import { TS_STD_FORGE_MODULE } from './constants.mjs';
|
|
5
5
|
|
|
6
6
|
|
|
@@ -48,7 +48,8 @@ export const preferSafeArrayLengthGuard = {
|
|
|
48
48
|
const sourceCode = context.sourceCode;
|
|
49
49
|
const program = sourceCode.ast;
|
|
50
50
|
const tsStdForgeImports = getImportDeclarations(program, TS_STD_FORGE_MODULE);
|
|
51
|
-
|
|
51
|
+
|
|
52
|
+
|
|
52
53
|
|
|
53
54
|
|
|
54
55
|
|
|
@@ -57,14 +58,7 @@ export const preferSafeArrayLengthGuard = {
|
|
|
57
58
|
|
|
58
59
|
const isArrayLike = (
|
|
59
60
|
|
|
60
|
-
expression) =>
|
|
61
|
-
if (checker === undefined) {
|
|
62
|
-
return false;
|
|
63
|
-
}
|
|
64
|
-
const type = getValueType(sourceCode, expression);
|
|
65
|
-
return (type !== undefined &&
|
|
66
|
-
(checker.isArrayType(type) || checker.isTupleType(type)));
|
|
67
|
-
};
|
|
61
|
+
expression) => typeHoldsAtEveryLayer(sourceCode, expression, (type, checker) => checker.isArrayType(type) || checker.isTupleType(type));
|
|
68
62
|
const mut_nodesToFix = [];
|
|
69
63
|
return {
|
|
70
64
|
BinaryExpression: (node) => {
|