@murky-web/typebuddy 2.0.0 → 3.0.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 +184 -72
- package/dist/collections.js +1 -2
- package/dist/empty.js +34 -13
- package/dist/guards.js +33 -9
- package/dist/ids.js +6 -6
- package/dist/oxlint.js +7 -7
- package/dist/parse.js +39 -15
- package/dist/result.js +9 -8
- package/dist/rules/async_rule.js +45 -28
- package/dist/rules/deliberate_throw.js +5 -6
- package/dist/rules/maybe_promise_rule.js +109 -47
- package/dist/rules/maybe_rule.js +7 -5
- package/dist/rules/nullable_rule.js +7 -5
- package/dist/rules/optional_rule.js +8 -4
- package/dist/rules/own_subtree.js +1 -2
- package/dist/rules/test_file.js +63 -0
- package/dist/rules/typebuddy_import.js +242 -35
- package/dist/rules/use_server.js +63 -0
- package/globals.d.ts +49 -44
- package/jsr.json +3 -2
- package/oxlint/index.ts +7 -5
- package/package.json +92 -91
- package/rules/async_rule.ts +87 -67
- package/rules/deliberate_throw.ts +11 -11
- package/rules/maybe_promise_rule.ts +229 -98
- package/rules/maybe_rule.ts +74 -47
- package/rules/nullable_rule.ts +78 -50
- package/rules/optional_rule.ts +80 -51
- package/rules/own_subtree.ts +33 -33
- package/rules/test_file.ts +114 -0
- package/rules/typebuddy_import.ts +479 -87
- package/rules/use_server.ts +117 -0
- package/src/empty.ts +57 -19
- package/src/guards.ts +96 -30
- package/src/ids.ts +10 -4
- package/src/index.ts +1 -18
- package/src/parse.ts +104 -25
- package/src/public_api.ts +22 -0
- package/src/result.ts +11 -9
- package/src/types/globals.ts +11 -11
- package/src/types/index.ts +21 -21
- package/src/types/json.ts +48 -11
- package/src/types/maybe_promise.ts +17 -12
- package/src/types/nullable.ts +2 -2
- package/src/types/optional.ts +6 -6
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// rules/test_file.ts
|
|
2
|
+
const TEST_DIRECTORIES = /* @__PURE__ */ new Set([
|
|
3
|
+
"__mocks__",
|
|
4
|
+
"__tests__",
|
|
5
|
+
"test",
|
|
6
|
+
"tests"
|
|
7
|
+
]);
|
|
8
|
+
const TEST_FILE_NAME = /\.(?:bench|spec|test)\.[^.]+/;
|
|
9
|
+
const SETUP_FILE_NAME = /^(?:setupTests|vitest-setup|vitest\.setup)\.[^.]+$/;
|
|
10
|
+
const ABSOLUTE_PATH = /^(?:\/|[A-Za-z]:\/)/;
|
|
11
|
+
function toSlashes(path) {
|
|
12
|
+
return path.replaceAll("\\", "/");
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* The directory segments of `filename` that count for the directory match: the
|
|
16
|
+
* ones below the project's working directory `cwd`. A relative filename is
|
|
17
|
+
* relative to the project already. An absolute one needs `cwd` to tell the
|
|
18
|
+
* project's own directories from the ones above it (`/tmp/test/app/src/x.ts` is
|
|
19
|
+
* not under a test directory); without `cwd`, or outside it, no directory
|
|
20
|
+
* counts, so the mistake is a test helper that gets linted, never a whole
|
|
21
|
+
* project that goes silent.
|
|
22
|
+
*/
|
|
23
|
+
function projectDirectories(path, cwd) {
|
|
24
|
+
const segments = path.split("/");
|
|
25
|
+
segments.pop();
|
|
26
|
+
if (!ABSOLUTE_PATH.test(path)) return segments;
|
|
27
|
+
if (cwd === void 0) return [];
|
|
28
|
+
const root = toSlashes(cwd).replace(/\/+$/, "");
|
|
29
|
+
if (root === "" || !path.startsWith(`${root}/`)) return [];
|
|
30
|
+
return path.slice(root.length + 1).split("/").slice(0, -1);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Whether `filename` is test code: test, spec and bench files, files under a
|
|
34
|
+
* `__tests__`, `tests`, `test` or `__mocks__` directory of the project rooted
|
|
35
|
+
* at `cwd`, and test setup files. Both `/` and `\` separate path segments.
|
|
36
|
+
*/
|
|
37
|
+
function isTestFile(filename, cwd) {
|
|
38
|
+
const path = toSlashes(filename);
|
|
39
|
+
const baseName = path.slice(path.lastIndexOf("/") + 1);
|
|
40
|
+
return TEST_FILE_NAME.test(baseName) || SETUP_FILE_NAME.test(baseName) || projectDirectories(path, cwd).some((segment) => TEST_DIRECTORIES.has(segment));
|
|
41
|
+
}
|
|
42
|
+
function getFilename(context) {
|
|
43
|
+
if ("filename" in context && typeof context.filename === "string") return context.filename;
|
|
44
|
+
return "";
|
|
45
|
+
}
|
|
46
|
+
function getCwd(context) {
|
|
47
|
+
if ("cwd" in context && typeof context.cwd === "string") return context.cwd;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Make a rule a no-op for test files: its `create` returns no visitors, so test
|
|
51
|
+
* helpers, fixtures and mocks are not asked to follow the typebuddy conventions
|
|
52
|
+
* (`MaybePromise` results, `Maybe`/`Nullable`/`Optional` types).
|
|
53
|
+
*/
|
|
54
|
+
function withoutTestFiles(rule) {
|
|
55
|
+
return {
|
|
56
|
+
...rule,
|
|
57
|
+
create(context) {
|
|
58
|
+
if (isTestFile(getFilename(context), getCwd(context))) return {};
|
|
59
|
+
return rule.create(context);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
export { isTestFile, withoutTestFiles };
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
|
|
1
|
+
// rules/typebuddy_import.ts
|
|
2
2
|
const PACKAGE_NAME = "@murky-web/typebuddy";
|
|
3
|
+
/**
|
|
4
|
+
* The entries that export `ok` and `err`. The result helpers live in their own
|
|
5
|
+
* entry so a caller that only needs them does not pull the guards along, and a
|
|
6
|
+
* file that imports them from there already has what the fix is about to add.
|
|
7
|
+
*/
|
|
8
|
+
const RESULT_HELPER_SOURCES = /* @__PURE__ */ new Set([PACKAGE_NAME, `${PACKAGE_NAME}/result`]);
|
|
9
|
+
const TYPE_SOURCES = /* @__PURE__ */ new Set([PACKAGE_NAME]);
|
|
3
10
|
function isNode(value) {
|
|
4
11
|
return typeof value === "object" && value !== null && "type" in value;
|
|
5
12
|
}
|
|
6
|
-
function isIdentifierNamed(node, name) {
|
|
7
|
-
return isNode(node) && node.type === "Identifier" && node["name"] === name;
|
|
8
|
-
}
|
|
9
13
|
function isImportDeclaration(node) {
|
|
10
14
|
return isNode(node) && node.type === "ImportDeclaration";
|
|
11
15
|
}
|
|
@@ -14,6 +18,11 @@ function getStringLiteralValue(node) {
|
|
|
14
18
|
if ((node.type === "Literal" || node.type === "StringLiteral") && typeof node["value"] === "string") return node["value"];
|
|
15
19
|
return null;
|
|
16
20
|
}
|
|
21
|
+
function getModuleExportName(node) {
|
|
22
|
+
if (!isNode(node)) return null;
|
|
23
|
+
if (node.type === "Identifier" && typeof node["name"] === "string") return node["name"];
|
|
24
|
+
return getStringLiteralValue(node);
|
|
25
|
+
}
|
|
17
26
|
function getProgram(node) {
|
|
18
27
|
if (!isNode(node)) return null;
|
|
19
28
|
let current = node;
|
|
@@ -24,56 +33,254 @@ function getProgramBody(program) {
|
|
|
24
33
|
if (!Array.isArray(program["body"])) return [];
|
|
25
34
|
return program["body"].filter(isNode);
|
|
26
35
|
}
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
+
/** `"use client"`, `"use cache"`, `"use strict"`: the directive prologue. */
|
|
37
|
+
function isDirective(statement) {
|
|
38
|
+
return statement.type === "ExpressionStatement" && typeof statement["directive"] === "string";
|
|
39
|
+
}
|
|
40
|
+
const MODULE_STATEMENTS = /* @__PURE__ */ new Set([
|
|
41
|
+
"ExportAllDeclaration",
|
|
42
|
+
"ExportDefaultDeclaration",
|
|
43
|
+
"ExportNamedDeclaration",
|
|
44
|
+
"ImportDeclaration",
|
|
45
|
+
"TSExportAssignment"
|
|
46
|
+
]);
|
|
47
|
+
function isModuleStatement(statement) {
|
|
48
|
+
if (MODULE_STATEMENTS.has(statement.type)) return true;
|
|
49
|
+
if (statement.type !== "TSImportEqualsDeclaration") return false;
|
|
50
|
+
const reference = statement["moduleReference"];
|
|
51
|
+
return isNode(reference) && reference.type === "TSExternalModuleReference";
|
|
36
52
|
}
|
|
37
53
|
/**
|
|
38
|
-
*
|
|
54
|
+
* Whether the file is a module. TypeScript decides this by its statements, not
|
|
55
|
+
* by the extension: a file without an `import` or `export` is a script, its
|
|
56
|
+
* top-level names are global, and adding an `import` would turn it into a
|
|
57
|
+
* module and take its globals away (`vite-env.d.ts` with `interface
|
|
58
|
+
* ImportMetaEnv`, `interface Window`, `declare var`).
|
|
59
|
+
*/
|
|
60
|
+
function isModule(program) {
|
|
61
|
+
return getProgramBody(program).some(isModuleStatement);
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Whether the file the node is in can take an import without changing what it
|
|
65
|
+
* is: it already is a module. A script file cannot, and `ok`/`err` have no
|
|
66
|
+
* global name to fall back on, so a rule that needs them there must not offer a
|
|
67
|
+
* fix.
|
|
39
68
|
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
69
|
+
* @param {unknown} node - Any node of the file.
|
|
70
|
+
* @returns {boolean} True when an import can be added to the file.
|
|
71
|
+
*/
|
|
72
|
+
function canAddImport(node) {
|
|
73
|
+
const program = getProgram(node);
|
|
74
|
+
return program !== null && isModule(program);
|
|
75
|
+
}
|
|
76
|
+
function isScopeLike(value) {
|
|
77
|
+
return typeof value === "object" && value !== null && "set" in value && value.set instanceof Map;
|
|
78
|
+
}
|
|
79
|
+
function getUpperScope(scope) {
|
|
80
|
+
const upper = scope.upper;
|
|
81
|
+
return isScopeLike(upper) ? upper : null;
|
|
82
|
+
}
|
|
83
|
+
/** The first binding of `name` visible from `node`, innermost scope first. */
|
|
84
|
+
function findBinding(sourceCode, node, name) {
|
|
85
|
+
const start = sourceCode.getScope(node);
|
|
86
|
+
let scope = isScopeLike(start) ? start : null;
|
|
87
|
+
while (scope) {
|
|
88
|
+
const variable = scope.set.get(name);
|
|
89
|
+
if (variable !== void 0) return variable;
|
|
90
|
+
scope = getUpperScope(scope);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function getDefinitions(variable) {
|
|
94
|
+
if (typeof variable === "object" && variable !== null && "defs" in variable && Array.isArray(variable.defs)) return variable.defs;
|
|
95
|
+
return [];
|
|
96
|
+
}
|
|
97
|
+
function sourcesFor(kind) {
|
|
98
|
+
return kind === "value" ? RESULT_HELPER_SOURCES : TYPE_SOURCES;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Whether one definition is `import { name } from "<package>"`, imported under
|
|
102
|
+
* its own name. A renamed import (`import { ok as okay }`), a default import
|
|
103
|
+
* and an import from another module are different bindings that happen to share
|
|
104
|
+
* the name. For a value, a type-only import does not count either: it has no
|
|
105
|
+
* runtime binding to call.
|
|
106
|
+
*/
|
|
107
|
+
function isPackageImport(definition, name, kind) {
|
|
108
|
+
if (typeof definition !== "object" || definition === null || !("type" in definition) || definition.type !== "ImportBinding" || !("node" in definition) || !("parent" in definition)) return false;
|
|
109
|
+
const specifier = definition.node;
|
|
110
|
+
const declaration = definition.parent;
|
|
111
|
+
if (!isNode(specifier) || specifier.type !== "ImportSpecifier" || !isImportDeclaration(declaration)) return false;
|
|
112
|
+
const source = getStringLiteralValue(declaration["source"]);
|
|
113
|
+
if (source === null || !sourcesFor(kind).has(source)) return false;
|
|
114
|
+
if (getModuleExportName(specifier["imported"]) !== name) return false;
|
|
115
|
+
if (kind === "value") return declaration["importKind"] !== "type" && specifier["importKind"] !== "type";
|
|
116
|
+
return true;
|
|
117
|
+
}
|
|
118
|
+
function getSpecifiers(declaration) {
|
|
119
|
+
return Array.isArray(declaration["specifiers"]) ? declaration["specifiers"].filter(isNode) : [];
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* The import of the package that the name can join, or `null` when there is
|
|
123
|
+
* none. A value joins `import { … }`. A type joins `import type { … }`, or else
|
|
124
|
+
* an `import { … }` of the package as `type Name` (an inline type specifier),
|
|
125
|
+
* so the file does not end up with two imports from one module. An import with
|
|
126
|
+
* a namespace specifier, or with no named specifier, is left alone.
|
|
127
|
+
*/
|
|
128
|
+
function findMergeTarget(imports, kind) {
|
|
129
|
+
const sources = sourcesFor(kind);
|
|
130
|
+
let inlineTarget = null;
|
|
131
|
+
for (const declaration of imports) {
|
|
132
|
+
const source = getStringLiteralValue(declaration["source"]);
|
|
133
|
+
const isTypeImport = declaration["importKind"] === "type";
|
|
134
|
+
if (source === null || !sources.has(source)) continue;
|
|
135
|
+
if (kind === "value" && isTypeImport) continue;
|
|
136
|
+
const specifiers = getSpecifiers(declaration);
|
|
137
|
+
if (specifiers.some((specifier) => {
|
|
138
|
+
return specifier.type === "ImportNamespaceSpecifier";
|
|
139
|
+
})) continue;
|
|
140
|
+
const range = specifiers.filter((specifier) => {
|
|
141
|
+
return specifier.type === "ImportSpecifier";
|
|
142
|
+
}).at(-1)?.["range"];
|
|
143
|
+
if (!Array.isArray(range) || typeof range[1] !== "number") continue;
|
|
144
|
+
if (kind === "value" || isTypeImport) return {
|
|
145
|
+
at: range[1],
|
|
146
|
+
inlineType: false
|
|
147
|
+
};
|
|
148
|
+
inlineTarget ??= {
|
|
149
|
+
at: range[1],
|
|
150
|
+
inlineType: true
|
|
151
|
+
};
|
|
152
|
+
}
|
|
153
|
+
return inlineTarget;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Where and what to insert so `name` is imported from the package, and whether
|
|
157
|
+
* it has to be at all.
|
|
158
|
+
*
|
|
159
|
+
* The answer comes from the scope at the place the fix is about to use the
|
|
160
|
+
* name, not from the list of imports. A catch parameter called `err`, a local
|
|
161
|
+
* called `ok`, or an unrelated library's `Maybe` all make a name mean something
|
|
162
|
+
* else there, and adding an import for it would not change that.
|
|
46
163
|
*
|
|
47
|
-
*
|
|
164
|
+
* Where the import goes: appended to an existing import of the same kind from
|
|
165
|
+
* the package; otherwise a new line after the last import; with no import,
|
|
166
|
+
* after the directive prologue (`"use client"` has to stay the first statement
|
|
167
|
+
* of the file or it stops being a directive); with neither, before the first
|
|
168
|
+
* statement.
|
|
48
169
|
*
|
|
49
|
-
*
|
|
170
|
+
* A script file (no `import` or `export`) gets no import: that would turn it
|
|
171
|
+
* into a module and take its globals away. A type is then written by its global
|
|
172
|
+
* name, as it was before this rule added imports; a value, which has no global
|
|
173
|
+
* name, gets no fix. Inside a module, `declare global` and `declare module`
|
|
174
|
+
* blocks get the import like any other code: the file is a module already, and
|
|
175
|
+
* the global name would break a project that does not load the globals entry.
|
|
50
176
|
*
|
|
51
|
-
* @param {
|
|
52
|
-
* @param {
|
|
53
|
-
* @
|
|
54
|
-
*
|
|
55
|
-
*
|
|
177
|
+
* @param {ImportSourceCode} sourceCode - Source code of the file, for scopes.
|
|
178
|
+
* @param {unknown} node - The node the fix uses `name` at.
|
|
179
|
+
* @param {string} name - The named export to import, e.g. `"err"`.
|
|
180
|
+
* @param {ImportKind} kind - `"value"` for a helper that is called, `"type"`
|
|
181
|
+
* for a type that is only named.
|
|
182
|
+
* @returns {ImportPlan} Whether to insert, with the zero-width insertion to
|
|
183
|
+
* apply.
|
|
56
184
|
*/
|
|
57
|
-
function
|
|
185
|
+
function planTypeBuddyImport(sourceCode, node, name, kind) {
|
|
58
186
|
const program = getProgram(node);
|
|
59
|
-
if (!program ||
|
|
187
|
+
if (!program || !isNode(node)) return { kind: "conflict" };
|
|
188
|
+
const binding = findBinding(sourceCode, node, name);
|
|
189
|
+
if (binding !== void 0) {
|
|
190
|
+
const definitions = getDefinitions(binding);
|
|
191
|
+
return { kind: definitions.length > 0 && definitions.every((definition) => {
|
|
192
|
+
return isPackageImport(definition, name, kind);
|
|
193
|
+
}) ? "present" : "conflict" };
|
|
194
|
+
}
|
|
195
|
+
if (!isModule(program)) return { kind: kind === "type" ? "global" : "conflict" };
|
|
60
196
|
const body = getProgramBody(program);
|
|
61
197
|
const imports = body.filter(isImportDeclaration);
|
|
62
|
-
const
|
|
63
|
-
if (
|
|
64
|
-
|
|
65
|
-
|
|
198
|
+
const merge = findMergeTarget(imports, kind);
|
|
199
|
+
if (merge !== null) return {
|
|
200
|
+
kind: "insert",
|
|
201
|
+
range: [merge.at, merge.at],
|
|
202
|
+
text: `, ${merge.inlineType ? "type " : ""}${name}`
|
|
203
|
+
};
|
|
204
|
+
const statement = kind === "type" ? `import type { ${name} } from "${PACKAGE_NAME}";` : `import { ${name} } from "${PACKAGE_NAME}";`;
|
|
205
|
+
const lastImport = imports.at(-1);
|
|
206
|
+
if (lastImport) {
|
|
207
|
+
const range = lastImport["range"];
|
|
208
|
+
if (!Array.isArray(range) || range.length < 2) return { kind: "conflict" };
|
|
209
|
+
const end = range[1];
|
|
210
|
+
return {
|
|
211
|
+
kind: "insert",
|
|
212
|
+
range: [end, end],
|
|
213
|
+
text: `\n${statement}`
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
const lastDirective = body.filter(isDirective).at(-1);
|
|
217
|
+
if (lastDirective) {
|
|
218
|
+
const range = lastDirective["range"];
|
|
219
|
+
if (!Array.isArray(range) || range.length < 2) return { kind: "conflict" };
|
|
66
220
|
const end = range[1];
|
|
67
221
|
return {
|
|
222
|
+
kind: "insert",
|
|
68
223
|
range: [end, end],
|
|
69
224
|
text: `\n${statement}`
|
|
70
225
|
};
|
|
71
226
|
}
|
|
227
|
+
const range = (body[0] ?? program)["range"];
|
|
228
|
+
if (!Array.isArray(range) || range.length < 2) return { kind: "conflict" };
|
|
72
229
|
const start = range[0];
|
|
73
230
|
return {
|
|
231
|
+
kind: "insert",
|
|
74
232
|
range: [start, start],
|
|
75
233
|
text: `${statement}\n`
|
|
76
234
|
};
|
|
77
235
|
}
|
|
78
|
-
|
|
79
|
-
|
|
236
|
+
/**
|
|
237
|
+
* Where the first non-directive statement of a body starts, for a fix that
|
|
238
|
+
* wraps the body. `"use cache"` and `"use server"` only count as directives at
|
|
239
|
+
* the very top of the body; wrapping them in a `try` turns them into expression
|
|
240
|
+
* statements that do nothing.
|
|
241
|
+
*
|
|
242
|
+
* @param {unknown} body - A block statement.
|
|
243
|
+
* @returns {number | null} The offset right after the directive prologue, or
|
|
244
|
+
* the offset right after the opening brace when there is none; null when the
|
|
245
|
+
* range is unusable.
|
|
246
|
+
*/
|
|
247
|
+
function getBodyStartAfterDirectives(body) {
|
|
248
|
+
if (!isNode(body) || !Array.isArray(body["range"])) return null;
|
|
249
|
+
const opening = body["range"][0];
|
|
250
|
+
if (typeof opening !== "number") return null;
|
|
251
|
+
const lastDirective = (Array.isArray(body["body"]) ? body["body"].filter(isNode) : []).filter(isDirective).at(-1);
|
|
252
|
+
if (lastDirective) {
|
|
253
|
+
const directiveRange = lastDirective["range"];
|
|
254
|
+
if (Array.isArray(directiveRange) && typeof directiveRange[1] === "number") return directiveRange[1];
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
return opening + 1;
|
|
258
|
+
}
|
|
259
|
+
/**
|
|
260
|
+
* Turns import plans into the fixes a rule returns, for one file.
|
|
261
|
+
*
|
|
262
|
+
* `fixes` returns `null` when the name is something else at that spot (or a
|
|
263
|
+
* value cannot be imported there), and a rule then offers no fix at all: a fix
|
|
264
|
+
* that calls the caught `err` or a different library's `Maybe` is worse than
|
|
265
|
+
* none.
|
|
266
|
+
*
|
|
267
|
+
* Every fix that needs an import carries it itself. oxlint applies a fix as a
|
|
268
|
+
* whole or not at all, and drops a fix that overlaps one it applied earlier in
|
|
269
|
+
* the same pass; an import attached to only the first fix of a file would be
|
|
270
|
+
* lost with that fix while the later fixes, which no longer overlap, applied
|
|
271
|
+
* without it. The price is that fixes needing the same import share one
|
|
272
|
+
* insertion point, so one `--fix` pass applies one of them; the next pass finds
|
|
273
|
+
* the import present and applies the rest.
|
|
274
|
+
*
|
|
275
|
+
* @param {ImportSourceCode} sourceCode - Source code of the file, for scopes.
|
|
276
|
+
* @returns {{ fixes: Function }} The planner.
|
|
277
|
+
*/
|
|
278
|
+
function createImportPlanner(sourceCode) {
|
|
279
|
+
return { fixes(node, fixer, name, kind) {
|
|
280
|
+
const plan = planTypeBuddyImport(sourceCode, node, name, kind);
|
|
281
|
+
if (plan.kind === "conflict") return null;
|
|
282
|
+
if (plan.kind === "insert") return [fixer.insertTextBeforeRange(plan.range, plan.text)];
|
|
283
|
+
return [];
|
|
284
|
+
} };
|
|
285
|
+
}
|
|
286
|
+
export { canAddImport, createImportPlanner, getBodyStartAfterDirectives, planTypeBuddyImport };
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// rules/use_server.ts
|
|
2
|
+
const USE_SERVER = "use server";
|
|
3
|
+
const FUNCTION_TYPES = /* @__PURE__ */ new Set([
|
|
4
|
+
"ArrowFunctionExpression",
|
|
5
|
+
"FunctionDeclaration",
|
|
6
|
+
"FunctionExpression"
|
|
7
|
+
]);
|
|
8
|
+
function isUseServerNode(value) {
|
|
9
|
+
return typeof value === "object" && value !== null && "type" in value && typeof value.type === "string";
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Whether a statement is the `"use server"` directive.
|
|
13
|
+
*
|
|
14
|
+
* Parsers disagree on the shape: ESTree-style ASTs set `directive` on the
|
|
15
|
+
* expression statement, others only give the string literal.
|
|
16
|
+
*/
|
|
17
|
+
function isUseServerDirective(statement) {
|
|
18
|
+
if (!isUseServerNode(statement) || statement.type !== "ExpressionStatement") return false;
|
|
19
|
+
if ("directive" in statement && statement.directive === USE_SERVER) return true;
|
|
20
|
+
return "expression" in statement && isUseServerNode(statement.expression) && statement.expression.type === "Literal" && "value" in statement.expression && statement.expression.value === USE_SERVER;
|
|
21
|
+
}
|
|
22
|
+
function startsWithUseServer(statements) {
|
|
23
|
+
return Array.isArray(statements) && statements.length > 0 && isUseServerDirective(statements[0]);
|
|
24
|
+
}
|
|
25
|
+
function isUseServerFunction(node) {
|
|
26
|
+
if (!FUNCTION_TYPES.has(node.type) || !isUseServerNode(node.body)) return false;
|
|
27
|
+
return node.body.type === "BlockStatement" && startsWithUseServer(node.body.body);
|
|
28
|
+
}
|
|
29
|
+
function isInsideUseServerFunction(node) {
|
|
30
|
+
let current = node;
|
|
31
|
+
while (isUseServerNode(current)) {
|
|
32
|
+
if (isUseServerFunction(current)) return true;
|
|
33
|
+
current = current.parent;
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Exempt server code from a rule.
|
|
39
|
+
*
|
|
40
|
+
* Functions in `"use server"` modules log and throw on failure; the page
|
|
41
|
+
* catches the throw with `<Errored>`. A returned failure would resolve the
|
|
42
|
+
* caller's promise as a success, so the result-returning rules must not touch
|
|
43
|
+
* them. The exemption covers a module whose first statement is the directive
|
|
44
|
+
* and a function whose body starts with it, nested code included.
|
|
45
|
+
*
|
|
46
|
+
* `Program` has to be registered as a visitor next to the guarded ones: it runs
|
|
47
|
+
* first and records whether the whole file is server code.
|
|
48
|
+
*/
|
|
49
|
+
function createUseServerGuard() {
|
|
50
|
+
let serverModule = false;
|
|
51
|
+
return {
|
|
52
|
+
Program(node) {
|
|
53
|
+
serverModule = startsWithUseServer(node.body);
|
|
54
|
+
},
|
|
55
|
+
guard(visitor) {
|
|
56
|
+
return (node) => {
|
|
57
|
+
if (serverModule || isInsideUseServerFunction(node)) return;
|
|
58
|
+
visitor(node);
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
export { createUseServerGuard };
|
package/globals.d.ts
CHANGED
|
@@ -1,60 +1,65 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
Stringified as StringifiedImport,
|
|
2
|
+
JsonifiedValue as JsonifiedValueImport,
|
|
3
|
+
Stringified as StringifiedImport,
|
|
5
4
|
} from "./src/types/json.js";
|
|
6
|
-
import type { Maybe as MaybeImport, ResolveMaybe as ResolveMaybeImport } from "./src/types/maybe.js";
|
|
7
5
|
import type {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
6
|
+
Maybe as MaybeImport,
|
|
7
|
+
ResolveMaybe as ResolveMaybeImport,
|
|
8
|
+
} from "./src/types/maybe.js";
|
|
9
|
+
import type {
|
|
10
|
+
Failed as FailedImport,
|
|
11
|
+
MaybePromise as MaybePromiseImport,
|
|
12
|
+
Success as SuccessImport,
|
|
11
13
|
} from "./src/types/maybe_promise.js";
|
|
12
14
|
import type {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
+
Nullable as NullableImport,
|
|
16
|
+
ResolveNullable as ResolveNullableImport,
|
|
15
17
|
} from "./src/types/nullable.js";
|
|
16
18
|
import type {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
+
Optional as OptionalImport,
|
|
20
|
+
ResolveOptional as ResolveOptionalImport,
|
|
19
21
|
} from "./src/types/optional.js";
|
|
20
22
|
|
|
21
23
|
declare global {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
24
|
+
type Failed<T extends null = null> = FailedImport<T>;
|
|
25
|
+
type JsonifiedValue<T> = JsonifiedValueImport<T>;
|
|
26
|
+
type Maybe<T> = MaybeImport<T>;
|
|
27
|
+
type MaybePromise<T> = MaybePromiseImport<T>;
|
|
28
|
+
type Nullable<T> = NullableImport<T>;
|
|
29
|
+
type Optional<T> = OptionalImport<T>;
|
|
30
|
+
type ResolveMaybe<T, R extends Maybe<T>> = ResolveMaybeImport<T, R>;
|
|
31
|
+
type ResolveNullable<T, R extends Nullable<T>> = ResolveNullableImport<
|
|
32
|
+
T,
|
|
33
|
+
R
|
|
34
|
+
>;
|
|
35
|
+
type ResolveOptional<T, R extends Optional<T>> = ResolveOptionalImport<
|
|
36
|
+
T,
|
|
37
|
+
R
|
|
38
|
+
>;
|
|
39
|
+
type Stringified<T> = StringifiedImport<T>;
|
|
40
|
+
type Success<T> = SuccessImport<T>;
|
|
33
41
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
42
|
+
interface JSON {
|
|
43
|
+
stringify<T>(
|
|
44
|
+
value: T,
|
|
45
|
+
replacer?: null,
|
|
46
|
+
space?: string | number,
|
|
47
|
+
): Stringified<T>;
|
|
40
48
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
replacer?: null,
|
|
44
|
-
): JsonifiedObjectImport<T>;
|
|
45
|
-
}
|
|
49
|
+
parse<T>(str: Stringified<T>, replacer?: null): JsonifiedValueImport<T>;
|
|
50
|
+
}
|
|
46
51
|
}
|
|
47
52
|
|
|
48
53
|
export type {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
54
|
+
FailedImport as Failed,
|
|
55
|
+
JsonifiedValueImport as JsonifiedValue,
|
|
56
|
+
MaybeImport as Maybe,
|
|
57
|
+
MaybePromiseImport as MaybePromise,
|
|
58
|
+
NullableImport as Nullable,
|
|
59
|
+
OptionalImport as Optional,
|
|
60
|
+
ResolveMaybeImport as ResolveMaybe,
|
|
61
|
+
ResolveNullableImport as ResolveNullable,
|
|
62
|
+
ResolveOptionalImport as ResolveOptional,
|
|
63
|
+
StringifiedImport as Stringified,
|
|
64
|
+
SuccessImport as Success,
|
|
60
65
|
};
|
package/jsr.json
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://jsr.io/schema/config-file.v1.json",
|
|
3
3
|
"name": "@murky-web/typebuddy",
|
|
4
|
-
"version": "
|
|
4
|
+
"version": "3.0.0",
|
|
5
5
|
"license": "ISC",
|
|
6
6
|
"exports": {
|
|
7
|
-
".": "./src/
|
|
7
|
+
".": "./src/public_api.ts",
|
|
8
8
|
"./oxlint": "./oxlint/index.ts"
|
|
9
9
|
},
|
|
10
10
|
"publish": {
|
|
@@ -18,6 +18,7 @@
|
|
|
18
18
|
"tests/**",
|
|
19
19
|
"smoke/**",
|
|
20
20
|
"globals.d.ts",
|
|
21
|
+
"src/index.ts",
|
|
21
22
|
"dist/**",
|
|
22
23
|
"node_modules/**"
|
|
23
24
|
]
|
package/oxlint/index.ts
CHANGED
|
@@ -3,13 +3,15 @@ import { errorSafeAsyncRule } from "../rules/maybe_promise_rule.js";
|
|
|
3
3
|
import { maybeRule } from "../rules/maybe_rule.js";
|
|
4
4
|
import { nullableRule } from "../rules/nullable_rule.js";
|
|
5
5
|
import { optionalRule } from "../rules/optional_rule.js";
|
|
6
|
+
import { withoutTestFiles } from "../rules/test_file.js";
|
|
6
7
|
|
|
8
|
+
// typebuddy does not apply to test files: every rule is a no-op there.
|
|
7
9
|
const rules = {
|
|
8
|
-
"prefer-maybe": maybeRule,
|
|
9
|
-
"prefer-maybe-promise": errorSafeAsyncRule,
|
|
10
|
-
"prefer-nullable": nullableRule,
|
|
11
|
-
"prefer-optional": optionalRule,
|
|
12
|
-
"require-try-catch": requireTryCatchAsyncRule,
|
|
10
|
+
"prefer-maybe": withoutTestFiles(maybeRule),
|
|
11
|
+
"prefer-maybe-promise": withoutTestFiles(errorSafeAsyncRule),
|
|
12
|
+
"prefer-nullable": withoutTestFiles(nullableRule),
|
|
13
|
+
"prefer-optional": withoutTestFiles(optionalRule),
|
|
14
|
+
"require-try-catch": withoutTestFiles(requireTryCatchAsyncRule),
|
|
13
15
|
} as const satisfies Record<string, unknown>;
|
|
14
16
|
|
|
15
17
|
type TypebuddyOxlintPlugin = {
|