@sshkeda/check 4.3.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 +3 -1
- package/docs/how-to-grant-a-permission.md +4 -2
- package/docs/how-to-make-check-pass.md +36 -21
- package/package.json +2 -2
- package/src/internal/ban-promise-combinators.ts +222 -0
- package/src/internal/ban-writes-to-native-prototypes.ts +207 -0
- package/src/internal/define-the-lint-plugin-and-configs.ts +4 -1
- 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-layout/internal/file-layout-errors.ts +10 -0
- package/src/lib/tasks/lib/enforce-file-layout/internal/find-export-path-mismatches.ts +51 -18
- package/src/lib/tasks/lib/enforce-file-layout/internal/find-file-layout-violations.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
|
|
|
@@ -83,6 +83,8 @@ Check passes.
|
|
|
83
83
|
|
|
84
84
|
## Why these rules
|
|
85
85
|
|
|
86
|
+
The rules keep code type-safe, observable and predictable. [How to Make Check Pass](docs/how-to-make-check-pass.md) gives each goal and how to decide if a new rule is worth it.
|
|
87
|
+
|
|
86
88
|
- **No comments.** When code is hard to follow, the fix is better names and simpler code, which stay true as the code changes. Comments drift out of date, and agents fill them with noise. When you need more explanation, ask your agent to explain the code.
|
|
87
89
|
- **No `type` or `interface` declarations.** Types are inferred from the code that produces each value, so every type has one source of truth and nothing to keep in sync.
|
|
88
90
|
- **No `let`, and no `while` or counting `for` loops.** Values don't change after they're made, which leaves an agent fewer ways to get state wrong.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# How to Grant a Permission
|
|
2
2
|
|
|
3
|
-
Grant a permission to one file or directory in `oxlint.config.ts`. The key is the path from the
|
|
3
|
+
Grant a permission to one file or directory in `oxlint.config.ts`. The key is the path from the directory that holds `oxlint.config.ts`, with no glob. Each name in `allow` lifts one ban for that path only.
|
|
4
4
|
|
|
5
5
|
```ts
|
|
6
6
|
export default lintConfig({
|
|
@@ -9,6 +9,8 @@ export default lintConfig({
|
|
|
9
9
|
});
|
|
10
10
|
```
|
|
11
11
|
|
|
12
|
+
Move the code that needs a grant into its own small file and grant that file. A grant covers the whole file, so Check stops reporting that ban for everything else in it.
|
|
13
|
+
|
|
12
14
|
- `readsConfig`: Allows Effect `Config`, `process.env` and `process.argv`.
|
|
13
15
|
- `spawnsCommands`: Allows Effect `ChildProcess`, `Bun.spawn` and `spawn` from `node:child_process`.
|
|
14
16
|
- `locksFiles`: Allows `open` from `node:fs/promises` and `constants` from `node:fs`.
|
|
@@ -26,4 +28,4 @@ export default lintConfig({
|
|
|
26
28
|
- `handlesEveryFailure`: Allows catching every error, through `Effect.catch`, `Effect.exit`, a forked fiber or `Effect.runPromise`.
|
|
27
29
|
- `fallsBackToFixedValues`: Allows answering every failure, or a whole library error such as `PlatformError`, with a fixed value that never reads the failure.
|
|
28
30
|
- `replacesTheConsole`: Allows `Console` from `node:console`, and reading, calling and assigning these members of the global `console` by name: `log`, `warn`, `error`, `info`, `debug`, `dir`, `dirxml`, `table`, `trace`, `assert`, `clear`, `count`, `countReset`, `group`, `groupCollapsed`, `groupEnd`, `time`, `timeEnd` and `timeLog`.
|
|
29
|
-
- `isGeneratedByATool`: Check skips this path, so it can't have any other permission.
|
|
31
|
+
- `isGeneratedByATool`: Check skips this path, so it can't have any other permission. Grant it only to a file a tool generates.
|
|
@@ -1,33 +1,29 @@
|
|
|
1
1
|
# How to Make Check Pass
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Each rule removes one way to write code that is wrong, hidden or hard to change, so one correct way stays.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
Fix the cause of each failure and keep the code simple. Use a grant only for an unavoidable exception. [How to Grant a Permission](how-to-grant-a-permission.md) lists each grant and what it allows.
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
Each rule serves one of three goals.
|
|
8
8
|
|
|
9
|
-
|
|
10
|
-
- Move the code that needs a grant into its own small file and grant that file. A grant covers the whole file, so Check stops reporting that ban for everything else in it.
|
|
11
|
-
- Design for the current requirement as if you were starting fresh. Delete code the new design doesn't need.
|
|
12
|
-
- Handle a forked effect's failure before the fork, or keep the fiber and join it.
|
|
13
|
-
- Wrap a Promise in `Effect.tryPromise` and `yield*` it instead of using `await`.
|
|
14
|
-
- Read a fact from where a tool states it, such as an error's fields, a command's exit code or its JSON output. Text written for people changes between releases, and code that searches it breaks quietly when it does.
|
|
15
|
-
- Return a fixed value only for the one failure you expect, such as a file that isn't there. Let every other failure fail.
|
|
16
|
-
- Write a job that moves through stages as an explicit state machine, with one tagged case per stage in one union, so the job is always in exactly one stage.
|
|
17
|
-
- Give each status its own struct holding the fields that status always has, and join them in a union.
|
|
18
|
-
- Grant `isGeneratedByATool` only to a file a tool generates.
|
|
9
|
+
## Type-safe
|
|
19
10
|
|
|
20
|
-
|
|
11
|
+
TypeScript knows the true type of every value. Each type has one source, and data from outside the program is checked where it enters.
|
|
21
12
|
|
|
22
|
-
- Use what Effect, Bun or a dependency already provides.
|
|
23
|
-
- Write each fact or decision once and derive the rest from it.
|
|
24
|
-
- Give each value a name that says what it holds.
|
|
25
13
|
- Let TypeScript infer types, or derive them from values and schemas you already use. Don't create a schema only to name a type.
|
|
26
14
|
- Validate unchecked data with a Schema before treating it as a typed value. A type assertion doesn't validate data.
|
|
15
|
+
- Use the known type for data the program made or already checked. Don't decode it again or make it `unknown`.
|
|
16
|
+
|
|
17
|
+
## Observable
|
|
18
|
+
|
|
19
|
+
You can see what the program does and why it fails. Every failure has a name and goes to a caller. Risky access is visible and limited.
|
|
20
|
+
|
|
21
|
+
- Handle a forked effect's failure before the fork, or keep the fiber and join it.
|
|
22
|
+
- Wrap a Promise in `Effect.tryPromise` and `yield*` it instead of using `await`.
|
|
23
|
+
- Return a fixed value only for the one failure you expect, such as a file that isn't there. Let every other failure fail.
|
|
27
24
|
- Name errors so they say what went wrong.
|
|
28
|
-
- Keep code near the files that use it. Extract shared code when other files need it.
|
|
29
25
|
|
|
30
|
-
|
|
26
|
+
### Make program output clear
|
|
31
27
|
|
|
32
28
|
- Follow Git and uv's style. Say what failed, name the affected file or value and explain the cause.
|
|
33
29
|
- Give the reader enough context to choose a fix.
|
|
@@ -36,6 +32,25 @@ Check may not catch every violation of these principles. Review your code agains
|
|
|
36
32
|
- Preserve useful messages from underlying tools, including error codes and exact names.
|
|
37
33
|
- Write short, full sentences. Use periods instead of semicolons between sentences.
|
|
38
34
|
|
|
39
|
-
##
|
|
35
|
+
## Predictable
|
|
36
|
+
|
|
37
|
+
The code has one shape. Values do not change after they are made. Names stay true when the code changes.
|
|
38
|
+
|
|
39
|
+
- Design for the current requirement as if you were starting fresh. Delete code the new design doesn't need.
|
|
40
|
+
- Use what Effect, Bun or a dependency already provides.
|
|
41
|
+
- Write each fact or decision once and derive the rest from it.
|
|
42
|
+
- Give each value a name that says what it holds.
|
|
43
|
+
- Keep code near the files that use it. Extract shared code when other files need it.
|
|
44
|
+
- Read a fact from where a tool states it, such as an error's fields, a command's exit code or its JSON output. Text written for people changes between releases, and code that searches it breaks quietly when it does.
|
|
45
|
+
- Write a job that moves through stages as an explicit state machine, with one tagged case per stage in one union, so the job is always in exactly one stage.
|
|
46
|
+
|
|
47
|
+
## Decide if a rule is worth it
|
|
48
|
+
|
|
49
|
+
When the user corrects a mistake, consider a rule or a test that prevents it. A rule is worth building when all of these are true:
|
|
50
|
+
|
|
51
|
+
1. It serves one of the three goals.
|
|
52
|
+
2. It removes a way to be wrong and does not add one.
|
|
53
|
+
3. One correct way stays.
|
|
54
|
+
4. It catches a real problem. Run it on real projects to confirm.
|
|
40
55
|
|
|
41
|
-
|
|
56
|
+
A change that loosens a rule adds a way. Make it only when the rule rejects code that already meets the goal.
|
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
|
+
});
|
|
@@ -0,0 +1,207 @@
|
|
|
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 { isAssignmentTarget, unwrappedExpression } from "./resolve-what-an-expression-imports.ts";
|
|
10
|
+
|
|
11
|
+
const OBJECT = "Object";
|
|
12
|
+
|
|
13
|
+
const ERROR = "Error";
|
|
14
|
+
|
|
15
|
+
const REFLECT = "Reflect";
|
|
16
|
+
|
|
17
|
+
const PROTOTYPE = "prototype";
|
|
18
|
+
|
|
19
|
+
const PARENT_PROTOTYPE = "__proto__";
|
|
20
|
+
|
|
21
|
+
const OBJECT_ASSIGN = "Object.assign";
|
|
22
|
+
|
|
23
|
+
const REFLECT_SET = "Reflect.set";
|
|
24
|
+
|
|
25
|
+
const CONSTRUCTORS_UNDER_OBJECT = [
|
|
26
|
+
"Array",
|
|
27
|
+
"ArrayBuffer",
|
|
28
|
+
"BigInt",
|
|
29
|
+
"Boolean",
|
|
30
|
+
"AsyncDisposableStack",
|
|
31
|
+
"DataView",
|
|
32
|
+
"Date",
|
|
33
|
+
"DisposableStack",
|
|
34
|
+
"Error",
|
|
35
|
+
"FinalizationRegistry",
|
|
36
|
+
"Function",
|
|
37
|
+
"Iterator",
|
|
38
|
+
"Map",
|
|
39
|
+
"Number",
|
|
40
|
+
"Promise",
|
|
41
|
+
"RegExp",
|
|
42
|
+
"Set",
|
|
43
|
+
"SharedArrayBuffer",
|
|
44
|
+
"String",
|
|
45
|
+
"Symbol",
|
|
46
|
+
"WeakMap",
|
|
47
|
+
"WeakRef",
|
|
48
|
+
"WeakSet",
|
|
49
|
+
] as const;
|
|
50
|
+
|
|
51
|
+
const ERROR_SUBCLASSES = [
|
|
52
|
+
"AggregateError",
|
|
53
|
+
"EvalError",
|
|
54
|
+
"RangeError",
|
|
55
|
+
"ReferenceError",
|
|
56
|
+
"SuppressedError",
|
|
57
|
+
"SyntaxError",
|
|
58
|
+
"TypeError",
|
|
59
|
+
"URIError",
|
|
60
|
+
] as const;
|
|
61
|
+
|
|
62
|
+
const TYPED_ARRAYS = [
|
|
63
|
+
"BigInt64Array",
|
|
64
|
+
"BigUint64Array",
|
|
65
|
+
"Float16Array",
|
|
66
|
+
"Float32Array",
|
|
67
|
+
"Float64Array",
|
|
68
|
+
"Int16Array",
|
|
69
|
+
"Int32Array",
|
|
70
|
+
"Int8Array",
|
|
71
|
+
"Uint16Array",
|
|
72
|
+
"Uint32Array",
|
|
73
|
+
"Uint8Array",
|
|
74
|
+
"Uint8ClampedArray",
|
|
75
|
+
] as const;
|
|
76
|
+
|
|
77
|
+
const PARENT_PROTOTYPES = new Map<string, string | undefined>([
|
|
78
|
+
[OBJECT, undefined],
|
|
79
|
+
...CONSTRUCTORS_UNDER_OBJECT.map((name) => [name, OBJECT] as const),
|
|
80
|
+
...ERROR_SUBCLASSES.map((name) => [name, ERROR] as const),
|
|
81
|
+
...TYPED_ARRAYS.map((name) => [name, undefined] as const),
|
|
82
|
+
]);
|
|
83
|
+
|
|
84
|
+
const OBJECT_WRITES: ReadonlySet<string> = new Set([
|
|
85
|
+
"assign",
|
|
86
|
+
"defineProperties",
|
|
87
|
+
"defineProperty",
|
|
88
|
+
"setPrototypeOf",
|
|
89
|
+
]);
|
|
90
|
+
|
|
91
|
+
const REFLECT_WRITES: ReadonlySet<string> = new Set([
|
|
92
|
+
"defineProperty",
|
|
93
|
+
"deleteProperty",
|
|
94
|
+
"set",
|
|
95
|
+
"setPrototypeOf",
|
|
96
|
+
]);
|
|
97
|
+
|
|
98
|
+
const intrinsic = (
|
|
99
|
+
kind: "constructor" | "container" | "prototype" | "reflect" | "write",
|
|
100
|
+
name: string,
|
|
101
|
+
) => ({ kind, name });
|
|
102
|
+
|
|
103
|
+
const globalIntrinsic = (name: string) => {
|
|
104
|
+
if (PARENT_PROTOTYPES.has(name)) {
|
|
105
|
+
return intrinsic("constructor", name);
|
|
106
|
+
}
|
|
107
|
+
if (name === REFLECT) {
|
|
108
|
+
return intrinsic("reflect", name);
|
|
109
|
+
}
|
|
110
|
+
return GLOBAL_CONTAINERS.has(name) ? intrinsic("container", name) : undefined;
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
const memberOfIntrinsic = (owner: ReturnType<typeof intrinsic> | undefined, key: string) => {
|
|
114
|
+
if (owner === undefined) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
if (owner.kind === "container") {
|
|
118
|
+
return globalIntrinsic(key);
|
|
119
|
+
}
|
|
120
|
+
const isPrototypeOfConstructor = owner.kind === "constructor" && key === PROTOTYPE;
|
|
121
|
+
if (isPrototypeOfConstructor) {
|
|
122
|
+
return intrinsic("prototype", owner.name);
|
|
123
|
+
}
|
|
124
|
+
const isObjectWrite =
|
|
125
|
+
owner.kind === "constructor" && owner.name === OBJECT && OBJECT_WRITES.has(key);
|
|
126
|
+
const isReflectWrite = owner.kind === "reflect" && REFLECT_WRITES.has(key);
|
|
127
|
+
const isWrite = isObjectWrite || isReflectWrite;
|
|
128
|
+
if (isWrite) {
|
|
129
|
+
return intrinsic("write", `${owner.name}.${key}`);
|
|
130
|
+
}
|
|
131
|
+
const isParentOfPrototype = owner.kind === "prototype" && key === PARENT_PROTOTYPE;
|
|
132
|
+
const parent = isParentOfPrototype ? PARENT_PROTOTYPES.get(owner.name) : undefined;
|
|
133
|
+
return parent === undefined ? undefined : intrinsic("prototype", parent);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
const intrinsicOf = (expression: ESTree.Node, sourceCode: SourceCode) =>
|
|
137
|
+
globalValueOf(expression, sourceCode, { global: globalIntrinsic, member: memberOfIntrinsic });
|
|
138
|
+
|
|
139
|
+
const prototypeWrittenThrough = (target: ESTree.Node, sourceCode: SourceCode) => {
|
|
140
|
+
const node = unwrappedExpression(target);
|
|
141
|
+
if (node.type !== "MemberExpression") {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const owner = intrinsicOf(node.object, sourceCode);
|
|
145
|
+
if (owner?.kind === "prototype") {
|
|
146
|
+
return owner.name;
|
|
147
|
+
}
|
|
148
|
+
const isPrototypeOfConstructor =
|
|
149
|
+
owner?.kind === "constructor" && staticMemberKey(node, sourceCode) === PROTOTYPE;
|
|
150
|
+
return isPrototypeOfConstructor ? owner.name : undefined;
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const isChangedInPlace = (node: ESTree.MemberExpression) => {
|
|
154
|
+
const changed = node.parent.type === "ChainExpression" ? node.parent : node;
|
|
155
|
+
const { parent } = changed;
|
|
156
|
+
const isUpdated = parent.type === "UpdateExpression" && parent.argument === changed;
|
|
157
|
+
const isDeleted =
|
|
158
|
+
parent.type === "UnaryExpression" &&
|
|
159
|
+
parent.operator === "delete" &&
|
|
160
|
+
parent.argument === changed;
|
|
161
|
+
return isUpdated || isDeleted;
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const targetOfWrite = (write: string, callArguments: readonly ESTree.Node[]) => {
|
|
165
|
+
const firstSpread = callArguments.findIndex((argument) => argument.type === "SpreadElement");
|
|
166
|
+
const hasSpread = firstSpread !== -1;
|
|
167
|
+
const argumentsBeforeSpread = hasSpread ? callArguments.slice(0, firstSpread) : callArguments;
|
|
168
|
+
const [target, , , receiver] = argumentsBeforeSpread;
|
|
169
|
+
if (write === REFLECT_SET) {
|
|
170
|
+
return hasSpread ? receiver : (receiver ?? target);
|
|
171
|
+
}
|
|
172
|
+
const isAssignWithoutSources = write === OBJECT_ASSIGN && callArguments.length === 1;
|
|
173
|
+
return isAssignWithoutSources ? undefined : target;
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const prototypeWrittenByCall = (node: ESTree.CallExpression, sourceCode: SourceCode) => {
|
|
177
|
+
const callee = intrinsicOf(node.callee, sourceCode);
|
|
178
|
+
const target = callee?.kind === "write" ? targetOfWrite(callee.name, node.arguments) : undefined;
|
|
179
|
+
const written = target === undefined ? undefined : intrinsicOf(target, sourceCode);
|
|
180
|
+
return written?.kind === "prototype" ? written.name : undefined;
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
export const noNativePrototypeWrites = defineRule({
|
|
184
|
+
meta: {
|
|
185
|
+
type: "problem",
|
|
186
|
+
messages: {
|
|
187
|
+
nativePrototype: "the {{name}} prototype is changed",
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
create(context) {
|
|
191
|
+
return {
|
|
192
|
+
MemberExpression(node) {
|
|
193
|
+
const isWritten = isAssignmentTarget(node) || isChangedInPlace(node);
|
|
194
|
+
const name = isWritten ? prototypeWrittenThrough(node, context.sourceCode) : undefined;
|
|
195
|
+
if (name !== undefined) {
|
|
196
|
+
context.report({ node, messageId: "nativePrototype", data: { name } });
|
|
197
|
+
}
|
|
198
|
+
},
|
|
199
|
+
CallExpression(node) {
|
|
200
|
+
const name = prototypeWrittenByCall(node, context.sourceCode);
|
|
201
|
+
if (name !== undefined) {
|
|
202
|
+
context.report({ node, messageId: "nativePrototype", data: { name } });
|
|
203
|
+
}
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
},
|
|
207
|
+
});
|
|
@@ -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,
|
|
@@ -61,6 +62,7 @@ import { noVagueErrorNames } from "./ban-vague-error-class-names.ts";
|
|
|
61
62
|
import { noVagueFileNames } from "./ban-vague-file-names.ts";
|
|
62
63
|
import { permissionBanRule } from "./ban-what-a-file-is-not-granted.ts";
|
|
63
64
|
import { noWhile } from "./ban-while-do-while-and-for-loops.ts";
|
|
65
|
+
import { noNativePrototypeWrites } from "./ban-writes-to-native-prototypes.ts";
|
|
64
66
|
import { noSharedHolders } from "./ban-writing-captured-objects-inside-effect-callbacks.ts";
|
|
65
67
|
import { noNodeConsole } from "./confine-node-console-to-its-console-export.ts";
|
|
66
68
|
import {
|
|
@@ -143,6 +145,8 @@ const NAMED_RULES = {
|
|
|
143
145
|
[UNUSED_CODE_GRANTS_RULE]: noUnusedCodeGrants,
|
|
144
146
|
"no-glob-scan": noGlobScan,
|
|
145
147
|
"no-unknown-boundaries": noUnknownBoundaries,
|
|
148
|
+
"no-native-prototype-writes": noNativePrototypeWrites,
|
|
149
|
+
"no-promise-combinators": noPromiseCombinators,
|
|
146
150
|
"no-loose-literal-constants": noLooseLiteralConstants,
|
|
147
151
|
"no-type-declarations": noTypeDeclarations,
|
|
148
152
|
"no-declared-values": noDeclaredValues,
|
|
@@ -235,7 +239,6 @@ const baseConfig = {
|
|
|
235
239
|
"eslint/no-else-return": "error",
|
|
236
240
|
"eslint/no-empty": "error",
|
|
237
241
|
"eslint/no-empty-function": ["error", { allow: ["privateConstructors"] }],
|
|
238
|
-
"eslint/no-extend-native": "error",
|
|
239
242
|
"eslint/no-implicit-coercion": "error",
|
|
240
243
|
"eslint/no-lone-blocks": "error",
|
|
241
244
|
"eslint/no-lonely-if": "error",
|