@autotelic/plumb 0.3.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/LICENSE +22 -0
- package/README.md +64 -0
- package/package.json +41 -0
- package/src/effect/index.ts +59 -0
- package/src/effect/rules/guarded-op-must-return-effect.ts +236 -0
- package/src/effect/rules/guarded-op-must-return-option.ts +97 -0
- package/src/effect/rules/no-cascading-layer-provide.ts +80 -0
- package/src/effect/rules/no-decode-unknown-option.ts +41 -0
- package/src/effect/rules/no-direct-browser-storage.ts +91 -0
- package/src/effect/rules/no-direct-fetch.ts +44 -0
- package/src/effect/rules/no-manual-field-guards.ts +144 -0
- package/src/effect/rules/no-nested-layer-provide.ts +51 -0
- package/src/effect/rules/no-schema-class-modeling.ts +59 -0
- package/src/effect/rules/no-service-constructor-imports.ts +50 -0
- package/src/effect/rules/no-silent-error-swallow.ts +99 -0
- package/src/effect/rules/no-static-effect-service-forwarders.ts +227 -0
- package/src/effect/rules/no-try-catch.ts +32 -0
- package/src/effect/rules/no-unguarded-json-parse.ts +211 -0
- package/src/effect/rules/option-core-needs-effect-public.ts +81 -0
- package/src/effect/rules/prefer-die-for-precondition-defects.ts +118 -0
- package/src/effect/rules/prefer-effect-gen-for-guard-ladders.ts +76 -0
- package/src/effect/rules/prefer-effect-match.ts +78 -0
- package/src/effect/rules/prefer-effect-predicate-over-boolean-function.ts +79 -0
- package/src/effect/rules/prefer-named-guard-predicate.ts +105 -0
- package/src/effect/rules/prefer-option-pipeline.ts +95 -0
- package/src/effect/rules/prefer-ordering-match.ts +152 -0
- package/src/effect/rules/prefer-tagged-enum.ts +68 -0
- package/src/effect/rules/require-schema-type-derivation.ts +83 -0
- package/src/index.ts +106 -0
- package/src/meta/index.ts +25 -0
- package/src/meta/rules/no-disable-directives.ts +38 -0
- package/src/meta/rules/no-manual-ancestor-walks.ts +27 -0
- package/src/meta/rules/prefer-before-file-scope.ts +57 -0
- package/src/meta/rules/require-create-once.ts +51 -0
- package/src/meta/rules/require-rule-tester.ts +57 -0
- package/src/rules/no-anonymous-wide-tuples.ts +38 -0
- package/src/rules/no-barrel-export-star.ts +30 -0
- package/src/rules/no-boolean-field-signals.ts +305 -0
- package/src/rules/no-builtin-throws.ts +83 -0
- package/src/rules/no-chained-type-assertions.ts +65 -0
- package/src/rules/no-conditional-empty-object-spread.ts +42 -0
- package/src/rules/no-duplicated-literal-union.ts +66 -0
- package/src/rules/no-exported-mutable-state.ts +111 -0
- package/src/rules/no-generic-export-names.ts +149 -0
- package/src/rules/no-impossible-branch-throw.ts +55 -0
- package/src/rules/no-known-value-widening.ts +225 -0
- package/src/rules/no-module-mocking.ts +95 -0
- package/src/rules/no-multiple-function-params.ts +192 -0
- package/src/rules/no-nondeterministic-core.ts +73 -0
- package/src/rules/no-object-parameters.ts +127 -0
- package/src/rules/no-optional-function-parameters.ts +49 -0
- package/src/rules/no-product-of-state-booleans.ts +80 -0
- package/src/rules/no-redundant-derived-field.ts +192 -0
- package/src/rules/no-reflect-apply.ts +32 -0
- package/src/rules/no-reflect-get.ts +32 -0
- package/src/rules/no-reinterpret-cast.ts +78 -0
- package/src/rules/no-runtime-typeof.ts +59 -0
- package/src/rules/no-sentinel-comparison-union.ts +148 -0
- package/src/rules/no-shape-in-symbol-names.ts +45 -0
- package/src/rules/no-single-use-private-functions.ts +329 -0
- package/src/rules/no-stacked-jsdoc-blocks.ts +65 -0
- package/src/rules/no-stray-inline-comments.ts +79 -0
- package/src/rules/no-swappable-primitive-params.ts +109 -0
- package/src/rules/no-tag-ladder-assertions.ts +71 -0
- package/src/rules/no-transposed-field-reads.ts +135 -0
- package/src/rules/no-unit-return-validators.ts +65 -0
- package/src/rules/no-unknown-parameters.ts +97 -0
- package/src/rules/no-unknown-returns.ts +120 -0
- package/src/rules/no-unknown-type-aliases.ts +70 -0
- package/src/rules/no-unsafe-dictionary-type.ts +132 -0
- package/src/rules/no-vague-test-filenames.ts +67 -0
- package/src/rules/no-widen-then-assert.ts +345 -0
- package/src/rules/prefer-payload-brand.ts +67 -0
- package/src/rules/prefer-property-tests.ts +43 -0
- package/src/rules/require-canonical-stringify-for-identity.ts +80 -0
- package/src/rules/require-deprecated-tag-for-legacy-comments.ts +45 -0
- package/src/rules/require-exhaustive-tag-switch.ts +42 -0
- package/src/rules/require-fc-block-predicate.ts +42 -0
- package/src/rules/require-jsdoc-on-exported.ts +74 -0
- package/src/rules/require-published-order.ts +109 -0
- package/src/rules/require-safety-comment-for-type-assertion.ts +61 -0
- package/src/rules/require-sort-comparator.ts +41 -0
- package/src/rules/test-utils.ts +68 -0
- package/src/shared/ancestors.ts +21 -0
- package/src/shared/child-nodes.ts +27 -0
- package/src/shared/dictionary-types.ts +524 -0
- package/src/shared/is-node.ts +13 -0
- package/src/shared/lexical-type-parameters.ts +62 -0
- package/src/shared/reflect-method.ts +41 -0
- package/src/shared/rule-options.ts +23 -0
- package/src/shared/structural.ts +81 -0
- package/src/testing/index.ts +3 -0
- package/src/testing/testable-rule.test.ts +68 -0
- package/src/testing/testable-rule.ts +86 -0
|
@@ -0,0 +1,524 @@
|
|
|
1
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
const BUILT_INS = new Set([
|
|
4
|
+
"Record",
|
|
5
|
+
"Readonly",
|
|
6
|
+
"Partial",
|
|
7
|
+
"Required",
|
|
8
|
+
"Pick",
|
|
9
|
+
"Omit",
|
|
10
|
+
"PropertyKey",
|
|
11
|
+
"NonNullable",
|
|
12
|
+
]);
|
|
13
|
+
const TRANSPARENT_WRAPPERS = new Set(["Readonly", "Partial", "Required", "NonNullable"]);
|
|
14
|
+
|
|
15
|
+
type TypeAliasEnvironment = ReadonlyMap<string, ESTree.TSType>;
|
|
16
|
+
|
|
17
|
+
type ResolvedType = {
|
|
18
|
+
readonly type: ESTree.TSType;
|
|
19
|
+
readonly substitutions: TypeAliasEnvironment;
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
export type UnsafeDictionary = {
|
|
23
|
+
readonly kind: "unsafe-dictionary";
|
|
24
|
+
readonly unsafeValue: "any" | "empty-object" | "object" | "union" | "unknown";
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type WideningTargetKind =
|
|
28
|
+
| "anonymous object"
|
|
29
|
+
| "generic container"
|
|
30
|
+
| "object"
|
|
31
|
+
| "open dictionary"
|
|
32
|
+
| "unknown";
|
|
33
|
+
|
|
34
|
+
export type WideningTarget = {
|
|
35
|
+
readonly kind: WideningTargetKind;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type TypeEnvironment = {
|
|
39
|
+
readonly aliases: ReadonlyMap<string, ESTree.TSTypeAliasDeclaration>;
|
|
40
|
+
readonly interfaces: ReadonlyMap<string, readonly ESTree.TSInterfaceDeclaration[]>;
|
|
41
|
+
readonly shadowedBuiltIns: ReadonlySet<string>;
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Build the alias/interface maps for a program so type references can be
|
|
46
|
+
* resolved to their declarations during dictionary classification.
|
|
47
|
+
* @param {ESTree.Program} program - Root AST node of the file under lint.
|
|
48
|
+
* @returns {TypeEnvironment} Environment mapping declared names to their declaration nodes.
|
|
49
|
+
*/
|
|
50
|
+
export function createTypeEnvironment(program: ESTree.Program): TypeEnvironment {
|
|
51
|
+
const aliases = new Map<string, ESTree.TSTypeAliasDeclaration>();
|
|
52
|
+
const interfaces = new Map<string, ESTree.TSInterfaceDeclaration[]>();
|
|
53
|
+
const shadowedBuiltIns = new Set<string>();
|
|
54
|
+
|
|
55
|
+
for (const statement of program.body) {
|
|
56
|
+
const declaration =
|
|
57
|
+
statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration"
|
|
58
|
+
? statement.declaration ?? null
|
|
59
|
+
: statement;
|
|
60
|
+
if (declaration?.type === "ImportDeclaration") {
|
|
61
|
+
for (const specifier of declaration.specifiers) {
|
|
62
|
+
if (BUILT_INS.has(specifier.local.name)) shadowedBuiltIns.add(specifier.local.name);
|
|
63
|
+
}
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
if (declaration?.type === "TSTypeAliasDeclaration") {
|
|
68
|
+
const existing = aliases.get(declaration.id.name);
|
|
69
|
+
if (existing === undefined) aliases.set(declaration.id.name, declaration);
|
|
70
|
+
else shadowedBuiltIns.add(declaration.id.name);
|
|
71
|
+
if (BUILT_INS.has(declaration.id.name)) shadowedBuiltIns.add(declaration.id.name);
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
if (declaration?.type === "TSInterfaceDeclaration") {
|
|
76
|
+
const declarations = interfaces.get(declaration.id.name) ?? [];
|
|
77
|
+
declarations.push(declaration);
|
|
78
|
+
interfaces.set(declaration.id.name, declarations);
|
|
79
|
+
if (BUILT_INS.has(declaration.id.name)) shadowedBuiltIns.add(declaration.id.name);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
if (declaration?.type === "TSEnumDeclaration") {
|
|
84
|
+
if (BUILT_INS.has(declaration.id.name)) shadowedBuiltIns.add(declaration.id.name);
|
|
85
|
+
continue;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (
|
|
89
|
+
(declaration?.type === "ClassDeclaration" ||
|
|
90
|
+
declaration?.type === "FunctionDeclaration") &&
|
|
91
|
+
declaration.id !== null
|
|
92
|
+
) {
|
|
93
|
+
if (BUILT_INS.has(declaration.id.name)) shadowedBuiltIns.add(declaration.id.name);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
return { aliases, interfaces, shadowedBuiltIns };
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
function typeReferenceName(type: ESTree.TSTypeReference): string | null {
|
|
101
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
function isBuiltIn(payload: { name: string; environment: TypeEnvironment }): boolean {
|
|
105
|
+
return BUILT_INS.has(payload.name) && !payload.environment.shadowedBuiltIns.has(payload.name);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function isUnappliedReferenceTo(payload: { type: ESTree.TSType; name: string }): boolean {
|
|
109
|
+
const unwrapped = unwrapTransparentType(payload.type);
|
|
110
|
+
return (
|
|
111
|
+
unwrapped.type === "TSTypeReference" &&
|
|
112
|
+
typeReferenceName(unwrapped) === payload.name &&
|
|
113
|
+
(unwrapped.typeArguments === null ||
|
|
114
|
+
unwrapped.typeArguments === undefined ||
|
|
115
|
+
unwrapped.typeArguments.params.length === 0)
|
|
116
|
+
);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
function unwrapTransparentType(type: ESTree.TSType): ESTree.TSType {
|
|
120
|
+
let current = type;
|
|
121
|
+
while (
|
|
122
|
+
current.type === "TSParenthesizedType" ||
|
|
123
|
+
(current.type === "TSTypeOperator" && current.operator === "readonly")
|
|
124
|
+
) {
|
|
125
|
+
current = current.typeAnnotation;
|
|
126
|
+
}
|
|
127
|
+
return current;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function isEffectivelyEmptyMember(member: ESTree.TSSignature): boolean {
|
|
131
|
+
return (
|
|
132
|
+
member.type === "TSPropertySignature" &&
|
|
133
|
+
member.optional === true &&
|
|
134
|
+
member.typeAnnotation !== null &&
|
|
135
|
+
member.typeAnnotation !== undefined &&
|
|
136
|
+
unwrapTransparentType(member.typeAnnotation.typeAnnotation).type === "TSNeverKeyword"
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
function resolvedSubstitutionArgument(payload: {
|
|
141
|
+
type: ESTree.TSType;
|
|
142
|
+
base: TypeAliasEnvironment;
|
|
143
|
+
resolving?: ReadonlySet<string>;
|
|
144
|
+
}): ESTree.TSType {
|
|
145
|
+
const { type, base } = payload;
|
|
146
|
+
const resolving = payload.resolving ?? new Set<string>();
|
|
147
|
+
const unwrapped = unwrapTransparentType(type);
|
|
148
|
+
if (unwrapped.type !== "TSTypeReference") return type;
|
|
149
|
+
const name = typeReferenceName(unwrapped);
|
|
150
|
+
if (name === null || resolving.has(name)) return type;
|
|
151
|
+
const substitution = base.get(name);
|
|
152
|
+
if (substitution === undefined) return type;
|
|
153
|
+
const nextResolving = new Set(resolving);
|
|
154
|
+
nextResolving.add(name);
|
|
155
|
+
return resolvedSubstitutionArgument({ type: substitution, base, resolving: nextResolving });
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
function aliasSubstitution(
|
|
159
|
+
alias: ESTree.TSTypeAliasDeclaration,
|
|
160
|
+
type: ESTree.TSTypeReference,
|
|
161
|
+
base: TypeAliasEnvironment,
|
|
162
|
+
): TypeAliasEnvironment | null {
|
|
163
|
+
const parameters = alias.typeParameters?.params ?? [];
|
|
164
|
+
const arguments_ = type.typeArguments?.params ?? [];
|
|
165
|
+
const next = new Map(base);
|
|
166
|
+
for (const [index, parameter] of parameters.entries()) {
|
|
167
|
+
const argument = arguments_[index] ?? parameter.default;
|
|
168
|
+
if (argument === null || argument === undefined) return null;
|
|
169
|
+
next.set(parameter.name.name, resolvedSubstitutionArgument({ type: argument, base }));
|
|
170
|
+
}
|
|
171
|
+
return next;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
function unsafeDirectValue(
|
|
175
|
+
type: ESTree.TSType,
|
|
176
|
+
environment: TypeEnvironment,
|
|
177
|
+
substitutions: TypeAliasEnvironment,
|
|
178
|
+
resolvingAliases: ReadonlySet<string>,
|
|
179
|
+
): UnsafeDictionary["unsafeValue"] | null {
|
|
180
|
+
const unwrapped = unwrapTransparentType(type);
|
|
181
|
+
if (unwrapped.type === "TSUnknownKeyword") return "unknown";
|
|
182
|
+
if (unwrapped.type === "TSAnyKeyword") return "any";
|
|
183
|
+
if (unwrapped.type === "TSObjectKeyword") return "object";
|
|
184
|
+
if (
|
|
185
|
+
unwrapped.type === "TSTypeLiteral" &&
|
|
186
|
+
(unwrapped.members.length === 0 || unwrapped.members.every(isEffectivelyEmptyMember))
|
|
187
|
+
)
|
|
188
|
+
return "empty-object";
|
|
189
|
+
if (unwrapped.type === "TSUnionType") {
|
|
190
|
+
return unwrapped.types.some(
|
|
191
|
+
(member) => unsafeDirectValue(member, environment, substitutions, resolvingAliases) !== null,
|
|
192
|
+
)
|
|
193
|
+
? "union"
|
|
194
|
+
: null;
|
|
195
|
+
}
|
|
196
|
+
if (unwrapped.type === "TSIntersectionType") {
|
|
197
|
+
const unsafeMembers = unwrapped.types.map((member) =>
|
|
198
|
+
unsafeDirectValue(member, environment, substitutions, resolvingAliases),
|
|
199
|
+
);
|
|
200
|
+
if (unsafeMembers.includes("any")) return "any";
|
|
201
|
+
const [firstUnsafeMember] = unsafeMembers;
|
|
202
|
+
return unsafeMembers.length > 0 &&
|
|
203
|
+
firstUnsafeMember !== undefined &&
|
|
204
|
+
unsafeMembers.every((member) => member !== null)
|
|
205
|
+
? firstUnsafeMember
|
|
206
|
+
: null;
|
|
207
|
+
}
|
|
208
|
+
if (unwrapped.type !== "TSTypeReference") return null;
|
|
209
|
+
const name = typeReferenceName(unwrapped);
|
|
210
|
+
if (name === null) return null;
|
|
211
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn({ name, environment })) {
|
|
212
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
213
|
+
return wrapped === undefined
|
|
214
|
+
? null
|
|
215
|
+
: unsafeDirectValue(wrapped, environment, substitutions, resolvingAliases);
|
|
216
|
+
}
|
|
217
|
+
const substitution = substitutions.get(name);
|
|
218
|
+
if (substitution !== undefined) {
|
|
219
|
+
return isUnappliedReferenceTo({ type: substitution, name })
|
|
220
|
+
? null
|
|
221
|
+
: unsafeDirectValue(substitution, environment, substitutions, resolvingAliases);
|
|
222
|
+
}
|
|
223
|
+
const interfaceDeclarations = environment.interfaces.get(name);
|
|
224
|
+
if (interfaceDeclarations !== undefined) {
|
|
225
|
+
const [declaration] = interfaceDeclarations;
|
|
226
|
+
const effectivelyEmpty =
|
|
227
|
+
interfaceDeclarations.length === 1 &&
|
|
228
|
+
declaration !== undefined &&
|
|
229
|
+
declaration.extends.length === 0 &&
|
|
230
|
+
(declaration.body.body.length === 0 ||
|
|
231
|
+
declaration.body.body.every(isEffectivelyEmptyMember));
|
|
232
|
+
return effectivelyEmpty ? "empty-object" : null;
|
|
233
|
+
}
|
|
234
|
+
const alias = environment.aliases.get(name);
|
|
235
|
+
if (alias === undefined || resolvingAliases.has(name)) return null;
|
|
236
|
+
const nextSubstitutions = aliasSubstitution(alias, unwrapped, substitutions);
|
|
237
|
+
if (nextSubstitutions === null) return null;
|
|
238
|
+
const nextResolving = new Set(resolvingAliases);
|
|
239
|
+
nextResolving.add(name);
|
|
240
|
+
return unsafeDirectValue(alias.typeAnnotation, environment, nextSubstitutions, nextResolving);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
function dictionaryValueTypes(
|
|
244
|
+
type: ESTree.TSType,
|
|
245
|
+
environment: TypeEnvironment,
|
|
246
|
+
substitutions: TypeAliasEnvironment,
|
|
247
|
+
resolvingAliases: ReadonlySet<string>,
|
|
248
|
+
): readonly ResolvedType[] {
|
|
249
|
+
const unwrapped = unwrapTransparentType(type);
|
|
250
|
+
|
|
251
|
+
if (unwrapped.type === "TSTypeLiteral") {
|
|
252
|
+
return unwrapped.members.flatMap((member): readonly ResolvedType[] =>
|
|
253
|
+
member.type === "TSIndexSignature" && member.typeAnnotation !== null
|
|
254
|
+
? [{ type: member.typeAnnotation.typeAnnotation, substitutions }]
|
|
255
|
+
: [],
|
|
256
|
+
);
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (unwrapped.type === "TSMappedType") {
|
|
260
|
+
return unwrapped.typeAnnotation === null
|
|
261
|
+
? []
|
|
262
|
+
: [{ type: unwrapped.typeAnnotation, substitutions }];
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
if (unwrapped.type !== "TSTypeReference") return [];
|
|
266
|
+
const name = typeReferenceName(unwrapped);
|
|
267
|
+
if (name === null) return [];
|
|
268
|
+
|
|
269
|
+
const substitution = substitutions.get(name);
|
|
270
|
+
if (substitution !== undefined) {
|
|
271
|
+
return isUnappliedReferenceTo({ type: substitution, name })
|
|
272
|
+
? []
|
|
273
|
+
: dictionaryValueTypes(substitution, environment, substitutions, resolvingAliases);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn({ name, environment })) {
|
|
277
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
278
|
+
return wrapped === undefined
|
|
279
|
+
? []
|
|
280
|
+
: dictionaryValueTypes(wrapped, environment, substitutions, resolvingAliases);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
if (name === "Record" && isBuiltIn({ name, environment })) {
|
|
284
|
+
const value = unwrapped.typeArguments?.params[1] ?? null;
|
|
285
|
+
return value === null ? [] : [{ type: value, substitutions }];
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
if ((name === "Pick" || name === "Omit") && isBuiltIn({ name, environment })) {
|
|
289
|
+
const source = unwrapped.typeArguments?.params[0];
|
|
290
|
+
return source === undefined
|
|
291
|
+
? []
|
|
292
|
+
: dictionaryValueTypes(source, environment, substitutions, resolvingAliases);
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
const alias = environment.aliases.get(name);
|
|
296
|
+
if (alias === undefined || resolvingAliases.has(name)) return [];
|
|
297
|
+
const nextSubstitutions = aliasSubstitution(alias, unwrapped, substitutions);
|
|
298
|
+
if (nextSubstitutions === null) return [];
|
|
299
|
+
const nextResolving = new Set(resolvingAliases);
|
|
300
|
+
nextResolving.add(name);
|
|
301
|
+
return dictionaryValueTypes(alias.typeAnnotation, environment, nextSubstitutions, nextResolving);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Classify a single type as an unsafe dictionary value, if it is one.
|
|
306
|
+
* @param {ESTree.TSType} valueType - Type annotation of the dictionary value slot.
|
|
307
|
+
* @param {TypeEnvironment} environment - Declared-alias environment from createTypeEnvironment.
|
|
308
|
+
* @returns {UnsafeDictionary | null} The unsafe kind ("any", "unknown", "object", "empty-object") or null.
|
|
309
|
+
*/
|
|
310
|
+
export function classifyUnsafeDictionaryValue(
|
|
311
|
+
valueType: ESTree.TSType,
|
|
312
|
+
environment: TypeEnvironment,
|
|
313
|
+
): UnsafeDictionary | null {
|
|
314
|
+
const unsafeValue = unsafeDirectValue(valueType, environment, new Map(), new Set());
|
|
315
|
+
return unsafeValue === null ? null : { kind: "unsafe-dictionary", unsafeValue };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Classify the value side of an index-signature or Record dictionary.
|
|
320
|
+
* @param {ESTree.TSType} type - The dictionary type node being inspected.
|
|
321
|
+
* @param {TypeEnvironment} environment - Declared-alias environment from createTypeEnvironment.
|
|
322
|
+
* @returns {UnsafeDictionary | null} Classification with kind and location, or null when safe.
|
|
323
|
+
*/
|
|
324
|
+
export function classifyUnsafeDictionary(
|
|
325
|
+
type: ESTree.TSType,
|
|
326
|
+
environment: TypeEnvironment,
|
|
327
|
+
): UnsafeDictionary | null {
|
|
328
|
+
for (const valueType of dictionaryValueTypes(type, environment, new Map(), new Set())) {
|
|
329
|
+
const unsafeValue = unsafeDirectValue(
|
|
330
|
+
valueType.type,
|
|
331
|
+
environment,
|
|
332
|
+
valueType.substitutions,
|
|
333
|
+
new Set(),
|
|
334
|
+
);
|
|
335
|
+
if (unsafeValue !== null) return { kind: "unsafe-dictionary", unsafeValue };
|
|
336
|
+
}
|
|
337
|
+
return null;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Describe what a widening assignment erases, for report messaging.
|
|
342
|
+
* @param {ESTree.TSType} type - The assigned (widened) type.
|
|
343
|
+
* @param {TypeEnvironment} environment - Declared-alias environment from createTypeEnvironment.
|
|
344
|
+
* @returns {WideningTarget | null} Human-readable target description, or null when not a widening.
|
|
345
|
+
*/
|
|
346
|
+
export function classifyWideningTarget(
|
|
347
|
+
type: ESTree.TSType,
|
|
348
|
+
environment: TypeEnvironment,
|
|
349
|
+
): WideningTarget | null {
|
|
350
|
+
const unwrapped = unwrapTransparentType(type);
|
|
351
|
+
if (unwrapped.type === "TSUnknownKeyword") return { kind: "unknown" };
|
|
352
|
+
if (unwrapped.type === "TSObjectKeyword") return { kind: "object" };
|
|
353
|
+
if (unwrapped.type === "TSTypeLiteral") {
|
|
354
|
+
return unwrapped.members.some((member) => member.type === "TSIndexSignature")
|
|
355
|
+
? { kind: "open dictionary" }
|
|
356
|
+
: unwrapped.members.length > 0
|
|
357
|
+
? { kind: "anonymous object" }
|
|
358
|
+
: null;
|
|
359
|
+
}
|
|
360
|
+
if (unwrapped.type === "TSMappedType") return { kind: "open dictionary" };
|
|
361
|
+
if (unwrapped.type !== "TSTypeReference") return null;
|
|
362
|
+
const name = typeReferenceName(unwrapped);
|
|
363
|
+
if (name === null) return null;
|
|
364
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn({ name, environment })) {
|
|
365
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
366
|
+
return wrapped === undefined ? null : classifyWideningTarget(wrapped, environment);
|
|
367
|
+
}
|
|
368
|
+
if (name === "Record" && isBuiltIn({ name, environment })) return { kind: "open dictionary" };
|
|
369
|
+
const alias = environment.aliases.get(name);
|
|
370
|
+
if (alias === undefined) return null;
|
|
371
|
+
if ((alias.typeParameters?.params.length ?? 0) > 0) {
|
|
372
|
+
const substitutions = aliasSubstitution(alias, unwrapped, new Map());
|
|
373
|
+
const resolves =
|
|
374
|
+
substitutions !== null &&
|
|
375
|
+
dictionaryValueTypes(
|
|
376
|
+
alias.typeAnnotation,
|
|
377
|
+
environment,
|
|
378
|
+
substitutions,
|
|
379
|
+
new Set([name]),
|
|
380
|
+
).length > 0;
|
|
381
|
+
return resolves ? { kind: "generic container" } : null;
|
|
382
|
+
}
|
|
383
|
+
const substitutions = aliasSubstitution(alias, unwrapped, new Map());
|
|
384
|
+
if (substitutions === null) return null;
|
|
385
|
+
const resolved = classifyAliasBroadTarget(
|
|
386
|
+
alias.typeAnnotation,
|
|
387
|
+
environment,
|
|
388
|
+
substitutions,
|
|
389
|
+
new Set([name]),
|
|
390
|
+
);
|
|
391
|
+
return resolved;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
function isBroadMappedKey(
|
|
395
|
+
type: ESTree.TSType,
|
|
396
|
+
environment: TypeEnvironment,
|
|
397
|
+
substitutions: TypeAliasEnvironment,
|
|
398
|
+
): boolean {
|
|
399
|
+
const unwrapped = unwrapTransparentType(type);
|
|
400
|
+
if (
|
|
401
|
+
unwrapped.type === "TSStringKeyword" ||
|
|
402
|
+
unwrapped.type === "TSNumberKeyword" ||
|
|
403
|
+
unwrapped.type === "TSSymbolKeyword"
|
|
404
|
+
) {
|
|
405
|
+
return true;
|
|
406
|
+
}
|
|
407
|
+
if (unwrapped.type === "TSUnionType") {
|
|
408
|
+
return unwrapped.types.every((member) =>
|
|
409
|
+
isBroadMappedKey(member, environment, substitutions),
|
|
410
|
+
);
|
|
411
|
+
}
|
|
412
|
+
if (unwrapped.type !== "TSTypeReference") return false;
|
|
413
|
+
const name = typeReferenceName(unwrapped);
|
|
414
|
+
if (name === null) return false;
|
|
415
|
+
const substitution = substitutions.get(name);
|
|
416
|
+
if (substitution !== undefined && !isUnappliedReferenceTo({ type: substitution, name })) {
|
|
417
|
+
return isBroadMappedKey(substitution, environment, substitutions);
|
|
418
|
+
}
|
|
419
|
+
return name === "PropertyKey" && isBuiltIn({ name, environment });
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
function classifyAliasBroadTarget(
|
|
423
|
+
type: ESTree.TSType,
|
|
424
|
+
environment: TypeEnvironment,
|
|
425
|
+
substitutions: TypeAliasEnvironment,
|
|
426
|
+
resolvingAliases: ReadonlySet<string>,
|
|
427
|
+
): WideningTarget | null {
|
|
428
|
+
const unwrapped = unwrapTransparentType(type);
|
|
429
|
+
if (unwrapped.type === "TSUnknownKeyword") return { kind: "unknown" };
|
|
430
|
+
if (unwrapped.type === "TSObjectKeyword") return { kind: "object" };
|
|
431
|
+
if (unwrapped.type === "TSTypeLiteral") {
|
|
432
|
+
return unwrapped.members.some((member) => member.type === "TSIndexSignature")
|
|
433
|
+
? { kind: "open dictionary" }
|
|
434
|
+
: null;
|
|
435
|
+
}
|
|
436
|
+
if (unwrapped.type === "TSMappedType") {
|
|
437
|
+
return isBroadMappedKey(unwrapped.constraint, environment, substitutions)
|
|
438
|
+
? { kind: "open dictionary" }
|
|
439
|
+
: null;
|
|
440
|
+
}
|
|
441
|
+
if (unwrapped.type !== "TSTypeReference") return null;
|
|
442
|
+
const name = typeReferenceName(unwrapped);
|
|
443
|
+
if (name === null) return null;
|
|
444
|
+
const substitution = substitutions.get(name);
|
|
445
|
+
if (substitution !== undefined) {
|
|
446
|
+
return isUnappliedReferenceTo({ type: substitution, name })
|
|
447
|
+
? null
|
|
448
|
+
: classifyAliasBroadTarget(
|
|
449
|
+
substitution,
|
|
450
|
+
environment,
|
|
451
|
+
substitutions,
|
|
452
|
+
resolvingAliases,
|
|
453
|
+
);
|
|
454
|
+
}
|
|
455
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn({ name, environment })) {
|
|
456
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
457
|
+
return wrapped === undefined
|
|
458
|
+
? null
|
|
459
|
+
: classifyAliasBroadTarget(wrapped, environment, substitutions, resolvingAliases);
|
|
460
|
+
}
|
|
461
|
+
if (name === "Record" && isBuiltIn({ name, environment })) {
|
|
462
|
+
return { kind: "open dictionary" };
|
|
463
|
+
}
|
|
464
|
+
const alias = environment.aliases.get(name);
|
|
465
|
+
if (alias === undefined || resolvingAliases.has(name)) return null;
|
|
466
|
+
const nextSubstitutions = aliasSubstitution(alias, unwrapped, substitutions);
|
|
467
|
+
if (nextSubstitutions === null) return null;
|
|
468
|
+
const nextResolving = new Set(resolvingAliases);
|
|
469
|
+
nextResolving.add(name);
|
|
470
|
+
return classifyAliasBroadTarget(
|
|
471
|
+
alias.typeAnnotation,
|
|
472
|
+
environment,
|
|
473
|
+
nextSubstitutions,
|
|
474
|
+
nextResolving,
|
|
475
|
+
);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
/**
|
|
479
|
+
* Whether an expression is an object literal carrying at least one property.
|
|
480
|
+
* @param {ESTree.Expression} expression - Candidate expression node.
|
|
481
|
+
* @returns {boolean} True only for ObjectExpression nodes with members.
|
|
482
|
+
*/
|
|
483
|
+
export function isPopulatedObjectExpression(expression: ESTree.Expression): boolean {
|
|
484
|
+
let current = expression;
|
|
485
|
+
while (
|
|
486
|
+
current.type === "ParenthesizedExpression" ||
|
|
487
|
+
current.type === "TSAsExpression" ||
|
|
488
|
+
current.type === "TSTypeAssertion" ||
|
|
489
|
+
current.type === "TSNonNullExpression"
|
|
490
|
+
) {
|
|
491
|
+
current = current.expression;
|
|
492
|
+
}
|
|
493
|
+
return current.type === "ObjectExpression" && current.properties.length > 0;
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* Whether an expression carries first-hand type evidence (schema parse,
|
|
498
|
+
* literal, typed constructor) and therefore needs no further guarding.
|
|
499
|
+
* @param {ESTree.Expression} expression - Candidate expression node.
|
|
500
|
+
* @returns {boolean} True when the expression is self-evidencing.
|
|
501
|
+
*/
|
|
502
|
+
export function isKnownEvidenceExpression(expression: ESTree.Expression): boolean {
|
|
503
|
+
let current = expression;
|
|
504
|
+
while (
|
|
505
|
+
current.type === "ParenthesizedExpression" ||
|
|
506
|
+
current.type === "TSAsExpression" ||
|
|
507
|
+
current.type === "TSTypeAssertion" ||
|
|
508
|
+
current.type === "TSNonNullExpression" ||
|
|
509
|
+
current.type === "TSSatisfiesExpression"
|
|
510
|
+
) {
|
|
511
|
+
current = current.expression;
|
|
512
|
+
}
|
|
513
|
+
if (current.type === "ObjectExpression") return true;
|
|
514
|
+
return (
|
|
515
|
+
current.type === "ArrayExpression" ||
|
|
516
|
+
current.type === "ArrowFunctionExpression" ||
|
|
517
|
+
current.type === "ClassExpression" ||
|
|
518
|
+
current.type === "FunctionExpression" ||
|
|
519
|
+
current.type === "NewExpression" ||
|
|
520
|
+
current.type === "Literal" ||
|
|
521
|
+
current.type === "TemplateLiteral" ||
|
|
522
|
+
current.type === "UnaryExpression"
|
|
523
|
+
);
|
|
524
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import { isRecordObject, type NodeFieldValue, readField, isString } from "./structural.ts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Whether a reflected node-field value is an AST node (as opposed to a child
|
|
7
|
+
* array, literal, or absent).
|
|
8
|
+
* @param {NodeFieldValue} value - Value obtained from reading an ESTree node field.
|
|
9
|
+
* @returns {boolean} True when the value is a single AST node.
|
|
10
|
+
*/
|
|
11
|
+
export function isNode(value: NodeFieldValue): value is ESTree.Node {
|
|
12
|
+
return isRecordObject(value) && isString(readField(value, "type"));
|
|
13
|
+
}
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
2
|
+
import { isNode } from "./is-node.ts";
|
|
3
|
+
import type { NodeFieldValue } from "../shared/structural.ts";
|
|
4
|
+
import { readField } from "../shared/structural.ts";
|
|
5
|
+
|
|
6
|
+
type VisitorKeys = Readonly<Record<string, readonly string[]>>;
|
|
7
|
+
|
|
8
|
+
function collectInferTypeParameterNames(
|
|
9
|
+
node: ESTree.Node,
|
|
10
|
+
visitorKeys: VisitorKeys,
|
|
11
|
+
names: Set<string>,
|
|
12
|
+
): void {
|
|
13
|
+
if (node.type === "TSInferType") names.add(node.typeParameter.name.name);
|
|
14
|
+
for (const key of visitorKeys[node.type] ?? []) {
|
|
15
|
+
const value = readField(node, key);
|
|
16
|
+
if (isNode(value)) {
|
|
17
|
+
collectInferTypeParameterNames(value, visitorKeys, names);
|
|
18
|
+
continue;
|
|
19
|
+
}
|
|
20
|
+
if (!Array.isArray(value)) continue;
|
|
21
|
+
for (const child of value) {
|
|
22
|
+
if (isNode(child)) collectInferTypeParameterNames(child, visitorKeys, names);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Collect type binders that are in scope at a node and can shadow module aliases.
|
|
29
|
+
*
|
|
30
|
+
* @param {ESTree.Node} node - The node whose lexical scope is being resolved.
|
|
31
|
+
* @param {ReadonlyArray<ESTree.Node>} ancestors - Nearest-first ancestor chain of `node`.
|
|
32
|
+
* @param {VisitorKeys} visitorKeys - Grammar keys used to descend conditional types.
|
|
33
|
+
* @returns {ReadonlySet<string>} Names of in-scope type binders.
|
|
34
|
+
*/
|
|
35
|
+
export function lexicalTypeParameterNames(
|
|
36
|
+
node: ESTree.Node,
|
|
37
|
+
ancestors: ReadonlyArray<ESTree.Node>,
|
|
38
|
+
visitorKeys: VisitorKeys,
|
|
39
|
+
): ReadonlySet<string> {
|
|
40
|
+
const names = new Set<string>();
|
|
41
|
+
const chain: Array<ESTree.Node> = [node, ...[...ancestors].reverse()];
|
|
42
|
+
for (let index = 1; index < chain.length; index += 1) {
|
|
43
|
+
const current = chain[index]!;
|
|
44
|
+
if (current.type === "Program") break;
|
|
45
|
+
const descendant = chain[index - 1]!;
|
|
46
|
+
if ("typeParameters" in current) {
|
|
47
|
+
for (const parameter of current.typeParameters?.params ?? []) {
|
|
48
|
+
names.add(parameter.name.name);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
if (
|
|
52
|
+
current.type === "TSMappedType" &&
|
|
53
|
+
(descendant === current.nameType || descendant === current.typeAnnotation)
|
|
54
|
+
) {
|
|
55
|
+
names.add(current.key.name);
|
|
56
|
+
}
|
|
57
|
+
if (current.type === "TSConditionalType" && descendant === current.trueType) {
|
|
58
|
+
collectInferTypeParameterNames(current.extendsType, visitorKeys, names);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
return names;
|
|
62
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { ESTree, Scope, SourceCode, Variable } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
interface ReflectMethodCallInput {
|
|
4
|
+
sourceCode: SourceCode;
|
|
5
|
+
callee: ESTree.Expression;
|
|
6
|
+
methodName: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
/** Reports whether a call target names one method on the global Reflect object.
|
|
10
|
+
*
|
|
11
|
+
* @param {SourceCode} sourceCode - The rule's source-code accessor.
|
|
12
|
+
* @param {ESTree.Expression} callee - The callee expression of the call.
|
|
13
|
+
* @param {string} methodName - The Reflect method name to match.
|
|
14
|
+
* @returns {boolean} True when the call targets the global `Reflect`.
|
|
15
|
+
*/
|
|
16
|
+
export function isGlobalReflectMethodCall({
|
|
17
|
+
sourceCode,
|
|
18
|
+
callee,
|
|
19
|
+
methodName,
|
|
20
|
+
}: ReflectMethodCallInput): boolean {
|
|
21
|
+
if (!("property" in callee) || !("object" in callee) || !("computed" in callee)) return false;
|
|
22
|
+
const object = callee.object;
|
|
23
|
+
if (object.type !== "Identifier" || object.name !== "Reflect") return false;
|
|
24
|
+
if (!sourceCode.isGlobalReference(object)) {
|
|
25
|
+
let resolved: Variable | null = null;
|
|
26
|
+
let scope: Scope | null = sourceCode.getScope(object);
|
|
27
|
+
while (scope !== null) {
|
|
28
|
+
const variable = scope.set.get(object.name);
|
|
29
|
+
if (variable !== undefined) {
|
|
30
|
+
resolved = variable;
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
33
|
+
scope = scope.upper;
|
|
34
|
+
}
|
|
35
|
+
if (resolved !== null && resolved.defs.length > 0) return false;
|
|
36
|
+
}
|
|
37
|
+
const property = callee.property;
|
|
38
|
+
return callee.computed
|
|
39
|
+
? property.type === "Literal" && property.value === methodName
|
|
40
|
+
: property.type === "Identifier" && property.name === methodName;
|
|
41
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE decode boundary for rule options.
|
|
3
|
+
*
|
|
4
|
+
* Rule configuration arrives from the lint config file as untyped JSON; the
|
|
5
|
+
* `typeof` discrimination in this module is that boundary decode, sanctioned
|
|
6
|
+
* by this module's single purpose. No other rule file should inspect option
|
|
7
|
+
* representations directly.
|
|
8
|
+
*/
|
|
9
|
+
export interface OptionRecord {
|
|
10
|
+
readonly [key: string]: unknown;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Read the first options entry as an object record.
|
|
15
|
+
*
|
|
16
|
+
* @param {readonly unknown[] | undefined} options - The raw `context.options` array.
|
|
17
|
+
* @returns {OptionRecord} The first entry as a record, or an empty record when absent.
|
|
18
|
+
*/
|
|
19
|
+
export function firstOptionRecord(options: readonly unknown[] | undefined): OptionRecord {
|
|
20
|
+
const first = options?.[0];
|
|
21
|
+
if (first === null || typeof first !== "object" || Array.isArray(first)) return {};
|
|
22
|
+
return first as Record<string, unknown>;
|
|
23
|
+
}
|