@sshkeda/check 4.4.0 → 4.5.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 +1 -1
- package/package.json +2 -2
- package/src/internal/ban-promise-combinators.ts +222 -0
- package/src/internal/ban-writes-to-native-prototypes.ts +8 -74
- package/src/internal/define-the-lint-plugin-and-configs.ts +2 -0
- package/src/internal/resolve-globals-through-constant-aliases.ts +186 -0
- package/src/lib/tasks/internal/declare-the-checker-queries-task.ts +2 -0
- package/src/lib/tasks/lib/enforce-file-types/index.ts +4 -1
- package/src/lib/tasks/lib/refuse-disable-directives/internal/find-disable-directives.ts +69 -20
- package/src/lib/tasks/lib/refuse-promise-chains/index.ts +1 -0
- package/src/lib/tasks/lib/refuse-promise-chains/internal/find-promise-chains.ts +141 -0
- package/src/lib/tasks/lib/refuse-promise-chains/internal/promise-chain-errors.ts +21 -0
- package/src/lib/tasks/lib/type-check-with-bun-check/index.ts +2 -3
- package/src/lib/tasks/lib/type-check-with-bun-check/internal/check-types-with-bun-check.ts +21 -5
- package/src/lib/tasks/shared/print-failures-and-keep-detail-in-the-trace.ts +1 -1
- package/src/lib/tasks/shared/watch-the-typescript-api-process.ts +47 -20
- package/src/shared/lint-rule-messages-and-patterns.ts +11 -4
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ Check sends your AI slop code back to the kitchen!
|
|
|
4
4
|
|
|
5
5
|
Check holds your TypeScript code to a strict standard built around Effect and Bun, so the code stays type-safe, observable and predictable.
|
|
6
6
|
|
|
7
|
-
It runs about 310 lint rules,
|
|
7
|
+
It runs about 310 lint rules, 68 of them its own, and about 80 checks on your project's layout, imports and config. It also runs oxfmt, Effect's diagnostics and `bun check`. Your agent keeps fixing what it reports until the slop is gone.
|
|
8
8
|
|
|
9
9
|
No comment or setting turns a rule off everywhere. A small set of rules was picked to allow an override, and you grant each one to a single file in `oxlint.config.ts`. See [How to Grant a Permission](docs/how-to-grant-a-permission.md).
|
|
10
10
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sshkeda/check",
|
|
3
|
-
"version": "4.
|
|
3
|
+
"version": "4.5.0",
|
|
4
4
|
"homepage": "https://github.com/sshkeda/check#readme",
|
|
5
5
|
"bugs": {
|
|
6
6
|
"url": "https://github.com/sshkeda/check/issues"
|
|
@@ -30,7 +30,7 @@
|
|
|
30
30
|
"@effect/platform-bun": "4.1.0",
|
|
31
31
|
"@effect/tsgo": "0.67.4",
|
|
32
32
|
"@oxlint/plugins": "1.88.0",
|
|
33
|
-
"@sshkeda/task": "
|
|
33
|
+
"@sshkeda/task": "3.0.4",
|
|
34
34
|
"oxc-parser": "0.153.0",
|
|
35
35
|
"oxc-resolver": "11.24.2",
|
|
36
36
|
"oxlint": "1.88.0",
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
GLOBAL_CONTAINERS,
|
|
6
|
+
globalValueOf,
|
|
7
|
+
staticMemberKey,
|
|
8
|
+
} from "./resolve-globals-through-constant-aliases.ts";
|
|
9
|
+
import {
|
|
10
|
+
constantValue,
|
|
11
|
+
propertyKeyName,
|
|
12
|
+
staticKeyName,
|
|
13
|
+
} from "./resolve-what-an-expression-imports.ts";
|
|
14
|
+
|
|
15
|
+
const PROMISE = "Promise";
|
|
16
|
+
|
|
17
|
+
const COMBINATORS: ReadonlySet<string> = new Set(["all", "allSettled", "race", "any"]);
|
|
18
|
+
|
|
19
|
+
const NO_OWN_STATICS: ReadonlySet<string> = new Set();
|
|
20
|
+
|
|
21
|
+
const promiseValue = (
|
|
22
|
+
kind: "constructor" | "container" | "subclass",
|
|
23
|
+
ownStatics: ReadonlySet<string> = NO_OWN_STATICS,
|
|
24
|
+
) => ({ kind, ownStatics });
|
|
25
|
+
|
|
26
|
+
const globalPromiseValue = (name: string) => {
|
|
27
|
+
if (name === PROMISE) {
|
|
28
|
+
return promiseValue("constructor");
|
|
29
|
+
}
|
|
30
|
+
return GLOBAL_CONTAINERS.has(name) ? promiseValue("container") : undefined;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const memberOfPromiseValue = (owner: ReturnType<typeof promiseValue> | undefined, key: string) =>
|
|
34
|
+
owner?.kind === "container" ? globalPromiseValue(key) : undefined;
|
|
35
|
+
|
|
36
|
+
const classMemberKeyName = (
|
|
37
|
+
member: ESTree.AccessorProperty | ESTree.MethodDefinition | ESTree.PropertyDefinition,
|
|
38
|
+
sourceCode: SourceCode,
|
|
39
|
+
) => {
|
|
40
|
+
const { computed, key } = member;
|
|
41
|
+
if (computed) {
|
|
42
|
+
return staticKeyName(constantValue(key, sourceCode));
|
|
43
|
+
}
|
|
44
|
+
return key.type === "Identifier" ? key.name : staticKeyName(key);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const ownStaticName = (element: ESTree.ClassElement, sourceCode: SourceCode) => {
|
|
48
|
+
const isClassMember =
|
|
49
|
+
element.type === "MethodDefinition" ||
|
|
50
|
+
element.type === "PropertyDefinition" ||
|
|
51
|
+
element.type === "AccessorProperty";
|
|
52
|
+
if (!isClassMember) {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
const isDeclaredOnly = element.type === "PropertyDefinition" && element.declare === true;
|
|
56
|
+
const isRuntimeStatic = element.static && !isDeclaredOnly;
|
|
57
|
+
return isRuntimeStatic ? classMemberKeyName(element, sourceCode) : undefined;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const ownStaticNames = (declaration: ESTree.Class, sourceCode: SourceCode) =>
|
|
61
|
+
declaration.body.body.flatMap((element) => {
|
|
62
|
+
const name = ownStaticName(element, sourceCode);
|
|
63
|
+
return name === undefined ? [] : [name];
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
const subclassOfPromiseValue = (
|
|
67
|
+
superClass: ReturnType<typeof promiseValue> | undefined,
|
|
68
|
+
declaration: ESTree.Class,
|
|
69
|
+
sourceCode: SourceCode,
|
|
70
|
+
) => {
|
|
71
|
+
const extendsPromise = superClass !== undefined && superClass.kind !== "container";
|
|
72
|
+
return extendsPromise
|
|
73
|
+
? promiseValue(
|
|
74
|
+
"subclass",
|
|
75
|
+
new Set([...superClass.ownStatics, ...ownStaticNames(declaration, sourceCode)]),
|
|
76
|
+
)
|
|
77
|
+
: undefined;
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
const promiseValueOf = (expression: ESTree.Node, sourceCode: SourceCode) =>
|
|
81
|
+
globalValueOf(expression, sourceCode, {
|
|
82
|
+
global: globalPromiseValue,
|
|
83
|
+
member: memberOfPromiseValue,
|
|
84
|
+
subclass: (superClass, declaration) =>
|
|
85
|
+
subclassOfPromiseValue(superClass, declaration, sourceCode),
|
|
86
|
+
ignoresTypeOnlyBindings: true,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const reachesNativeCombinator = (
|
|
90
|
+
value: ReturnType<typeof promiseValue> | undefined,
|
|
91
|
+
member: string,
|
|
92
|
+
) => {
|
|
93
|
+
const isConstructor = value?.kind === "constructor";
|
|
94
|
+
const isSubclassWithoutOwnMember = value?.kind === "subclass" && !value.ownStatics.has(member);
|
|
95
|
+
return COMBINATORS.has(member) && (isConstructor || isSubclassWithoutOwnMember);
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const isPromiseConstructor = (expression: ESTree.Node, sourceCode: SourceCode) =>
|
|
99
|
+
promiseValueOf(expression, sourceCode)?.kind === "constructor";
|
|
100
|
+
|
|
101
|
+
const classBodyOfStaticSuper = (node: ESTree.Node): ESTree.Node | undefined => {
|
|
102
|
+
const { parent } = node;
|
|
103
|
+
if (parent === null) {
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const isClassMemberValue =
|
|
107
|
+
(parent.type === "MethodDefinition" ||
|
|
108
|
+
parent.type === "PropertyDefinition" ||
|
|
109
|
+
parent.type === "AccessorProperty") &&
|
|
110
|
+
parent.value === node;
|
|
111
|
+
if (isClassMemberValue) {
|
|
112
|
+
return parent.static ? parent.parent : undefined;
|
|
113
|
+
}
|
|
114
|
+
const isOwnFunction = node.type === "FunctionExpression" || node.type === "FunctionDeclaration";
|
|
115
|
+
if (isOwnFunction) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
return parent.type === "StaticBlock" ? parent.parent : classBodyOfStaticSuper(parent);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
const staticSuperValueOf = (node: ESTree.Super, sourceCode: SourceCode) => {
|
|
122
|
+
const home = classBodyOfStaticSuper(node)?.parent;
|
|
123
|
+
const isClass = home?.type === "ClassDeclaration" || home?.type === "ClassExpression";
|
|
124
|
+
const superClass = isClass ? home.superClass : null;
|
|
125
|
+
return superClass === null ? undefined : promiseValueOf(superClass, sourceCode);
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const exportedBindings = (declarator: ESTree.VariableDeclarator): readonly ESTree.Node[] => {
|
|
129
|
+
const { id, init: initializer } = declarator;
|
|
130
|
+
if (id.type === "Identifier") {
|
|
131
|
+
return initializer === null ? [] : [initializer];
|
|
132
|
+
}
|
|
133
|
+
if (id.type !== "ObjectPattern") {
|
|
134
|
+
return [];
|
|
135
|
+
}
|
|
136
|
+
return id.properties.flatMap((property) => {
|
|
137
|
+
const isBoundByName = property.type === "Property" && property.value.type === "Identifier";
|
|
138
|
+
return isBoundByName ? [property.value] : [];
|
|
139
|
+
});
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
const exportedValues = (node: ESTree.ExportNamedDeclaration) => {
|
|
143
|
+
const isTypeOrReexport = node.source !== null || node.exportKind === "type";
|
|
144
|
+
if (isTypeOrReexport) {
|
|
145
|
+
return [];
|
|
146
|
+
}
|
|
147
|
+
const { declaration } = node;
|
|
148
|
+
if (declaration?.type === "VariableDeclaration") {
|
|
149
|
+
return declaration.declarations.flatMap((declarator) => exportedBindings(declarator));
|
|
150
|
+
}
|
|
151
|
+
return node.specifiers.flatMap((specifier) =>
|
|
152
|
+
specifier.exportKind === "type" ? [] : [specifier.local],
|
|
153
|
+
);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
export const noPromiseCombinators = defineRule({
|
|
157
|
+
meta: {
|
|
158
|
+
type: "problem",
|
|
159
|
+
messages: {
|
|
160
|
+
combinator: "Promise.{{member}} is used",
|
|
161
|
+
exported: "the Promise constructor is exported",
|
|
162
|
+
},
|
|
163
|
+
},
|
|
164
|
+
create(context) {
|
|
165
|
+
const { sourceCode } = context;
|
|
166
|
+
const reportDestructuredCombinators = (pattern: ESTree.Node, source: ESTree.Node | null) => {
|
|
167
|
+
const isObjectPatternWithSource = pattern.type === "ObjectPattern" && source !== null;
|
|
168
|
+
if (!isObjectPatternWithSource) {
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const value = promiseValueOf(source, sourceCode);
|
|
172
|
+
for (const property of pattern.properties) {
|
|
173
|
+
const member = propertyKeyName(property);
|
|
174
|
+
const isNativeCombinator = member !== undefined && reachesNativeCombinator(value, member);
|
|
175
|
+
if (isNativeCombinator) {
|
|
176
|
+
context.report({ node: property, messageId: "combinator", data: { member } });
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
};
|
|
180
|
+
return {
|
|
181
|
+
MemberExpression(node) {
|
|
182
|
+
const member = staticMemberKey(node, sourceCode);
|
|
183
|
+
const isCombinatorKey = member !== undefined && COMBINATORS.has(member);
|
|
184
|
+
if (!isCombinatorKey) {
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
const { object } = node;
|
|
188
|
+
const receiver =
|
|
189
|
+
object.type === "Super"
|
|
190
|
+
? staticSuperValueOf(object, sourceCode)
|
|
191
|
+
: promiseValueOf(object, sourceCode);
|
|
192
|
+
if (reachesNativeCombinator(receiver, member)) {
|
|
193
|
+
context.report({ node, messageId: "combinator", data: { member } });
|
|
194
|
+
}
|
|
195
|
+
},
|
|
196
|
+
VariableDeclarator(node) {
|
|
197
|
+
const { id, init: initializer } = node;
|
|
198
|
+
reportDestructuredCombinators(id, initializer);
|
|
199
|
+
},
|
|
200
|
+
AssignmentPattern(node) {
|
|
201
|
+
reportDestructuredCombinators(node.left, node.right);
|
|
202
|
+
},
|
|
203
|
+
AssignmentExpression(node) {
|
|
204
|
+
if (node.operator === "=") {
|
|
205
|
+
reportDestructuredCombinators(node.left, node.right);
|
|
206
|
+
}
|
|
207
|
+
},
|
|
208
|
+
ExportDefaultDeclaration(node) {
|
|
209
|
+
if (isPromiseConstructor(node.declaration, sourceCode)) {
|
|
210
|
+
context.report({ node, messageId: "exported" });
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
ExportNamedDeclaration(node) {
|
|
214
|
+
for (const value of exportedValues(node)) {
|
|
215
|
+
if (isPromiseConstructor(value, sourceCode)) {
|
|
216
|
+
context.report({ node: value, messageId: "exported" });
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
};
|
|
221
|
+
},
|
|
222
|
+
});
|
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
import { defineRule } from "@oxlint/plugins";
|
|
2
|
-
import type { ESTree, SourceCode
|
|
2
|
+
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
resolvedVariable,
|
|
11
|
-
staticKeyName,
|
|
12
|
-
unwrappedExpression,
|
|
13
|
-
} from "./resolve-what-an-expression-imports.ts";
|
|
5
|
+
GLOBAL_CONTAINERS,
|
|
6
|
+
globalValueOf,
|
|
7
|
+
staticMemberKey,
|
|
8
|
+
} from "./resolve-globals-through-constant-aliases.ts";
|
|
9
|
+
import { isAssignmentTarget, unwrappedExpression } from "./resolve-what-an-expression-imports.ts";
|
|
14
10
|
|
|
15
11
|
const OBJECT = "Object";
|
|
16
12
|
|
|
@@ -85,8 +81,6 @@ const PARENT_PROTOTYPES = new Map<string, string | undefined>([
|
|
|
85
81
|
...TYPED_ARRAYS.map((name) => [name, undefined] as const),
|
|
86
82
|
]);
|
|
87
83
|
|
|
88
|
-
const GLOBAL_CONTAINERS: ReadonlySet<string> = new Set(["global", "globalThis", "self", "window"]);
|
|
89
|
-
|
|
90
84
|
const OBJECT_WRITES: ReadonlySet<string> = new Set([
|
|
91
85
|
"assign",
|
|
92
86
|
"defineProperties",
|
|
@@ -139,68 +133,8 @@ const memberOfIntrinsic = (owner: ReturnType<typeof intrinsic> | undefined, key:
|
|
|
139
133
|
return parent === undefined ? undefined : intrinsic("prototype", parent);
|
|
140
134
|
};
|
|
141
135
|
|
|
142
|
-
const
|
|
143
|
-
|
|
144
|
-
const isUnresolvedComputedKey = name === undefined && node.computed;
|
|
145
|
-
return isUnresolvedComputedKey ? staticKeyName(constantValue(node.property, sourceCode)) : name;
|
|
146
|
-
};
|
|
147
|
-
|
|
148
|
-
const constantAlias = (node: ESTree.Node, sourceCode: SourceCode) => {
|
|
149
|
-
const variable = resolvedVariable(node, sourceCode);
|
|
150
|
-
const definitions = variable?.defs ?? [];
|
|
151
|
-
const [definition] = definitions;
|
|
152
|
-
const declarator = definitions.length === 1 ? definition?.node : undefined;
|
|
153
|
-
const isConstantDeclarator =
|
|
154
|
-
declarator?.type === "VariableDeclarator" &&
|
|
155
|
-
declarator.parent.type === "VariableDeclaration" &&
|
|
156
|
-
declarator.parent.kind === "const";
|
|
157
|
-
const value = isConstantDeclarator ? declarator.init : null;
|
|
158
|
-
const isConstantWithAValue =
|
|
159
|
-
variable !== undefined && definition !== undefined && isConstantDeclarator && value !== null;
|
|
160
|
-
if (!isConstantWithAValue) {
|
|
161
|
-
return;
|
|
162
|
-
}
|
|
163
|
-
if (declarator.id.type === "Identifier") {
|
|
164
|
-
return { variable, value, key: undefined };
|
|
165
|
-
}
|
|
166
|
-
const property = definition.name.parent;
|
|
167
|
-
const isDestructuredByKey =
|
|
168
|
-
property.type === "Property" &&
|
|
169
|
-
property.value === definition.name &&
|
|
170
|
-
property.parent === declarator.id;
|
|
171
|
-
const key = isDestructuredByKey ? propertyKeyName(property) : undefined;
|
|
172
|
-
return key === undefined ? undefined : { variable, value, key };
|
|
173
|
-
};
|
|
174
|
-
|
|
175
|
-
const intrinsicOf = (
|
|
176
|
-
expression: ESTree.Node,
|
|
177
|
-
sourceCode: SourceCode,
|
|
178
|
-
seen: ReadonlySet<Variable> = new Set(),
|
|
179
|
-
): ReturnType<typeof intrinsic> | undefined => {
|
|
180
|
-
const node = unwrappedExpression(expression);
|
|
181
|
-
if (node.type === "ChainExpression") {
|
|
182
|
-
return intrinsicOf(node.expression, sourceCode, seen);
|
|
183
|
-
}
|
|
184
|
-
if (node.type === "MemberExpression") {
|
|
185
|
-
const key = staticMemberKey(node, sourceCode);
|
|
186
|
-
return key === undefined
|
|
187
|
-
? undefined
|
|
188
|
-
: memberOfIntrinsic(intrinsicOf(node.object, sourceCode, seen), key);
|
|
189
|
-
}
|
|
190
|
-
if (node.type !== "Identifier") {
|
|
191
|
-
return;
|
|
192
|
-
}
|
|
193
|
-
if (isGlobalReference(node, sourceCode, node.name)) {
|
|
194
|
-
return globalIntrinsic(node.name);
|
|
195
|
-
}
|
|
196
|
-
const alias = constantAlias(node, sourceCode);
|
|
197
|
-
const isUnknownOrSeen = alias === undefined || seen.has(alias.variable);
|
|
198
|
-
if (isUnknownOrSeen) {
|
|
199
|
-
return;
|
|
200
|
-
}
|
|
201
|
-
const value = intrinsicOf(alias.value, sourceCode, new Set([...seen, alias.variable]));
|
|
202
|
-
return alias.key === undefined ? value : memberOfIntrinsic(value, alias.key);
|
|
203
|
-
};
|
|
136
|
+
const intrinsicOf = (expression: ESTree.Node, sourceCode: SourceCode) =>
|
|
137
|
+
globalValueOf(expression, sourceCode, { global: globalIntrinsic, member: memberOfIntrinsic });
|
|
204
138
|
|
|
205
139
|
const prototypeWrittenThrough = (target: ESTree.Node, sourceCode: SourceCode) => {
|
|
206
140
|
const node = unwrappedExpression(target);
|
|
@@ -43,6 +43,7 @@ import { noIndexLogic } from "./ban-logic-in-index-files.ts";
|
|
|
43
43
|
import { noNamespaceAliases } from "./ban-namespaces-used-without-a-member.ts";
|
|
44
44
|
import { nodeBuiltinRule } from "./ban-node-builtins-that-effect-replaces.ts";
|
|
45
45
|
import { noUncheckedParsing } from "./ban-parsing-outside-data-without-a-schema.ts";
|
|
46
|
+
import { noPromiseCombinators } from "./ban-promise-combinators.ts";
|
|
46
47
|
import { noReExports } from "./ban-re-exports.ts";
|
|
47
48
|
import {
|
|
48
49
|
CLOCK_PROPERTIES,
|
|
@@ -145,6 +146,7 @@ const NAMED_RULES = {
|
|
|
145
146
|
"no-glob-scan": noGlobScan,
|
|
146
147
|
"no-unknown-boundaries": noUnknownBoundaries,
|
|
147
148
|
"no-native-prototype-writes": noNativePrototypeWrites,
|
|
149
|
+
"no-promise-combinators": noPromiseCombinators,
|
|
148
150
|
"no-loose-literal-constants": noLooseLiteralConstants,
|
|
149
151
|
"no-type-declarations": noTypeDeclarations,
|
|
150
152
|
"no-declared-values": noDeclaredValues,
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import type { Definition, ESTree, Scope, SourceCode, Variable } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
constantValue,
|
|
5
|
+
memberName,
|
|
6
|
+
propertyKeyName,
|
|
7
|
+
staticKeyName,
|
|
8
|
+
unwrappedExpression,
|
|
9
|
+
} from "./resolve-what-an-expression-imports.ts";
|
|
10
|
+
|
|
11
|
+
export const GLOBAL_CONTAINERS: ReadonlySet<string> = new Set([
|
|
12
|
+
"global",
|
|
13
|
+
"globalThis",
|
|
14
|
+
"self",
|
|
15
|
+
"window",
|
|
16
|
+
]);
|
|
17
|
+
|
|
18
|
+
export const staticMemberKey = (node: ESTree.MemberExpression, sourceCode: SourceCode) => {
|
|
19
|
+
const name = memberName(node);
|
|
20
|
+
const isUnresolvedComputedKey = name === undefined && node.computed;
|
|
21
|
+
return isUnresolvedComputedKey ? staticKeyName(constantValue(node.property, sourceCode)) : name;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const TYPE_DECLARATIONS: ReadonlySet<string> = new Set([
|
|
25
|
+
"TSInterfaceDeclaration",
|
|
26
|
+
"TSTypeAliasDeclaration",
|
|
27
|
+
"TSTypeParameter",
|
|
28
|
+
]);
|
|
29
|
+
|
|
30
|
+
const isTypeOnlyDefinition = (definition: Definition) => {
|
|
31
|
+
const { node, parent } = definition;
|
|
32
|
+
const isTypeOnlySpecifier = node.type === "ImportSpecifier" && node.importKind === "type";
|
|
33
|
+
const isTypeOnlyImport = parent?.type === "ImportDeclaration" && parent.importKind === "type";
|
|
34
|
+
return TYPE_DECLARATIONS.has(node.type) || isTypeOnlySpecifier || isTypeOnlyImport;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
const bindingVisibleFrom = (
|
|
38
|
+
scope: Scope,
|
|
39
|
+
name: string,
|
|
40
|
+
ignoresTypeOnlyBindings: boolean,
|
|
41
|
+
): { readonly variable: Variable; readonly definitions: readonly Definition[] } | undefined => {
|
|
42
|
+
const variable = scope.set.get(name);
|
|
43
|
+
const allDefinitions = variable?.defs ?? [];
|
|
44
|
+
const definitions = ignoresTypeOnlyBindings
|
|
45
|
+
? allDefinitions.filter((definition) => !isTypeOnlyDefinition(definition))
|
|
46
|
+
: allDefinitions;
|
|
47
|
+
const isTypeOnlyBinding = allDefinitions.length > 0 && definitions.length === 0;
|
|
48
|
+
const isVisibleBinding = variable !== undefined && !isTypeOnlyBinding;
|
|
49
|
+
if (isVisibleBinding) {
|
|
50
|
+
return { variable, definitions };
|
|
51
|
+
}
|
|
52
|
+
return scope.upper === null
|
|
53
|
+
? undefined
|
|
54
|
+
: bindingVisibleFrom(scope.upper, name, ignoresTypeOnlyBindings);
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const bindingOf = (node: ESTree.Node, sourceCode: SourceCode, ignoresTypeOnlyBindings: boolean) =>
|
|
58
|
+
node.type === "Identifier"
|
|
59
|
+
? bindingVisibleFrom(sourceCode.getScope(node), node.name, ignoresTypeOnlyBindings)
|
|
60
|
+
: undefined;
|
|
61
|
+
|
|
62
|
+
const isGlobalBinding = (
|
|
63
|
+
node: ESTree.Node,
|
|
64
|
+
sourceCode: SourceCode,
|
|
65
|
+
ignoresTypeOnlyBindings: boolean,
|
|
66
|
+
) => {
|
|
67
|
+
const binding = bindingOf(node, sourceCode, ignoresTypeOnlyBindings);
|
|
68
|
+
return binding === undefined || binding.definitions.length === 0;
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
const onlyDefinitionOf = (
|
|
72
|
+
node: ESTree.Node,
|
|
73
|
+
sourceCode: SourceCode,
|
|
74
|
+
ignoresTypeOnlyBindings: boolean,
|
|
75
|
+
) => {
|
|
76
|
+
const binding = bindingOf(node, sourceCode, ignoresTypeOnlyBindings);
|
|
77
|
+
const variable = binding?.variable;
|
|
78
|
+
const definitions = binding?.definitions ?? [];
|
|
79
|
+
const [definition] = definitions;
|
|
80
|
+
const isOnlyDefinition =
|
|
81
|
+
variable !== undefined && definition !== undefined && definitions.length === 1;
|
|
82
|
+
return isOnlyDefinition ? { variable, definition } : undefined;
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const constantAlias = (
|
|
86
|
+
node: ESTree.Node,
|
|
87
|
+
sourceCode: SourceCode,
|
|
88
|
+
ignoresTypeOnlyBindings: boolean,
|
|
89
|
+
) => {
|
|
90
|
+
const defined = onlyDefinitionOf(node, sourceCode, ignoresTypeOnlyBindings);
|
|
91
|
+
const declarator = defined?.definition.node;
|
|
92
|
+
const isConstantDeclarator =
|
|
93
|
+
declarator?.type === "VariableDeclarator" &&
|
|
94
|
+
declarator.parent.type === "VariableDeclaration" &&
|
|
95
|
+
declarator.parent.kind === "const";
|
|
96
|
+
const value = isConstantDeclarator ? declarator.init : null;
|
|
97
|
+
const isConstantWithAValue = defined !== undefined && isConstantDeclarator && value !== null;
|
|
98
|
+
if (!isConstantWithAValue) {
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
const { variable, definition } = defined;
|
|
102
|
+
if (declarator.id.type === "Identifier") {
|
|
103
|
+
return { variable, value, key: undefined };
|
|
104
|
+
}
|
|
105
|
+
const property = definition.name.parent;
|
|
106
|
+
const isDestructuredByKey =
|
|
107
|
+
property.type === "Property" &&
|
|
108
|
+
property.value === definition.name &&
|
|
109
|
+
property.parent === declarator.id;
|
|
110
|
+
const key = isDestructuredByKey ? propertyKeyName(property) : undefined;
|
|
111
|
+
return key === undefined ? undefined : { variable, value, key };
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const declaredClass = (
|
|
115
|
+
node: ESTree.Node,
|
|
116
|
+
sourceCode: SourceCode,
|
|
117
|
+
ignoresTypeOnlyBindings: boolean,
|
|
118
|
+
) => {
|
|
119
|
+
const defined = onlyDefinitionOf(node, sourceCode, ignoresTypeOnlyBindings);
|
|
120
|
+
const declared = defined?.definition.node;
|
|
121
|
+
const isDeclaredClass =
|
|
122
|
+
(declared?.type === "ClassDeclaration" || declared?.type === "ClassExpression") &&
|
|
123
|
+
defined !== undefined;
|
|
124
|
+
return isDeclaredClass ? { variable: defined.variable, declared } : undefined;
|
|
125
|
+
};
|
|
126
|
+
|
|
127
|
+
export const globalValueOf = <Value>(
|
|
128
|
+
expression: ESTree.Node,
|
|
129
|
+
sourceCode: SourceCode,
|
|
130
|
+
resolver: {
|
|
131
|
+
readonly global: (name: string) => Value | undefined;
|
|
132
|
+
readonly member: (owner: Value | undefined, key: string) => Value | undefined;
|
|
133
|
+
readonly subclass?: (
|
|
134
|
+
superClass: Value | undefined,
|
|
135
|
+
declaration: ESTree.Class,
|
|
136
|
+
) => Value | undefined;
|
|
137
|
+
readonly ignoresTypeOnlyBindings?: boolean;
|
|
138
|
+
},
|
|
139
|
+
seen: ReadonlySet<Variable> = new Set(),
|
|
140
|
+
): Value | undefined => {
|
|
141
|
+
const node = unwrappedExpression(expression);
|
|
142
|
+
if (node.type === "ChainExpression") {
|
|
143
|
+
return globalValueOf(node.expression, sourceCode, resolver, seen);
|
|
144
|
+
}
|
|
145
|
+
if (node.type === "MemberExpression") {
|
|
146
|
+
const key = staticMemberKey(node, sourceCode);
|
|
147
|
+
return key === undefined
|
|
148
|
+
? undefined
|
|
149
|
+
: resolver.member(globalValueOf(node.object, sourceCode, resolver, seen), key);
|
|
150
|
+
}
|
|
151
|
+
const isClass = node.type === "ClassDeclaration" || node.type === "ClassExpression";
|
|
152
|
+
if (isClass) {
|
|
153
|
+
const { subclass } = resolver;
|
|
154
|
+
const { superClass } = node;
|
|
155
|
+
const isSubclassOfAValue = subclass !== undefined && superClass !== null;
|
|
156
|
+
return isSubclassOfAValue
|
|
157
|
+
? subclass(globalValueOf(superClass, sourceCode, resolver, seen), node)
|
|
158
|
+
: undefined;
|
|
159
|
+
}
|
|
160
|
+
if (node.type !== "Identifier") {
|
|
161
|
+
return;
|
|
162
|
+
}
|
|
163
|
+
const ignoresTypeOnlyBindings = resolver.ignoresTypeOnlyBindings === true;
|
|
164
|
+
if (isGlobalBinding(node, sourceCode, ignoresTypeOnlyBindings)) {
|
|
165
|
+
return resolver.global(node.name);
|
|
166
|
+
}
|
|
167
|
+
const alias = constantAlias(node, sourceCode, ignoresTypeOnlyBindings);
|
|
168
|
+
const isUnseenAlias = alias !== undefined && !seen.has(alias.variable);
|
|
169
|
+
if (isUnseenAlias) {
|
|
170
|
+
const value = globalValueOf(
|
|
171
|
+
alias.value,
|
|
172
|
+
sourceCode,
|
|
173
|
+
resolver,
|
|
174
|
+
new Set([...seen, alias.variable]),
|
|
175
|
+
);
|
|
176
|
+
return alias.key === undefined ? value : resolver.member(value, alias.key);
|
|
177
|
+
}
|
|
178
|
+
const declared =
|
|
179
|
+
resolver.subclass === undefined
|
|
180
|
+
? undefined
|
|
181
|
+
: declaredClass(node, sourceCode, ignoresTypeOnlyBindings);
|
|
182
|
+
const isUnseenClass = declared !== undefined && !seen.has(declared.variable);
|
|
183
|
+
return isUnseenClass
|
|
184
|
+
? globalValueOf(declared.declared, sourceCode, resolver, new Set([...seen, declared.variable]))
|
|
185
|
+
: undefined;
|
|
186
|
+
};
|
|
@@ -11,6 +11,7 @@ import { findIndexReExportViolations } from "../lib/judge-index-re-exports/index
|
|
|
11
11
|
import { findExportPlacementViolations } from "../lib/place-exports-in-the-narrowest-directory/index.ts";
|
|
12
12
|
import { findErrorMappingsToDone } from "../lib/refuse-error-mappings-that-end-streams/index.ts";
|
|
13
13
|
import { findUnobservedForkFailures } from "../lib/refuse-forks-whose-failures-nothing-observes/index.ts";
|
|
14
|
+
import { findPromiseChains } from "../lib/refuse-promise-chains/index.ts";
|
|
14
15
|
import { findServiceLayerViolations } from "../lib/require-separate-service-layers/index.ts";
|
|
15
16
|
import { inEachPackage } from "../shared/check-each-nested-package.ts";
|
|
16
17
|
import { lintConfigFile } from "../shared/declare-lint-config-task.ts";
|
|
@@ -36,6 +37,7 @@ export const checkerQueries = Task.make({
|
|
|
36
37
|
"layer-provides": findLayerProvidesOutsideProgramStarts,
|
|
37
38
|
"error-mappings": findErrorMappingsToDone,
|
|
38
39
|
"fork-failures": findUnobservedForkFailures,
|
|
40
|
+
"promise-chains": findPromiseChains,
|
|
39
41
|
"re-exports": findIndexReExportViolations,
|
|
40
42
|
}),
|
|
41
43
|
),
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import { Task } from "@sshkeda/task";
|
|
2
2
|
import { Effect } from "effect";
|
|
3
3
|
|
|
4
|
+
import { lintConfigFile } from "../../shared/declare-lint-config-task.ts";
|
|
4
5
|
import { ProjectQueryCachesLive } from "../../shared/provide-project-query-caches.ts";
|
|
5
6
|
import { fileTypeViolationsOfEachPackage } from "./internal/find-file-type-violations.ts";
|
|
6
7
|
|
|
7
8
|
export const fileTypes = Task.make({
|
|
8
9
|
name: "file-types",
|
|
9
|
-
run: fileTypeViolationsOfEachPackage.pipe(
|
|
10
|
+
run: Effect.andThen(lintConfigFile, fileTypeViolationsOfEachPackage).pipe(
|
|
11
|
+
Effect.provide(ProjectQueryCachesLive),
|
|
12
|
+
),
|
|
10
13
|
});
|
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import { Array as Arrays, Effect, FileSystem, Path, Result } from "effect";
|
|
2
2
|
import { parseSync } from "oxc-parser";
|
|
3
|
+
import type { Comment } from "oxc-parser";
|
|
3
4
|
|
|
4
5
|
import { FORMAT_CONFIG_FILE } from "../../../../../shared/config-file-names.ts";
|
|
5
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
isDirective,
|
|
8
|
+
isTypeScriptSuppression,
|
|
9
|
+
TYPESCRIPT_NO_CHECK,
|
|
10
|
+
} from "../../../../../shared/lint-rule-messages-and-patterns.ts";
|
|
11
|
+
import { isDeclarationFile } from "../../../shared/classify-files-by-role.ts";
|
|
6
12
|
import { lineNumbersIn, lineOfTheFirstLabel } from "../../../shared/count-the-line-of-an-offset.ts";
|
|
7
|
-
import {
|
|
13
|
+
import { scriptFiles } from "../../../shared/list-linted-files.ts";
|
|
14
|
+
import { ownedFilesAmong } from "../../../shared/list-owned-files.ts";
|
|
8
15
|
import { isFormatConfigTextRefusedIn } from "../../../shared/parse-config-files-that-import-from-check.ts";
|
|
9
16
|
import { quietViolations } from "../../../shared/print-failures-and-keep-detail-in-the-trace.ts";
|
|
10
17
|
import { SourceFileUnparsable } from "../../../shared/project-config-and-tool-errors.ts";
|
|
@@ -15,13 +22,34 @@ import {
|
|
|
15
22
|
SourceFileUnreadable,
|
|
16
23
|
} from "./disable-directive-errors.ts";
|
|
17
24
|
|
|
18
|
-
|
|
25
|
+
const LEADING_SLASHES = /^\/+/u;
|
|
26
|
+
|
|
27
|
+
const LINE_COMMENT = "Line";
|
|
28
|
+
|
|
29
|
+
const sourceTextOf = (file: string, directory: string) =>
|
|
19
30
|
Effect.gen(function* () {
|
|
20
31
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
21
32
|
const path = yield* Path.Path;
|
|
22
|
-
|
|
33
|
+
return yield* fileSystem
|
|
23
34
|
.readFileString(path.isAbsolute(file) ? file : path.join(directory, file))
|
|
24
35
|
.pipe(Effect.mapError((error) => new SourceFileUnreadable({ file, reason: error.message })));
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
const directivesAmong = (file: string, text: string, comments: readonly Comment[]) => {
|
|
39
|
+
const lineAt = lineNumbersIn(text);
|
|
40
|
+
return comments.map(
|
|
41
|
+
(comment) =>
|
|
42
|
+
new DisableDirective({
|
|
43
|
+
file,
|
|
44
|
+
line: lineAt(comment.start),
|
|
45
|
+
directive: comment.value.replace(LEADING_SLASHES, "").trim(),
|
|
46
|
+
}),
|
|
47
|
+
);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export const disableDirectivesIn = (file: string, directory = ".") =>
|
|
51
|
+
Effect.gen(function* () {
|
|
52
|
+
const text = yield* sourceTextOf(file, directory);
|
|
25
53
|
const parsed = parseSync(file, text);
|
|
26
54
|
if (parsed.errors.length > 0) {
|
|
27
55
|
return parsed.errors.map(
|
|
@@ -33,30 +61,51 @@ export const disableDirectivesIn = (file: string, directory = ".") =>
|
|
|
33
61
|
}),
|
|
34
62
|
);
|
|
35
63
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
.
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
64
|
+
return directivesAmong(
|
|
65
|
+
file,
|
|
66
|
+
text,
|
|
67
|
+
parsed.comments.filter((comment) => isDirective(comment.value)),
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const typeScriptDirectivesIn = (file: string, directory: string) =>
|
|
72
|
+
Effect.gen(function* () {
|
|
73
|
+
const text = yield* sourceTextOf(file, directory);
|
|
74
|
+
const parsed = parseSync(file, text);
|
|
75
|
+
if (parsed.errors.length > 0) {
|
|
76
|
+
return [];
|
|
77
|
+
}
|
|
78
|
+
const firstStatementStart = parsed.program.body[0]?.start ?? text.length;
|
|
79
|
+
return directivesAmong(
|
|
80
|
+
file,
|
|
81
|
+
text,
|
|
82
|
+
parsed.comments.filter((comment) => {
|
|
83
|
+
const isLeadingLineComment =
|
|
84
|
+
comment.type === LINE_COMMENT && comment.end <= firstStatementStart;
|
|
85
|
+
const isNoCheck = isLeadingLineComment && TYPESCRIPT_NO_CHECK.test(comment.value);
|
|
86
|
+
return isNoCheck || isTypeScriptSuppression(comment.value);
|
|
87
|
+
}),
|
|
88
|
+
);
|
|
47
89
|
});
|
|
48
90
|
|
|
49
91
|
export const findDisableDirectivesInLintedFilesOf = (directory: string) =>
|
|
50
92
|
Effect.gen(function* () {
|
|
51
93
|
const path = yield* Path.Path;
|
|
52
94
|
const isFormatConfigRefused = yield* isFormatConfigTextRefusedIn(directory);
|
|
53
|
-
const
|
|
54
|
-
|
|
95
|
+
const scripts = yield* scriptFiles(path.join(directory, TYPESCRIPT_PROJECT_FILE));
|
|
96
|
+
const owned = yield* ownedFilesAmong(directory, scripts);
|
|
97
|
+
const ownedFiles = new Set(owned);
|
|
98
|
+
const files = owned.filter((file) => !isFormatConfigRefused || file !== FORMAT_CONFIG_FILE);
|
|
99
|
+
const generatedSources = scripts.filter(
|
|
100
|
+
(file) => !ownedFiles.has(file) && !isDeclarationFile(file),
|
|
55
101
|
);
|
|
56
102
|
const scans = yield* Effect.forEach(
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
103
|
+
[
|
|
104
|
+
...files.map((file) => disableDirectivesIn(file, directory)),
|
|
105
|
+
...generatedSources.map((file) => typeScriptDirectivesIn(file, directory)),
|
|
106
|
+
],
|
|
107
|
+
(scan) =>
|
|
108
|
+
scan.pipe(
|
|
60
109
|
Effect.map(Result.succeed),
|
|
61
110
|
Effect.catchTag("SourceFileUnreadable", (unscanned) =>
|
|
62
111
|
Effect.succeed(Result.fail(unscanned)),
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { findPromiseChains } from "./internal/find-promise-chains.ts";
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import type { Node, SourceFile } from "typescript/unstable/ast";
|
|
3
|
+
import {
|
|
4
|
+
isElementAccessExpression,
|
|
5
|
+
isPropertyAccessExpression,
|
|
6
|
+
isStringLiteralLikeNode,
|
|
7
|
+
} from "typescript/unstable/ast/is";
|
|
8
|
+
import { isErrorType, SignatureKind, TypeFlags } from "typescript/unstable/async";
|
|
9
|
+
import type { Checker, Type } from "typescript/unstable/async";
|
|
10
|
+
|
|
11
|
+
import {
|
|
12
|
+
callsIn,
|
|
13
|
+
CONCURRENT_TYPESCRIPT_REQUESTS,
|
|
14
|
+
nodePosition,
|
|
15
|
+
typeMembers,
|
|
16
|
+
typeScriptRequest,
|
|
17
|
+
} from "../../../shared/query-the-typescript-project.ts";
|
|
18
|
+
import type { openTypeScriptProjects } from "../../../shared/query-the-typescript-project.ts";
|
|
19
|
+
import { withoutWrappers } from "../../../shared/read-piped-steps-through-wrappers.ts";
|
|
20
|
+
import { PromiseChained, PromiseChainsRefused } from "./promise-chain-errors.ts";
|
|
21
|
+
|
|
22
|
+
const THEN = "then";
|
|
23
|
+
const CHAINING_METHODS: ReadonlySet<string> = new Set([THEN, "catch", "finally"]);
|
|
24
|
+
|
|
25
|
+
const memberCalledBy = (callee: Node) => {
|
|
26
|
+
if (isPropertyAccessExpression(callee)) {
|
|
27
|
+
return { receiver: callee.expression, key: callee.name };
|
|
28
|
+
}
|
|
29
|
+
if (!isElementAccessExpression(callee)) {
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
const key = callee.argumentExpression;
|
|
33
|
+
return isStringLiteralLikeNode(key) ? { receiver: callee.expression, key } : undefined;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
const hasCallableThen = (checker: Checker, member: Type) =>
|
|
37
|
+
Effect.gen(function* () {
|
|
38
|
+
const thenProperty = yield* typeScriptRequest("read a then property", () =>
|
|
39
|
+
checker.getPropertyOfType(member, THEN),
|
|
40
|
+
);
|
|
41
|
+
if (thenProperty === undefined) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
const thenType = yield* typeScriptRequest("read the type of a then property", () =>
|
|
45
|
+
checker.getTypeOfSymbol(thenProperty),
|
|
46
|
+
);
|
|
47
|
+
const thenTypes = thenType === undefined ? [] : yield* typeMembers(thenType);
|
|
48
|
+
const signatures = yield* Effect.forEach(thenTypes, (thenMember) =>
|
|
49
|
+
typeScriptRequest("read the signatures of a then property", () =>
|
|
50
|
+
checker.getSignaturesOfType(thenMember, SignatureKind.Call),
|
|
51
|
+
),
|
|
52
|
+
);
|
|
53
|
+
return signatures.some((memberSignatures) => memberSignatures.length > 0);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const isThenable = (checker: Checker, receiver: Type) =>
|
|
57
|
+
Effect.gen(function* () {
|
|
58
|
+
const isUnknown = (receiver.flags & TypeFlags.AnyOrUnknown) !== 0 || isErrorType(receiver);
|
|
59
|
+
if (isUnknown) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
const apparent = yield* typeScriptRequest("read the apparent type of a receiver", () =>
|
|
63
|
+
checker.getApparentType(receiver),
|
|
64
|
+
);
|
|
65
|
+
const members = apparent === undefined ? [] : yield* typeMembers(apparent);
|
|
66
|
+
const thenables = yield* Effect.forEach(members, (member) => hasCallableThen(checker, member));
|
|
67
|
+
return thenables.some(Boolean);
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const chainedCallsIn = (sourceFile: SourceFile) =>
|
|
71
|
+
callsIn(sourceFile).flatMap((call) => {
|
|
72
|
+
const member = memberCalledBy(withoutWrappers(call.expression));
|
|
73
|
+
const isChaining = member !== undefined && CHAINING_METHODS.has(member.key.text);
|
|
74
|
+
return isChaining ? [member] : [];
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
const projectViolations = (
|
|
78
|
+
checker: Checker,
|
|
79
|
+
sourceFiles: readonly { readonly sourceFile: SourceFile; readonly displayName: string }[],
|
|
80
|
+
) =>
|
|
81
|
+
Effect.gen(function* () {
|
|
82
|
+
const candidates = sourceFiles.flatMap(({ sourceFile, displayName }) => {
|
|
83
|
+
const chained = chainedCallsIn(sourceFile);
|
|
84
|
+
return chained.length === 0 ? [] : [{ sourceFile, displayName, chained }];
|
|
85
|
+
});
|
|
86
|
+
const files = yield* Effect.forEach(
|
|
87
|
+
candidates,
|
|
88
|
+
({ sourceFile, displayName, chained }) =>
|
|
89
|
+
typeScriptRequest("read the types of chained receivers", () =>
|
|
90
|
+
checker.getTypeAtLocation(chained.map(({ receiver }) => receiver)),
|
|
91
|
+
).pipe(Effect.map((types) => ({ sourceFile, displayName, chained, types }))),
|
|
92
|
+
{ concurrency: CONCURRENT_TYPESCRIPT_REQUESTS },
|
|
93
|
+
);
|
|
94
|
+
const distinctTypes = [
|
|
95
|
+
...new Map(
|
|
96
|
+
files.flatMap(({ types }) =>
|
|
97
|
+
types.flatMap((type) => (type === undefined ? [] : [[type.id, type] as const])),
|
|
98
|
+
),
|
|
99
|
+
).values(),
|
|
100
|
+
];
|
|
101
|
+
const thenables = yield* Effect.forEach(distinctTypes, (type) => isThenable(checker, type), {
|
|
102
|
+
concurrency: CONCURRENT_TYPESCRIPT_REQUESTS,
|
|
103
|
+
});
|
|
104
|
+
const thenableTypeIds = new Set(
|
|
105
|
+
distinctTypes.flatMap((type, index) => (thenables[index] === true ? [type.id] : [])),
|
|
106
|
+
);
|
|
107
|
+
return files.flatMap(({ sourceFile, displayName, chained, types }) =>
|
|
108
|
+
chained
|
|
109
|
+
.filter((_member, index) => {
|
|
110
|
+
const type = types[index];
|
|
111
|
+
return type !== undefined && thenableTypeIds.has(type.id);
|
|
112
|
+
})
|
|
113
|
+
.toSorted((left, right) => left.key.pos - right.key.pos)
|
|
114
|
+
.map(({ key }) => {
|
|
115
|
+
const { line, column } = nodePosition(sourceFile, key);
|
|
116
|
+
return new PromiseChained({
|
|
117
|
+
location: `${displayName}:${line}:${column}`,
|
|
118
|
+
method: key.text,
|
|
119
|
+
});
|
|
120
|
+
}),
|
|
121
|
+
);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
export const findPromiseChains = <OpeningError, OpeningRequirement>(
|
|
125
|
+
_projectFile: string,
|
|
126
|
+
openedProjects: Effect.Effect<
|
|
127
|
+
Effect.Success<ReturnType<typeof openTypeScriptProjects>>,
|
|
128
|
+
OpeningError,
|
|
129
|
+
OpeningRequirement
|
|
130
|
+
>,
|
|
131
|
+
) =>
|
|
132
|
+
Effect.gen(function* () {
|
|
133
|
+
const violationsOfEachProject = yield* Effect.forEach(
|
|
134
|
+
yield* openedProjects,
|
|
135
|
+
({ checker, sourceFiles }) => projectViolations(checker, sourceFiles),
|
|
136
|
+
);
|
|
137
|
+
const violations = violationsOfEachProject.flat();
|
|
138
|
+
if (violations.length > 0) {
|
|
139
|
+
return yield* new PromiseChainsRefused({ violations });
|
|
140
|
+
}
|
|
141
|
+
});
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Schema } from "effect";
|
|
2
|
+
|
|
3
|
+
import { listedViolations } from "../../../../../shared/format-reported-failures.ts";
|
|
4
|
+
|
|
5
|
+
export class PromiseChained extends Schema.TaggedError<PromiseChained>()("PromiseChained", {
|
|
6
|
+
location: Schema.String,
|
|
7
|
+
method: Schema.String,
|
|
8
|
+
}) {
|
|
9
|
+
public override get message() {
|
|
10
|
+
return `${this.method} is called on a Promise`;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export class PromiseChainsRefused extends Schema.TaggedError<PromiseChainsRefused>()(
|
|
15
|
+
"PromiseChainsRefused",
|
|
16
|
+
{ violations: Schema.Array(PromiseChained) },
|
|
17
|
+
) {
|
|
18
|
+
public override get message() {
|
|
19
|
+
return listedViolations(this.violations);
|
|
20
|
+
}
|
|
21
|
+
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
1
|
import { Task } from "@sshkeda/task";
|
|
2
2
|
import { Effect } from "effect";
|
|
3
3
|
|
|
4
|
-
import { inEachPackage } from "../../shared/check-each-nested-package.ts";
|
|
5
4
|
import { typeScriptConfig } from "../../shared/declare-typescript-config-task.ts";
|
|
6
|
-
import {
|
|
5
|
+
import { typesCheckedInEachPackage } from "./internal/check-types-with-bun-check.ts";
|
|
7
6
|
|
|
8
7
|
export const typeCheck = Task.make({
|
|
9
8
|
name: "type-check",
|
|
10
|
-
run: Effect.andThen(typeScriptConfig,
|
|
9
|
+
run: Effect.andThen(typeScriptConfig, typesCheckedInEachPackage),
|
|
11
10
|
});
|
|
@@ -1,17 +1,23 @@
|
|
|
1
|
-
import { Effect, FileSystem, Path } from "effect";
|
|
1
|
+
import { Effect, FileSystem, Path, Ref } from "effect";
|
|
2
2
|
|
|
3
|
+
import { inEachPackage } from "../../../shared/check-each-nested-package.ts";
|
|
3
4
|
import { PACKAGE_MANIFEST_FILE } from "../../../shared/find-named-package-manifests.ts";
|
|
4
5
|
import { isFormatConfigTextRefusedIn } from "../../../shared/parse-config-files-that-import-from-check.ts";
|
|
5
6
|
import { quietToolFailure } from "../../../shared/print-failures-and-keep-detail-in-the-trace.ts";
|
|
6
7
|
import { TYPESCRIPT_PROJECT_FILE } from "../../../shared/query-the-typescript-project.ts";
|
|
7
8
|
import { absolutePathOf } from "../../../shared/resolve-absolute-paths.ts";
|
|
8
9
|
import { runTool } from "../../../shared/run-tools-and-decode-their-reports.ts";
|
|
10
|
+
import { formatting } from "../../format-with-bun-fmt/index.ts";
|
|
9
11
|
import { typeCheckReport } from "./read-bun-check-output.ts";
|
|
10
12
|
|
|
11
13
|
const BUN = "bun";
|
|
12
14
|
const EMPTY_PACKAGE_MANIFEST = "{}\n";
|
|
13
15
|
|
|
14
|
-
const typeCheckRun = (
|
|
16
|
+
const typeCheckRun = (
|
|
17
|
+
directory: string,
|
|
18
|
+
isFormatConfigRefused: boolean,
|
|
19
|
+
leaveToTheFormatCheck: Effect.Effect<void>,
|
|
20
|
+
) =>
|
|
15
21
|
Effect.gen(function* () {
|
|
16
22
|
const fileSystem = yield* FileSystem.FileSystem;
|
|
17
23
|
const path = yield* Path.Path;
|
|
@@ -37,7 +43,7 @@ const typeCheckRun = (directory: string, isFormatConfigRefused: boolean) =>
|
|
|
37
43
|
Effect.catchTag("ToolExited", (exited) =>
|
|
38
44
|
typeCheckReport(exited.standardOutput, isFormatConfigRefused)
|
|
39
45
|
.reportsOnlyWhatTheFormatCheckReports
|
|
40
|
-
?
|
|
46
|
+
? leaveToTheFormatCheck
|
|
41
47
|
: Effect.fail(exited),
|
|
42
48
|
),
|
|
43
49
|
);
|
|
@@ -48,7 +54,17 @@ const typeCheckRun = (directory: string, isFormatConfigRefused: boolean) =>
|
|
|
48
54
|
),
|
|
49
55
|
);
|
|
50
56
|
|
|
51
|
-
|
|
57
|
+
const checkTypesWithBunCheckIn = (directory: string, leaveToTheFormatCheck: Effect.Effect<void>) =>
|
|
52
58
|
Effect.flatMap(isFormatConfigTextRefusedIn(directory), (isFormatConfigRefused) =>
|
|
53
|
-
typeCheckRun(directory, isFormatConfigRefused),
|
|
59
|
+
typeCheckRun(directory, isFormatConfigRefused, leaveToTheFormatCheck),
|
|
54
60
|
);
|
|
61
|
+
|
|
62
|
+
export const typesCheckedInEachPackage = Effect.gen(function* () {
|
|
63
|
+
const isLeftToTheFormatCheck = yield* Ref.make(false);
|
|
64
|
+
yield* inEachPackage("type-check", (packageDirectory) =>
|
|
65
|
+
checkTypesWithBunCheckIn(packageDirectory, Ref.set(isLeftToTheFormatCheck, true)),
|
|
66
|
+
);
|
|
67
|
+
if (yield* Ref.get(isLeftToTheFormatCheck)) {
|
|
68
|
+
yield* formatting;
|
|
69
|
+
}
|
|
70
|
+
});
|
|
@@ -78,7 +78,7 @@ export const quietToolFailure =
|
|
|
78
78
|
failQuietly(
|
|
79
79
|
tool,
|
|
80
80
|
exited,
|
|
81
|
-
Arrays.
|
|
81
|
+
Arrays.prepend(report.violations, exitError(exited, report.notes)),
|
|
82
82
|
),
|
|
83
83
|
),
|
|
84
84
|
Effect.catchTag("ToolReportUndecodable", (unreadableReport) =>
|
|
@@ -44,21 +44,36 @@ const TYPESCRIPT_API_CLIENT = Schema.declare(
|
|
|
44
44
|
CLIENT_MEMBERS.every((member) => Predicate.hasProperty(value, member)),
|
|
45
45
|
);
|
|
46
46
|
|
|
47
|
-
const PROCESS_MEMBERS = ["once", "on", "kill", "stdout"] as const;
|
|
47
|
+
const PROCESS_MEMBERS = ["once", "on", "kill", "stdout", "exitCode", "signalCode"] as const;
|
|
48
|
+
|
|
49
|
+
const OUTPUT_MEMBERS = ["once", "destroyed"] as const;
|
|
48
50
|
|
|
49
51
|
const isWatchableProcess = Schema.is(
|
|
50
52
|
Schema.declare(
|
|
51
53
|
(
|
|
52
54
|
value,
|
|
53
55
|
): value is {
|
|
54
|
-
readonly stdout: {
|
|
56
|
+
readonly stdout: {
|
|
57
|
+
readonly destroyed: boolean;
|
|
58
|
+
once(event: "close", listener: () => void): void;
|
|
59
|
+
} | null;
|
|
60
|
+
readonly exitCode: number | null;
|
|
61
|
+
readonly signalCode: NodeJS.Signals | null;
|
|
55
62
|
once(
|
|
56
63
|
event: "exit",
|
|
57
64
|
listener: (code: number | null, signal: NodeJS.Signals | null) => void,
|
|
58
65
|
): void;
|
|
59
66
|
on(event: "error", listener: (error: Error) => void): void;
|
|
60
67
|
kill(signal: NodeJS.Signals): boolean;
|
|
61
|
-
} =>
|
|
68
|
+
} => {
|
|
69
|
+
const hasProcessMembers = PROCESS_MEMBERS.every((member) =>
|
|
70
|
+
Predicate.hasProperty(value, member),
|
|
71
|
+
);
|
|
72
|
+
const output = Predicate.hasProperty(value, "stdout") ? value.stdout : undefined;
|
|
73
|
+
const hasOutputMembers =
|
|
74
|
+
output === null || OUTPUT_MEMBERS.every((member) => Predicate.hasProperty(output, member));
|
|
75
|
+
return hasProcessMembers && hasOutputMembers;
|
|
76
|
+
},
|
|
62
77
|
),
|
|
63
78
|
);
|
|
64
79
|
|
|
@@ -85,23 +100,37 @@ const watchedProcess = (
|
|
|
85
100
|
) => {
|
|
86
101
|
const child: unknown = client[CLIENT_PROCESS_FIELD];
|
|
87
102
|
if (!isWatchableProcess(child)) {
|
|
88
|
-
return
|
|
103
|
+
return Option.none();
|
|
89
104
|
}
|
|
90
105
|
const output = { closed: false };
|
|
91
|
-
|
|
106
|
+
const onExit = (code: number | null, signal: NodeJS.Signals | null) => {
|
|
92
107
|
stopWith(stopped, child, exitReason(code, signal, output.closed));
|
|
93
108
|
Deferred.doneUnsafe(exited, Effect.void);
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
stopWith(stopped, child, `the TypeScript API process failed: ${error.message}`);
|
|
97
|
-
});
|
|
98
|
-
child.stdout?.once("close", () => {
|
|
109
|
+
};
|
|
110
|
+
const onOutputClosed = () => {
|
|
99
111
|
output.closed = true;
|
|
100
112
|
child.kill(TERMINATING_SIGNAL);
|
|
113
|
+
};
|
|
114
|
+
child.once("exit", onExit);
|
|
115
|
+
child.on("error", (error) => {
|
|
116
|
+
stopWith(stopped, child, `the TypeScript API process failed: ${error.message}`);
|
|
101
117
|
});
|
|
102
|
-
|
|
118
|
+
child.stdout?.once("close", onOutputClosed);
|
|
119
|
+
const hasExited = child.exitCode !== null || child.signalCode !== null;
|
|
120
|
+
if (hasExited) {
|
|
121
|
+
onExit(child.exitCode, child.signalCode);
|
|
122
|
+
}
|
|
123
|
+
if (child.stdout?.destroyed === true) {
|
|
124
|
+
onOutputClosed();
|
|
125
|
+
}
|
|
126
|
+
return Option.some(child);
|
|
103
127
|
};
|
|
104
128
|
|
|
129
|
+
const startupFailed = (reason: string) => new TypeScriptApiFailed({ operation: STARTING, reason });
|
|
130
|
+
|
|
131
|
+
const startupStep = <Value>(step: () => Promise<Value>) =>
|
|
132
|
+
Effect.tryPromise({ try: step, catch: (cause) => startupFailed(String(cause)) });
|
|
133
|
+
|
|
105
134
|
const answered = <Answer>(
|
|
106
135
|
stopped: Deferred.Deferred<never, TypeScriptApiProcessStopped>,
|
|
107
136
|
child: Parameters<typeof stopWith>[1],
|
|
@@ -137,19 +166,17 @@ const startedSession = (sourceTextOverrides: ReadonlyMap<string, string>) =>
|
|
|
137
166
|
);
|
|
138
167
|
const client = yield* Schema.decodeUnknownEffect(TYPESCRIPT_API_CLIENT)(
|
|
139
168
|
api[API_CLIENT_FIELD],
|
|
140
|
-
).pipe(
|
|
141
|
-
Effect.mapError(
|
|
142
|
-
() => new TypeScriptApiFailed({ operation: STARTING, reason: CLIENT_CHANGED }),
|
|
143
|
-
),
|
|
144
|
-
);
|
|
169
|
+
).pipe(Effect.mapError(() => startupFailed(CLIENT_CHANGED)));
|
|
145
170
|
const stopped = yield* Deferred.make<never, TypeScriptApiProcessStopped>();
|
|
146
171
|
const exited = yield* Deferred.make<void>();
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
172
|
+
yield* startupStep(() => client.connect());
|
|
173
|
+
const watched = yield* Effect.try({
|
|
174
|
+
try: () => watchedProcess(client, stopped, exited),
|
|
175
|
+
catch: (cause) => startupFailed(String(cause)),
|
|
150
176
|
});
|
|
151
177
|
if (Option.isNone(watched)) {
|
|
152
|
-
|
|
178
|
+
yield* startupStep(() => client.close());
|
|
179
|
+
return yield* startupFailed(CLIENT_CHANGED);
|
|
153
180
|
}
|
|
154
181
|
const child = watched.value;
|
|
155
182
|
const runPromise = Effect.runPromiseWith(yield* Effect.context());
|
|
@@ -1,12 +1,19 @@
|
|
|
1
1
|
import { isBuiltin } from "node:module";
|
|
2
2
|
|
|
3
|
-
const DIRECTIVE =
|
|
4
|
-
/^\s*(?:(?:oxlint|eslint)-(?:disable|enable)|@ts-(?:ignore|expect-error|nocheck)|@effect-diagnostics)/u;
|
|
3
|
+
const DIRECTIVE = /^\s*(?:(?:oxlint|eslint)-(?:disable|enable)|@ts-nocheck|@effect-diagnostics)/u;
|
|
5
4
|
|
|
6
|
-
const
|
|
5
|
+
const TYPESCRIPT_SUPPRESSION_ON_THE_LAST_LINE =
|
|
6
|
+
/(?:^|\n)[\s*/]*@ts-(?:ignore|expect-error)[^\n]*$/u;
|
|
7
|
+
|
|
8
|
+
export const TYPESCRIPT_NO_CHECK = /^\/?\s*@ts-nocheck(?![a-z-])/iu;
|
|
9
|
+
|
|
10
|
+
export const isTypeScriptSuppression = (commentText: string) =>
|
|
11
|
+
TYPESCRIPT_SUPPRESSION_ON_THE_LAST_LINE.test(commentText);
|
|
7
12
|
|
|
8
13
|
export const isDirective = (commentText: string) =>
|
|
9
|
-
DIRECTIVE.test(commentText) ||
|
|
14
|
+
DIRECTIVE.test(commentText) ||
|
|
15
|
+
isTypeScriptSuppression(commentText) ||
|
|
16
|
+
TYPESCRIPT_NO_CHECK.test(commentText);
|
|
10
17
|
|
|
11
18
|
export const EFFECT_OPERATION_RULES = {
|
|
12
19
|
"no-dropped-failures": {
|