@murky-web/typebuddy 2.1.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 +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 +6 -6
- package/dist/rules/async_rule.js +39 -25
- package/dist/rules/deliberate_throw.js +5 -6
- package/dist/rules/maybe_promise_rule.js +99 -40
- 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 +1 -2
- package/globals.d.ts +49 -44
- package/jsr.json +24 -23
- package/oxlint/index.ts +9 -7
- package/package.json +92 -91
- package/rules/async_rule.ts +91 -75
- package/rules/deliberate_throw.ts +11 -11
- package/rules/maybe_promise_rule.ts +238 -111
- package/rules/maybe_rule.ts +76 -49
- package/rules/nullable_rule.ts +77 -52
- package/rules/optional_rule.ts +79 -53
- 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 +5 -4
- package/src/collections.ts +1 -1
- package/src/empty.ts +58 -20
- package/src/guards.ts +100 -34
- package/src/ids.ts +10 -4
- package/src/index.ts +1 -18
- package/src/parse.ts +108 -29
- package/src/public_api.ts +22 -0
- package/src/result.ts +8 -7
- 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
package/rules/maybe_rule.ts
CHANGED
|
@@ -1,57 +1,84 @@
|
|
|
1
|
+
import { createImportPlanner } from "./typebuddy_import.js";
|
|
2
|
+
|
|
3
|
+
type RuleFixer = {
|
|
4
|
+
replaceText(node: unknown, text: string): unknown;
|
|
5
|
+
insertTextBeforeRange(range: [number, number], text: string): unknown;
|
|
6
|
+
};
|
|
7
|
+
|
|
1
8
|
const rule = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}) {
|
|
15
|
-
const types = node.types;
|
|
9
|
+
create(context: {
|
|
10
|
+
getSourceCode(): {
|
|
11
|
+
getText(node: unknown): string;
|
|
12
|
+
getScope(node: unknown): unknown;
|
|
13
|
+
};
|
|
14
|
+
report(descriptor: {
|
|
15
|
+
node: unknown;
|
|
16
|
+
messageId: string;
|
|
17
|
+
data: { type: string };
|
|
18
|
+
fix(fixer: RuleFixer): unknown;
|
|
19
|
+
}): void;
|
|
20
|
+
}) {
|
|
16
21
|
const sourceCode = context.getSourceCode();
|
|
22
|
+
const imports = createImportPlanner(sourceCode);
|
|
17
23
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
);
|
|
22
|
-
const otherTypes = types.filter(
|
|
23
|
-
(typeNode) =>
|
|
24
|
-
typeNode.type !== "TSNullKeyword" &&
|
|
25
|
-
typeNode.type !== "TSUndefinedKeyword",
|
|
26
|
-
);
|
|
24
|
+
return {
|
|
25
|
+
TSUnionType(node: { types: Array<{ type: string }> }) {
|
|
26
|
+
const types = node.types;
|
|
27
27
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
28
|
+
const nullType = types.find(
|
|
29
|
+
(typeNode) => typeNode.type === "TSNullKeyword"
|
|
30
|
+
);
|
|
31
|
+
const undefinedType = types.find(
|
|
32
|
+
(typeNode) => typeNode.type === "TSUndefinedKeyword"
|
|
33
|
+
);
|
|
34
|
+
const otherTypes = types.filter(
|
|
35
|
+
(typeNode) =>
|
|
36
|
+
typeNode.type !== "TSNullKeyword" &&
|
|
37
|
+
typeNode.type !== "TSUndefinedKeyword"
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
if (nullType && !undefinedType && otherTypes.length === 1) {
|
|
41
|
+
const typeText = sourceCode.getText(otherTypes[0]);
|
|
42
|
+
context.report({
|
|
43
|
+
node,
|
|
44
|
+
messageId: "useMaybe",
|
|
45
|
+
data: { type: typeText },
|
|
46
|
+
fix(fixer) {
|
|
47
|
+
// `Maybe` has to be the package's type where the
|
|
48
|
+
// fix puts it: imported, or already imported.
|
|
49
|
+
// Another library's `Maybe` (or a local one) would
|
|
50
|
+
// change what the type means, so then there is no
|
|
51
|
+
// fix.
|
|
52
|
+
const importFixes = imports.fixes(
|
|
53
|
+
node,
|
|
54
|
+
fixer,
|
|
55
|
+
"Maybe",
|
|
56
|
+
"type"
|
|
57
|
+
);
|
|
58
|
+
if (importFixes === null) return null;
|
|
59
|
+
|
|
60
|
+
return [
|
|
61
|
+
...importFixes,
|
|
62
|
+
fixer.replaceText(node, `Maybe<${typeText}>`)
|
|
63
|
+
];
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
};
|
|
53
69
|
},
|
|
54
|
-
|
|
70
|
+
defaultOptions: [],
|
|
71
|
+
meta: {
|
|
72
|
+
type: "suggestion",
|
|
73
|
+
docs: {
|
|
74
|
+
description: "Use Maybe<T> for T | null"
|
|
75
|
+
},
|
|
76
|
+
fixable: "code",
|
|
77
|
+
schema: [],
|
|
78
|
+
messages: {
|
|
79
|
+
useMaybe: "Use Maybe<{{type}}> instead of {{type}} | null"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
55
82
|
};
|
|
56
83
|
|
|
57
84
|
export { rule as maybeRule };
|
package/rules/nullable_rule.ts
CHANGED
|
@@ -1,59 +1,84 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
data: { type: string };
|
|
8
|
-
fix(fixer: { replaceText(node: unknown, text: string): unknown }): unknown;
|
|
9
|
-
}): void;
|
|
10
|
-
}) {
|
|
11
|
-
return {
|
|
12
|
-
TSUnionType(node: {
|
|
13
|
-
types: Array<{ type: string }>;
|
|
14
|
-
}) {
|
|
15
|
-
const types = node.types;
|
|
16
|
-
if (types.length !== 3) return;
|
|
1
|
+
import { createImportPlanner } from "./typebuddy_import.js";
|
|
2
|
+
|
|
3
|
+
type RuleFixer = {
|
|
4
|
+
replaceText(node: unknown, text: string): unknown;
|
|
5
|
+
insertTextBeforeRange(range: [number, number], text: string): unknown;
|
|
6
|
+
};
|
|
17
7
|
|
|
8
|
+
const rule = {
|
|
9
|
+
create(context: {
|
|
10
|
+
getSourceCode(): {
|
|
11
|
+
getText(node: unknown): string;
|
|
12
|
+
getScope(node: unknown): unknown;
|
|
13
|
+
};
|
|
14
|
+
report(descriptor: {
|
|
15
|
+
node: unknown;
|
|
16
|
+
messageId: string;
|
|
17
|
+
data: { type: string };
|
|
18
|
+
fix(fixer: RuleFixer): unknown;
|
|
19
|
+
}): void;
|
|
20
|
+
}) {
|
|
18
21
|
const sourceCode = context.getSourceCode();
|
|
19
|
-
const
|
|
20
|
-
const undefinedType = types.find(
|
|
21
|
-
(typeNode) => typeNode.type === "TSUndefinedKeyword",
|
|
22
|
-
);
|
|
23
|
-
const otherType = types.find(
|
|
24
|
-
(typeNode) =>
|
|
25
|
-
typeNode.type !== "TSNullKeyword" &&
|
|
26
|
-
typeNode.type !== "TSUndefinedKeyword",
|
|
27
|
-
);
|
|
22
|
+
const imports = createImportPlanner(sourceCode);
|
|
28
23
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
24
|
+
return {
|
|
25
|
+
TSUnionType(node: { types: Array<{ type: string }> }) {
|
|
26
|
+
const types = node.types;
|
|
27
|
+
if (types.length !== 3) return;
|
|
28
|
+
|
|
29
|
+
const nullType = types.find(
|
|
30
|
+
(typeNode) => typeNode.type === "TSNullKeyword"
|
|
31
|
+
);
|
|
32
|
+
const undefinedType = types.find(
|
|
33
|
+
(typeNode) => typeNode.type === "TSUndefinedKeyword"
|
|
34
|
+
);
|
|
35
|
+
const otherType = types.find(
|
|
36
|
+
(typeNode) =>
|
|
37
|
+
typeNode.type !== "TSNullKeyword" &&
|
|
38
|
+
typeNode.type !== "TSUndefinedKeyword"
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
if (nullType && undefinedType && otherType) {
|
|
42
|
+
const typeText = sourceCode.getText(otherType);
|
|
43
|
+
context.report({
|
|
44
|
+
node,
|
|
45
|
+
messageId: "useNullable",
|
|
46
|
+
data: { type: typeText },
|
|
47
|
+
fix(fixer) {
|
|
48
|
+
// See `prefer-maybe`: the fix only uses the
|
|
49
|
+
// package's own `Nullable`, imported where it is
|
|
50
|
+
// not already.
|
|
51
|
+
const importFixes = imports.fixes(
|
|
52
|
+
node,
|
|
53
|
+
fixer,
|
|
54
|
+
"Nullable",
|
|
55
|
+
"type"
|
|
56
|
+
);
|
|
57
|
+
if (importFixes === null) return null;
|
|
58
|
+
|
|
59
|
+
return [
|
|
60
|
+
...importFixes,
|
|
61
|
+
fixer.replaceText(node, `Nullable<${typeText}>`)
|
|
62
|
+
];
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
55
68
|
},
|
|
56
|
-
|
|
69
|
+
defaultOptions: [],
|
|
70
|
+
meta: {
|
|
71
|
+
type: "suggestion",
|
|
72
|
+
docs: {
|
|
73
|
+
description: "Use Nullable<T> instead of T | null | undefined"
|
|
74
|
+
},
|
|
75
|
+
fixable: "code",
|
|
76
|
+
schema: [],
|
|
77
|
+
messages: {
|
|
78
|
+
useNullable:
|
|
79
|
+
"Use Nullable<{{type}}> instead of {{type}} | null | undefined"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
57
82
|
};
|
|
58
83
|
|
|
59
84
|
export { rule as nullableRule };
|
package/rules/optional_rule.ts
CHANGED
|
@@ -1,58 +1,84 @@
|
|
|
1
|
+
import { createImportPlanner } from "./typebuddy_import.js";
|
|
2
|
+
|
|
3
|
+
type RuleFixer = {
|
|
4
|
+
replaceText(node: unknown, text: string): unknown;
|
|
5
|
+
insertTextBeforeRange(range: [number, number], text: string): unknown;
|
|
6
|
+
};
|
|
7
|
+
|
|
1
8
|
const rule = {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
9
|
+
create(context: {
|
|
10
|
+
getSourceCode(): {
|
|
11
|
+
getText(node: unknown): string;
|
|
12
|
+
getScope(node: unknown): unknown;
|
|
13
|
+
};
|
|
14
|
+
report(descriptor: {
|
|
15
|
+
node: unknown;
|
|
16
|
+
messageId: string;
|
|
17
|
+
data: { type: string };
|
|
18
|
+
fix(fixer: RuleFixer): unknown;
|
|
19
|
+
}): void;
|
|
20
|
+
}) {
|
|
21
|
+
const sourceCode = context.getSourceCode();
|
|
22
|
+
const imports = createImportPlanner(sourceCode);
|
|
23
|
+
|
|
24
|
+
return {
|
|
25
|
+
TSUnionType(node: { types: Array<{ type: string }> }) {
|
|
26
|
+
if (node.types.length !== 2) return;
|
|
27
|
+
|
|
28
|
+
const undefinedType = node.types.find(
|
|
29
|
+
(typeNode) => typeNode.type === "TSUndefinedKeyword"
|
|
30
|
+
);
|
|
31
|
+
// `null | undefined` has no non-nullish member to name, and
|
|
32
|
+
// rewriting it to `Optional<null>` only restated it with more
|
|
33
|
+
// syntax.
|
|
34
|
+
const otherType = node.types.find(
|
|
35
|
+
(typeNode) =>
|
|
36
|
+
typeNode.type !== "TSUndefinedKeyword" &&
|
|
37
|
+
typeNode.type !== "TSNullKeyword"
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
if (undefinedType && otherType) {
|
|
41
|
+
const typeText = sourceCode.getText(otherType);
|
|
42
|
+
|
|
43
|
+
context.report({
|
|
44
|
+
node,
|
|
45
|
+
messageId: "preferOptional",
|
|
46
|
+
data: { type: typeText },
|
|
47
|
+
fix(fixer) {
|
|
48
|
+
// See `prefer-maybe`: the fix only uses the
|
|
49
|
+
// package's own `Optional`, imported where it is
|
|
50
|
+
// not already.
|
|
51
|
+
const importFixes = imports.fixes(
|
|
52
|
+
node,
|
|
53
|
+
fixer,
|
|
54
|
+
"Optional",
|
|
55
|
+
"type"
|
|
56
|
+
);
|
|
57
|
+
if (importFixes === null) return null;
|
|
58
|
+
|
|
59
|
+
return [
|
|
60
|
+
...importFixes,
|
|
61
|
+
fixer.replaceText(node, `Optional<${typeText}>`)
|
|
62
|
+
];
|
|
63
|
+
}
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
};
|
|
54
68
|
},
|
|
55
|
-
|
|
69
|
+
defaultOptions: [],
|
|
70
|
+
meta: {
|
|
71
|
+
type: "suggestion",
|
|
72
|
+
docs: {
|
|
73
|
+
description: "Enforce using Optional<T> instead of T | undefined"
|
|
74
|
+
},
|
|
75
|
+
fixable: "code",
|
|
76
|
+
schema: [],
|
|
77
|
+
messages: {
|
|
78
|
+
preferOptional:
|
|
79
|
+
"Use Optional<{{type}}> instead of {{type}} | undefined"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
56
82
|
};
|
|
57
83
|
|
|
58
84
|
export { rule as optionalRule };
|
package/rules/own_subtree.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
type AstNode = {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
type: string;
|
|
3
|
+
[key: string]: unknown;
|
|
4
4
|
};
|
|
5
5
|
|
|
6
6
|
const FUNCTION_TYPES = new Set([
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
"ArrowFunctionExpression",
|
|
8
|
+
"FunctionDeclaration",
|
|
9
|
+
"FunctionExpression"
|
|
10
10
|
]);
|
|
11
11
|
|
|
12
12
|
function isNode(value: unknown): value is AstNode {
|
|
13
|
-
|
|
13
|
+
return typeof value === "object" && value !== null && "type" in value;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
16
|
/**
|
|
@@ -31,40 +31,40 @@ function isNode(value: unknown): value is AstNode {
|
|
|
31
31
|
* @returns {void}
|
|
32
32
|
*/
|
|
33
33
|
function walkOwnSubtree(
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
root: unknown,
|
|
35
|
+
visit: (node: AstNode) => boolean | void
|
|
36
36
|
): void {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
const pending: AstNode[] = [root];
|
|
42
|
-
while (pending.length > 0) {
|
|
43
|
-
// `pop` on a non-empty array always yields a node; the length check above
|
|
44
|
-
// is the loop condition, but `noUncheckedIndexedAccess` cannot see that.
|
|
45
|
-
const current = pending.pop();
|
|
46
|
-
if (current === undefined) {
|
|
47
|
-
break;
|
|
37
|
+
if (!isNode(root)) {
|
|
38
|
+
return;
|
|
48
39
|
}
|
|
49
40
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
41
|
+
const pending: AstNode[] = [root];
|
|
42
|
+
while (pending.length > 0) {
|
|
43
|
+
// `pop` on a non-empty array always yields a node; the length check above
|
|
44
|
+
// is the loop condition, but `noUncheckedIndexedAccess` cannot see that.
|
|
45
|
+
const current = pending.pop();
|
|
46
|
+
if (current === undefined) {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (visit(current) === false) {
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
54
|
+
for (const [key, value] of Object.entries(current)) {
|
|
55
|
+
if (key === "parent") {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
58
|
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
59
|
+
const candidates = Array.isArray(value) ? value : [value];
|
|
60
|
+
for (const candidate of candidates) {
|
|
61
|
+
if (!isNode(candidate) || FUNCTION_TYPES.has(candidate.type)) {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
pending.push(candidate);
|
|
65
|
+
}
|
|
63
66
|
}
|
|
64
|
-
pending.push(candidate);
|
|
65
|
-
}
|
|
66
67
|
}
|
|
67
|
-
}
|
|
68
68
|
}
|
|
69
69
|
|
|
70
70
|
export { walkOwnSubtree };
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// Directory names whose contents are test code, matched as whole path
|
|
2
|
+
// segments: `src/__tests__/x.ts` and `tests/helper.ts` count, `src/latest/x.ts`
|
|
3
|
+
// and `src/testing/x.ts` do not.
|
|
4
|
+
const TEST_DIRECTORIES = new Set(["__mocks__", "__tests__", "test", "tests"]);
|
|
5
|
+
|
|
6
|
+
// `x.test.ts`, `x.spec.tsx`, `x.bench.ts`: the marker has to be its own dot
|
|
7
|
+
// segment, so `latest.ts` and `contest.ts` stay source files.
|
|
8
|
+
const TEST_FILE_NAME = /\.(?:bench|spec|test)\.[^.]+/;
|
|
9
|
+
|
|
10
|
+
// Test setup files by name: `vitest.setup.*`, `vitest-setup.*` and the
|
|
11
|
+
// Jest-style `setupTests.*`.
|
|
12
|
+
const SETUP_FILE_NAME = /^(?:setupTests|vitest-setup|vitest\.setup)\.[^.]+$/;
|
|
13
|
+
|
|
14
|
+
const ABSOLUTE_PATH = /^(?:\/|[A-Za-z]:\/)/;
|
|
15
|
+
|
|
16
|
+
function toSlashes(path: string): string {
|
|
17
|
+
return path.replaceAll("\\", "/");
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The directory segments of `filename` that count for the directory match: the
|
|
22
|
+
* ones below the project's working directory `cwd`. A relative filename is
|
|
23
|
+
* relative to the project already. An absolute one needs `cwd` to tell the
|
|
24
|
+
* project's own directories from the ones above it (`/tmp/test/app/src/x.ts` is
|
|
25
|
+
* not under a test directory); without `cwd`, or outside it, no directory
|
|
26
|
+
* counts, so the mistake is a test helper that gets linted, never a whole
|
|
27
|
+
* project that goes silent.
|
|
28
|
+
*/
|
|
29
|
+
function projectDirectories(path: string, cwd: string | undefined): string[] {
|
|
30
|
+
const segments = path.split("/");
|
|
31
|
+
segments.pop();
|
|
32
|
+
|
|
33
|
+
if (!ABSOLUTE_PATH.test(path)) {
|
|
34
|
+
return segments;
|
|
35
|
+
}
|
|
36
|
+
if (cwd === undefined) {
|
|
37
|
+
return [];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const root = toSlashes(cwd).replace(/\/+$/, "");
|
|
41
|
+
if (root === "" || !path.startsWith(`${root}/`)) {
|
|
42
|
+
return [];
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return path
|
|
46
|
+
.slice(root.length + 1)
|
|
47
|
+
.split("/")
|
|
48
|
+
.slice(0, -1);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Whether `filename` is test code: test, spec and bench files, files under a
|
|
53
|
+
* `__tests__`, `tests`, `test` or `__mocks__` directory of the project rooted
|
|
54
|
+
* at `cwd`, and test setup files. Both `/` and `\` separate path segments.
|
|
55
|
+
*/
|
|
56
|
+
function isTestFile(filename: string, cwd?: string): boolean {
|
|
57
|
+
const path = toSlashes(filename);
|
|
58
|
+
const baseName = path.slice(path.lastIndexOf("/") + 1);
|
|
59
|
+
|
|
60
|
+
return (
|
|
61
|
+
TEST_FILE_NAME.test(baseName) ||
|
|
62
|
+
SETUP_FILE_NAME.test(baseName) ||
|
|
63
|
+
projectDirectories(path, cwd).some((segment) =>
|
|
64
|
+
TEST_DIRECTORIES.has(segment)
|
|
65
|
+
)
|
|
66
|
+
);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// The rules type their context by what they use (`getSourceCode`, `report`),
|
|
70
|
+
// which leaves out what oxlint's JS plugin API puts on every context: the
|
|
71
|
+
// absolute `filename` and the `cwd` oxlint runs in.
|
|
72
|
+
function getFilename(context: object): string {
|
|
73
|
+
if ("filename" in context && typeof context.filename === "string") {
|
|
74
|
+
return context.filename;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return "";
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function getCwd(context: object): string | undefined {
|
|
81
|
+
if ("cwd" in context && typeof context.cwd === "string") {
|
|
82
|
+
return context.cwd;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Make a rule a no-op for test files: its `create` returns no visitors, so test
|
|
90
|
+
* helpers, fixtures and mocks are not asked to follow the typebuddy conventions
|
|
91
|
+
* (`MaybePromise` results, `Maybe`/`Nullable`/`Optional` types).
|
|
92
|
+
*/
|
|
93
|
+
function withoutTestFiles<
|
|
94
|
+
Context extends object,
|
|
95
|
+
Visitors extends object,
|
|
96
|
+
Rest
|
|
97
|
+
>(
|
|
98
|
+
rule: Rest & { create(context: Context): Visitors }
|
|
99
|
+
): Omit<Rest, "create"> & {
|
|
100
|
+
create(context: Context): Visitors | Record<string, never>;
|
|
101
|
+
} {
|
|
102
|
+
return {
|
|
103
|
+
...rule,
|
|
104
|
+
create(context: Context): Visitors | Record<string, never> {
|
|
105
|
+
if (isTestFile(getFilename(context), getCwd(context))) {
|
|
106
|
+
return {};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return rule.create(context);
|
|
110
|
+
}
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export { isTestFile, withoutTestFiles };
|