eslint-rule-forge 0.2.0 → 0.3.0
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 +28 -9
- package/dist/index.d.mts +1 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/rules/index.d.mts +6 -0
- package/dist/rules/index.d.mts.map +1 -0
- package/dist/rules/index.mjs +6 -0
- package/dist/rules/index.mjs.map +1 -0
- package/dist/rules/no-side-effect-import.d.mts +18 -0
- package/dist/rules/no-side-effect-import.d.mts.map +1 -0
- package/dist/rules/no-side-effect-import.mjs +45 -0
- package/dist/rules/no-side-effect-import.mjs.map +1 -0
- package/dist/rules/prefer-array-is-array.d.mts +13 -0
- package/dist/rules/prefer-array-is-array.d.mts.map +1 -0
- package/dist/rules/prefer-array-is-array.mjs +81 -0
- package/dist/rules/prefer-array-is-array.mjs.map +1 -0
- package/dist/rules/prefer-is-non-null-object.d.mts +11 -0
- package/dist/rules/prefer-is-non-null-object.d.mts.map +1 -0
- package/dist/rules/prefer-is-non-null-object.mjs +192 -0
- package/dist/rules/prefer-is-non-null-object.mjs.map +1 -0
- package/dist/rules/prefer-is-record-and-has-key.d.mts +11 -0
- package/dist/rules/prefer-is-record-and-has-key.d.mts.map +1 -0
- package/dist/rules/prefer-is-record-and-has-key.mjs +226 -0
- package/dist/rules/prefer-is-record-and-has-key.mjs.map +1 -0
- package/dist/rules/prefer-safe-parse.d.mts +31 -0
- package/dist/rules/prefer-safe-parse.d.mts.map +1 -0
- package/dist/rules/prefer-safe-parse.mjs +139 -0
- package/dist/rules/prefer-safe-parse.mjs.map +1 -0
- package/dist/type-wrappers.d.mts +17 -1
- package/dist/type-wrappers.d.mts.map +1 -1
- package/dist/type-wrappers.mjs +35 -1
- package/dist/type-wrappers.mjs.map +1 -1
- package/package.json +5 -5
- package/src/index.mts +1 -0
- package/src/rules/index.mts +5 -0
- package/src/rules/no-side-effect-import.mts +68 -0
- package/src/rules/prefer-array-is-array.mts +119 -0
- package/src/rules/prefer-is-non-null-object.mts +276 -0
- package/src/rules/prefer-is-record-and-has-key.mts +321 -0
- package/src/rules/prefer-safe-parse.mts +252 -0
- package/src/type-wrappers.mts +55 -1
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, } from '@typescript-eslint/utils';
|
|
2
|
+
import * as ts from 'typescript';
|
|
3
|
+
import { buildNamedImportFix, getImportDeclarations, getNamedImports, } from '../imports.mjs';
|
|
4
|
+
import { getArgumentText } from '../parentheses.mjs';
|
|
5
|
+
import { isIdentifierNamed, skipTypeWrappers, typeHoldsAtEveryLayer, } from '../type-wrappers.mjs';
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
export const createPreferIsRecordAndHasKey = ({ moduleName, }) => ({
|
|
12
|
+
meta: {
|
|
13
|
+
type: 'suggestion',
|
|
14
|
+
docs: {
|
|
15
|
+
description: `Replace \`Object.hasOwn(obj, key)\` or \`key in obj\` with \`isRecord(obj) && hasKey(obj, key)\` from ${moduleName}`,
|
|
16
|
+
},
|
|
17
|
+
fixable: 'code',
|
|
18
|
+
schema: [],
|
|
19
|
+
messages: {
|
|
20
|
+
useIsRecordAndHasKey: `Replace \`{{original}}\` with \`isRecord({{objName}}) && hasKey({{objName}}, {{keyName}})\` from ${moduleName}.`,
|
|
21
|
+
useHasKey: `Replace \`{{original}}\` with \`hasKey({{objName}}, {{keyName}})\` from ${moduleName}.`,
|
|
22
|
+
},
|
|
23
|
+
},
|
|
24
|
+
create: (context) => {
|
|
25
|
+
const sourceCode = context.sourceCode;
|
|
26
|
+
const program = sourceCode.ast;
|
|
27
|
+
const moduleImports = getImportDeclarations(program, moduleName);
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
const isKnownRecord = (
|
|
38
|
+
|
|
39
|
+
expression) => typeHoldsAtEveryLayer(sourceCode, expression, (type, checker) => isAlreadyRecordType(checker, type));
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
const mayBeArray = (
|
|
48
|
+
|
|
49
|
+
expression) => sourceCode.parserServices?.program != null &&
|
|
50
|
+
!typeHoldsAtEveryLayer(sourceCode, expression, (type, checker) => !mayBeArrayType(checker, type));
|
|
51
|
+
const mut_nodesToFix = [];
|
|
52
|
+
return {
|
|
53
|
+
|
|
54
|
+
CallExpression: (node) => {
|
|
55
|
+
const callee = skipTypeWrappers(node.callee);
|
|
56
|
+
if (callee.type !== AST_NODE_TYPES.MemberExpression ||
|
|
57
|
+
!isIdentifierNamed(callee.object, 'Object') ||
|
|
58
|
+
callee.property.type !== AST_NODE_TYPES.Identifier ||
|
|
59
|
+
callee.property.name !== 'hasOwn') {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (node.arguments.length !== 2) {
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
const objArg = node.arguments[0];
|
|
67
|
+
const keyArg = node.arguments[1];
|
|
68
|
+
if (objArg === undefined || keyArg === undefined) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (objArg.type === AST_NODE_TYPES.SpreadElement ||
|
|
73
|
+
keyArg.type === AST_NODE_TYPES.SpreadElement ||
|
|
74
|
+
mayBeArray(objArg)) {
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
mut_nodesToFix.push({
|
|
78
|
+
node,
|
|
79
|
+
objExpression: objArg,
|
|
80
|
+
keyExpression: keyArg,
|
|
81
|
+
});
|
|
82
|
+
},
|
|
83
|
+
|
|
84
|
+
BinaryExpression: (node) => {
|
|
85
|
+
if (node.operator !== 'in') {
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
const keyExpression = node.left;
|
|
89
|
+
const objExpression = node.right;
|
|
90
|
+
|
|
91
|
+
if (keyExpression.type === AST_NODE_TYPES.PrivateIdentifier ||
|
|
92
|
+
mayBeArray(objExpression)) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
mut_nodesToFix.push({
|
|
96
|
+
node,
|
|
97
|
+
objExpression,
|
|
98
|
+
keyExpression,
|
|
99
|
+
});
|
|
100
|
+
},
|
|
101
|
+
'Program:exit': () => {
|
|
102
|
+
const namedImports = getNamedImports(moduleImports);
|
|
103
|
+
const rewrites = mut_nodesToFix.map((entry) => {
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
const needsIsRecord = !isKnownRecord(entry.objExpression);
|
|
107
|
+
return {
|
|
108
|
+
...entry,
|
|
109
|
+
needsIsRecord,
|
|
110
|
+
|
|
111
|
+
fixable: !needsIsRecord || isSideEffectFree(entry.objExpression),
|
|
112
|
+
};
|
|
113
|
+
});
|
|
114
|
+
const fixableRewrites = rewrites.filter(({ fixable }) => fixable);
|
|
115
|
+
const importsToAdd = fixableRewrites.length === 0
|
|
116
|
+
? []
|
|
117
|
+
: [
|
|
118
|
+
...(fixableRewrites.some(({ needsIsRecord }) => needsIsRecord) && !namedImports.includes('isRecord')
|
|
119
|
+
? ['isRecord']
|
|
120
|
+
: []),
|
|
121
|
+
...(namedImports.includes('hasKey') ? [] : ['hasKey']),
|
|
122
|
+
];
|
|
123
|
+
const importCarrier = fixableRewrites[0];
|
|
124
|
+
for (const rewrite of rewrites) {
|
|
125
|
+
const { node, objExpression, keyExpression, needsIsRecord } = rewrite;
|
|
126
|
+
const objText = getArgumentText(sourceCode, objExpression);
|
|
127
|
+
const keyText = getArgumentText(sourceCode, keyExpression);
|
|
128
|
+
const originalText = sourceCode.getText(node);
|
|
129
|
+
context.report({
|
|
130
|
+
node,
|
|
131
|
+
messageId: needsIsRecord ? 'useIsRecordAndHasKey' : 'useHasKey',
|
|
132
|
+
data: {
|
|
133
|
+
original: originalText,
|
|
134
|
+
objName: objText,
|
|
135
|
+
keyName: keyText,
|
|
136
|
+
},
|
|
137
|
+
fix: !rewrite.fixable
|
|
138
|
+
? undefined
|
|
139
|
+
: (fixer) => {
|
|
140
|
+
const replacement = needsIsRecord
|
|
141
|
+
? `(isRecord(${objText}) && hasKey(${objText}, ${keyText}))`
|
|
142
|
+
: `hasKey(${objText}, ${keyText})`;
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
const importFixes = rewrite === importCarrier && importsToAdd.length > 0
|
|
148
|
+
? buildNamedImportFix(fixer, program, moduleName, importsToAdd)
|
|
149
|
+
: [];
|
|
150
|
+
return [
|
|
151
|
+
...importFixes,
|
|
152
|
+
fixer.replaceText(node, replacement),
|
|
153
|
+
];
|
|
154
|
+
},
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
},
|
|
160
|
+
defaultOptions: [],
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
const isSideEffectFree = (
|
|
167
|
+
|
|
168
|
+
wrapped) => {
|
|
169
|
+
const node = skipTypeWrappers(wrapped);
|
|
170
|
+
return (node.type === AST_NODE_TYPES.Identifier ||
|
|
171
|
+
node.type === AST_NODE_TYPES.ThisExpression ||
|
|
172
|
+
(node.type === AST_NODE_TYPES.MemberExpression &&
|
|
173
|
+
!node.computed &&
|
|
174
|
+
isSideEffectFree(node.object)));
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
|
|
181
|
+
const mayBeArrayType = (
|
|
182
|
+
|
|
183
|
+
checker,
|
|
184
|
+
|
|
185
|
+
type) => {
|
|
186
|
+
if (type.isUnionOrIntersection()) {
|
|
187
|
+
return type.types.some((member) => mayBeArrayType(checker, member));
|
|
188
|
+
}
|
|
189
|
+
if ((type.flags &
|
|
190
|
+
(ts.TypeFlags.TypeParameter |
|
|
191
|
+
ts.TypeFlags.IndexedAccess |
|
|
192
|
+
ts.TypeFlags.Conditional |
|
|
193
|
+
ts.TypeFlags.Substitution)) !==
|
|
194
|
+
0) {
|
|
195
|
+
const constraint = checker.getBaseConstraintOfType(type);
|
|
196
|
+
return constraint !== undefined && mayBeArrayType(checker, constraint);
|
|
197
|
+
}
|
|
198
|
+
return checker.isArrayType(type) || checker.isTupleType(type);
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
const isAlreadyRecordType = (
|
|
215
|
+
|
|
216
|
+
checker,
|
|
217
|
+
|
|
218
|
+
type) => type.isUnion()
|
|
219
|
+
? type.types.every((member) => isAlreadyRecordType(checker, member))
|
|
220
|
+
: checker.getIndexInfoOfType(type, ts.IndexKind.String) !== undefined &&
|
|
221
|
+
checker.getSignaturesOfType(type, ts.SignatureKind.Call).length === 0 &&
|
|
222
|
+
checker.getSignaturesOfType(type, ts.SignatureKind.Construct).length ===
|
|
223
|
+
0 &&
|
|
224
|
+
!checker.isArrayType(type) &&
|
|
225
|
+
!checker.isTupleType(type);
|
|
226
|
+
//# sourceMappingURL=prefer-is-record-and-has-key.mjs.map
|
|
@@ -0,0 +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,KAAK,EAAE,MAAM,YAAY,CAAC;AACjC,OAAO,EACL,mBAAmB,EACnB,qBAAqB,EACrB,eAAe,GAChB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,eAAe,EAAE,MAAM,oB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E,CAAC;IAED,OAAO,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,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,OAAO,CAAC,mBAAmB,CAAC,IAAI,EAAE,EAAE,CAAC,aAAa,CAAC,IAAI,CAAC,CAAC,MAAM,KAAK,CAAC;QACrE,OAAO,CAAC,mBAAmB,CAAC,IAAI,EAAE,EAAE,CAAC,aAAa,CAAC,SAAS,CAAC,CAAC,MAAM;YAClE,CAAC;QACH,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC;QAC1B,CAAC,OAAO,CAAC,WAAW,CAAC,IAAI,CAAC,CAAC"}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type TSESLint } from '@typescript-eslint/utils';
|
|
2
|
+
/** What {@link createPreferSafeParseInt} and its float twin are told. */
|
|
3
|
+
export type SafeParseRuleConfig = Readonly<{
|
|
4
|
+
/** The module that exports `namespace` and `Result`. */
|
|
5
|
+
moduleName: string;
|
|
6
|
+
/** The exported namespace the parse function is reached through. */
|
|
7
|
+
namespace: string;
|
|
8
|
+
/**
|
|
9
|
+
* The member of `namespace` that parses a `string` into a
|
|
10
|
+
* `Result<number, …>`.
|
|
11
|
+
*/
|
|
12
|
+
method: string;
|
|
13
|
+
}>;
|
|
14
|
+
/**
|
|
15
|
+
* Builds a rule that replaces the base-10 forms of `parseInt` and
|
|
16
|
+
* `Number.parseInt` — `parseInt(x)` and `parseInt(x, 10)` — with
|
|
17
|
+
* `Result.unwrapOkOr(<namespace>.<method>(x), Number.NaN)`.
|
|
18
|
+
*
|
|
19
|
+
* A call that passes another radix is left alone: the replacement takes none,
|
|
20
|
+
* so it would mean something the replacement cannot say.
|
|
21
|
+
*/
|
|
22
|
+
export declare const createPreferSafeParseInt: (config: SafeParseRuleConfig) => TSESLint.RuleModule<'useSafeParseInt', readonly []>;
|
|
23
|
+
/**
|
|
24
|
+
* Builds a rule that replaces `parseFloat(x)`, `Number.parseFloat(x)` and
|
|
25
|
+
* `Number(x)` with `Result.unwrapOkOr(<namespace>.<method>(x), Number.NaN)`.
|
|
26
|
+
*
|
|
27
|
+
* Like every rewrite here, `Number(x)` is only flagged when `x` is purely a
|
|
28
|
+
* `string`, which leaves `Number(someBoolean)` and `Number(someNumber)` alone.
|
|
29
|
+
*/
|
|
30
|
+
export declare const createPreferSafeParseFloat: (config: SafeParseRuleConfig) => TSESLint.RuleModule<'useSafeParseFloat', readonly []>;
|
|
31
|
+
//# sourceMappingURL=prefer-safe-parse.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefer-safe-parse.d.mts","sourceRoot":"","sources":["../../src/rules/prefer-safe-parse.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EAEd,MAAM,0BAA0B,CAAC;AAelC,yEAAyE;AACzE,MAAM,MAAM,mBAAmB,GAAG,QAAQ,CAAC;IACzC,wDAAwD;IACxD,UAAU,EAAE,MAAM,CAAC;IAEnB,oEAAoE;IACpE,SAAS,EAAE,MAAM,CAAC;IAElB;;;OAGG;IACH,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC,CAAC;AAEH;;;;;;;GAOG;AACH,eAAO,MAAM,wBAAwB,WAC3B,mBAAmB,KAC1B,QAAQ,CAAC,UAAU,CAAC,iBAAiB,EAAE,SAAS,EAAE,CAsBjD,CAAC;AAEL;;;;;;GAMG;AACH,eAAO,MAAM,0BAA0B,WAC7B,mBAAmB,KAC1B,QAAQ,CAAC,UAAU,CAAC,mBAAmB,EAAE,SAAS,EAAE,CAUnD,CAAC"}
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import { AST_NODE_TYPES, } from '@typescript-eslint/utils';
|
|
2
|
+
import * as ts from 'typescript';
|
|
3
|
+
import { buildNamedImportFix, getImportDeclarations, getNamedImports, } from '../imports.mjs';
|
|
4
|
+
import { getArgumentText } from '../parentheses.mjs';
|
|
5
|
+
import { isIdentifierNamed, skipTypeWrappers, typeHoldsAtEveryLayer, } from '../type-wrappers.mjs';
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
export const createPreferSafeParseInt = (config) => createSafeParseRule(config, 'useSafeParseInt', {
|
|
15
|
+
messages: { useSafeParseInt: messageText(config) },
|
|
16
|
+
description: `Replace \`parseInt(x)\` or \`Number.parseInt(x, 10)\` with \`Result.unwrapOkOr(${config.namespace}.${config.method}(x), Number.NaN)\` from ${config.moduleName}.`,
|
|
17
|
+
isTarget: (callee, args) => {
|
|
18
|
+
if (!isGlobalCall(callee, 'parseInt') &&
|
|
19
|
+
!isNumberMethod(callee, 'parseInt')) {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
const radix = args[1] === undefined ? undefined : skipTypeWrappers(args[1]);
|
|
25
|
+
return (radix === undefined ||
|
|
26
|
+
(radix.type === AST_NODE_TYPES.Literal && radix.value === 10));
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
export const createPreferSafeParseFloat = (config) => createSafeParseRule(config, 'useSafeParseFloat', {
|
|
37
|
+
messages: { useSafeParseFloat: messageText(config) },
|
|
38
|
+
description: `Replace \`parseFloat(x)\`, \`Number.parseFloat(x)\`, or \`Number(x)\` (when x is a string) with \`Result.unwrapOkOr(${config.namespace}.${config.method}(x), Number.NaN)\` from ${config.moduleName}.`,
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
isTarget: (callee) => isGlobalCall(callee, 'parseFloat') ||
|
|
42
|
+
isNumberMethod(callee, 'parseFloat') ||
|
|
43
|
+
isGlobalCall(callee, 'Number'),
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const createSafeParseRule = ({ moduleName, namespace, method }, messageId, { messages, description, isTarget, }) => {
|
|
47
|
+
const parseFunction = `${namespace}.${method}`;
|
|
48
|
+
|
|
49
|
+
const requiredImports = [namespace, 'Result'].toSorted();
|
|
50
|
+
return {
|
|
51
|
+
meta: {
|
|
52
|
+
type: 'suggestion',
|
|
53
|
+
docs: { description },
|
|
54
|
+
fixable: 'code',
|
|
55
|
+
schema: [],
|
|
56
|
+
messages,
|
|
57
|
+
},
|
|
58
|
+
create: (context) => {
|
|
59
|
+
const sourceCode = context.sourceCode;
|
|
60
|
+
const program = sourceCode.ast;
|
|
61
|
+
const moduleImports = getImportDeclarations(program, moduleName);
|
|
62
|
+
const mut_nodesToFix = [];
|
|
63
|
+
return {
|
|
64
|
+
CallExpression: (node) => {
|
|
65
|
+
if (!isTarget(skipTypeWrappers(node.callee), node.arguments)) {
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const firstArg = node.arguments[0];
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
if (firstArg === undefined ||
|
|
72
|
+
firstArg.type === AST_NODE_TYPES.SpreadElement) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
if (!typeHoldsAtEveryLayer(sourceCode, firstArg, isStringType)) {
|
|
83
|
+
return;
|
|
84
|
+
}
|
|
85
|
+
mut_nodesToFix.push({ node, argExpression: firstArg });
|
|
86
|
+
},
|
|
87
|
+
'Program:exit': () => {
|
|
88
|
+
const namedImports = getNamedImports(moduleImports);
|
|
89
|
+
const missingImports = requiredImports.filter((name) => !namedImports.includes(name));
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
for (const [index, { node, argExpression },] of mut_nodesToFix.entries()) {
|
|
94
|
+
const argText = getArgumentText(sourceCode, argExpression);
|
|
95
|
+
context.report({
|
|
96
|
+
node,
|
|
97
|
+
messageId,
|
|
98
|
+
data: {
|
|
99
|
+
original: sourceCode.getText(node),
|
|
100
|
+
argName: argText,
|
|
101
|
+
},
|
|
102
|
+
fix: (fixer) => {
|
|
103
|
+
const core = `Result.unwrapOkOr(${parseFunction}(${argText}), Number.NaN)`;
|
|
104
|
+
const importFixes = index === 0 && missingImports.length > 0
|
|
105
|
+
? buildNamedImportFix(fixer, program, moduleName, missingImports)
|
|
106
|
+
: [];
|
|
107
|
+
return [...importFixes, fixer.replaceText(node, core)];
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
},
|
|
112
|
+
};
|
|
113
|
+
},
|
|
114
|
+
defaultOptions: [],
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
const messageText = ({ moduleName, namespace, method, }) => `Replace \`{{original}}\` with \`Result.unwrapOkOr(${namespace}.${method}({{argName}}), Number.NaN)\` from ${moduleName}.`;
|
|
118
|
+
|
|
119
|
+
const isGlobalCall = (callee, name) => callee.type === AST_NODE_TYPES.Identifier && callee.name === name;
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
const isNumberMethod = (callee, name) => callee.type === AST_NODE_TYPES.MemberExpression &&
|
|
125
|
+
!callee.computed &&
|
|
126
|
+
isIdentifierNamed(callee.object, 'Number') &&
|
|
127
|
+
callee.property.type === AST_NODE_TYPES.Identifier &&
|
|
128
|
+
callee.property.name === name;
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
const isStringType = (type) => {
|
|
135
|
+
const parts = type.isUnion() ? type.types : [type];
|
|
136
|
+
return (parts.length > 0 &&
|
|
137
|
+
parts.every((t) => (t.flags & ts.TypeFlags.StringLike) !== 0));
|
|
138
|
+
};
|
|
139
|
+
//# sourceMappingURL=prefer-safe-parse.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"prefer-safe-parse.mjs","sourceRoot":"","sources":["../../src/rules/prefer-safe-parse.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,GAGf,MAAM,0BAA0B,CAAC;AAElC,OAAO,KAAK,EAAE,MAAM,YAAY,CAAC;AACjC,OAAO,EACL,mBAAmB,EACnB,qBAAqB,EACrB,eAAe,GAChB,MAAM,gBAAgB,CAAC;AACxB,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EACL,iBAAiB,EACjB,gBAAgB,EAChB,qBAAqB,GACtB,MAAM,sBAAsB,CAAC;AAiB9B;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,wBAAwB,GAAG,CACtC,MAA2B,EAC0B,EAAE,CACvD,mBAAmB,CAAC,MAAM,EAAE,iBAAiB,EAAE;IAC7C,QAAQ,EAAE,EAAE,eAAe,EAAE,WAAW,CAAC,MAAM,CAAC,EAAE;IAClD,WAAW,EAAE,kFAAkF,MAAM,CAAC,SAAS,IAAI,MAAM,CAAC,MAAM,2BAA2B,MAAM,CAAC,UAAU,GAAG;IAC/K,QAAQ,EAAE,CAAC,MAAM,EAAE,IAAI,EAAE,EAAE;QACzB,IACE,CAAC,YAAY,CAAC,MAAM,EAAE,UAAU,CAAC;YACjC,CAAC,cAAc,CAAC,MAAM,EAAE,UAAU,CAAC,EACnC,CAAC;YACD,OAAO,KAAK,CAAC;QACf,CAAC;QAED,oEAAoE;QACpE,kEAA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UAAU,IAAI,MAAM,CAAC,IAAI,KAAK,IAAI,CAAC;AAEpE;;;GAGG;AACH,MAAM,cAAc,GAAG,CAAC,MAA2B,EAAE,IAAY,EAAW,EAAE,CAC5E,MAAM,CAAC,IAAI,KAAK,cAAc,CAAC,gBAAgB;IAC/C,CAAC,MAAM,CAAC,QAAQ;IAChB,iBAAiB,CAAC,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC;IAC1C,MAAM,CAAC,QAAQ,CAAC,IAAI,KAAK,cAAc,CAAC,UAAU;IAClD,MAAM,CAAC,QAAQ,CAAC,IAAI,KAAK,IAAI,CAAC;AAEhC;;;;GAIG;AACH,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"}
|
package/dist/type-wrappers.d.mts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { type TSESTree } from '@typescript-eslint/utils';
|
|
1
|
+
import { type TSESLint, type TSESTree } from '@typescript-eslint/utils';
|
|
2
2
|
import type { DeepReadonly, RelaxedExtract } from 'ts-type-forge';
|
|
3
|
+
import type * as ts from 'typescript';
|
|
3
4
|
/**
|
|
4
5
|
* The four expressions that change a value's type and nothing else: `x as T`,
|
|
5
6
|
* `x!`, `x satisfies T` and `<T>x`.
|
|
@@ -41,4 +42,19 @@ export declare const skipTypeWrappers: <N extends DeepReadonly<TSESTree.Node>>(n
|
|
|
41
42
|
* layer too.
|
|
42
43
|
*/
|
|
43
44
|
export declare const typeWrapperLayers: <N extends DeepReadonly<TSESTree.Node>>(node: N) => readonly (N | WrappedExpression<N>)[];
|
|
45
|
+
/**
|
|
46
|
+
* Whether `node`, read through its type wrappers, is the identifier `name`:
|
|
47
|
+
* `Number`, `(Number as NumberConstructor)` and `Number!` are the same object.
|
|
48
|
+
*/
|
|
49
|
+
export declare const isIdentifierNamed: (node: DeepReadonly<TSESTree.Node>, name: string) => boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Whether `holds` is true of the type of every layer of type wrappers around
|
|
52
|
+
* `node` (see {@link typeWrapperLayers}), outermost to innermost; `false`
|
|
53
|
+
* without type information. A widening cast (`s as string | number`) is the
|
|
54
|
+
* author saying the value may be more than it is, and is respected; a
|
|
55
|
+
* narrowing one (`s!`, `v as unknown as string`) cannot vouch for what the
|
|
56
|
+
* value's own type does not, and the fix, which keeps the wrapper, has to
|
|
57
|
+
* type-check at every layer too.
|
|
58
|
+
*/
|
|
59
|
+
export declare const typeHoldsAtEveryLayer: (sourceCode: TSESLint.SourceCode, node: TSESTree.Node, holds: (type: ts.Type, typeChecker: ts.TypeChecker) => boolean) => boolean;
|
|
44
60
|
//# sourceMappingURL=type-wrappers.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type-wrappers.d.mts","sourceRoot":"","sources":["../src/type-wrappers.mts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"type-wrappers.d.mts","sourceRoot":"","sources":["../src/type-wrappers.mts"],"names":[],"mappings":"AAAA,OAAO,EAEL,KAAK,QAAQ,EACb,KAAK,QAAQ,EACd,MAAM,0BAA0B,CAAC;AAClC,OAAO,KAAK,EAAE,YAAY,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAClE,OAAO,KAAK,KAAK,EAAE,MAAM,YAAY,CAAC;AAEtC;;;GAGG;AACH,MAAM,MAAM,WAAW,GACnB,QAAQ,CAAC,cAAc,GACvB,QAAQ,CAAC,m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|
@@ -30,6 +30,40 @@ export const skipTypeWrappers = (node) => isTypeWrapper(node) ? skipTypeWrappers
|
|
|
30
30
|
export const typeWrapperLayers = (node) => isTypeWrapper(node)
|
|
31
31
|
? [node, ...typeWrapperLayers(node.expression)]
|
|
32
32
|
: [node];
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
export const isIdentifierNamed = (node, name) => {
|
|
38
|
+
const unwrapped = skipTypeWrappers(node);
|
|
39
|
+
return (unwrapped.type === AST_NODE_TYPES.Identifier && unwrapped.name === name);
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
export const typeHoldsAtEveryLayer = (
|
|
51
|
+
|
|
52
|
+
sourceCode,
|
|
53
|
+
|
|
54
|
+
node,
|
|
55
|
+
|
|
56
|
+
holds) => {
|
|
57
|
+
const services = sourceCode.parserServices;
|
|
58
|
+
const checker = services?.program?.getTypeChecker();
|
|
59
|
+
if (checker === undefined) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return typeWrapperLayers(node).every((layer) => {
|
|
63
|
+
const tsNode = services?.esTreeNodeToTSNodeMap?.get(layer);
|
|
64
|
+
return (tsNode !== undefined && holds(checker.getTypeAtLocation(tsNode), checker));
|
|
65
|
+
});
|
|
66
|
+
};
|
|
33
67
|
const TYPE_WRAPPERS = new Set([
|
|
34
68
|
AST_NODE_TYPES.TSAsExpression,
|
|
35
69
|
AST_NODE_TYPES.TSNonNullExpression,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"type-wrappers.mjs","sourceRoot":"","sources":["../src/type-wrappers.mts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"type-wrappers.mjs","sourceRoot":"","sources":["../src/type-wrappers.mts"],"names":[],"mappings":"AAAA,OAAO,EACL,cAAc,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f,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;AAEF,MAAM,aAAa,GAAgC,IAAI,GAAG,CAAC;IACzD,cAAc,CAAC,cAAc;IAC7B,cAAc,CAAC,mBAAmB;IAClC,cAAc,CAAC,qBAAqB;IACpC,cAAc,CAAC,eAAe;CAC/B,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "eslint-rule-forge",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Helpers for writing typescript-eslint rules that see through type wrappers and parentheses, and resolve what a file imports.",
|
|
6
6
|
"keywords": [
|
|
@@ -41,13 +41,13 @@
|
|
|
41
41
|
"@typescript-eslint/rule-tester": "8.71.1",
|
|
42
42
|
"dedent": "1.7.2",
|
|
43
43
|
"eslint": "10.12.0",
|
|
44
|
-
"eslint-config-typed": "5.19.
|
|
45
|
-
"eslint-plugin-ts-fortress": "1.3.
|
|
46
|
-
"eslint-plugin-ts-type-forge": "0.8.
|
|
44
|
+
"eslint-config-typed": "5.19.1",
|
|
45
|
+
"eslint-plugin-ts-fortress": "1.3.6",
|
|
46
|
+
"eslint-plugin-ts-type-forge": "0.8.11",
|
|
47
47
|
"prettier": "3.9.9",
|
|
48
48
|
"prettier-plugin-organize-imports": "4.3.0",
|
|
49
49
|
"prettier-plugin-packagejson": "3.0.2",
|
|
50
|
-
"ts-data-forge": "14.
|
|
50
|
+
"ts-data-forge": "14.9.0",
|
|
51
51
|
"ts-repo-utils": "10.7.1"
|
|
52
52
|
},
|
|
53
53
|
"peerDependencies": {
|
package/src/index.mts
CHANGED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Builds a rule that removes `import '<moduleName>';`, an import that binds no
|
|
5
|
+
* name. The rule matches the bare specifier exactly, so a subpath such as
|
|
6
|
+
* `<moduleName>/global` and every other module are left alone.
|
|
7
|
+
*
|
|
8
|
+
* Why such an import is wrong depends on the module — one with
|
|
9
|
+
* `sideEffects: false` loads for nothing, one with no runtime entry point fails
|
|
10
|
+
* to resolve — so the caller says it, in `description` and `message`, and
|
|
11
|
+
* chooses `type` accordingly.
|
|
12
|
+
*/
|
|
13
|
+
export const createNoSideEffectImport = ({
|
|
14
|
+
moduleName,
|
|
15
|
+
type,
|
|
16
|
+
description,
|
|
17
|
+
message,
|
|
18
|
+
}: Readonly<{
|
|
19
|
+
moduleName: string;
|
|
20
|
+
type: TSESLint.RuleMetaData<string, unknown>['type'];
|
|
21
|
+
description: string;
|
|
22
|
+
message: string;
|
|
23
|
+
}>): TSESLint.RuleModule<'removeSideEffectImport', readonly []> =>
|
|
24
|
+
({
|
|
25
|
+
meta: {
|
|
26
|
+
type,
|
|
27
|
+
docs: { description },
|
|
28
|
+
fixable: 'code',
|
|
29
|
+
schema: [],
|
|
30
|
+
messages: { removeSideEffectImport: message },
|
|
31
|
+
},
|
|
32
|
+
|
|
33
|
+
create: (context) => ({
|
|
34
|
+
ImportDeclaration: (node) => {
|
|
35
|
+
if (node.source.value !== moduleName || node.specifiers.length > 0) {
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
context.report({
|
|
40
|
+
node,
|
|
41
|
+
messageId: 'removeSideEffectImport',
|
|
42
|
+
fix: (fixer) => removeStatement(fixer, context.sourceCode, node),
|
|
43
|
+
});
|
|
44
|
+
},
|
|
45
|
+
}),
|
|
46
|
+
defaultOptions: [],
|
|
47
|
+
}) as const;
|
|
48
|
+
|
|
49
|
+
/* eslint-disable @typescript-eslint/prefer-readonly-parameter-types */
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Removes a statement along with the rest of its line, so that deleting it does
|
|
53
|
+
* not leave a blank line where it was.
|
|
54
|
+
*/
|
|
55
|
+
const removeStatement = (
|
|
56
|
+
fixer: TSESLint.RuleFixer,
|
|
57
|
+
sourceCode: TSESLint.SourceCode,
|
|
58
|
+
node: TSESTree.Node,
|
|
59
|
+
): TSESLint.RuleFix => {
|
|
60
|
+
const restOfLine = /^[^\S\n]*\n/u.exec(
|
|
61
|
+
sourceCode.getText().slice(node.range[1]),
|
|
62
|
+
);
|
|
63
|
+
|
|
64
|
+
return fixer.removeRange([
|
|
65
|
+
node.range[0],
|
|
66
|
+
node.range[1] + (restOfLine?.[0].length ?? 0),
|
|
67
|
+
]);
|
|
68
|
+
};
|