@murky-web/typebuddy 3.0.0 → 3.0.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 +3 -3
- package/globals.d.ts +7 -7
- package/jsr.json +24 -24
- package/oxlint/index.ts +3 -3
- package/package.json +1 -1
- package/rules/async_rule.ts +19 -19
- package/rules/maybe_promise_rule.ts +36 -36
- package/rules/maybe_rule.ts +11 -11
- package/rules/nullable_rule.ts +11 -14
- package/rules/optional_rule.ts +10 -13
- package/rules/own_subtree.ts +2 -2
- package/rules/test_file.ts +4 -4
- package/rules/typebuddy_import.ts +9 -9
- package/rules/use_server.ts +3 -3
- package/src/collections.ts +1 -1
- package/src/empty.ts +1 -1
- package/src/guards.ts +5 -5
- package/src/parse.ts +4 -4
- package/src/public_api.ts +1 -1
- package/src/result.ts +3 -3
- package/src/types/globals.ts +1 -1
- package/src/types/index.ts +2 -2
package/README.md
CHANGED
|
@@ -39,7 +39,7 @@ import {
|
|
|
39
39
|
isString,
|
|
40
40
|
parseNumber,
|
|
41
41
|
isUuidString,
|
|
42
|
-
isEmptyObject
|
|
42
|
+
isEmptyObject
|
|
43
43
|
} from "@murky-web/typebuddy";
|
|
44
44
|
|
|
45
45
|
const value: unknown = "123";
|
|
@@ -103,8 +103,8 @@ If you want the `typebuddy` utility types globally, opt in once in your project:
|
|
|
103
103
|
// tsconfig.json
|
|
104
104
|
{
|
|
105
105
|
"compilerOptions": {
|
|
106
|
-
"types": ["@murky-web/typebuddy/globals"]
|
|
107
|
-
}
|
|
106
|
+
"types": ["@murky-web/typebuddy/globals"]
|
|
107
|
+
}
|
|
108
108
|
}
|
|
109
109
|
```
|
|
110
110
|
|
package/globals.d.ts
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
JsonifiedValue as JsonifiedValueImport,
|
|
3
|
-
Stringified as StringifiedImport
|
|
3
|
+
Stringified as StringifiedImport
|
|
4
4
|
} from "./src/types/json.js";
|
|
5
5
|
import type {
|
|
6
6
|
Maybe as MaybeImport,
|
|
7
|
-
ResolveMaybe as ResolveMaybeImport
|
|
7
|
+
ResolveMaybe as ResolveMaybeImport
|
|
8
8
|
} from "./src/types/maybe.js";
|
|
9
9
|
import type {
|
|
10
10
|
Failed as FailedImport,
|
|
11
11
|
MaybePromise as MaybePromiseImport,
|
|
12
|
-
Success as SuccessImport
|
|
12
|
+
Success as SuccessImport
|
|
13
13
|
} from "./src/types/maybe_promise.js";
|
|
14
14
|
import type {
|
|
15
15
|
Nullable as NullableImport,
|
|
16
|
-
ResolveNullable as ResolveNullableImport
|
|
16
|
+
ResolveNullable as ResolveNullableImport
|
|
17
17
|
} from "./src/types/nullable.js";
|
|
18
18
|
import type {
|
|
19
19
|
Optional as OptionalImport,
|
|
20
|
-
ResolveOptional as ResolveOptionalImport
|
|
20
|
+
ResolveOptional as ResolveOptionalImport
|
|
21
21
|
} from "./src/types/optional.js";
|
|
22
22
|
|
|
23
23
|
declare global {
|
|
@@ -43,7 +43,7 @@ declare global {
|
|
|
43
43
|
stringify<T>(
|
|
44
44
|
value: T,
|
|
45
45
|
replacer?: null,
|
|
46
|
-
space?: string | number
|
|
46
|
+
space?: string | number
|
|
47
47
|
): Stringified<T>;
|
|
48
48
|
|
|
49
49
|
parse<T>(str: Stringified<T>, replacer?: null): JsonifiedValueImport<T>;
|
|
@@ -61,5 +61,5 @@ export type {
|
|
|
61
61
|
ResolveNullableImport as ResolveNullable,
|
|
62
62
|
ResolveOptionalImport as ResolveOptional,
|
|
63
63
|
StringifiedImport as Stringified,
|
|
64
|
-
SuccessImport as Success
|
|
64
|
+
SuccessImport as Success
|
|
65
65
|
};
|
package/jsr.json
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
2
|
+
"$schema": "https://jsr.io/schema/config-file.v1.json",
|
|
3
|
+
"name": "@murky-web/typebuddy",
|
|
4
|
+
"version": "3.0.1",
|
|
5
|
+
"license": "ISC",
|
|
6
|
+
"exports": {
|
|
7
|
+
".": "./src/public_api.ts",
|
|
8
|
+
"./oxlint": "./oxlint/index.ts"
|
|
9
|
+
},
|
|
10
|
+
"publish": {
|
|
11
|
+
"include": [
|
|
12
|
+
"README.md",
|
|
13
|
+
"src/**/*.ts",
|
|
14
|
+
"oxlint/**/*.ts",
|
|
15
|
+
"rules/**/*.ts"
|
|
16
|
+
],
|
|
17
|
+
"exclude": [
|
|
18
|
+
"tests/**",
|
|
19
|
+
"smoke/**",
|
|
20
|
+
"globals.d.ts",
|
|
21
|
+
"src/index.ts",
|
|
22
|
+
"dist/**",
|
|
23
|
+
"node_modules/**"
|
|
24
|
+
]
|
|
25
|
+
}
|
|
26
26
|
}
|
package/oxlint/index.ts
CHANGED
|
@@ -11,7 +11,7 @@ const rules = {
|
|
|
11
11
|
"prefer-maybe-promise": withoutTestFiles(errorSafeAsyncRule),
|
|
12
12
|
"prefer-nullable": withoutTestFiles(nullableRule),
|
|
13
13
|
"prefer-optional": withoutTestFiles(optionalRule),
|
|
14
|
-
"require-try-catch": withoutTestFiles(requireTryCatchAsyncRule)
|
|
14
|
+
"require-try-catch": withoutTestFiles(requireTryCatchAsyncRule)
|
|
15
15
|
} as const satisfies Record<string, unknown>;
|
|
16
16
|
|
|
17
17
|
type TypebuddyOxlintPlugin = {
|
|
@@ -23,9 +23,9 @@ type TypebuddyOxlintPlugin = {
|
|
|
23
23
|
|
|
24
24
|
const plugin: TypebuddyOxlintPlugin = {
|
|
25
25
|
meta: {
|
|
26
|
-
name: "typebuddy"
|
|
26
|
+
name: "typebuddy"
|
|
27
27
|
},
|
|
28
|
-
rules
|
|
28
|
+
rules
|
|
29
29
|
};
|
|
30
30
|
|
|
31
31
|
export default plugin;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@murky-web/typebuddy",
|
|
3
|
-
"version": "3.0.
|
|
3
|
+
"version": "3.0.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"description": "Your new best friend for simple typescript guards every project needs.",
|
|
6
6
|
"homepage": "https://github.com/MurkyTheMurloc/web_dev_tools/tree/main/packages/typebuddy",
|
package/rules/async_rule.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { throwsDeliberately } from "./deliberate_throw.js";
|
|
2
2
|
import {
|
|
3
3
|
createImportPlanner,
|
|
4
|
-
getBodyStartAfterDirectives
|
|
4
|
+
getBodyStartAfterDirectives
|
|
5
5
|
} from "./typebuddy_import.js";
|
|
6
6
|
import { createUseServerGuard } from "./use_server.js";
|
|
7
7
|
|
|
@@ -70,7 +70,7 @@ const rule = {
|
|
|
70
70
|
// body; inside a `try` they are plain expression statements.
|
|
71
71
|
function wrapBlockFixes(
|
|
72
72
|
bodyNode: AstNode,
|
|
73
|
-
fixer: RuleFixer
|
|
73
|
+
fixer: RuleFixer
|
|
74
74
|
): unknown[] | null {
|
|
75
75
|
const range = bodyNode.range;
|
|
76
76
|
const tryStart = getBodyStartAfterDirectives(bodyNode);
|
|
@@ -86,8 +86,8 @@ const rule = {
|
|
|
86
86
|
fixer.insertTextBeforeRange([tryStart, tryStart], "\ntry {"),
|
|
87
87
|
fixer.insertTextBeforeRange(
|
|
88
88
|
[range[1] - 1, range[1] - 1],
|
|
89
|
-
"} catch {\nreturn err();\n}\n"
|
|
90
|
-
)
|
|
89
|
+
"} catch {\nreturn err();\n}\n"
|
|
90
|
+
)
|
|
91
91
|
];
|
|
92
92
|
}
|
|
93
93
|
|
|
@@ -103,7 +103,7 @@ const rule = {
|
|
|
103
103
|
function wrapExpressionBodyFixes(
|
|
104
104
|
node: AstNode,
|
|
105
105
|
bodyNode: AstNode,
|
|
106
|
-
fixer: RuleFixer
|
|
106
|
+
fixer: RuleFixer
|
|
107
107
|
): unknown[] | null {
|
|
108
108
|
const arrowRange = node.range;
|
|
109
109
|
const bodyRange = bodyNode.range;
|
|
@@ -133,12 +133,12 @@ const rule = {
|
|
|
133
133
|
return [
|
|
134
134
|
fixer.insertTextBeforeRange(
|
|
135
135
|
[start, start],
|
|
136
|
-
"{\ntry {\nreturn "
|
|
136
|
+
"{\ntry {\nreturn "
|
|
137
137
|
),
|
|
138
138
|
fixer.insertTextBeforeRange(
|
|
139
139
|
[end, end],
|
|
140
|
-
";\n} catch {\nreturn err();\n}\n}"
|
|
141
|
-
)
|
|
140
|
+
";\n} catch {\nreturn err();\n}\n}"
|
|
141
|
+
)
|
|
142
142
|
];
|
|
143
143
|
}
|
|
144
144
|
|
|
@@ -153,7 +153,7 @@ const rule = {
|
|
|
153
153
|
*/
|
|
154
154
|
function suggestWrap(
|
|
155
155
|
scopeNode: AstNode,
|
|
156
|
-
wrap: (fixer: RuleFixer) => unknown[] | null
|
|
156
|
+
wrap: (fixer: RuleFixer) => unknown[] | null
|
|
157
157
|
) {
|
|
158
158
|
return [
|
|
159
159
|
{
|
|
@@ -168,15 +168,15 @@ const rule = {
|
|
|
168
168
|
scopeNode,
|
|
169
169
|
fixer,
|
|
170
170
|
"err",
|
|
171
|
-
"value"
|
|
171
|
+
"value"
|
|
172
172
|
);
|
|
173
173
|
if (importFixes === null) {
|
|
174
174
|
return null;
|
|
175
175
|
}
|
|
176
176
|
|
|
177
177
|
return [...importFixes, ...wrapFixes];
|
|
178
|
-
}
|
|
179
|
-
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
180
|
];
|
|
181
181
|
}
|
|
182
182
|
|
|
@@ -203,7 +203,7 @@ const rule = {
|
|
|
203
203
|
messageId: "missingTryCatch",
|
|
204
204
|
suggest: suggestWrap(node, (fixer) => {
|
|
205
205
|
return wrapExpressionBodyFixes(node, bodyNode, fixer);
|
|
206
|
-
})
|
|
206
|
+
})
|
|
207
207
|
});
|
|
208
208
|
return;
|
|
209
209
|
}
|
|
@@ -216,7 +216,7 @@ const rule = {
|
|
|
216
216
|
messageId: "missingTryCatch",
|
|
217
217
|
suggest: suggestWrap(bodyNode, (fixer) => {
|
|
218
218
|
return wrapBlockFixes(bodyNode, fixer);
|
|
219
|
-
})
|
|
219
|
+
})
|
|
220
220
|
});
|
|
221
221
|
}
|
|
222
222
|
|
|
@@ -226,7 +226,7 @@ const rule = {
|
|
|
226
226
|
Program,
|
|
227
227
|
FunctionDeclaration: guard(checkFunction),
|
|
228
228
|
FunctionExpression: guard(checkFunction),
|
|
229
|
-
ArrowFunctionExpression: guard(checkFunction)
|
|
229
|
+
ArrowFunctionExpression: guard(checkFunction)
|
|
230
230
|
};
|
|
231
231
|
},
|
|
232
232
|
defaultOptions: [],
|
|
@@ -235,16 +235,16 @@ const rule = {
|
|
|
235
235
|
hasSuggestions: true,
|
|
236
236
|
docs: {
|
|
237
237
|
description:
|
|
238
|
-
"Async functions should have a try-catch block returning an error result."
|
|
238
|
+
"Async functions should have a try-catch block returning an error result."
|
|
239
239
|
},
|
|
240
240
|
schema: [],
|
|
241
241
|
messages: {
|
|
242
242
|
missingTryCatch:
|
|
243
243
|
"Async functions must have a try-catch block returning an error result.",
|
|
244
244
|
suggestTryCatch:
|
|
245
|
-
"Wrap the body in try/catch and return err() from the catch."
|
|
246
|
-
}
|
|
247
|
-
}
|
|
245
|
+
"Wrap the body in try/catch and return err() from the catch."
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
248
|
};
|
|
249
249
|
|
|
250
250
|
export { rule as requireTryCatchAsyncRule };
|
|
@@ -115,7 +115,7 @@ function hasIsErrorFlag(node: unknown, expected: boolean): boolean {
|
|
|
115
115
|
|
|
116
116
|
function getObjectPropertyValue(
|
|
117
117
|
node: unknown,
|
|
118
|
-
propertyName: string
|
|
118
|
+
propertyName: string
|
|
119
119
|
): AstNode | null {
|
|
120
120
|
if (
|
|
121
121
|
!isNode(node) ||
|
|
@@ -229,7 +229,7 @@ const rule = {
|
|
|
229
229
|
at: AstNode,
|
|
230
230
|
fixer: RuleFixer,
|
|
231
231
|
helperName: "ok" | "err",
|
|
232
|
-
rewrite: unknown
|
|
232
|
+
rewrite: unknown
|
|
233
233
|
): unknown[] | null {
|
|
234
234
|
const importFixes = imports.fixes(at, fixer, helperName, "value");
|
|
235
235
|
if (importFixes === null) return null;
|
|
@@ -245,7 +245,7 @@ const rule = {
|
|
|
245
245
|
at: AstNode,
|
|
246
246
|
messageId: string,
|
|
247
247
|
helperName: "ok" | "err",
|
|
248
|
-
rewrite: (fixer: RuleFixer) => unknown
|
|
248
|
+
rewrite: (fixer: RuleFixer) => unknown
|
|
249
249
|
): RuleSuggestion[] {
|
|
250
250
|
return [
|
|
251
251
|
{
|
|
@@ -255,10 +255,10 @@ const rule = {
|
|
|
255
255
|
at,
|
|
256
256
|
fixer,
|
|
257
257
|
helperName,
|
|
258
|
-
rewrite(fixer)
|
|
258
|
+
rewrite(fixer)
|
|
259
259
|
);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
262
|
];
|
|
263
263
|
}
|
|
264
264
|
|
|
@@ -284,13 +284,13 @@ const rule = {
|
|
|
284
284
|
typeName,
|
|
285
285
|
fixer,
|
|
286
286
|
"MaybePromise",
|
|
287
|
-
"type"
|
|
287
|
+
"type"
|
|
288
288
|
);
|
|
289
289
|
if (typeImport === null) return null;
|
|
290
290
|
|
|
291
291
|
return [
|
|
292
292
|
...typeImport,
|
|
293
|
-
fixer.replaceText(typeName, "MaybePromise")
|
|
293
|
+
fixer.replaceText(typeName, "MaybePromise")
|
|
294
294
|
];
|
|
295
295
|
};
|
|
296
296
|
|
|
@@ -305,7 +305,7 @@ const rule = {
|
|
|
305
305
|
messageId: "replaceWithMaybePromise",
|
|
306
306
|
fix(fixer) {
|
|
307
307
|
return rewrite(fixer);
|
|
308
|
-
}
|
|
308
|
+
}
|
|
309
309
|
});
|
|
310
310
|
return;
|
|
311
311
|
}
|
|
@@ -318,9 +318,9 @@ const rule = {
|
|
|
318
318
|
messageId: "suggestMaybePromise",
|
|
319
319
|
fix(fixer) {
|
|
320
320
|
return rewrite(fixer);
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
]
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
]
|
|
324
324
|
});
|
|
325
325
|
}
|
|
326
326
|
|
|
@@ -359,7 +359,7 @@ const rule = {
|
|
|
359
359
|
function wrapReturnValue(
|
|
360
360
|
node: AstNode,
|
|
361
361
|
isAsync: boolean,
|
|
362
|
-
returnType: AstNode | null
|
|
362
|
+
returnType: AstNode | null
|
|
363
363
|
) {
|
|
364
364
|
if (!isAsync) return;
|
|
365
365
|
|
|
@@ -374,9 +374,9 @@ const rule = {
|
|
|
374
374
|
node,
|
|
375
375
|
fixer,
|
|
376
376
|
"ok",
|
|
377
|
-
fixer.replaceText(node, "return ok();")
|
|
377
|
+
fixer.replaceText(node, "return ok();")
|
|
378
378
|
);
|
|
379
|
-
}
|
|
379
|
+
}
|
|
380
380
|
});
|
|
381
381
|
}
|
|
382
382
|
return;
|
|
@@ -417,9 +417,9 @@ const rule = {
|
|
|
417
417
|
node,
|
|
418
418
|
fixer,
|
|
419
419
|
"ok",
|
|
420
|
-
fixer.replaceText(argument, `ok(${valueText})`)
|
|
420
|
+
fixer.replaceText(argument, `ok(${valueText})`)
|
|
421
421
|
);
|
|
422
|
-
}
|
|
422
|
+
}
|
|
423
423
|
});
|
|
424
424
|
return;
|
|
425
425
|
}
|
|
@@ -437,9 +437,9 @@ const rule = {
|
|
|
437
437
|
node,
|
|
438
438
|
fixer,
|
|
439
439
|
"err",
|
|
440
|
-
fixer.replaceText(argument, "err()")
|
|
440
|
+
fixer.replaceText(argument, "err()")
|
|
441
441
|
);
|
|
442
|
-
}
|
|
442
|
+
}
|
|
443
443
|
});
|
|
444
444
|
return;
|
|
445
445
|
}
|
|
@@ -464,10 +464,10 @@ const rule = {
|
|
|
464
464
|
const argumentText = sourceCode.getText(argument);
|
|
465
465
|
return fixer.replaceText(
|
|
466
466
|
argument,
|
|
467
|
-
`ok(${argumentText})
|
|
467
|
+
`ok(${argumentText})`
|
|
468
468
|
);
|
|
469
|
-
}
|
|
470
|
-
)
|
|
469
|
+
}
|
|
470
|
+
)
|
|
471
471
|
});
|
|
472
472
|
}
|
|
473
473
|
}
|
|
@@ -526,9 +526,9 @@ const rule = {
|
|
|
526
526
|
statement,
|
|
527
527
|
fixer,
|
|
528
528
|
"err",
|
|
529
|
-
fixer.replaceText(statement, "return err();")
|
|
529
|
+
fixer.replaceText(statement, "return err();")
|
|
530
530
|
);
|
|
531
|
-
}
|
|
531
|
+
}
|
|
532
532
|
});
|
|
533
533
|
return;
|
|
534
534
|
}
|
|
@@ -548,9 +548,9 @@ const rule = {
|
|
|
548
548
|
statement,
|
|
549
549
|
fixer,
|
|
550
550
|
"err",
|
|
551
|
-
fixer.replaceText(argument, "err()")
|
|
551
|
+
fixer.replaceText(argument, "err()")
|
|
552
552
|
);
|
|
553
|
-
}
|
|
553
|
+
}
|
|
554
554
|
});
|
|
555
555
|
}
|
|
556
556
|
return;
|
|
@@ -558,7 +558,7 @@ const rule = {
|
|
|
558
558
|
|
|
559
559
|
context.report({
|
|
560
560
|
node: statement,
|
|
561
|
-
messageId: "returnFailedPromise"
|
|
561
|
+
messageId: "returnFailedPromise"
|
|
562
562
|
});
|
|
563
563
|
}
|
|
564
564
|
|
|
@@ -658,15 +658,15 @@ const rule = {
|
|
|
658
658
|
fixer.insertTextBeforeRange(
|
|
659
659
|
[
|
|
660
660
|
handlerBody["range"][1] - 1,
|
|
661
|
-
handlerBody["range"][1] - 1
|
|
661
|
+
handlerBody["range"][1] - 1
|
|
662
662
|
],
|
|
663
663
|
// The newline matters: `catch { log() }` would
|
|
664
664
|
// otherwise become `catch { log() return err(); }`,
|
|
665
665
|
// which does not parse.
|
|
666
|
-
"\nreturn err();\n"
|
|
667
|
-
)
|
|
666
|
+
"\nreturn err();\n"
|
|
667
|
+
)
|
|
668
668
|
);
|
|
669
|
-
}
|
|
669
|
+
}
|
|
670
670
|
});
|
|
671
671
|
}
|
|
672
672
|
|
|
@@ -680,7 +680,7 @@ const rule = {
|
|
|
680
680
|
TSDeclareFunction: guard(checkSignatureReturnType),
|
|
681
681
|
TSFunctionType: guard(checkSignatureReturnType),
|
|
682
682
|
TSMethodSignature: guard(checkSignatureReturnType),
|
|
683
|
-
TryStatement: guard(processTryCatch)
|
|
683
|
+
TryStatement: guard(processTryCatch)
|
|
684
684
|
};
|
|
685
685
|
},
|
|
686
686
|
defaultOptions: [],
|
|
@@ -688,7 +688,7 @@ const rule = {
|
|
|
688
688
|
type: "suggestion",
|
|
689
689
|
docs: {
|
|
690
690
|
description:
|
|
691
|
-
"Ensure async functions' return values follow the MaybePromise pattern."
|
|
691
|
+
"Ensure async functions' return values follow the MaybePromise pattern."
|
|
692
692
|
},
|
|
693
693
|
fixable: "code",
|
|
694
694
|
hasSuggestions: true,
|
|
@@ -706,9 +706,9 @@ const rule = {
|
|
|
706
706
|
returnVoidPromise:
|
|
707
707
|
"Return ok() for async functions with Promise<void> return type.",
|
|
708
708
|
returnFailedPromise:
|
|
709
|
-
"Return err() in the catch block of async functions."
|
|
710
|
-
}
|
|
711
|
-
}
|
|
709
|
+
"Return err() in the catch block of async functions."
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
712
|
};
|
|
713
713
|
|
|
714
714
|
export { rule as errorSafeAsyncRule };
|
package/rules/maybe_rule.ts
CHANGED
|
@@ -26,15 +26,15 @@ const rule = {
|
|
|
26
26
|
const types = node.types;
|
|
27
27
|
|
|
28
28
|
const nullType = types.find(
|
|
29
|
-
(typeNode) => typeNode.type === "TSNullKeyword"
|
|
29
|
+
(typeNode) => typeNode.type === "TSNullKeyword"
|
|
30
30
|
);
|
|
31
31
|
const undefinedType = types.find(
|
|
32
|
-
(typeNode) => typeNode.type === "TSUndefinedKeyword"
|
|
32
|
+
(typeNode) => typeNode.type === "TSUndefinedKeyword"
|
|
33
33
|
);
|
|
34
34
|
const otherTypes = types.filter(
|
|
35
35
|
(typeNode) =>
|
|
36
36
|
typeNode.type !== "TSNullKeyword" &&
|
|
37
|
-
typeNode.type !== "TSUndefinedKeyword"
|
|
37
|
+
typeNode.type !== "TSUndefinedKeyword"
|
|
38
38
|
);
|
|
39
39
|
|
|
40
40
|
if (nullType && !undefinedType && otherTypes.length === 1) {
|
|
@@ -53,32 +53,32 @@ const rule = {
|
|
|
53
53
|
node,
|
|
54
54
|
fixer,
|
|
55
55
|
"Maybe",
|
|
56
|
-
"type"
|
|
56
|
+
"type"
|
|
57
57
|
);
|
|
58
58
|
if (importFixes === null) return null;
|
|
59
59
|
|
|
60
60
|
return [
|
|
61
61
|
...importFixes,
|
|
62
|
-
fixer.replaceText(node, `Maybe<${typeText}>`)
|
|
62
|
+
fixer.replaceText(node, `Maybe<${typeText}>`)
|
|
63
63
|
];
|
|
64
|
-
}
|
|
64
|
+
}
|
|
65
65
|
});
|
|
66
66
|
}
|
|
67
|
-
}
|
|
67
|
+
}
|
|
68
68
|
};
|
|
69
69
|
},
|
|
70
70
|
defaultOptions: [],
|
|
71
71
|
meta: {
|
|
72
72
|
type: "suggestion",
|
|
73
73
|
docs: {
|
|
74
|
-
description: "Use Maybe<T> for T | null"
|
|
74
|
+
description: "Use Maybe<T> for T | null"
|
|
75
75
|
},
|
|
76
76
|
fixable: "code",
|
|
77
77
|
schema: [],
|
|
78
78
|
messages: {
|
|
79
|
-
useMaybe: "Use Maybe<{{type}}> instead of {{type}} | null"
|
|
80
|
-
}
|
|
81
|
-
}
|
|
79
|
+
useMaybe: "Use Maybe<{{type}}> instead of {{type}} | null"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
82
|
};
|
|
83
83
|
|
|
84
84
|
export { rule as maybeRule };
|
package/rules/nullable_rule.ts
CHANGED
|
@@ -27,15 +27,15 @@ const rule = {
|
|
|
27
27
|
if (types.length !== 3) return;
|
|
28
28
|
|
|
29
29
|
const nullType = types.find(
|
|
30
|
-
(typeNode) => typeNode.type === "TSNullKeyword"
|
|
30
|
+
(typeNode) => typeNode.type === "TSNullKeyword"
|
|
31
31
|
);
|
|
32
32
|
const undefinedType = types.find(
|
|
33
|
-
(typeNode) => typeNode.type === "TSUndefinedKeyword"
|
|
33
|
+
(typeNode) => typeNode.type === "TSUndefinedKeyword"
|
|
34
34
|
);
|
|
35
35
|
const otherType = types.find(
|
|
36
36
|
(typeNode) =>
|
|
37
37
|
typeNode.type !== "TSNullKeyword" &&
|
|
38
|
-
typeNode.type !== "TSUndefinedKeyword"
|
|
38
|
+
typeNode.type !== "TSUndefinedKeyword"
|
|
39
39
|
);
|
|
40
40
|
|
|
41
41
|
if (nullType && undefinedType && otherType) {
|
|
@@ -52,36 +52,33 @@ const rule = {
|
|
|
52
52
|
node,
|
|
53
53
|
fixer,
|
|
54
54
|
"Nullable",
|
|
55
|
-
"type"
|
|
55
|
+
"type"
|
|
56
56
|
);
|
|
57
57
|
if (importFixes === null) return null;
|
|
58
58
|
|
|
59
59
|
return [
|
|
60
60
|
...importFixes,
|
|
61
|
-
fixer.replaceText(
|
|
62
|
-
node,
|
|
63
|
-
`Nullable<${typeText}>`,
|
|
64
|
-
),
|
|
61
|
+
fixer.replaceText(node, `Nullable<${typeText}>`)
|
|
65
62
|
];
|
|
66
|
-
}
|
|
63
|
+
}
|
|
67
64
|
});
|
|
68
65
|
}
|
|
69
|
-
}
|
|
66
|
+
}
|
|
70
67
|
};
|
|
71
68
|
},
|
|
72
69
|
defaultOptions: [],
|
|
73
70
|
meta: {
|
|
74
71
|
type: "suggestion",
|
|
75
72
|
docs: {
|
|
76
|
-
description: "Use Nullable<T> instead of T | null | undefined"
|
|
73
|
+
description: "Use Nullable<T> instead of T | null | undefined"
|
|
77
74
|
},
|
|
78
75
|
fixable: "code",
|
|
79
76
|
schema: [],
|
|
80
77
|
messages: {
|
|
81
78
|
useNullable:
|
|
82
|
-
"Use Nullable<{{type}}> instead of {{type}} | null | undefined"
|
|
83
|
-
}
|
|
84
|
-
}
|
|
79
|
+
"Use Nullable<{{type}}> instead of {{type}} | null | undefined"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
85
82
|
};
|
|
86
83
|
|
|
87
84
|
export { rule as nullableRule };
|
package/rules/optional_rule.ts
CHANGED
|
@@ -26,7 +26,7 @@ const rule = {
|
|
|
26
26
|
if (node.types.length !== 2) return;
|
|
27
27
|
|
|
28
28
|
const undefinedType = node.types.find(
|
|
29
|
-
(typeNode) => typeNode.type === "TSUndefinedKeyword"
|
|
29
|
+
(typeNode) => typeNode.type === "TSUndefinedKeyword"
|
|
30
30
|
);
|
|
31
31
|
// `null | undefined` has no non-nullish member to name, and
|
|
32
32
|
// rewriting it to `Optional<null>` only restated it with more
|
|
@@ -34,7 +34,7 @@ const rule = {
|
|
|
34
34
|
const otherType = node.types.find(
|
|
35
35
|
(typeNode) =>
|
|
36
36
|
typeNode.type !== "TSUndefinedKeyword" &&
|
|
37
|
-
typeNode.type !== "TSNullKeyword"
|
|
37
|
+
typeNode.type !== "TSNullKeyword"
|
|
38
38
|
);
|
|
39
39
|
|
|
40
40
|
if (undefinedType && otherType) {
|
|
@@ -52,36 +52,33 @@ const rule = {
|
|
|
52
52
|
node,
|
|
53
53
|
fixer,
|
|
54
54
|
"Optional",
|
|
55
|
-
"type"
|
|
55
|
+
"type"
|
|
56
56
|
);
|
|
57
57
|
if (importFixes === null) return null;
|
|
58
58
|
|
|
59
59
|
return [
|
|
60
60
|
...importFixes,
|
|
61
|
-
fixer.replaceText(
|
|
62
|
-
node,
|
|
63
|
-
`Optional<${typeText}>`,
|
|
64
|
-
),
|
|
61
|
+
fixer.replaceText(node, `Optional<${typeText}>`)
|
|
65
62
|
];
|
|
66
|
-
}
|
|
63
|
+
}
|
|
67
64
|
});
|
|
68
65
|
}
|
|
69
|
-
}
|
|
66
|
+
}
|
|
70
67
|
};
|
|
71
68
|
},
|
|
72
69
|
defaultOptions: [],
|
|
73
70
|
meta: {
|
|
74
71
|
type: "suggestion",
|
|
75
72
|
docs: {
|
|
76
|
-
description: "Enforce using Optional<T> instead of T | undefined"
|
|
73
|
+
description: "Enforce using Optional<T> instead of T | undefined"
|
|
77
74
|
},
|
|
78
75
|
fixable: "code",
|
|
79
76
|
schema: [],
|
|
80
77
|
messages: {
|
|
81
78
|
preferOptional:
|
|
82
|
-
"Use Optional<{{type}}> instead of {{type}} | undefined"
|
|
83
|
-
}
|
|
84
|
-
}
|
|
79
|
+
"Use Optional<{{type}}> instead of {{type}} | undefined"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
85
82
|
};
|
|
86
83
|
|
|
87
84
|
export { rule as optionalRule };
|
package/rules/own_subtree.ts
CHANGED
|
@@ -6,7 +6,7 @@ type AstNode = {
|
|
|
6
6
|
const FUNCTION_TYPES = new Set([
|
|
7
7
|
"ArrowFunctionExpression",
|
|
8
8
|
"FunctionDeclaration",
|
|
9
|
-
"FunctionExpression"
|
|
9
|
+
"FunctionExpression"
|
|
10
10
|
]);
|
|
11
11
|
|
|
12
12
|
function isNode(value: unknown): value is AstNode {
|
|
@@ -32,7 +32,7 @@ function isNode(value: unknown): value is AstNode {
|
|
|
32
32
|
*/
|
|
33
33
|
function walkOwnSubtree(
|
|
34
34
|
root: unknown,
|
|
35
|
-
visit: (node: AstNode) => boolean | void
|
|
35
|
+
visit: (node: AstNode) => boolean | void
|
|
36
36
|
): void {
|
|
37
37
|
if (!isNode(root)) {
|
|
38
38
|
return;
|
package/rules/test_file.ts
CHANGED
|
@@ -61,7 +61,7 @@ function isTestFile(filename: string, cwd?: string): boolean {
|
|
|
61
61
|
TEST_FILE_NAME.test(baseName) ||
|
|
62
62
|
SETUP_FILE_NAME.test(baseName) ||
|
|
63
63
|
projectDirectories(path, cwd).some((segment) =>
|
|
64
|
-
TEST_DIRECTORIES.has(segment)
|
|
64
|
+
TEST_DIRECTORIES.has(segment)
|
|
65
65
|
)
|
|
66
66
|
);
|
|
67
67
|
}
|
|
@@ -93,9 +93,9 @@ function getCwd(context: object): string | undefined {
|
|
|
93
93
|
function withoutTestFiles<
|
|
94
94
|
Context extends object,
|
|
95
95
|
Visitors extends object,
|
|
96
|
-
Rest
|
|
96
|
+
Rest
|
|
97
97
|
>(
|
|
98
|
-
rule: Rest & { create(context: Context): Visitors }
|
|
98
|
+
rule: Rest & { create(context: Context): Visitors }
|
|
99
99
|
): Omit<Rest, "create"> & {
|
|
100
100
|
create(context: Context): Visitors | Record<string, never>;
|
|
101
101
|
} {
|
|
@@ -107,7 +107,7 @@ function withoutTestFiles<
|
|
|
107
107
|
}
|
|
108
108
|
|
|
109
109
|
return rule.create(context);
|
|
110
|
-
}
|
|
110
|
+
}
|
|
111
111
|
};
|
|
112
112
|
}
|
|
113
113
|
|
|
@@ -114,7 +114,7 @@ const MODULE_STATEMENTS = new Set([
|
|
|
114
114
|
"ExportDefaultDeclaration",
|
|
115
115
|
"ExportNamedDeclaration",
|
|
116
116
|
"ImportDeclaration",
|
|
117
|
-
"TSExportAssignment"
|
|
117
|
+
"TSExportAssignment"
|
|
118
118
|
]);
|
|
119
119
|
|
|
120
120
|
function isModuleStatement(statement: AstNode): boolean {
|
|
@@ -179,7 +179,7 @@ function getUpperScope(scope: ScopeLike): ScopeLike | null {
|
|
|
179
179
|
function findBinding(
|
|
180
180
|
sourceCode: ImportSourceCode,
|
|
181
181
|
node: unknown,
|
|
182
|
-
name: string
|
|
182
|
+
name: string
|
|
183
183
|
): unknown {
|
|
184
184
|
const start: unknown = sourceCode.getScope(node);
|
|
185
185
|
let scope: ScopeLike | null = isScopeLike(start) ? start : null;
|
|
@@ -222,7 +222,7 @@ function sourcesFor(kind: ImportKind): ReadonlySet<string> {
|
|
|
222
222
|
function isPackageImport(
|
|
223
223
|
definition: unknown,
|
|
224
224
|
name: string,
|
|
225
|
-
kind: ImportKind
|
|
225
|
+
kind: ImportKind
|
|
226
226
|
): boolean {
|
|
227
227
|
if (
|
|
228
228
|
typeof definition !== "object" ||
|
|
@@ -287,7 +287,7 @@ type MergeTarget = {
|
|
|
287
287
|
*/
|
|
288
288
|
function findMergeTarget(
|
|
289
289
|
imports: readonly AstNode[],
|
|
290
|
-
kind: ImportKind
|
|
290
|
+
kind: ImportKind
|
|
291
291
|
): MergeTarget | null {
|
|
292
292
|
const sources = sourcesFor(kind);
|
|
293
293
|
let inlineTarget: MergeTarget | null = null;
|
|
@@ -363,7 +363,7 @@ function planTypeBuddyImport(
|
|
|
363
363
|
sourceCode: ImportSourceCode,
|
|
364
364
|
node: unknown,
|
|
365
365
|
name: string,
|
|
366
|
-
kind: ImportKind
|
|
366
|
+
kind: ImportKind
|
|
367
367
|
): ImportPlan {
|
|
368
368
|
const program = getProgram(node);
|
|
369
369
|
if (!program || !isNode(node)) {
|
|
@@ -394,7 +394,7 @@ function planTypeBuddyImport(
|
|
|
394
394
|
return {
|
|
395
395
|
kind: "insert",
|
|
396
396
|
range: [merge.at, merge.at],
|
|
397
|
-
text: `, ${merge.inlineType ? "type " : ""}${name}
|
|
397
|
+
text: `, ${merge.inlineType ? "type " : ""}${name}`
|
|
398
398
|
};
|
|
399
399
|
}
|
|
400
400
|
|
|
@@ -502,7 +502,7 @@ function createImportPlanner(sourceCode: ImportSourceCode) {
|
|
|
502
502
|
node: unknown,
|
|
503
503
|
fixer: ImportFixer,
|
|
504
504
|
name: string,
|
|
505
|
-
kind: ImportKind
|
|
505
|
+
kind: ImportKind
|
|
506
506
|
): unknown[] | null {
|
|
507
507
|
const plan = planTypeBuddyImport(sourceCode, node, name, kind);
|
|
508
508
|
if (plan.kind === "conflict") {
|
|
@@ -513,7 +513,7 @@ function createImportPlanner(sourceCode: ImportSourceCode) {
|
|
|
513
513
|
}
|
|
514
514
|
|
|
515
515
|
return [];
|
|
516
|
-
}
|
|
516
|
+
}
|
|
517
517
|
};
|
|
518
518
|
}
|
|
519
519
|
|
|
@@ -521,6 +521,6 @@ export {
|
|
|
521
521
|
canAddImport,
|
|
522
522
|
createImportPlanner,
|
|
523
523
|
getBodyStartAfterDirectives,
|
|
524
|
-
planTypeBuddyImport
|
|
524
|
+
planTypeBuddyImport
|
|
525
525
|
};
|
|
526
526
|
export type { ImportFixer, ImportKind, ImportPlan, ImportSourceCode };
|
package/rules/use_server.ts
CHANGED
|
@@ -9,7 +9,7 @@ const USE_SERVER = "use server";
|
|
|
9
9
|
const FUNCTION_TYPES = new Set([
|
|
10
10
|
"ArrowFunctionExpression",
|
|
11
11
|
"FunctionDeclaration",
|
|
12
|
-
"FunctionExpression"
|
|
12
|
+
"FunctionExpression"
|
|
13
13
|
]);
|
|
14
14
|
|
|
15
15
|
function isUseServerNode(value: unknown): value is UseServerNode {
|
|
@@ -101,7 +101,7 @@ function createUseServerGuard() {
|
|
|
101
101
|
serverModule = startsWithUseServer(node.body);
|
|
102
102
|
},
|
|
103
103
|
guard<Node extends UseServerNode>(
|
|
104
|
-
visitor: (node: Node) => void
|
|
104
|
+
visitor: (node: Node) => void
|
|
105
105
|
): (node: Node) => void {
|
|
106
106
|
return (node) => {
|
|
107
107
|
if (serverModule || isInsideUseServerFunction(node)) {
|
|
@@ -110,7 +110,7 @@ function createUseServerGuard() {
|
|
|
110
110
|
|
|
111
111
|
visitor(node);
|
|
112
112
|
};
|
|
113
|
-
}
|
|
113
|
+
}
|
|
114
114
|
};
|
|
115
115
|
}
|
|
116
116
|
|
package/src/collections.ts
CHANGED
|
@@ -21,7 +21,7 @@ function getKeys<T extends Record<string, unknown>>(object: T): (keyof T)[] {
|
|
|
21
21
|
*/
|
|
22
22
|
function arrayContainsCommonValue<T>(
|
|
23
23
|
array1: readonly T[],
|
|
24
|
-
array2: readonly T[]
|
|
24
|
+
array2: readonly T[]
|
|
25
25
|
): boolean {
|
|
26
26
|
if (!isArray(array1) || !isArray(array2)) {
|
|
27
27
|
return false;
|
package/src/empty.ts
CHANGED
package/src/guards.ts
CHANGED
|
@@ -169,7 +169,7 @@ function fastIsArray(value: unknown): value is readonly unknown[] {
|
|
|
169
169
|
* @returns {boolean} True if the value is a finite number.
|
|
170
170
|
*/
|
|
171
171
|
function isNumber<T extends number>(
|
|
172
|
-
value: Nullable<T
|
|
172
|
+
value: Nullable<T>
|
|
173
173
|
): value is FiniteNumber<T>;
|
|
174
174
|
function isNumber(value: unknown): value is FiniteNumber;
|
|
175
175
|
function isNumber(value: unknown): value is FiniteNumber {
|
|
@@ -190,7 +190,7 @@ function isNumber(value: unknown): value is FiniteNumber {
|
|
|
190
190
|
* @returns {boolean} True if the value is an object.
|
|
191
191
|
*/
|
|
192
192
|
function isObject<T extends Record<string, unknown>>(
|
|
193
|
-
value: Nullable<T
|
|
193
|
+
value: Nullable<T>
|
|
194
194
|
): value is T;
|
|
195
195
|
function isObject(value: unknown): value is Record<string, unknown>;
|
|
196
196
|
function isObject(value: unknown): value is Record<string, unknown> {
|
|
@@ -242,7 +242,7 @@ function isFunction(value: unknown): value is UnknownFunction {
|
|
|
242
242
|
* @returns {boolean} True if the value is a promise.
|
|
243
243
|
*/
|
|
244
244
|
function isPromise<T>(
|
|
245
|
-
value: Nullable<Readonly<PromiseLike<T
|
|
245
|
+
value: Nullable<Readonly<PromiseLike<T>>>
|
|
246
246
|
): value is PromiseLike<T>;
|
|
247
247
|
function isPromise(value: unknown): value is PromiseLike<unknown>;
|
|
248
248
|
function isPromise(value: unknown): value is PromiseLike<unknown> {
|
|
@@ -345,7 +345,7 @@ function isEmptyObject(value: unknown): value is EmptyObject {
|
|
|
345
345
|
*/
|
|
346
346
|
function isInstanceOf<T>(
|
|
347
347
|
value: unknown,
|
|
348
|
-
constructor: new (...args: unknown[]) => T
|
|
348
|
+
constructor: new (...args: unknown[]) => T
|
|
349
349
|
): value is T {
|
|
350
350
|
return value instanceof constructor;
|
|
351
351
|
}
|
|
@@ -403,5 +403,5 @@ export {
|
|
|
403
403
|
isRegExp,
|
|
404
404
|
isString,
|
|
405
405
|
isSymbol,
|
|
406
|
-
isUndefined
|
|
406
|
+
isUndefined
|
|
407
407
|
};
|
package/src/parse.ts
CHANGED
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
isNumber,
|
|
11
11
|
isObject,
|
|
12
12
|
isString,
|
|
13
|
-
isUndefined
|
|
13
|
+
isUndefined
|
|
14
14
|
} from "./guards.js";
|
|
15
15
|
import type { Optional } from "./types/optional.js";
|
|
16
16
|
|
|
@@ -53,7 +53,7 @@ function withoutNegativeZero(value: number): number {
|
|
|
53
53
|
}
|
|
54
54
|
|
|
55
55
|
function cloneDefaultArray(
|
|
56
|
-
defaultValue?: readonly unknown[]
|
|
56
|
+
defaultValue?: readonly unknown[]
|
|
57
57
|
): unknown[] | undefined {
|
|
58
58
|
if (!defaultValue) {
|
|
59
59
|
return defaultValue;
|
|
@@ -211,7 +211,7 @@ function parseArray<T>(value: unknown, defaultValue: readonly T[]): T[];
|
|
|
211
211
|
function parseArray(value: unknown): unknown[] | undefined;
|
|
212
212
|
function parseArray(
|
|
213
213
|
value: unknown,
|
|
214
|
-
defaultValue?: readonly unknown[]
|
|
214
|
+
defaultValue?: readonly unknown[]
|
|
215
215
|
): unknown[] | undefined {
|
|
216
216
|
if (isArray(value)) {
|
|
217
217
|
return [...value];
|
|
@@ -290,5 +290,5 @@ export {
|
|
|
290
290
|
parseFloat,
|
|
291
291
|
parseInteger,
|
|
292
292
|
parseNumber,
|
|
293
|
-
parseString
|
|
293
|
+
parseString
|
|
294
294
|
};
|
package/src/public_api.ts
CHANGED
package/src/result.ts
CHANGED
|
@@ -27,7 +27,7 @@ import type { Failed, Result, Success } from "./types/maybe_promise.js";
|
|
|
27
27
|
* `Success` member when the condition is met.
|
|
28
28
|
*/
|
|
29
29
|
function isSuccess<R extends Readonly<Result<unknown>>>(
|
|
30
|
-
result: R
|
|
30
|
+
result: R
|
|
31
31
|
): result is Extract<R, { isError: false }> {
|
|
32
32
|
return !result.isError;
|
|
33
33
|
}
|
|
@@ -40,7 +40,7 @@ function ok<T>(...args: readonly [T] | []): Success<T | void> {
|
|
|
40
40
|
const [value] = args;
|
|
41
41
|
return {
|
|
42
42
|
isError: false,
|
|
43
|
-
value
|
|
43
|
+
value
|
|
44
44
|
};
|
|
45
45
|
}
|
|
46
46
|
|
|
@@ -52,7 +52,7 @@ function err<T extends null>(...args: readonly [T] | []): Failed {
|
|
|
52
52
|
const [value] = args;
|
|
53
53
|
return {
|
|
54
54
|
isError: true,
|
|
55
|
-
value: value ?? null
|
|
55
|
+
value: value ?? null
|
|
56
56
|
};
|
|
57
57
|
}
|
|
58
58
|
|
package/src/types/globals.ts
CHANGED
package/src/types/index.ts
CHANGED
|
@@ -5,7 +5,7 @@ import type {
|
|
|
5
5
|
Failed,
|
|
6
6
|
MaybePromise,
|
|
7
7
|
Result,
|
|
8
|
-
Success
|
|
8
|
+
Success
|
|
9
9
|
} from "./maybe_promise.js";
|
|
10
10
|
import type { Nullable, ResolveNullable } from "./nullable.js";
|
|
11
11
|
import type { Optional, ResolveOptional } from "./optional.js";
|
|
@@ -24,5 +24,5 @@ export type {
|
|
|
24
24
|
ResolveOptional,
|
|
25
25
|
Result,
|
|
26
26
|
Stringified,
|
|
27
|
-
Success
|
|
27
|
+
Success
|
|
28
28
|
};
|