eslint-plugin-ts-std-forge 0.1.11 → 0.1.13
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/plugin.d.mts +3 -3
- package/dist/rules/prefer-is-non-null-object.d.mts +1 -5
- package/dist/rules/prefer-is-non-null-object.d.mts.map +1 -1
- package/dist/rules/prefer-is-non-null-object.mjs +4 -184
- package/dist/rules/prefer-is-non-null-object.mjs.map +1 -1
- package/dist/rules/prefer-is-record-and-has-key.d.mts +1 -5
- package/dist/rules/prefer-is-record-and-has-key.d.mts.map +1 -1
- package/dist/rules/prefer-is-record-and-has-key.mjs +4 -219
- package/dist/rules/prefer-is-record-and-has-key.mjs.map +1 -1
- package/dist/rules/prefer-safe-array-is-array.d.mts +1 -5
- package/dist/rules/prefer-safe-array-is-array.d.mts.map +1 -1
- package/dist/rules/prefer-safe-array-is-array.mjs +5 -78
- package/dist/rules/prefer-safe-array-is-array.mjs.map +1 -1
- package/dist/rules/prefer-safe-array-length-guard.mjs +1 -2
- package/dist/rules/prefer-safe-array-length-guard.mjs.map +1 -1
- package/dist/rules/prefer-safe-number-parse-integer.d.mts +1 -12
- package/dist/rules/prefer-safe-number-parse-integer.d.mts.map +1 -1
- package/dist/rules/prefer-safe-number-parse-integer.mjs +6 -108
- package/dist/rules/prefer-safe-number-parse-integer.mjs.map +1 -1
- package/dist/rules/prefer-safe-number-parse.d.mts +1 -12
- package/dist/rules/prefer-safe-number-parse.d.mts.map +1 -1
- package/dist/rules/prefer-safe-number-parse.mjs +6 -103
- package/dist/rules/prefer-safe-number-parse.mjs.map +1 -1
- package/dist/rules/rules.d.mts +3 -3
- package/package.json +9 -9
- package/src/rules/prefer-is-non-null-object.mts +4 -274
- package/src/rules/prefer-is-record-and-has-key.mts +4 -322
- package/src/rules/prefer-is-record-and-has-key.test.mts +48 -0
- package/src/rules/prefer-safe-array-is-array.mts +5 -119
- package/src/rules/prefer-safe-array-is-array.test.mts +18 -14
- package/src/rules/prefer-safe-array-length-guard.mts +1 -1
- package/src/rules/prefer-safe-number-parse-integer.mts +6 -172
- package/src/rules/prefer-safe-number-parse-integer.test.mts +22 -8
- package/src/rules/prefer-safe-number-parse.mts +6 -163
- package/src/rules/prefer-safe-number-parse.test.mts +30 -9
- package/dist/rules/ast-utils.d.mts +0 -18
- package/dist/rules/ast-utils.d.mts.map +0 -1
- package/dist/rules/ast-utils.mjs +0 -39
- package/dist/rules/ast-utils.mjs.map +0 -1
- package/src/rules/ast-utils.mts +0 -57
|
@@ -1,110 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { buildNamedImportFix, getArgumentText, getImportDeclarations, getNamedImports, skipTypeWrappers, } from 'eslint-rule-forge';
|
|
3
|
-
import * as ts from 'typescript';
|
|
4
|
-
import { isIdentifierNamed, typeHoldsAtEveryLayer } from './ast-utils.mjs';
|
|
1
|
+
import { createPreferSafeParseInt } from 'eslint-rule-forge';
|
|
5
2
|
import { TS_STD_FORGE_MODULE } from './constants.mjs';
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
const isStringType = (type) => {
|
|
14
|
-
const parts = type.isUnion() ? type.types : [type];
|
|
15
|
-
return (parts.length > 0 &&
|
|
16
|
-
parts.every((t) => (t.flags & ts.TypeFlags.StringLike) !== 0));
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
export const preferSafeNumberParseInteger = {
|
|
26
|
-
meta: {
|
|
27
|
-
type: 'suggestion',
|
|
28
|
-
docs: {
|
|
29
|
-
description: 'Replace `parseInt(x)` or `Number.parseInt(x, 10)` with `Result.unwrapOkOr(SafeNumber.parseInteger(x), Number.NaN)` from ts-std-forge.',
|
|
30
|
-
},
|
|
31
|
-
fixable: 'code',
|
|
32
|
-
schema: [],
|
|
33
|
-
messages: {
|
|
34
|
-
useSafeNumberParseInteger: 'Replace `{{original}}` with `Result.unwrapOkOr(SafeNumber.parseInteger({{argName}}), Number.NaN)` from ts-std-forge.',
|
|
35
|
-
},
|
|
36
|
-
},
|
|
37
|
-
create: (context) => {
|
|
38
|
-
const sourceCode = context.sourceCode;
|
|
39
|
-
const program = sourceCode.ast;
|
|
40
|
-
const tsStdForgeImports = getImportDeclarations(program, TS_STD_FORGE_MODULE);
|
|
41
|
-
const mut_nodesToFix = [];
|
|
42
|
-
return {
|
|
43
|
-
CallExpression: (node) => {
|
|
44
|
-
const callee = skipTypeWrappers(node.callee);
|
|
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
|
-
isIdentifierNamed(callee.object, 'Number') &&
|
|
51
|
-
callee.property.type === AST_NODE_TYPES.Identifier &&
|
|
52
|
-
callee.property.name === 'parseInt';
|
|
53
|
-
if (!isGlobalParseInt && !isNumberParseInt) {
|
|
54
|
-
return;
|
|
55
|
-
}
|
|
56
|
-
const args = node.arguments;
|
|
57
|
-
const firstArg = args[0];
|
|
58
|
-
if (firstArg === undefined) {
|
|
59
|
-
return;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
if (firstArg.type === AST_NODE_TYPES.SpreadElement) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
if (args.length >= 2) {
|
|
68
|
-
const radix = args[1] === undefined ? undefined : skipTypeWrappers(args[1]);
|
|
69
|
-
if (radix?.type !== AST_NODE_TYPES.Literal || radix.value !== 10) {
|
|
70
|
-
return;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
if (!typeHoldsAtEveryLayer(sourceCode, firstArg, isStringType)) {
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
mut_nodesToFix.push({ node, argExpression: firstArg });
|
|
83
|
-
},
|
|
84
|
-
'Program:exit': () => {
|
|
85
|
-
const namedImports = getNamedImports(tsStdForgeImports);
|
|
86
|
-
const missingImports = ['Result', 'SafeNumber'].filter((name) => !namedImports.includes(name));
|
|
87
|
-
for (const [index, { node, argExpression },] of mut_nodesToFix.entries()) {
|
|
88
|
-
const argText = getArgumentText(sourceCode, argExpression);
|
|
89
|
-
context.report({
|
|
90
|
-
node,
|
|
91
|
-
messageId: 'useSafeNumberParseInteger',
|
|
92
|
-
data: {
|
|
93
|
-
original: sourceCode.getText(node),
|
|
94
|
-
argName: argText,
|
|
95
|
-
},
|
|
96
|
-
fix: (fixer) => {
|
|
97
|
-
const core = `Result.unwrapOkOr(SafeNumber.parseInteger(${argText}), Number.NaN)`;
|
|
98
|
-
const importFixes = index === 0 && missingImports.length > 0
|
|
99
|
-
? buildNamedImportFix(fixer, program, TS_STD_FORGE_MODULE, missingImports)
|
|
100
|
-
: [];
|
|
101
|
-
return [...importFixes, fixer.replaceText(node, core)];
|
|
102
|
-
},
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
},
|
|
106
|
-
};
|
|
107
|
-
},
|
|
108
|
-
defaultOptions: [],
|
|
109
|
-
};
|
|
3
|
+
export const preferSafeNumberParseInteger = createPreferSafeParseInt({
|
|
4
|
+
moduleName: TS_STD_FORGE_MODULE,
|
|
5
|
+
namespace: 'SafeNumber',
|
|
6
|
+
method: 'parseInteger',
|
|
7
|
+
});
|
|
110
8
|
//# sourceMappingURL=prefer-safe-number-parse-integer.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-safe-number-parse-integer.mjs","sourceRoot":"","sources":["../../src/rules/prefer-safe-number-parse-integer.mts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"prefer-safe-number-parse-integer.mjs","sourceRoot":"","sources":["../../src/rules/prefer-safe-number-parse-integer.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD,MAAM,CAAC,MAAM,4BAA4B,GAAG,wBAAwB,CAAC;IACnE,UAAU,EAAE,mBAAmB;IAC/B,SAAS,EAAE,YAAY;IACvB,MAAM,EAAE,cAAc;CACvB,CAAC,CAAC"}
|
|
@@ -1,19 +1,8 @@
|
|
|
1
|
-
import { type TSESLint } from '@typescript-eslint/utils';
|
|
2
|
-
type Options = readonly [];
|
|
3
|
-
type MessageIds = 'useSafeNumberParse';
|
|
4
1
|
/**
|
|
5
|
-
* Detects `parseFloat(x)`, `Number.parseFloat(x)`, and `Number(x)` (when x
|
|
6
|
-
* is typed as `string`) and replaces them with
|
|
7
|
-
* `Result.unwrapOkOr(SafeNumber.parse(x), Number.NaN)` (ts-std-forge).
|
|
8
|
-
*
|
|
9
|
-
* `Number(x)` is only flagged when x is purely a string type to avoid false
|
|
10
|
-
* positives for `Number(someBoolean)` or `Number(someNumber)` uses.
|
|
11
|
-
*
|
|
12
2
|
* The ts-data-forge rule of the same shape names `Num.safeParseFloat`, which
|
|
13
3
|
* has delegated to `SafeNumber.parse` since the D-49 inversion and wraps the
|
|
14
4
|
* result in a `FiniteNumber` brand. This one names the implementation, for
|
|
15
5
|
* callers who do not want the brand or the extra dependency.
|
|
16
6
|
*/
|
|
17
|
-
export declare const preferSafeNumberParse:
|
|
18
|
-
export {};
|
|
7
|
+
export declare const preferSafeNumberParse: import("@typescript-eslint/utils/ts-eslint").RuleModule<"useSafeParseFloat", readonly [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
|
|
19
8
|
//# sourceMappingURL=prefer-safe-number-parse.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-safe-number-parse.d.mts","sourceRoot":"","sources":["../../src/rules/prefer-safe-number-parse.mts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"prefer-safe-number-parse.d.mts","sourceRoot":"","sources":["../../src/rules/prefer-safe-number-parse.mts"],"names":[],"mappings":"AAGA;;;;;GAKG;AACH,eAAO,MAAM,qBAAqB,+JAIhC,CAAC"}
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { buildNamedImportFix, getArgumentText, getImportDeclarations, getNamedImports, skipTypeWrappers, } from 'eslint-rule-forge';
|
|
3
|
-
import * as ts from 'typescript';
|
|
4
|
-
import { isIdentifierNamed, typeHoldsAtEveryLayer } from './ast-utils.mjs';
|
|
1
|
+
import { createPreferSafeParseFloat } from 'eslint-rule-forge';
|
|
5
2
|
import { TS_STD_FORGE_MODULE } from './constants.mjs';
|
|
6
3
|
|
|
7
4
|
|
|
@@ -9,103 +6,9 @@ import { TS_STD_FORGE_MODULE } from './constants.mjs';
|
|
|
9
6
|
|
|
10
7
|
|
|
11
8
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
};
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
export const preferSafeNumberParse = {
|
|
32
|
-
meta: {
|
|
33
|
-
type: 'suggestion',
|
|
34
|
-
docs: {
|
|
35
|
-
description: 'Replace `parseFloat(x)`, `Number.parseFloat(x)`, or `Number(x)` (when x is a string) with `Result.unwrapOkOr(SafeNumber.parse(x), Number.NaN)` from ts-std-forge.',
|
|
36
|
-
},
|
|
37
|
-
fixable: 'code',
|
|
38
|
-
schema: [],
|
|
39
|
-
messages: {
|
|
40
|
-
useSafeNumberParse: 'Replace `{{original}}` with `Result.unwrapOkOr(SafeNumber.parse({{argName}}), Number.NaN)` from ts-std-forge.',
|
|
41
|
-
},
|
|
42
|
-
},
|
|
43
|
-
create: (context) => {
|
|
44
|
-
const sourceCode = context.sourceCode;
|
|
45
|
-
const program = sourceCode.ast;
|
|
46
|
-
const tsStdForgeImports = getImportDeclarations(program, TS_STD_FORGE_MODULE);
|
|
47
|
-
const mut_nodesToFix = [];
|
|
48
|
-
return {
|
|
49
|
-
CallExpression: (node) => {
|
|
50
|
-
const callee = skipTypeWrappers(node.callee);
|
|
51
|
-
|
|
52
|
-
const isGlobalParseFloat = callee.type === AST_NODE_TYPES.Identifier &&
|
|
53
|
-
callee.name === 'parseFloat';
|
|
54
|
-
|
|
55
|
-
const isNumberParseFloat = callee.type === AST_NODE_TYPES.MemberExpression &&
|
|
56
|
-
!callee.computed &&
|
|
57
|
-
isIdentifierNamed(callee.object, 'Number') &&
|
|
58
|
-
callee.property.type === AST_NODE_TYPES.Identifier &&
|
|
59
|
-
callee.property.name === 'parseFloat';
|
|
60
|
-
|
|
61
|
-
const isNumberCall = callee.type === AST_NODE_TYPES.Identifier && callee.name === 'Number';
|
|
62
|
-
if (!isGlobalParseFloat && !isNumberParseFloat && !isNumberCall) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
const args = node.arguments;
|
|
66
|
-
const firstArg = args[0];
|
|
67
|
-
if (firstArg === undefined) {
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (firstArg.type === AST_NODE_TYPES.SpreadElement) {
|
|
72
|
-
return;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
if (!typeHoldsAtEveryLayer(sourceCode, firstArg, isStringType)) {
|
|
81
|
-
return;
|
|
82
|
-
}
|
|
83
|
-
mut_nodesToFix.push({ node, argExpression: firstArg });
|
|
84
|
-
},
|
|
85
|
-
'Program:exit': () => {
|
|
86
|
-
const namedImports = getNamedImports(tsStdForgeImports);
|
|
87
|
-
const missingImports = ['Result', 'SafeNumber'].filter((name) => !namedImports.includes(name));
|
|
88
|
-
for (const [index, { node, argExpression },] of mut_nodesToFix.entries()) {
|
|
89
|
-
const argText = getArgumentText(sourceCode, argExpression);
|
|
90
|
-
context.report({
|
|
91
|
-
node,
|
|
92
|
-
messageId: 'useSafeNumberParse',
|
|
93
|
-
data: {
|
|
94
|
-
original: sourceCode.getText(node),
|
|
95
|
-
argName: argText,
|
|
96
|
-
},
|
|
97
|
-
fix: (fixer) => {
|
|
98
|
-
const core = `Result.unwrapOkOr(SafeNumber.parse(${argText}), Number.NaN)`;
|
|
99
|
-
const importFixes = index === 0 && missingImports.length > 0
|
|
100
|
-
? buildNamedImportFix(fixer, program, TS_STD_FORGE_MODULE, missingImports)
|
|
101
|
-
: [];
|
|
102
|
-
return [...importFixes, fixer.replaceText(node, core)];
|
|
103
|
-
},
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
},
|
|
107
|
-
};
|
|
108
|
-
},
|
|
109
|
-
defaultOptions: [],
|
|
110
|
-
};
|
|
9
|
+
export const preferSafeNumberParse = createPreferSafeParseFloat({
|
|
10
|
+
moduleName: TS_STD_FORGE_MODULE,
|
|
11
|
+
namespace: 'SafeNumber',
|
|
12
|
+
method: 'parse',
|
|
13
|
+
});
|
|
111
14
|
//# sourceMappingURL=prefer-safe-number-parse.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"prefer-safe-number-parse.mjs","sourceRoot":"","sources":["../../src/rules/prefer-safe-number-parse.mts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"prefer-safe-number-parse.mjs","sourceRoot":"","sources":["../../src/rules/prefer-safe-number-parse.mts"],"names":[],"mappings":"AAAA,OAAO,EAAE,0BAA0B,EAAE,MAAM,mBAAmB,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAEtD;;;;;GAKG;AACH,MAAM,CAAC,MAAM,qBAAqB,GAAG,0BAA0B,CAAC;IAC9D,UAAU,EAAE,mBAAmB;IAC/B,SAAS,EAAE,YAAY;IACvB,MAAM,EAAE,OAAO;CAChB,CAAC,CAAC"}
|
package/dist/rules/rules.d.mts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
export declare const tsStdForgeRules: {
|
|
2
2
|
readonly 'prefer-is-non-null-object': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useIsNonNullObject", readonly [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
|
|
3
3
|
readonly 'prefer-is-record-and-has-key': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useHasKey" | "useIsRecordAndHasKey", readonly [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
|
|
4
|
-
readonly 'prefer-safe-array-is-array': import("@typescript-eslint/utils/ts-eslint").RuleModule<"
|
|
4
|
+
readonly 'prefer-safe-array-is-array': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useIsArray", readonly [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
|
|
5
5
|
readonly 'prefer-safe-array-length-guard': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useIsEmpty" | "useIsNonEmpty", readonly [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
|
|
6
|
-
readonly 'prefer-safe-number-parse': import("@typescript-eslint/utils/ts-eslint").RuleModule<"
|
|
7
|
-
readonly 'prefer-safe-number-parse-integer': import("@typescript-eslint/utils/ts-eslint").RuleModule<"
|
|
6
|
+
readonly 'prefer-safe-number-parse': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useSafeParseFloat", readonly [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
|
|
7
|
+
readonly 'prefer-safe-number-parse-integer': import("@typescript-eslint/utils/ts-eslint").RuleModule<"useSafeParseInt", readonly [], unknown, import("@typescript-eslint/utils/ts-eslint").RuleListener>;
|
|
8
8
|
};
|
|
9
9
|
//# sourceMappingURL=rules.d.mts.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-plugin-ts-std-forge",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.13",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "ESLint rules that steer code toward ts-std-forge idioms.",
|
|
6
6
|
"keywords": [
|
|
@@ -34,19 +34,19 @@
|
|
|
34
34
|
"LICENSE"
|
|
35
35
|
],
|
|
36
36
|
"dependencies": {
|
|
37
|
-
"@typescript-eslint/utils": "^8.71.
|
|
38
|
-
"eslint-rule-forge": "^0.
|
|
37
|
+
"@typescript-eslint/utils": "^8.71.2",
|
|
38
|
+
"eslint-rule-forge": "^0.3.1",
|
|
39
39
|
"ts-std-forge": "0.5.2",
|
|
40
40
|
"ts-type-forge": "~9.2.2"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@typescript-eslint/parser": "8.71.
|
|
44
|
-
"@typescript-eslint/rule-tester": "8.71.
|
|
43
|
+
"@typescript-eslint/parser": "8.71.2",
|
|
44
|
+
"@typescript-eslint/rule-tester": "8.71.2",
|
|
45
45
|
"dedent": "1.7.2",
|
|
46
|
-
"eslint-config-typed": "5.19.
|
|
47
|
-
"eslint-plugin-ts-fortress": "1.3.
|
|
48
|
-
"eslint-plugin-ts-type-forge": "0.8.
|
|
49
|
-
"prettier": "3.9.
|
|
46
|
+
"eslint-config-typed": "5.19.2",
|
|
47
|
+
"eslint-plugin-ts-fortress": "1.3.8",
|
|
48
|
+
"eslint-plugin-ts-type-forge": "0.8.13",
|
|
49
|
+
"prettier": "3.9.10",
|
|
50
50
|
"prettier-plugin-organize-imports": "4.3.0",
|
|
51
51
|
"prettier-plugin-packagejson": "3.0.2",
|
|
52
52
|
"ts-repo-utils": "10.7.1"
|
|
@@ -1,276 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
AST_NODE_TYPES,
|
|
3
|
-
type TSESLint,
|
|
4
|
-
type TSESTree,
|
|
5
|
-
} from '@typescript-eslint/utils';
|
|
6
|
-
import {
|
|
7
|
-
buildNamedImportFix,
|
|
8
|
-
getImportDeclarations,
|
|
9
|
-
getNamedImports,
|
|
10
|
-
getRangeWithParens,
|
|
11
|
-
parenthesesDepth,
|
|
12
|
-
skipTypeWrappers,
|
|
13
|
-
} from 'eslint-rule-forge';
|
|
1
|
+
import { createPreferIsNonNullObject } from 'eslint-rule-forge';
|
|
14
2
|
import { TS_STD_FORGE_MODULE } from './constants.mjs';
|
|
15
3
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
export const preferIsNonNullObject: TSESLint.RuleModule<MessageIds, Options> = {
|
|
21
|
-
meta: {
|
|
22
|
-
type: 'suggestion',
|
|
23
|
-
docs: {
|
|
24
|
-
description:
|
|
25
|
-
'Replace `typeof u === "object" && u !== null`, in either order and anywhere in an `&&` chain, with `isNonNullObject(u)` from ts-std-forge.',
|
|
26
|
-
},
|
|
27
|
-
fixable: 'code',
|
|
28
|
-
schema: [],
|
|
29
|
-
messages: {
|
|
30
|
-
useIsNonNullObject:
|
|
31
|
-
'Replace the object/null check with `isNonNullObject()` from ts-std-forge.',
|
|
32
|
-
},
|
|
33
|
-
},
|
|
34
|
-
|
|
35
|
-
create: (context) => {
|
|
36
|
-
const sourceCode = context.sourceCode;
|
|
37
|
-
|
|
38
|
-
const program = sourceCode.ast;
|
|
39
|
-
|
|
40
|
-
const tsStdForgeImports = getImportDeclarations(
|
|
41
|
-
program,
|
|
42
|
-
TS_STD_FORGE_MODULE,
|
|
43
|
-
);
|
|
44
|
-
|
|
45
|
-
const mut_pairsToFix: NonNullObjectPair[] = [];
|
|
46
|
-
|
|
47
|
-
return {
|
|
48
|
-
LogicalExpression: (node) => {
|
|
49
|
-
// A chain is read once, from its outermost `&&`.
|
|
50
|
-
if (node.operator !== '&&' || isChainOperand(sourceCode, node)) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
mut_pairsToFix.push(...findNonNullObjectPairs(sourceCode, node));
|
|
55
|
-
},
|
|
56
|
-
'Program:exit': () => {
|
|
57
|
-
const hasIsNonNullObjectImport =
|
|
58
|
-
getNamedImports(tsStdForgeImports).includes('isNonNullObject');
|
|
59
|
-
|
|
60
|
-
// The import fix lives on the first report only:
|
|
61
|
-
// `insertTextBefore(program, …)` produces overlapping ranges
|
|
62
|
-
// otherwise and only one of them would survive a pass. A first report
|
|
63
|
-
// silenced by an `eslint-disable` comment therefore takes the import
|
|
64
|
-
// with it.
|
|
65
|
-
for (const [
|
|
66
|
-
index,
|
|
67
|
-
{ range, identifierName },
|
|
68
|
-
] of mut_pairsToFix.entries()) {
|
|
69
|
-
context.report({
|
|
70
|
-
loc: {
|
|
71
|
-
start: sourceCode.getLocFromIndex(range[0]),
|
|
72
|
-
end: sourceCode.getLocFromIndex(range[1]),
|
|
73
|
-
},
|
|
74
|
-
messageId: 'useIsNonNullObject',
|
|
75
|
-
fix: (fixer) => {
|
|
76
|
-
const replacement = `isNonNullObject(${identifierName})`;
|
|
77
|
-
|
|
78
|
-
const importFixes =
|
|
79
|
-
index === 0 && !hasIsNonNullObjectImport
|
|
80
|
-
? buildNamedImportFix(fixer, program, TS_STD_FORGE_MODULE, [
|
|
81
|
-
'isNonNullObject',
|
|
82
|
-
])
|
|
83
|
-
: [];
|
|
84
|
-
|
|
85
|
-
return [
|
|
86
|
-
...importFixes,
|
|
87
|
-
fixer.replaceTextRange(range, replacement),
|
|
88
|
-
];
|
|
89
|
-
},
|
|
90
|
-
});
|
|
91
|
-
}
|
|
92
|
-
},
|
|
93
|
-
};
|
|
94
|
-
},
|
|
95
|
-
defaultOptions: [],
|
|
96
|
-
} as const;
|
|
97
|
-
|
|
98
|
-
/**
|
|
99
|
-
* A `typeof u === 'object'` and a `u !== null` next to each other in an `&&`
|
|
100
|
-
* chain, as the source range from the first to the second (parentheses
|
|
101
|
-
* included) and the name of `u`.
|
|
102
|
-
*/
|
|
103
|
-
type NonNullObjectPair = Readonly<{
|
|
104
|
-
range: TSESTree.Range;
|
|
105
|
-
identifierName: string;
|
|
106
|
-
}>;
|
|
107
|
-
|
|
108
|
-
/**
|
|
109
|
-
* Whether `node` is an operand of an enclosing `&&` chain: a `&&` written
|
|
110
|
-
* directly under another one, without parentheses.
|
|
111
|
-
*/
|
|
112
|
-
const isChainOperand = (
|
|
113
|
-
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
114
|
-
sourceCode: TSESLint.SourceCode,
|
|
115
|
-
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
116
|
-
node: TSESTree.LogicalExpression,
|
|
117
|
-
): boolean =>
|
|
118
|
-
node.parent.type === AST_NODE_TYPES.LogicalExpression &&
|
|
119
|
-
node.parent.operator === '&&' &&
|
|
120
|
-
parenthesesDepth(sourceCode, node) === 0;
|
|
121
|
-
|
|
122
|
-
/**
|
|
123
|
-
* Every non-overlapping pair in the `&&` chain `node` heads, left to right. The
|
|
124
|
-
* two halves may come in either order, and the pair may have operands before
|
|
125
|
-
* and after it: `a && u !== null && typeof u === 'object' && b`. They have to
|
|
126
|
-
* be next to each other, so that the pair is one range of the source.
|
|
127
|
-
*/
|
|
128
|
-
const findNonNullObjectPairs = (
|
|
129
|
-
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
130
|
-
sourceCode: TSESLint.SourceCode,
|
|
131
|
-
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
132
|
-
node: TSESTree.LogicalExpression,
|
|
133
|
-
): readonly NonNullObjectPair[] => {
|
|
134
|
-
const operands = chainOperands(sourceCode, node);
|
|
135
|
-
|
|
136
|
-
const pairsFrom = (index: number): readonly NonNullObjectPair[] => {
|
|
137
|
-
const first = operands[index];
|
|
138
|
-
|
|
139
|
-
const second = operands[index + 1];
|
|
140
|
-
|
|
141
|
-
if (first === undefined || second === undefined) {
|
|
142
|
-
return [];
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
const identifierName =
|
|
146
|
-
pairIdentifierName(first, second) ?? pairIdentifierName(second, first);
|
|
147
|
-
|
|
148
|
-
return identifierName === undefined
|
|
149
|
-
? pairsFrom(index + 1)
|
|
150
|
-
: [
|
|
151
|
-
{
|
|
152
|
-
range: [
|
|
153
|
-
getRangeWithParens(sourceCode, first)[0],
|
|
154
|
-
getRangeWithParens(sourceCode, second)[1],
|
|
155
|
-
],
|
|
156
|
-
identifierName,
|
|
157
|
-
},
|
|
158
|
-
...pairsFrom(index + 2),
|
|
159
|
-
];
|
|
160
|
-
};
|
|
161
|
-
|
|
162
|
-
return pairsFrom(0);
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* The operands of the `&&` chain `node`, left to right. A parenthesized `&&`
|
|
167
|
-
* is one operand: its halves cannot be replaced without its parentheses.
|
|
168
|
-
*/
|
|
169
|
-
const chainOperands = (
|
|
170
|
-
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
171
|
-
sourceCode: TSESLint.SourceCode,
|
|
172
|
-
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
173
|
-
node: TSESTree.LogicalExpression,
|
|
174
|
-
): readonly TSESTree.Expression[] =>
|
|
175
|
-
[node.left, node.right].flatMap((operand) =>
|
|
176
|
-
operand.type === AST_NODE_TYPES.LogicalExpression &&
|
|
177
|
-
operand.operator === '&&' &&
|
|
178
|
-
parenthesesDepth(sourceCode, operand) === 0
|
|
179
|
-
? chainOperands(sourceCode, operand)
|
|
180
|
-
: [operand],
|
|
181
|
-
);
|
|
182
|
-
|
|
183
|
-
// Every operand below is read through its type wrappers: `typeof (u satisfies
|
|
184
|
-
// unknown) === 'object' && u! !== null` is the same check.
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* The name of `u` when `typeofCheck` is `typeof u === 'object'` and
|
|
188
|
-
* `nullCheck` is `u !== null` for the same `u`.
|
|
189
|
-
*/
|
|
190
|
-
const pairIdentifierName = (
|
|
191
|
-
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
192
|
-
typeofCheck: TSESTree.Expression,
|
|
193
|
-
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
194
|
-
nullCheck: TSESTree.Expression,
|
|
195
|
-
): string | undefined => {
|
|
196
|
-
const identifierName = getTypeofObjectIdentifierName(
|
|
197
|
-
skipTypeWrappers(typeofCheck),
|
|
198
|
-
);
|
|
199
|
-
|
|
200
|
-
return identifierName !== undefined &&
|
|
201
|
-
identifierName ===
|
|
202
|
-
getNonNullCheckIdentifierName(skipTypeWrappers(nullCheck))
|
|
203
|
-
? identifierName
|
|
204
|
-
: undefined;
|
|
205
|
-
};
|
|
206
|
-
|
|
207
|
-
/** `u` of `typeof u === 'object'`, in either operand order. */
|
|
208
|
-
const getTypeofObjectIdentifierName = (
|
|
209
|
-
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
210
|
-
node: TSESTree.Node,
|
|
211
|
-
): string | undefined => {
|
|
212
|
-
if (
|
|
213
|
-
node.type !== AST_NODE_TYPES.BinaryExpression ||
|
|
214
|
-
node.operator !== '==='
|
|
215
|
-
) {
|
|
216
|
-
return undefined;
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
const left = skipTypeWrappers(node.left);
|
|
220
|
-
|
|
221
|
-
const right = skipTypeWrappers(node.right);
|
|
222
|
-
|
|
223
|
-
const typeofSide = isLiteralOf(right, 'object')
|
|
224
|
-
? left
|
|
225
|
-
: isLiteralOf(left, 'object')
|
|
226
|
-
? right
|
|
227
|
-
: undefined;
|
|
228
|
-
|
|
229
|
-
if (
|
|
230
|
-
typeofSide?.type !== AST_NODE_TYPES.UnaryExpression ||
|
|
231
|
-
typeofSide.operator !== 'typeof'
|
|
232
|
-
) {
|
|
233
|
-
return undefined;
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
const argument = skipTypeWrappers(typeofSide.argument);
|
|
237
|
-
|
|
238
|
-
return argument.type === AST_NODE_TYPES.Identifier
|
|
239
|
-
? argument.name
|
|
240
|
-
: undefined;
|
|
241
|
-
};
|
|
242
|
-
|
|
243
|
-
/**
|
|
244
|
-
* `u` of `u !== null`, in either operand order. `u != null` also excludes
|
|
245
|
-
* `undefined`, which `typeof u === 'object'` has already excluded, so it is
|
|
246
|
-
* the same check here.
|
|
247
|
-
*/
|
|
248
|
-
const getNonNullCheckIdentifierName = (
|
|
249
|
-
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
250
|
-
node: TSESTree.Node,
|
|
251
|
-
): string | undefined => {
|
|
252
|
-
if (
|
|
253
|
-
node.type !== AST_NODE_TYPES.BinaryExpression ||
|
|
254
|
-
(node.operator !== '!==' && node.operator !== '!=')
|
|
255
|
-
) {
|
|
256
|
-
return undefined;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
const left = skipTypeWrappers(node.left);
|
|
260
|
-
|
|
261
|
-
const right = skipTypeWrappers(node.right);
|
|
262
|
-
|
|
263
|
-
const checked = isLiteralOf(right, null)
|
|
264
|
-
? left
|
|
265
|
-
: isLiteralOf(left, null)
|
|
266
|
-
? right
|
|
267
|
-
: undefined;
|
|
268
|
-
|
|
269
|
-
return checked?.type === AST_NODE_TYPES.Identifier ? checked.name : undefined;
|
|
270
|
-
};
|
|
271
|
-
|
|
272
|
-
const isLiteralOf = (
|
|
273
|
-
// eslint-disable-next-line @typescript-eslint/prefer-readonly-parameter-types
|
|
274
|
-
node: TSESTree.Node,
|
|
275
|
-
value: string | null,
|
|
276
|
-
): boolean => node.type === AST_NODE_TYPES.Literal && node.value === value;
|
|
4
|
+
export const preferIsNonNullObject = createPreferIsNonNullObject({
|
|
5
|
+
moduleName: TS_STD_FORGE_MODULE,
|
|
6
|
+
});
|