oxlint-plugin-samdotci 0.0.0-stage → 0.1.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.
Files changed (33) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +53 -2
  3. package/dist/index.d.ts +12 -0
  4. package/dist/index.js +25 -0
  5. package/dist/production-file.d.ts +1 -0
  6. package/dist/production-file.js +7 -0
  7. package/dist/rules/no-chained-type-assertions.d.ts +2 -0
  8. package/dist/rules/no-chained-type-assertions.js +55 -0
  9. package/dist/rules/no-discarded-map-error.d.ts +2 -0
  10. package/dist/rules/no-discarded-map-error.js +152 -0
  11. package/dist/rules/no-known-value-widening.d.ts +3 -0
  12. package/dist/rules/no-known-value-widening.js +306 -0
  13. package/dist/rules/no-runtime-typeof.d.ts +2 -0
  14. package/dist/rules/no-runtime-typeof.js +48 -0
  15. package/dist/rules/no-unannotated-any.d.ts +2 -0
  16. package/dist/rules/no-unannotated-any.js +25 -0
  17. package/dist/rules/no-unknown-parameters.d.ts +2 -0
  18. package/dist/rules/no-unknown-parameters.js +57 -0
  19. package/dist/rules/no-unsafe-dictionary-type.d.ts +3 -0
  20. package/dist/rules/no-unsafe-dictionary-type.js +122 -0
  21. package/dist/rules/no-widen-then-assert.d.ts +2 -0
  22. package/dist/rules/no-widen-then-assert.js +274 -0
  23. package/dist/shared/dictionary-types.d.ts +19 -0
  24. package/dist/shared/dictionary-types.js +338 -0
  25. package/dist/shared/function-parameters.d.ts +8 -0
  26. package/dist/shared/function-parameters.js +40 -0
  27. package/dist/shared/lexical-type-parameters.d.ts +5 -0
  28. package/dist/shared/lexical-type-parameters.js +44 -0
  29. package/dist/shared/scope.d.ts +3 -0
  30. package/dist/shared/scope.js +6 -0
  31. package/dist/shared/type-alias-resolution.d.ts +20 -0
  32. package/dist/shared/type-alias-resolution.js +114 -0
  33. package/package.json +62 -4
@@ -0,0 +1,338 @@
1
+ import { createTypeAliasEnvironment, hasVisibleTypeBinding, visibleTypeAlias, } from "./type-alias-resolution.js";
2
+ const BUILT_INS = new Set([
3
+ "Record",
4
+ "Readonly",
5
+ "Partial",
6
+ "Required",
7
+ "Pick",
8
+ "Omit",
9
+ "PropertyKey",
10
+ "NonNullable",
11
+ ]);
12
+ const TRANSPARENT_WRAPPERS = new Set(["Readonly", "Partial", "Required", "NonNullable"]);
13
+ const rootContext = { substitutions: new Map(), resolvingAliases: new Set() };
14
+ function declaredStatement(statement) {
15
+ return statement.type === "ExportNamedDeclaration" ||
16
+ statement.type === "ExportDefaultDeclaration"
17
+ ? (statement.declaration ?? null)
18
+ : statement;
19
+ }
20
+ export function createTypeEnvironment(program, visitorKeys) {
21
+ const interfaces = new Map();
22
+ for (const statement of program.body) {
23
+ const declaration = declaredStatement(statement);
24
+ if (declaration?.type !== "TSInterfaceDeclaration")
25
+ continue;
26
+ const declarations = interfaces.get(declaration.id.name) ?? [];
27
+ declarations.push(declaration);
28
+ interfaces.set(declaration.id.name, declarations);
29
+ }
30
+ return {
31
+ interfaces,
32
+ typeAliases: createTypeAliasEnvironment(program, visitorKeys),
33
+ };
34
+ }
35
+ function typeReferenceName(type) {
36
+ return type.typeName.type === "Identifier" ? type.typeName.name : null;
37
+ }
38
+ function isBuiltIn(name, use, environment) {
39
+ return BUILT_INS.has(name) && !hasVisibleTypeBinding(name, use, environment.typeAliases);
40
+ }
41
+ function isTransparentWrapper(name, reference, environment) {
42
+ return TRANSPARENT_WRAPPERS.has(name) && isBuiltIn(name, reference, environment);
43
+ }
44
+ function isUnappliedReferenceTo(type, name) {
45
+ const unwrapped = unwrapTransparentType(type);
46
+ return (unwrapped.type === "TSTypeReference" &&
47
+ typeReferenceName(unwrapped) === name &&
48
+ (unwrapped.typeArguments === null ||
49
+ unwrapped.typeArguments === undefined ||
50
+ unwrapped.typeArguments.params.length === 0));
51
+ }
52
+ function unwrapTransparentType(type) {
53
+ let current = type;
54
+ while (current.type === "TSParenthesizedType" ||
55
+ (current.type === "TSTypeOperator" && current.operator === "readonly")) {
56
+ current = current.typeAnnotation;
57
+ }
58
+ return current;
59
+ }
60
+ function isNeverType(type) {
61
+ return unwrapTransparentType(type).type === "TSNeverKeyword";
62
+ }
63
+ function isEffectivelyEmptyMember(member) {
64
+ return (member.type === "TSPropertySignature" &&
65
+ member.optional &&
66
+ member.typeAnnotation !== null &&
67
+ member.typeAnnotation !== undefined &&
68
+ isNeverType(member.typeAnnotation.typeAnnotation));
69
+ }
70
+ function isEffectivelyEmptyTypeLiteral(type) {
71
+ return type.members.length === 0 || type.members.every(isEffectivelyEmptyMember);
72
+ }
73
+ function isEffectivelyEmptyInterface(declarations) {
74
+ if (declarations.length !== 1)
75
+ return false;
76
+ const [type] = declarations;
77
+ return (type !== undefined &&
78
+ type.extends.length === 0 &&
79
+ (type.body.body.length === 0 || type.body.body.every(isEffectivelyEmptyMember)));
80
+ }
81
+ function resolvedSubstitutionArgument(type, base, resolving = new Set()) {
82
+ const unwrapped = unwrapTransparentType(type);
83
+ if (unwrapped.type !== "TSTypeReference")
84
+ return type;
85
+ const name = typeReferenceName(unwrapped);
86
+ if (name === null || resolving.has(name))
87
+ return type;
88
+ const substitution = base.get(name);
89
+ if (substitution === undefined)
90
+ return type;
91
+ const nextResolving = new Set(resolving);
92
+ nextResolving.add(name);
93
+ return resolvedSubstitutionArgument(substitution, base, nextResolving);
94
+ }
95
+ function aliasSubstitution(alias, type, base) {
96
+ const parameters = alias.typeParameters?.params ?? [];
97
+ const typeArguments = type.typeArguments?.params ?? [];
98
+ const next = new Map(base);
99
+ for (const [index, parameter] of parameters.entries()) {
100
+ const argument = typeArguments[index] ?? parameter.default;
101
+ if (argument === null || argument === undefined)
102
+ return null;
103
+ next.set(parameter.name.name, resolvedSubstitutionArgument(argument, next));
104
+ }
105
+ return next;
106
+ }
107
+ function resolveAlias(name, reference, environment, context) {
108
+ if (context.resolvingAliases.has(name))
109
+ return null;
110
+ const declaration = visibleTypeAlias(name, reference, environment.typeAliases);
111
+ if (declaration === null)
112
+ return null;
113
+ const substitutions = aliasSubstitution(declaration, reference, context.substitutions);
114
+ if (substitutions === null)
115
+ return null;
116
+ const resolvingAliases = new Set(context.resolvingAliases);
117
+ resolvingAliases.add(name);
118
+ return { declaration, substitutions, resolvingAliases };
119
+ }
120
+ function unsafeDirectValue(type, environment, context) {
121
+ const unwrapped = unwrapTransparentType(type);
122
+ switch (unwrapped.type) {
123
+ case "TSUnknownKeyword":
124
+ return "unknown";
125
+ case "TSAnyKeyword":
126
+ return "any";
127
+ case "TSObjectKeyword":
128
+ return "object";
129
+ case "TSTypeLiteral":
130
+ return isEffectivelyEmptyTypeLiteral(unwrapped) ? "empty-object" : null;
131
+ case "TSUnionType":
132
+ return unwrapped.types.some((member) => unsafeDirectValue(member, environment, context) !== null)
133
+ ? "union"
134
+ : null;
135
+ case "TSIntersectionType": {
136
+ const unsafeMembers = unwrapped.types.map((member) => unsafeDirectValue(member, environment, context));
137
+ if (unsafeMembers.includes("any"))
138
+ return "any";
139
+ return unsafeMembers.every((member) => member !== null) ? (unsafeMembers[0] ?? null) : null;
140
+ }
141
+ case "TSTypeReference":
142
+ return unsafeReferenceValue(unwrapped, environment, context);
143
+ default:
144
+ return null;
145
+ }
146
+ }
147
+ function unsafeReferenceValue(reference, environment, context) {
148
+ const name = typeReferenceName(reference);
149
+ if (name === null)
150
+ return null;
151
+ if (isTransparentWrapper(name, reference, environment)) {
152
+ const wrapped = reference.typeArguments?.params[0];
153
+ return wrapped === undefined ? null : unsafeDirectValue(wrapped, environment, context);
154
+ }
155
+ const substitution = context.substitutions.get(name);
156
+ if (substitution !== undefined) {
157
+ return isUnappliedReferenceTo(substitution, name)
158
+ ? null
159
+ : unsafeDirectValue(substitution, environment, context);
160
+ }
161
+ const interfaceDeclarations = environment.interfaces.get(name);
162
+ if (interfaceDeclarations !== undefined) {
163
+ return isEffectivelyEmptyInterface(interfaceDeclarations) ? "empty-object" : null;
164
+ }
165
+ const alias = resolveAlias(name, reference, environment, context);
166
+ return alias === null
167
+ ? null
168
+ : unsafeDirectValue(alias.declaration.typeAnnotation, environment, alias);
169
+ }
170
+ function dictionaryValueTypes(type, environment, context) {
171
+ const unwrapped = unwrapTransparentType(type);
172
+ const { substitutions } = context;
173
+ if (unwrapped.type === "TSTypeLiteral") {
174
+ return unwrapped.members.flatMap((member) => member.type === "TSIndexSignature" && member.typeAnnotation !== null
175
+ ? [{ type: member.typeAnnotation.typeAnnotation, substitutions }]
176
+ : []);
177
+ }
178
+ if (unwrapped.type === "TSMappedType") {
179
+ return unwrapped.typeAnnotation === null
180
+ ? []
181
+ : [{ type: unwrapped.typeAnnotation, substitutions }];
182
+ }
183
+ return unwrapped.type === "TSTypeReference"
184
+ ? dictionaryReferenceValues(unwrapped, environment, context)
185
+ : [];
186
+ }
187
+ function dictionaryReferenceValues(reference, environment, context) {
188
+ const { substitutions } = context;
189
+ const name = typeReferenceName(reference);
190
+ if (name === null)
191
+ return [];
192
+ const substitution = substitutions.get(name);
193
+ if (substitution !== undefined) {
194
+ return isUnappliedReferenceTo(substitution, name)
195
+ ? []
196
+ : dictionaryValueTypes(substitution, environment, context);
197
+ }
198
+ if (name === "Record" && isBuiltIn(name, reference, environment)) {
199
+ const value = reference.typeArguments?.params[1];
200
+ return value === undefined ? [] : [{ type: value, substitutions }];
201
+ }
202
+ if ((TRANSPARENT_WRAPPERS.has(name) || name === "Pick" || name === "Omit") &&
203
+ isBuiltIn(name, reference, environment)) {
204
+ const source = reference.typeArguments?.params[0];
205
+ return source === undefined ? [] : dictionaryValueTypes(source, environment, context);
206
+ }
207
+ const alias = resolveAlias(name, reference, environment, context);
208
+ return alias === null
209
+ ? []
210
+ : dictionaryValueTypes(alias.declaration.typeAnnotation, environment, alias);
211
+ }
212
+ export function classifyUnsafeDictionaryValue(valueType, environment) {
213
+ const unsafeValue = unsafeDirectValue(valueType, environment, rootContext);
214
+ return unsafeValue === null ? null : { kind: "unsafe-dictionary", unsafeValue };
215
+ }
216
+ export function classifyUnsafeDictionary(type, environment) {
217
+ const valueTypes = dictionaryValueTypes(type, environment, rootContext);
218
+ for (const { type: value, substitutions } of valueTypes) {
219
+ const unsafeValue = unsafeDirectValue(value, environment, { ...rootContext, substitutions });
220
+ if (unsafeValue !== null)
221
+ return { kind: "unsafe-dictionary", unsafeValue };
222
+ }
223
+ return null;
224
+ }
225
+ export function classifyWideningTarget(type, environment) {
226
+ return wideningTarget(type, environment, null);
227
+ }
228
+ function wideningTarget(type, environment, enclosingAlias) {
229
+ const unwrapped = unwrapTransparentType(type);
230
+ switch (unwrapped.type) {
231
+ case "TSUnknownKeyword":
232
+ return { kind: "unknown" };
233
+ case "TSObjectKeyword":
234
+ return { kind: "object" };
235
+ case "TSTypeLiteral":
236
+ if (unwrapped.members.some((member) => member.type === "TSIndexSignature")) {
237
+ return { kind: "open dictionary" };
238
+ }
239
+ return enclosingAlias === null && unwrapped.members.length > 0
240
+ ? { kind: "anonymous object" }
241
+ : null;
242
+ case "TSMappedType":
243
+ return enclosingAlias === null ||
244
+ isBroadMappedKey(unwrapped.constraint, environment, enclosingAlias.substitutions)
245
+ ? { kind: "open dictionary" }
246
+ : null;
247
+ case "TSTypeReference":
248
+ return wideningReferenceTarget(unwrapped, environment, enclosingAlias);
249
+ default:
250
+ return null;
251
+ }
252
+ }
253
+ function wideningReferenceTarget(reference, environment, enclosingAlias) {
254
+ const name = typeReferenceName(reference);
255
+ if (name === null)
256
+ return null;
257
+ const context = enclosingAlias ?? rootContext;
258
+ const substitution = context.substitutions.get(name);
259
+ if (substitution !== undefined) {
260
+ return isUnappliedReferenceTo(substitution, name)
261
+ ? null
262
+ : wideningTarget(substitution, environment, enclosingAlias);
263
+ }
264
+ if (isTransparentWrapper(name, reference, environment)) {
265
+ const wrapped = reference.typeArguments?.params[0];
266
+ return wrapped === undefined ? null : wideningTarget(wrapped, environment, enclosingAlias);
267
+ }
268
+ if (name === "Record" && isBuiltIn(name, reference, environment)) {
269
+ return hasBroadRecordKey(reference, environment, context.substitutions)
270
+ ? { kind: "open dictionary" }
271
+ : null;
272
+ }
273
+ return wideningTargetFromAlias(resolveAlias(name, reference, environment, context), environment, enclosingAlias === null);
274
+ }
275
+ function wideningTargetFromAlias(alias, environment, direct) {
276
+ if (alias === null)
277
+ return null;
278
+ const target = wideningTarget(alias.declaration.typeAnnotation, environment, alias);
279
+ if (direct && (alias.declaration.typeParameters?.params.length ?? 0) > 0) {
280
+ return target?.kind === "open dictionary" ? { kind: "generic container" } : null;
281
+ }
282
+ return target;
283
+ }
284
+ function hasBroadRecordKey(type, environment, substitutions) {
285
+ const key = type.typeArguments?.params[0];
286
+ return key === undefined || isBroadMappedKey(key, environment, substitutions);
287
+ }
288
+ function isBroadMappedKey(type, environment, substitutions, visitedAliases = new Set()) {
289
+ const unwrapped = unwrapTransparentType(type);
290
+ if (unwrapped.type === "TSStringKeyword" ||
291
+ unwrapped.type === "TSNumberKeyword" ||
292
+ unwrapped.type === "TSSymbolKeyword") {
293
+ return true;
294
+ }
295
+ if (unwrapped.type === "TSUnionType") {
296
+ return unwrapped.types.some((member) => isBroadMappedKey(member, environment, substitutions, visitedAliases));
297
+ }
298
+ if (unwrapped.type !== "TSTypeReference")
299
+ return false;
300
+ const name = typeReferenceName(unwrapped);
301
+ if (name === null)
302
+ return false;
303
+ const substitution = substitutions.get(name);
304
+ if (substitution !== undefined && !isUnappliedReferenceTo(substitution, name)) {
305
+ return isBroadMappedKey(substitution, environment, substitutions, visitedAliases);
306
+ }
307
+ if (name === "PropertyKey" && isBuiltIn(name, unwrapped, environment))
308
+ return true;
309
+ const alias = visibleTypeAlias(name, unwrapped, environment.typeAliases);
310
+ if (alias === null ||
311
+ (alias.typeParameters?.params.length ?? 0) > 0 ||
312
+ visitedAliases.has(name)) {
313
+ return false;
314
+ }
315
+ const nextVisited = new Set(visitedAliases);
316
+ nextVisited.add(name);
317
+ return isBroadMappedKey(alias.typeAnnotation, environment, substitutions, nextVisited);
318
+ }
319
+ export function isKnownEvidenceExpression(expression) {
320
+ let current = expression;
321
+ while (current.type === "ParenthesizedExpression" ||
322
+ current.type === "TSAsExpression" ||
323
+ current.type === "TSTypeAssertion" ||
324
+ current.type === "TSNonNullExpression" ||
325
+ current.type === "TSSatisfiesExpression") {
326
+ current = current.expression;
327
+ }
328
+ if (current.type === "ObjectExpression")
329
+ return true;
330
+ return (current.type === "ArrayExpression" ||
331
+ current.type === "ArrowFunctionExpression" ||
332
+ current.type === "ClassExpression" ||
333
+ current.type === "FunctionExpression" ||
334
+ current.type === "NewExpression" ||
335
+ current.type === "Literal" ||
336
+ current.type === "TemplateLiteral" ||
337
+ current.type === "UnaryExpression");
338
+ }
@@ -0,0 +1,8 @@
1
+ import type { ESTree, OxlintSourceCode } from "effect-oxlint";
2
+ export type FunctionParameter = ESTree.ParamPattern;
3
+ /** Return whether a type is or contains TypeScript's absorbing unknown top type. */
4
+ export declare function containsUnknownType(type: ESTree.TSType): boolean;
5
+ /** Return the TypeScript annotation attached to a function parameter or its wrapped binding. */
6
+ export declare function functionParameterTypeAnnotation(parameter: FunctionParameter): ESTree.TSTypeAnnotation | null | undefined;
7
+ /** Return only a function parameter's local binding, excluding its annotation and default value. */
8
+ export declare function functionParameterBindingName(parameter: FunctionParameter, sourceCode: OxlintSourceCode): string;
@@ -0,0 +1,40 @@
1
+ /** Return whether a type is or contains TypeScript's absorbing unknown top type. */
2
+ export function containsUnknownType(type) {
3
+ if (type.type === "TSUnknownKeyword")
4
+ return true;
5
+ if (type.type === "TSParenthesizedType")
6
+ return containsUnknownType(type.typeAnnotation);
7
+ return type.type === "TSUnionType" && type.types.some(containsUnknownType);
8
+ }
9
+ /** Return the TypeScript annotation attached to a function parameter or its wrapped binding. */
10
+ export function functionParameterTypeAnnotation(parameter) {
11
+ if (parameter.type === "TSParameterProperty") {
12
+ return functionParameterTypeAnnotation(parameter.parameter);
13
+ }
14
+ if (parameter.type === "RestElement") {
15
+ return parameter.typeAnnotation ?? functionParameterTypeAnnotation(parameter.argument);
16
+ }
17
+ if (parameter.type === "AssignmentPattern") {
18
+ return parameter.typeAnnotation ?? functionParameterTypeAnnotation(parameter.left);
19
+ }
20
+ return parameter.typeAnnotation;
21
+ }
22
+ /** Return only a function parameter's local binding, excluding its annotation and default value. */
23
+ export function functionParameterBindingName(parameter, sourceCode) {
24
+ if (parameter.type === "TSParameterProperty") {
25
+ return functionParameterBindingName(parameter.parameter, sourceCode);
26
+ }
27
+ if (parameter.type === "AssignmentPattern") {
28
+ return functionParameterBindingName(parameter.left, sourceCode);
29
+ }
30
+ if (parameter.type === "RestElement") {
31
+ return functionParameterBindingName(parameter.argument, sourceCode);
32
+ }
33
+ if (parameter.type === "Identifier")
34
+ return parameter.name;
35
+ const sourceText = sourceCode.getText(parameter);
36
+ const annotationStart = parameter.typeAnnotation?.start;
37
+ return annotationStart === undefined
38
+ ? sourceText
39
+ : sourceText.slice(0, annotationStart - parameter.start).trimEnd();
40
+ }
@@ -0,0 +1,5 @@
1
+ import type { ESTree } from "effect-oxlint";
2
+ type VisitorKeys = Readonly<Record<string, readonly string[]>>;
3
+ /** Collect type binders that are in scope at a node and can shadow module aliases. */
4
+ export declare function lexicalTypeParameterNames(node: ESTree.Node, visitorKeys: VisitorKeys): ReadonlySet<string>;
5
+ export {};
@@ -0,0 +1,44 @@
1
+ function isNode(value) {
2
+ return (typeof value === "object" && value !== null && "type" in value && typeof value.type === "string");
3
+ }
4
+ function collectInferTypeParameterNames(node, visitorKeys, names) {
5
+ if (node.type === "TSInferType")
6
+ names.add(node.typeParameter.name.name);
7
+ const record = node;
8
+ for (const key of visitorKeys[node.type] ?? []) {
9
+ const value = record[key];
10
+ if (isNode(value)) {
11
+ collectInferTypeParameterNames(value, visitorKeys, names);
12
+ continue;
13
+ }
14
+ if (!Array.isArray(value))
15
+ continue;
16
+ for (const child of value) {
17
+ if (isNode(child))
18
+ collectInferTypeParameterNames(child, visitorKeys, names);
19
+ }
20
+ }
21
+ }
22
+ /** Collect type binders that are in scope at a node and can shadow module aliases. */
23
+ export function lexicalTypeParameterNames(node, visitorKeys) {
24
+ const names = new Set();
25
+ let descendant = node;
26
+ let current = node;
27
+ while (current !== null && current.type !== "Program") {
28
+ if ("typeParameters" in current) {
29
+ for (const parameter of current.typeParameters?.params ?? []) {
30
+ names.add(parameter.name.name);
31
+ }
32
+ }
33
+ if (current.type === "TSMappedType" &&
34
+ (descendant === current.nameType || descendant === current.typeAnnotation)) {
35
+ names.add(current.key.name);
36
+ }
37
+ if (current.type === "TSConditionalType" && descendant === current.trueType) {
38
+ collectInferTypeParameterNames(current.extendsType, visitorKeys, names);
39
+ }
40
+ descendant = current;
41
+ current = current.parent;
42
+ }
43
+ return names;
44
+ }
@@ -0,0 +1,3 @@
1
+ import { type ESTree, type OxlintSourceCode, type Variable } from "effect-oxlint";
2
+ /** Resolve an identifier to its binding by walking lexical scopes upward. */
3
+ export declare function resolveVariable(sourceCode: OxlintSourceCode, identifier: ESTree.IdentifierReference): Variable | null;
@@ -0,0 +1,6 @@
1
+ import { Option } from "effect";
2
+ import { Scope } from "effect-oxlint";
3
+ /** Resolve an identifier to its binding by walking lexical scopes upward. */
4
+ export function resolveVariable(sourceCode, identifier) {
5
+ return Option.getOrNull(Scope.findVariableUp(sourceCode.getScope(identifier), identifier.name));
6
+ }
@@ -0,0 +1,20 @@
1
+ import type { ESTree } from "effect-oxlint";
2
+ type VisitorKeys = Readonly<Record<string, readonly string[]>>;
3
+ type TypeScope = ESTree.Node;
4
+ type TypeBinding = {
5
+ readonly alias: ESTree.TSTypeAliasDeclaration | null;
6
+ readonly name: string;
7
+ readonly scope: TypeScope;
8
+ };
9
+ export type TypeAliasEnvironment = {
10
+ readonly aliases: readonly ESTree.TSTypeAliasDeclaration[];
11
+ readonly bindingsByName: ReadonlyMap<string, readonly TypeBinding[]>;
12
+ readonly visitorKeys: VisitorKeys;
13
+ };
14
+ /** Collect every lexical type alias and competing type binding in a program. */
15
+ export declare function createTypeAliasEnvironment(program: ESTree.Program, visitorKeys: VisitorKeys): TypeAliasEnvironment;
16
+ /** Resolve the nearest visible alias with this name, respecting lexical shadowing. */
17
+ export declare function visibleTypeAlias(name: string, use: ESTree.Node, environment: TypeAliasEnvironment): ESTree.TSTypeAliasDeclaration | null;
18
+ /** Return whether a local declaration shadows a built-in type at this use. */
19
+ export declare function hasVisibleTypeBinding(name: string, use: ESTree.Node, environment: TypeAliasEnvironment): boolean;
20
+ export {};
@@ -0,0 +1,114 @@
1
+ import { lexicalTypeParameterNames } from "./lexical-type-parameters.js";
2
+ const environmentsByProgram = new WeakMap();
3
+ function isNode(value) {
4
+ return (typeof value === "object" && value !== null && "type" in value && typeof value.type === "string");
5
+ }
6
+ function enclosingTypeScope(node) {
7
+ let current = node.parent;
8
+ while (current !== null) {
9
+ if (current.type === "Program" ||
10
+ current.type === "BlockStatement" ||
11
+ current.type === "TSModuleBlock" ||
12
+ current.type === "StaticBlock" ||
13
+ current.type === "SwitchStatement") {
14
+ return current;
15
+ }
16
+ current = current.parent;
17
+ }
18
+ return node;
19
+ }
20
+ function declaredTypeBinding(node) {
21
+ if (node.type === "TSTypeAliasDeclaration") {
22
+ return { alias: node, name: node.id.name };
23
+ }
24
+ if (node.type === "TSInterfaceDeclaration" ||
25
+ node.type === "TSEnumDeclaration" ||
26
+ node.type === "ClassDeclaration" ||
27
+ node.type === "ClassExpression") {
28
+ return node.id === null ? null : { alias: null, name: node.id.name };
29
+ }
30
+ if (node.type === "ImportSpecifier" ||
31
+ node.type === "ImportDefaultSpecifier" ||
32
+ node.type === "ImportNamespaceSpecifier") {
33
+ return { alias: null, name: node.local.name };
34
+ }
35
+ return null;
36
+ }
37
+ function collectTypeBindings(node, visitorKeys, bindingsByName, aliases) {
38
+ const declared = declaredTypeBinding(node);
39
+ if (declared !== null) {
40
+ const bindings = bindingsByName.get(declared.name) ?? [];
41
+ bindings.push({ ...declared, scope: enclosingTypeScope(node) });
42
+ bindingsByName.set(declared.name, bindings);
43
+ if (declared.alias !== null)
44
+ aliases.push(declared.alias);
45
+ }
46
+ // SAFETY: Oxlint's visitor keys identify only ESTree child-node properties.
47
+ const fields = node;
48
+ for (const key of visitorKeys[node.type] ?? []) {
49
+ const value = fields[key];
50
+ if (isNode(value)) {
51
+ collectTypeBindings(value, visitorKeys, bindingsByName, aliases);
52
+ continue;
53
+ }
54
+ if (!Array.isArray(value))
55
+ continue;
56
+ for (const child of value) {
57
+ if (isNode(child)) {
58
+ collectTypeBindings(child, visitorKeys, bindingsByName, aliases);
59
+ }
60
+ }
61
+ }
62
+ }
63
+ /** Collect every lexical type alias and competing type binding in a program. */
64
+ export function createTypeAliasEnvironment(program, visitorKeys) {
65
+ const cached = environmentsByProgram.get(program);
66
+ if (cached !== undefined)
67
+ return cached;
68
+ const bindingsByName = new Map();
69
+ const aliases = [];
70
+ collectTypeBindings(program, visitorKeys, bindingsByName, aliases);
71
+ const environment = { aliases, bindingsByName, visitorKeys };
72
+ environmentsByProgram.set(program, environment);
73
+ return environment;
74
+ }
75
+ function ancestorDistance(ancestor, node) {
76
+ let current = node;
77
+ let distance = 0;
78
+ while (current !== null) {
79
+ if (current === ancestor)
80
+ return distance;
81
+ current = current.parent;
82
+ distance += 1;
83
+ }
84
+ return null;
85
+ }
86
+ function nearestTypeBindings(name, use, environment) {
87
+ const candidates = environment.bindingsByName.get(name) ?? [];
88
+ let nearestDistance = Number.POSITIVE_INFINITY;
89
+ let nearest = [];
90
+ for (const candidate of candidates) {
91
+ const distance = ancestorDistance(candidate.scope, use);
92
+ if (distance === null || distance > nearestDistance)
93
+ continue;
94
+ if (distance === nearestDistance) {
95
+ nearest.push(candidate);
96
+ continue;
97
+ }
98
+ nearestDistance = distance;
99
+ nearest = [candidate];
100
+ }
101
+ return nearest;
102
+ }
103
+ /** Resolve the nearest visible alias with this name, respecting lexical shadowing. */
104
+ export function visibleTypeAlias(name, use, environment) {
105
+ if (lexicalTypeParameterNames(use, environment.visitorKeys).has(name))
106
+ return null;
107
+ const bindings = nearestTypeBindings(name, use, environment);
108
+ return bindings.length === 1 ? (bindings[0]?.alias ?? null) : null;
109
+ }
110
+ /** Return whether a local declaration shadows a built-in type at this use. */
111
+ export function hasVisibleTypeBinding(name, use, environment) {
112
+ return (lexicalTypeParameterNames(use, environment.visitorKeys).has(name) ||
113
+ nearestTypeBindings(name, use, environment).length > 0);
114
+ }
package/package.json CHANGED
@@ -1,6 +1,64 @@
1
1
  {
2
2
  "name": "oxlint-plugin-samdotci",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Shared Oxlint rules for TypeScript and Effect.",
5
+ "license": "MIT",
6
+ "repository": {
7
+ "type": "git",
8
+ "url": "git+https://github.com/samdotci/oxlint-plugin-samdotci.git"
9
+ },
10
+ "files": [
11
+ "dist"
12
+ ],
13
+ "type": "module",
14
+ "exports": {
15
+ ".": {
16
+ "types": "./dist/index.d.ts",
17
+ "default": "./dist/index.js"
18
+ },
19
+ "./package.json": "./package.json"
20
+ },
21
+ "scripts": {
22
+ "prepare": "bun run effect-tsgo patch --oxlint && bun run lefthook install",
23
+ "build": "tsc -p tsconfig.build.json",
24
+ "prepack": "bun run build",
25
+ "verify": "bun run fmt:check && bun run lint && bun run test && bun run fallow audit",
26
+ "lint": "oxlint --deny-warnings",
27
+ "lint:fix": "oxlint --fix",
28
+ "fmt": "oxfmt",
29
+ "fmt:check": "oxfmt --check",
30
+ "test": "bun run build && vitest run",
31
+ "fallow": "./bin/fallow"
32
+ },
33
+ "dependencies": {
34
+ "effect": "^4.1.2",
35
+ "effect-oxlint": "^0.5.1"
36
+ },
37
+ "devDependencies": {
38
+ "@commitlint/cli": "^21.2.4",
39
+ "@commitlint/config-conventional": "^21.2.4",
40
+ "@effect/tsgo": "^0.67.4",
41
+ "@types/bun": "^1.4.4",
42
+ "@vitest/coverage-v8": "^5.1.0",
43
+ "fallow": "^3.43.1",
44
+ "fallow-type-aware": "^3.43.1",
45
+ "lefthook": "2.2.2",
46
+ "oxfmt": "^0.76.0",
47
+ "oxlint": "1.91.0",
48
+ "oxlint-tsgolint": "7.0.2005",
49
+ "typescript": "7.0.2",
50
+ "vitest": "^5.1.0"
51
+ },
52
+ "peerDependencies": {
53
+ "oxlint": "^1.91.0"
54
+ },
55
+ "optionalDependencies": {
56
+ "@fallow-cli/darwin-arm64": "^3.43.1",
57
+ "@fallow-cli/darwin-x64": "^3.43.1",
58
+ "@fallow-cli/linux-arm64-gnu": "^3.43.1",
59
+ "@fallow-cli/linux-x64-gnu": "^3.43.1"
60
+ },
61
+ "engines": {
62
+ "node": "^20.19.0 || >=22.12.0"
63
+ }
64
+ }