@murky-web/typebuddy 3.0.0 → 3.1.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 +23 -10
- package/dist/oxlint.js +2 -0
- package/dist/rules/async_rule.js +18 -1
- package/dist/rules/maybe_promise_rule.js +25 -7
- package/dist/rules/maybe_rule.js +5 -2
- package/dist/rules/nullable_rule.js +5 -2
- package/dist/rules/optional_rule.js +5 -2
- package/dist/rules/useless_undefined_rule.js +512 -0
- package/globals.d.ts +7 -7
- package/jsr.json +24 -24
- package/oxlint/index.ts +8 -4
- package/package.json +3 -3
- package/rules/async_rule.ts +47 -20
- package/rules/maybe_promise_rule.ts +60 -40
- package/rules/maybe_rule.ts +17 -13
- package/rules/nullable_rule.ts +16 -16
- package/rules/optional_rule.ts +15 -15
- 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/rules/useless_undefined_rule.ts +982 -0
- 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 -5
- package/src/types/globals.ts +1 -1
- package/src/types/index.ts +2 -2
package/rules/optional_rule.ts
CHANGED
|
@@ -14,7 +14,7 @@ const rule = {
|
|
|
14
14
|
report(descriptor: {
|
|
15
15
|
node: unknown;
|
|
16
16
|
messageId: string;
|
|
17
|
-
data: { type: string };
|
|
17
|
+
data: { type: string; union: string };
|
|
18
18
|
fix(fixer: RuleFixer): unknown;
|
|
19
19
|
}): void;
|
|
20
20
|
}) {
|
|
@@ -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) {
|
|
@@ -43,7 +43,10 @@ const rule = {
|
|
|
43
43
|
context.report({
|
|
44
44
|
node,
|
|
45
45
|
messageId: "preferOptional",
|
|
46
|
-
data: {
|
|
46
|
+
data: {
|
|
47
|
+
type: typeText,
|
|
48
|
+
union: sourceCode.getText(node)
|
|
49
|
+
},
|
|
47
50
|
fix(fixer) {
|
|
48
51
|
// See `prefer-maybe`: the fix only uses the
|
|
49
52
|
// package's own `Optional`, imported where it is
|
|
@@ -52,36 +55,33 @@ const rule = {
|
|
|
52
55
|
node,
|
|
53
56
|
fixer,
|
|
54
57
|
"Optional",
|
|
55
|
-
"type"
|
|
58
|
+
"type"
|
|
56
59
|
);
|
|
57
60
|
if (importFixes === null) return null;
|
|
58
61
|
|
|
59
62
|
return [
|
|
60
63
|
...importFixes,
|
|
61
|
-
fixer.replaceText(
|
|
62
|
-
node,
|
|
63
|
-
`Optional<${typeText}>`,
|
|
64
|
-
),
|
|
64
|
+
fixer.replaceText(node, `Optional<${typeText}>`)
|
|
65
65
|
];
|
|
66
|
-
}
|
|
66
|
+
}
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
|
-
}
|
|
69
|
+
}
|
|
70
70
|
};
|
|
71
71
|
},
|
|
72
72
|
defaultOptions: [],
|
|
73
73
|
meta: {
|
|
74
74
|
type: "suggestion",
|
|
75
75
|
docs: {
|
|
76
|
-
description: "Enforce using Optional<T> instead of T | undefined"
|
|
76
|
+
description: "Enforce using Optional<T> instead of T | undefined"
|
|
77
77
|
},
|
|
78
78
|
fixable: "code",
|
|
79
79
|
schema: [],
|
|
80
80
|
messages: {
|
|
81
81
|
preferOptional:
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
}
|
|
82
|
+
'`{{union}}` is the type typebuddy names `Optional<{{type}}>`. Both mean the same, and one spelling keeps `T | undefined` recognisable as the thing `isOptional` narrows. Fix: `Optional<{{type}}>` with `import type { Optional } from "@murky-web/typebuddy";` (`--fix` applies it where `Optional` is not already another binding).'
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
85
|
};
|
|
86
86
|
|
|
87
87
|
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
|
|