@ref0/workspace 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.
- package/LICENSE +201 -0
- package/README.md +69 -2
- package/dist/bin.d.mts +1 -0
- package/dist/bin.mjs +30 -0
- package/dist/docs-budget.mjs +122 -0
- package/dist/lint/anti-slop/LICENSE +21 -0
- package/dist/lint/anti-slop/UPSTREAM.md +11 -0
- package/dist/lint/anti-slop/effect/plugin.d.mts +3 -0
- package/dist/lint/anti-slop/effect/plugin.mjs +19 -0
- package/dist/lint/anti-slop/effect/rules/no-manual-effect-error-tag.mjs +33 -0
- package/dist/lint/anti-slop/effect/rules/no-manual-tag-comparison.mjs +32 -0
- package/dist/lint/anti-slop/effect/rules/no-manual-tagged-construction.mjs +22 -0
- package/dist/lint/anti-slop/effect/rules/no-service-constructor-imports.mjs +36 -0
- package/dist/lint/anti-slop/effect/rules/prefer-effect-match.mjs +40 -0
- package/dist/lint/anti-slop/effect/shared/tagged-values.mjs +40 -0
- package/dist/lint/anti-slop/plugin.d.mts +3 -0
- package/dist/lint/anti-slop/plugin.mjs +45 -0
- package/dist/lint/anti-slop/rules/no-array-filter-map.mjs +31 -0
- package/dist/lint/anti-slop/rules/no-chained-type-assertions.mjs +56 -0
- package/dist/lint/anti-slop/rules/no-conditional-empty-object-spread.mjs +32 -0
- package/dist/lint/anti-slop/rules/no-known-value-widening.mjs +236 -0
- package/dist/lint/anti-slop/rules/no-module-mocking.mjs +48 -0
- package/dist/lint/anti-slop/rules/no-object-parameters.mjs +48 -0
- package/dist/lint/anti-slop/rules/no-reduce-accumulator-copy.mjs +85 -0
- package/dist/lint/anti-slop/rules/no-reflect-apply.mjs +21 -0
- package/dist/lint/anti-slop/rules/no-reflect-get.mjs +21 -0
- package/dist/lint/anti-slop/rules/no-runtime-typeof.mjs +52 -0
- package/dist/lint/anti-slop/rules/no-shape-in-symbol-names.mjs +36 -0
- package/dist/lint/anti-slop/rules/no-unknown-parameters.mjs +44 -0
- package/dist/lint/anti-slop/rules/no-unknown-returns.mjs +47 -0
- package/dist/lint/anti-slop/rules/no-unknown-type-aliases.mjs +33 -0
- package/dist/lint/anti-slop/rules/no-unsafe-dictionary-type.mjs +121 -0
- package/dist/lint/anti-slop/rules/no-widen-then-assert.mjs +191 -0
- package/dist/lint/anti-slop/rules/require-readable-spacing.mjs +86 -0
- package/dist/lint/anti-slop/rules/require-safety-comment-for-type-assertion.mjs +82 -0
- package/dist/lint/anti-slop/shared/array-method.mjs +70 -0
- package/dist/lint/anti-slop/shared/child-nodes.mjs +24 -0
- package/dist/lint/anti-slop/shared/dictionary-types.mjs +248 -0
- package/dist/lint/anti-slop/shared/function-parameters.mjs +25 -0
- package/dist/lint/anti-slop/shared/lexical-type-parameters.mjs +21 -0
- package/dist/lint/anti-slop/shared/reflect-method.mjs +16 -0
- package/dist/lint/anti-slop/shared/scope.mjs +12 -0
- package/dist/lint/anti-slop/shared/string-literal.mjs +6 -0
- package/dist/lint/anti-slop/shared/type-alias-resolution.mjs +134 -0
- package/dist/lint/anti-slop/vendor/eslint-stylistic/LICENSE +22 -0
- package/dist/lint/anti-slop/vendor/eslint-stylistic/UPSTREAM.md +29 -0
- package/dist/lint/anti-slop/vendor/eslint-stylistic/padding-line-ast.mjs +32 -0
- package/dist/lint/anti-slop/vendor/eslint-stylistic/padding-line-between-statements.mjs +608 -0
- package/dist/lint/config.d.mts +20 -0
- package/dist/lint/config.mjs +135 -0
- package/dist/lint/generic/effect-encoding.mjs +45 -0
- package/dist/lint/generic/entry-reexports.mjs +81 -0
- package/dist/lint/generic/env-at-entrypoints.mjs +65 -0
- package/dist/lint/generic/export-type-side-effects.mjs +22 -0
- package/dist/lint/generic/index-barrel.mjs +14 -0
- package/dist/lint/generic/log-messages.mjs +39 -0
- package/dist/lint/generic/plugin.d.mts +6 -0
- package/dist/lint/generic/plugin.mjs +26 -0
- package/dist/lint/generic/seeded-properties.mjs +78 -0
- package/dist/lint/plugins.d.mts +9 -0
- package/dist/lint/plugins.mjs +14 -0
- package/dist/lint/t3code/LICENSE +21 -0
- package/dist/lint/t3code/UPSTREAM.md +24 -0
- package/dist/lint/t3code/plugin.d.mts +2 -0
- package/dist/lint/t3code/plugin.mjs +20 -0
- package/dist/lint/t3code/rules/no-global-process-runtime.mjs +76 -0
- package/dist/lint/t3code/rules/no-inline-schema-compile.mjs +112 -0
- package/dist/lint/t3code/rules/no-manual-effect-runtime-in-tests.mjs +67 -0
- package/dist/lint/t3code/rules/no-native-title-tooltip.mjs +33 -0
- package/dist/lint/t3code/rules/no-test-in-loop.mjs +69 -0
- package/dist/lint/t3code/rules/no-unscoped-has.mjs +147 -0
- package/dist/lint/t3code/utils.mjs +21 -0
- package/package.json +52 -3
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
2
|
+
import * as Schema from "effect/Schema";
|
|
3
|
+
// src/lint/anti-slop/rules/require-safety-comment-for-type-assertion.ts
|
|
4
|
+
const DEFAULT_SAFETY_MARKERS = ["SAFETY"];
|
|
5
|
+
const commentOwnerKinds = /* @__PURE__ */ new Set([
|
|
6
|
+
"ExpressionStatement",
|
|
7
|
+
"PropertyDefinition",
|
|
8
|
+
"ReturnStatement",
|
|
9
|
+
"ThrowStatement",
|
|
10
|
+
"VariableDeclaration"
|
|
11
|
+
]);
|
|
12
|
+
function isConstAssertion(node) {
|
|
13
|
+
return node.typeAnnotation.type === "TSTypeReference" && node.typeAnnotation.typeName.type === "Identifier" && node.typeAnnotation.typeName.name === "const";
|
|
14
|
+
}
|
|
15
|
+
const hasMarkers = Schema.is(Schema.Struct({ markers: Schema.Array(Schema.String) }));
|
|
16
|
+
function configuredSafetyMarkers(option) {
|
|
17
|
+
if (!hasMarkers(option)) return DEFAULT_SAFETY_MARKERS;
|
|
18
|
+
const markers = option.markers.flatMap((marker) => marker.trim().length > 0 ? [marker.trim()] : []);
|
|
19
|
+
return markers.length > 0 ? markers : DEFAULT_SAFETY_MARKERS;
|
|
20
|
+
}
|
|
21
|
+
function markerPattern(markers) {
|
|
22
|
+
const alternation = markers.map((marker) => marker.replaceAll(/[.*+?^${}()|[\]\\]/gu, String.raw`\$&`)).join("|");
|
|
23
|
+
return new RegExp(String.raw`(?:^|[^\p{L}\p{N}_])(?:${alternation})\s*:\s*\S`, "u");
|
|
24
|
+
}
|
|
25
|
+
function hasSafetyJustificationBefore(sourceCode, owner, assertion, pattern) {
|
|
26
|
+
return sourceCode.getCommentsBefore(owner).some((comment) => comment.end <= assertion.start && pattern.test(comment.value));
|
|
27
|
+
}
|
|
28
|
+
function hasSafetyComment(sourceCode, node, pattern) {
|
|
29
|
+
let current = node;
|
|
30
|
+
while (true) {
|
|
31
|
+
if (hasSafetyJustificationBefore(sourceCode, current, node, pattern)) return true;
|
|
32
|
+
if (commentOwnerKinds.has(current.type)) {
|
|
33
|
+
const exportDeclaration = current.parent;
|
|
34
|
+
return exportDeclaration.type === "ExportNamedDeclaration" && exportDeclaration.declaration === current && hasSafetyJustificationBefore(sourceCode, exportDeclaration, node, pattern);
|
|
35
|
+
}
|
|
36
|
+
if (current.parent.type === "Program") return false;
|
|
37
|
+
current = current.parent;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
/** Require every non-const type assertion to state the invariant TypeScript cannot express. */
|
|
41
|
+
const requireSafetyCommentForTypeAssertionRule = defineRule({
|
|
42
|
+
meta: {
|
|
43
|
+
type: "problem",
|
|
44
|
+
docs: { description: "Require a nearby SAFETY comment for every TypeScript type assertion except const assertions." },
|
|
45
|
+
messages: { missingSafetyComment: "This type assertion has no `{{marker}}:` justification. State the checked invariant immediately before the assertion or its containing statement." },
|
|
46
|
+
schema: [{
|
|
47
|
+
type: "object",
|
|
48
|
+
properties: { markers: {
|
|
49
|
+
type: "array",
|
|
50
|
+
items: {
|
|
51
|
+
type: "string",
|
|
52
|
+
minLength: 1
|
|
53
|
+
},
|
|
54
|
+
minItems: 1,
|
|
55
|
+
uniqueItems: true
|
|
56
|
+
} },
|
|
57
|
+
additionalProperties: false
|
|
58
|
+
}],
|
|
59
|
+
defaultOptions: [{ markers: ["SAFETY"] }]
|
|
60
|
+
},
|
|
61
|
+
createOnce(context) {
|
|
62
|
+
const patterns = /* @__PURE__ */ new Map();
|
|
63
|
+
const checkAssertion = (node) => {
|
|
64
|
+
if (isConstAssertion(node)) return;
|
|
65
|
+
const markers = configuredSafetyMarkers(context.options?.[0]);
|
|
66
|
+
const patternKey = markers.join("\0");
|
|
67
|
+
const pattern = patterns.get(patternKey) ?? markerPattern(markers);
|
|
68
|
+
patterns.set(patternKey, pattern);
|
|
69
|
+
if (hasSafetyComment(context.sourceCode, node, pattern)) return;
|
|
70
|
+
context.report({
|
|
71
|
+
node,
|
|
72
|
+
messageId: "missingSafetyComment",
|
|
73
|
+
data: { marker: markers[0] ?? DEFAULT_SAFETY_MARKERS[0] }
|
|
74
|
+
});
|
|
75
|
+
};
|
|
76
|
+
return {
|
|
77
|
+
TSAsExpression: checkAssertion,
|
|
78
|
+
TSTypeAssertion: checkAssertion
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
});
|
|
82
|
+
export { requireSafetyCommentForTypeAssertionRule };
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
import { isStringLiteral } from "./string-literal.mjs";
|
|
2
|
+
// src/lint/anti-slop/shared/array-method.ts
|
|
3
|
+
/** Unwrap syntax-only wrappers when inspecting array methods and accumulator references. */
|
|
4
|
+
function unwrapArrayExpression(node) {
|
|
5
|
+
while (node.type === "ParenthesizedExpression" || node.type === "ChainExpression" || node.type === "TSAsExpression" || node.type === "TSTypeAssertion" || node.type === "TSNonNullExpression" || node.type === "TSSatisfiesExpression") node = node.expression;
|
|
6
|
+
return node;
|
|
7
|
+
}
|
|
8
|
+
/** Resolve a local binding by scope, not by identifier spelling. */
|
|
9
|
+
function resolveArrayBinding(sourceCode, node) {
|
|
10
|
+
node = unwrapArrayExpression(node);
|
|
11
|
+
if (node.type !== "Identifier") return null;
|
|
12
|
+
let scope = sourceCode.getScope(node);
|
|
13
|
+
while (scope !== null) {
|
|
14
|
+
const variable = scope.set.get(node.name);
|
|
15
|
+
if (variable !== void 0) return variable;
|
|
16
|
+
scope = scope.upper;
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
/** Read static method names, including computed string literals, without evaluating expressions. */
|
|
21
|
+
function arrayMethodTarget(node) {
|
|
22
|
+
node = unwrapArrayExpression(node);
|
|
23
|
+
if (node.type !== "MemberExpression") return null;
|
|
24
|
+
const property = node.property;
|
|
25
|
+
if (!node.computed && property.type === "Identifier") return {
|
|
26
|
+
name: property.name,
|
|
27
|
+
object: node.object
|
|
28
|
+
};
|
|
29
|
+
if (node.computed && isStringLiteral(property)) return {
|
|
30
|
+
name: property.value,
|
|
31
|
+
object: node.object
|
|
32
|
+
};
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
35
|
+
function isArrayAnnotation(type) {
|
|
36
|
+
if (type.type === "TSArrayType" || type.type === "TSTupleType") return true;
|
|
37
|
+
if (type.type === "TSParenthesizedType") return isArrayAnnotation(type.typeAnnotation);
|
|
38
|
+
if (type.type === "TSTypeOperator" && type.operator === "readonly") return isArrayAnnotation(type.typeAnnotation);
|
|
39
|
+
return type.type === "TSTypeReference" && type.typeName.type === "Identifier" && (type.typeName.name === "Array" || type.typeName.name === "ReadonlyArray");
|
|
40
|
+
}
|
|
41
|
+
/** Recognize local array evidence; unknown receivers and iterator pipelines are deliberately excluded. */
|
|
42
|
+
function isKnownArrayExpression(sourceCode, node, visited = /* @__PURE__ */ new Set()) {
|
|
43
|
+
node = unwrapArrayExpression(node);
|
|
44
|
+
if (node.type === "ArrayExpression") return true;
|
|
45
|
+
if (node.type === "CallExpression") {
|
|
46
|
+
const method = arrayMethodTarget(node.callee);
|
|
47
|
+
return method !== null && [
|
|
48
|
+
"map",
|
|
49
|
+
"filter",
|
|
50
|
+
"flatMap",
|
|
51
|
+
"slice",
|
|
52
|
+
"concat",
|
|
53
|
+
"toSorted",
|
|
54
|
+
"toReversed",
|
|
55
|
+
"toSpliced"
|
|
56
|
+
].includes(method.name) && isKnownArrayExpression(sourceCode, method.object, visited);
|
|
57
|
+
}
|
|
58
|
+
if (node.type !== "Identifier") return false;
|
|
59
|
+
const variable = resolveArrayBinding(sourceCode, node);
|
|
60
|
+
if (variable === null || visited.has(variable)) return false;
|
|
61
|
+
visited.add(variable);
|
|
62
|
+
if (variable.references.some((reference) => reference.isWrite() && !reference.init)) return false;
|
|
63
|
+
for (const identifier of variable.identifiers) {
|
|
64
|
+
const annotation = identifier.typeAnnotation?.typeAnnotation;
|
|
65
|
+
if (annotation !== void 0) return isArrayAnnotation(annotation);
|
|
66
|
+
}
|
|
67
|
+
for (const definition of variable.defs) if (definition.type === "Variable" && definition.node.type === "VariableDeclarator" && definition.node.id.type === "Identifier" && definition.node.init !== null && definition.node.parent.type === "VariableDeclaration" && definition.node.parent.kind === "const") return isKnownArrayExpression(sourceCode, definition.node.init, visited);
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
export { arrayMethodTarget, isKnownArrayExpression, resolveArrayBinding, unwrapArrayExpression };
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// src/lint/anti-slop/shared/child-nodes.ts
|
|
2
|
+
/**
|
|
3
|
+
* A visitor key holds a node, `null`, or an array of them, and only a node is an object, with a
|
|
4
|
+
* `type`.
|
|
5
|
+
*/
|
|
6
|
+
function isNode(value) {
|
|
7
|
+
return value instanceof Object && "type" in value;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The nodes under `node`'s visitor keys, in the order Oxlint visits them. Rules walk every node of
|
|
11
|
+
* every file through this, so it tests each value as cheaply as it can: a Schema guard here took
|
|
12
|
+
* most of a second of each lint run.
|
|
13
|
+
*/
|
|
14
|
+
function childNodes(node, visitorKeys) {
|
|
15
|
+
const children = [];
|
|
16
|
+
for (const key of visitorKeys[node.type] ?? []) {
|
|
17
|
+
const value = Object.getOwnPropertyDescriptor(node, key)?.value;
|
|
18
|
+
if (Array.isArray(value)) {
|
|
19
|
+
for (const item of value) if (isNode(item)) children.push(item);
|
|
20
|
+
} else if (isNode(value)) children.push(value);
|
|
21
|
+
}
|
|
22
|
+
return children;
|
|
23
|
+
}
|
|
24
|
+
export { childNodes };
|
|
@@ -0,0 +1,248 @@
|
|
|
1
|
+
import { createTypeAliasEnvironment, hasVisibleTypeBinding, visibleTypeAlias } from "./type-alias-resolution.mjs";
|
|
2
|
+
// src/lint/anti-slop/shared/dictionary-types.ts
|
|
3
|
+
const BUILT_INS = /* @__PURE__ */ new Set([
|
|
4
|
+
"Record",
|
|
5
|
+
"Readonly",
|
|
6
|
+
"Partial",
|
|
7
|
+
"Required",
|
|
8
|
+
"Pick",
|
|
9
|
+
"Omit",
|
|
10
|
+
"PropertyKey",
|
|
11
|
+
"NonNullable"
|
|
12
|
+
]);
|
|
13
|
+
const TRANSPARENT_WRAPPERS = /* @__PURE__ */ new Set([
|
|
14
|
+
"Readonly",
|
|
15
|
+
"Partial",
|
|
16
|
+
"Required",
|
|
17
|
+
"NonNullable"
|
|
18
|
+
]);
|
|
19
|
+
function declaredStatement(statement) {
|
|
20
|
+
return statement.type === "ExportNamedDeclaration" || statement.type === "ExportDefaultDeclaration" ? statement.declaration ?? null : statement;
|
|
21
|
+
}
|
|
22
|
+
function createTypeEnvironment(program, visitorKeys) {
|
|
23
|
+
const interfaces = /* @__PURE__ */ new Map();
|
|
24
|
+
for (const statement of program.body) {
|
|
25
|
+
const declaration = declaredStatement(statement);
|
|
26
|
+
if (declaration?.type !== "TSInterfaceDeclaration") continue;
|
|
27
|
+
const declarations = interfaces.get(declaration.id.name) ?? [];
|
|
28
|
+
declarations.push(declaration);
|
|
29
|
+
interfaces.set(declaration.id.name, declarations);
|
|
30
|
+
}
|
|
31
|
+
return {
|
|
32
|
+
interfaces,
|
|
33
|
+
typeAliases: createTypeAliasEnvironment(program, visitorKeys)
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
function typeReferenceName(type) {
|
|
37
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
38
|
+
}
|
|
39
|
+
function isBuiltIn(name, use, environment) {
|
|
40
|
+
return BUILT_INS.has(name) && !hasVisibleTypeBinding(name, use, environment.typeAliases);
|
|
41
|
+
}
|
|
42
|
+
function isUnappliedReferenceTo(type, name) {
|
|
43
|
+
const unwrapped = unwrapTransparentType(type);
|
|
44
|
+
return unwrapped.type === "TSTypeReference" && typeReferenceName(unwrapped) === name && (unwrapped.typeArguments === null || unwrapped.typeArguments === void 0 || unwrapped.typeArguments.params.length === 0);
|
|
45
|
+
}
|
|
46
|
+
function unwrapTransparentType(type) {
|
|
47
|
+
let current = type;
|
|
48
|
+
while (current.type === "TSParenthesizedType" || current.type === "TSTypeOperator" && current.operator === "readonly") current = current.typeAnnotation;
|
|
49
|
+
return current;
|
|
50
|
+
}
|
|
51
|
+
function isNeverType(type) {
|
|
52
|
+
return unwrapTransparentType(type).type === "TSNeverKeyword";
|
|
53
|
+
}
|
|
54
|
+
function isEffectivelyEmptyMember(member) {
|
|
55
|
+
return member.type === "TSPropertySignature" && member.optional === true && member.typeAnnotation !== null && member.typeAnnotation !== void 0 && isNeverType(member.typeAnnotation.typeAnnotation);
|
|
56
|
+
}
|
|
57
|
+
function isEffectivelyEmptyTypeLiteral(type) {
|
|
58
|
+
return type.members.length === 0 || type.members.every(isEffectivelyEmptyMember);
|
|
59
|
+
}
|
|
60
|
+
function isEffectivelyEmptyInterface(declarations) {
|
|
61
|
+
if (declarations.length !== 1) return false;
|
|
62
|
+
const [type] = declarations;
|
|
63
|
+
return type !== void 0 && type.extends.length === 0 && (type.body.body.length === 0 || type.body.body.every(isEffectivelyEmptyMember));
|
|
64
|
+
}
|
|
65
|
+
function resolvedSubstitutionArgument(type, base, resolving = /* @__PURE__ */ new Set()) {
|
|
66
|
+
const unwrapped = unwrapTransparentType(type);
|
|
67
|
+
if (unwrapped.type !== "TSTypeReference") return type;
|
|
68
|
+
const name = typeReferenceName(unwrapped);
|
|
69
|
+
if (name === null || resolving.has(name)) return type;
|
|
70
|
+
const substitution = base.get(name);
|
|
71
|
+
if (substitution === void 0) return type;
|
|
72
|
+
const nextResolving = new Set(resolving);
|
|
73
|
+
nextResolving.add(name);
|
|
74
|
+
return resolvedSubstitutionArgument(substitution, base, nextResolving);
|
|
75
|
+
}
|
|
76
|
+
function aliasSubstitution(alias, type, base) {
|
|
77
|
+
const parameters = alias.typeParameters?.params ?? [];
|
|
78
|
+
const arguments_ = type.typeArguments?.params ?? [];
|
|
79
|
+
const next = new Map(base);
|
|
80
|
+
for (const [index, parameter] of parameters.entries()) {
|
|
81
|
+
const argument = arguments_[index] ?? parameter.default;
|
|
82
|
+
if (argument === null || argument === void 0) return null;
|
|
83
|
+
next.set(parameter.name.name, resolvedSubstitutionArgument(argument, next));
|
|
84
|
+
}
|
|
85
|
+
return next;
|
|
86
|
+
}
|
|
87
|
+
function unsafeDirectValue(type, environment, substitutions, resolvingAliases) {
|
|
88
|
+
const unwrapped = unwrapTransparentType(type);
|
|
89
|
+
if (unwrapped.type === "TSUnknownKeyword") return "unknown";
|
|
90
|
+
if (unwrapped.type === "TSAnyKeyword") return "any";
|
|
91
|
+
if (unwrapped.type === "TSObjectKeyword") return "object";
|
|
92
|
+
if (unwrapped.type === "TSTypeLiteral" && isEffectivelyEmptyTypeLiteral(unwrapped)) return "empty-object";
|
|
93
|
+
if (unwrapped.type === "TSUnionType") return unwrapped.types.some((member) => unsafeDirectValue(member, environment, substitutions, resolvingAliases) !== null) ? "union" : null;
|
|
94
|
+
if (unwrapped.type === "TSIntersectionType") {
|
|
95
|
+
const unsafeMembers = unwrapped.types.map((member) => unsafeDirectValue(member, environment, substitutions, resolvingAliases));
|
|
96
|
+
if (unsafeMembers.includes("any")) return "any";
|
|
97
|
+
const [first] = unsafeMembers;
|
|
98
|
+
return first !== void 0 && unsafeMembers.every((member) => member !== null) ? first : null;
|
|
99
|
+
}
|
|
100
|
+
if (unwrapped.type !== "TSTypeReference") return null;
|
|
101
|
+
const name = typeReferenceName(unwrapped);
|
|
102
|
+
if (name === null) return null;
|
|
103
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn(name, unwrapped, environment)) {
|
|
104
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
105
|
+
return wrapped === void 0 ? null : unsafeDirectValue(wrapped, environment, substitutions, resolvingAliases);
|
|
106
|
+
}
|
|
107
|
+
const substitution = substitutions.get(name);
|
|
108
|
+
if (substitution !== void 0) return isUnappliedReferenceTo(substitution, name) ? null : unsafeDirectValue(substitution, environment, substitutions, resolvingAliases);
|
|
109
|
+
const interfaceDeclarations = environment.interfaces.get(name);
|
|
110
|
+
if (interfaceDeclarations !== void 0) return isEffectivelyEmptyInterface(interfaceDeclarations) ? "empty-object" : null;
|
|
111
|
+
const alias = visibleTypeAlias(name, unwrapped, environment.typeAliases);
|
|
112
|
+
if (alias === null || resolvingAliases.has(name)) return null;
|
|
113
|
+
const nextSubstitutions = aliasSubstitution(alias, unwrapped, substitutions);
|
|
114
|
+
if (nextSubstitutions === null) return null;
|
|
115
|
+
const nextResolving = new Set(resolvingAliases);
|
|
116
|
+
nextResolving.add(name);
|
|
117
|
+
return unsafeDirectValue(alias.typeAnnotation, environment, nextSubstitutions, nextResolving);
|
|
118
|
+
}
|
|
119
|
+
function dictionaryValueTypes(type, environment, substitutions, resolvingAliases) {
|
|
120
|
+
const unwrapped = unwrapTransparentType(type);
|
|
121
|
+
if (unwrapped.type === "TSTypeLiteral") return unwrapped.members.flatMap((member) => member.type === "TSIndexSignature" && member.typeAnnotation !== null ? [{
|
|
122
|
+
type: member.typeAnnotation.typeAnnotation,
|
|
123
|
+
substitutions
|
|
124
|
+
}] : []);
|
|
125
|
+
if (unwrapped.type === "TSMappedType") return unwrapped.typeAnnotation === null ? [] : [{
|
|
126
|
+
type: unwrapped.typeAnnotation,
|
|
127
|
+
substitutions
|
|
128
|
+
}];
|
|
129
|
+
if (unwrapped.type !== "TSTypeReference") return [];
|
|
130
|
+
const name = typeReferenceName(unwrapped);
|
|
131
|
+
if (name === null) return [];
|
|
132
|
+
const substitution = substitutions.get(name);
|
|
133
|
+
if (substitution !== void 0) return isUnappliedReferenceTo(substitution, name) ? [] : dictionaryValueTypes(substitution, environment, substitutions, resolvingAliases);
|
|
134
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn(name, unwrapped, environment)) {
|
|
135
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
136
|
+
return wrapped === void 0 ? [] : dictionaryValueTypes(wrapped, environment, substitutions, resolvingAliases);
|
|
137
|
+
}
|
|
138
|
+
if (name === "Record" && isBuiltIn(name, unwrapped, environment)) {
|
|
139
|
+
const value = unwrapped.typeArguments?.params[1] ?? null;
|
|
140
|
+
return value === null ? [] : [{
|
|
141
|
+
type: value,
|
|
142
|
+
substitutions
|
|
143
|
+
}];
|
|
144
|
+
}
|
|
145
|
+
if ((name === "Pick" || name === "Omit") && isBuiltIn(name, unwrapped, environment)) {
|
|
146
|
+
const source = unwrapped.typeArguments?.params[0];
|
|
147
|
+
return source === void 0 ? [] : dictionaryValueTypes(source, environment, substitutions, resolvingAliases);
|
|
148
|
+
}
|
|
149
|
+
const alias = visibleTypeAlias(name, unwrapped, environment.typeAliases);
|
|
150
|
+
if (alias === null || resolvingAliases.has(name)) return [];
|
|
151
|
+
const nextSubstitutions = aliasSubstitution(alias, unwrapped, substitutions);
|
|
152
|
+
if (nextSubstitutions === null) return [];
|
|
153
|
+
const nextResolving = new Set(resolvingAliases);
|
|
154
|
+
nextResolving.add(name);
|
|
155
|
+
return dictionaryValueTypes(alias.typeAnnotation, environment, nextSubstitutions, nextResolving);
|
|
156
|
+
}
|
|
157
|
+
function classifyUnsafeDictionaryValue(valueType, environment) {
|
|
158
|
+
const unsafeValue = unsafeDirectValue(valueType, environment, /* @__PURE__ */ new Map(), /* @__PURE__ */ new Set());
|
|
159
|
+
return unsafeValue === null ? null : {
|
|
160
|
+
kind: "unsafe-dictionary",
|
|
161
|
+
unsafeValue
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
function classifyUnsafeDictionary(type, environment) {
|
|
165
|
+
for (const valueType of dictionaryValueTypes(type, environment, /* @__PURE__ */ new Map(), /* @__PURE__ */ new Set())) {
|
|
166
|
+
const unsafeValue = unsafeDirectValue(valueType.type, environment, valueType.substitutions, /* @__PURE__ */ new Set());
|
|
167
|
+
if (unsafeValue !== null) return {
|
|
168
|
+
kind: "unsafe-dictionary",
|
|
169
|
+
unsafeValue
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
function classifyWideningTarget(type, environment) {
|
|
175
|
+
const unwrapped = unwrapTransparentType(type);
|
|
176
|
+
if (unwrapped.type === "TSUnknownKeyword") return { kind: "unknown" };
|
|
177
|
+
if (unwrapped.type === "TSObjectKeyword") return { kind: "object" };
|
|
178
|
+
if (unwrapped.type === "TSTypeLiteral") return unwrapped.members.some((member) => member.type === "TSIndexSignature") ? { kind: "open dictionary" } : unwrapped.members.length > 0 ? { kind: "anonymous object" } : null;
|
|
179
|
+
if (unwrapped.type === "TSMappedType") return { kind: "open dictionary" };
|
|
180
|
+
if (unwrapped.type !== "TSTypeReference") return null;
|
|
181
|
+
const name = typeReferenceName(unwrapped);
|
|
182
|
+
if (name === null) return null;
|
|
183
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn(name, unwrapped, environment)) {
|
|
184
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
185
|
+
return wrapped === void 0 ? null : classifyWideningTarget(wrapped, environment);
|
|
186
|
+
}
|
|
187
|
+
if (name === "Record" && isBuiltIn(name, unwrapped, environment)) return hasBroadRecordKey(unwrapped, environment, /* @__PURE__ */ new Map()) ? { kind: "open dictionary" } : null;
|
|
188
|
+
const alias = visibleTypeAlias(name, unwrapped, environment.typeAliases);
|
|
189
|
+
if (alias === null) return null;
|
|
190
|
+
if ((alias.typeParameters?.params.length ?? 0) > 0) {
|
|
191
|
+
const substitutions = aliasSubstitution(alias, unwrapped, /* @__PURE__ */ new Map());
|
|
192
|
+
return (substitutions === null ? null : classifyAliasBroadTarget(alias.typeAnnotation, environment, substitutions, /* @__PURE__ */ new Set([name])))?.kind === "open dictionary" ? { kind: "generic container" } : null;
|
|
193
|
+
}
|
|
194
|
+
const substitutions = aliasSubstitution(alias, unwrapped, /* @__PURE__ */ new Map());
|
|
195
|
+
if (substitutions === null) return null;
|
|
196
|
+
return classifyAliasBroadTarget(alias.typeAnnotation, environment, substitutions, /* @__PURE__ */ new Set([name]));
|
|
197
|
+
}
|
|
198
|
+
function hasBroadRecordKey(type, environment, substitutions) {
|
|
199
|
+
const key = type.typeArguments?.params[0];
|
|
200
|
+
return key === void 0 || isBroadMappedKey(key, environment, substitutions);
|
|
201
|
+
}
|
|
202
|
+
function isBroadMappedKey(type, environment, substitutions, visitedAliases = /* @__PURE__ */ new Set()) {
|
|
203
|
+
const unwrapped = unwrapTransparentType(type);
|
|
204
|
+
if (unwrapped.type === "TSStringKeyword" || unwrapped.type === "TSNumberKeyword" || unwrapped.type === "TSSymbolKeyword") return true;
|
|
205
|
+
if (unwrapped.type === "TSUnionType") return unwrapped.types.some((member) => isBroadMappedKey(member, environment, substitutions, visitedAliases));
|
|
206
|
+
if (unwrapped.type !== "TSTypeReference") return false;
|
|
207
|
+
const name = typeReferenceName(unwrapped);
|
|
208
|
+
if (name === null) return false;
|
|
209
|
+
const substitution = substitutions.get(name);
|
|
210
|
+
if (substitution !== void 0 && !isUnappliedReferenceTo(substitution, name)) return isBroadMappedKey(substitution, environment, substitutions, visitedAliases);
|
|
211
|
+
if (name === "PropertyKey" && isBuiltIn(name, unwrapped, environment)) return true;
|
|
212
|
+
const alias = visibleTypeAlias(name, unwrapped, environment.typeAliases);
|
|
213
|
+
if (alias === null || (alias.typeParameters?.params.length ?? 0) > 0 || visitedAliases.has(name)) return false;
|
|
214
|
+
const nextVisited = new Set(visitedAliases);
|
|
215
|
+
nextVisited.add(name);
|
|
216
|
+
return isBroadMappedKey(alias.typeAnnotation, environment, substitutions, nextVisited);
|
|
217
|
+
}
|
|
218
|
+
function classifyAliasBroadTarget(type, environment, substitutions, resolvingAliases) {
|
|
219
|
+
const unwrapped = unwrapTransparentType(type);
|
|
220
|
+
if (unwrapped.type === "TSUnknownKeyword") return { kind: "unknown" };
|
|
221
|
+
if (unwrapped.type === "TSObjectKeyword") return { kind: "object" };
|
|
222
|
+
if (unwrapped.type === "TSTypeLiteral") return unwrapped.members.some((member) => member.type === "TSIndexSignature") ? { kind: "open dictionary" } : null;
|
|
223
|
+
if (unwrapped.type === "TSMappedType") return isBroadMappedKey(unwrapped.constraint, environment, substitutions) ? { kind: "open dictionary" } : null;
|
|
224
|
+
if (unwrapped.type !== "TSTypeReference") return null;
|
|
225
|
+
const name = typeReferenceName(unwrapped);
|
|
226
|
+
if (name === null) return null;
|
|
227
|
+
const substitution = substitutions.get(name);
|
|
228
|
+
if (substitution !== void 0) return isUnappliedReferenceTo(substitution, name) ? null : classifyAliasBroadTarget(substitution, environment, substitutions, resolvingAliases);
|
|
229
|
+
if (TRANSPARENT_WRAPPERS.has(name) && isBuiltIn(name, unwrapped, environment)) {
|
|
230
|
+
const wrapped = unwrapped.typeArguments?.params[0];
|
|
231
|
+
return wrapped === void 0 ? null : classifyAliasBroadTarget(wrapped, environment, substitutions, resolvingAliases);
|
|
232
|
+
}
|
|
233
|
+
if (name === "Record" && isBuiltIn(name, unwrapped, environment)) return hasBroadRecordKey(unwrapped, environment, substitutions) ? { kind: "open dictionary" } : null;
|
|
234
|
+
const alias = visibleTypeAlias(name, unwrapped, environment.typeAliases);
|
|
235
|
+
if (alias === null || 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 classifyAliasBroadTarget(alias.typeAnnotation, environment, nextSubstitutions, nextResolving);
|
|
241
|
+
}
|
|
242
|
+
function isKnownEvidenceExpression(expression) {
|
|
243
|
+
let current = expression;
|
|
244
|
+
while (current.type === "ParenthesizedExpression" || current.type === "TSAsExpression" || current.type === "TSTypeAssertion" || current.type === "TSNonNullExpression" || current.type === "TSSatisfiesExpression") current = current.expression;
|
|
245
|
+
if (current.type === "ObjectExpression") return true;
|
|
246
|
+
return current.type === "ArrayExpression" || current.type === "ArrowFunctionExpression" || current.type === "ClassExpression" || current.type === "FunctionExpression" || current.type === "NewExpression" || current.type === "Literal" || current.type === "TemplateLiteral" || current.type === "UnaryExpression";
|
|
247
|
+
}
|
|
248
|
+
export { classifyUnsafeDictionary, classifyUnsafeDictionaryValue, classifyWideningTarget, createTypeEnvironment, isKnownEvidenceExpression };
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
// src/lint/anti-slop/shared/function-parameters.ts
|
|
2
|
+
/** Return whether a type is or contains TypeScript's absorbing unknown top type. */
|
|
3
|
+
function containsUnknownType(type) {
|
|
4
|
+
if (type.type === "TSUnknownKeyword") return true;
|
|
5
|
+
if (type.type === "TSParenthesizedType") return containsUnknownType(type.typeAnnotation);
|
|
6
|
+
return type.type === "TSUnionType" && type.types.some(containsUnknownType);
|
|
7
|
+
}
|
|
8
|
+
/** Return the TypeScript annotation attached to a function parameter or its wrapped binding. */
|
|
9
|
+
function functionParameterTypeAnnotation(parameter) {
|
|
10
|
+
if (parameter.type === "TSParameterProperty") return functionParameterTypeAnnotation(parameter.parameter);
|
|
11
|
+
if (parameter.type === "RestElement") return parameter.typeAnnotation ?? functionParameterTypeAnnotation(parameter.argument);
|
|
12
|
+
if (parameter.type === "AssignmentPattern") return parameter.typeAnnotation ?? functionParameterTypeAnnotation(parameter.left);
|
|
13
|
+
return parameter.typeAnnotation;
|
|
14
|
+
}
|
|
15
|
+
/** Return only a function parameter's local binding, excluding its annotation and default value. */
|
|
16
|
+
function functionParameterBindingName(parameter, sourceCode) {
|
|
17
|
+
if (parameter.type === "TSParameterProperty") return functionParameterBindingName(parameter.parameter, sourceCode);
|
|
18
|
+
if (parameter.type === "AssignmentPattern") return functionParameterBindingName(parameter.left, sourceCode);
|
|
19
|
+
if (parameter.type === "RestElement") return functionParameterBindingName(parameter.argument, sourceCode);
|
|
20
|
+
if (parameter.type === "Identifier") return parameter.name;
|
|
21
|
+
const sourceText = sourceCode.getText(parameter);
|
|
22
|
+
const annotationStart = parameter.typeAnnotation?.start;
|
|
23
|
+
return annotationStart === void 0 ? sourceText : sourceText.slice(0, annotationStart - parameter.start).trimEnd();
|
|
24
|
+
}
|
|
25
|
+
export { containsUnknownType, functionParameterBindingName, functionParameterTypeAnnotation };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { childNodes } from "./child-nodes.mjs";
|
|
2
|
+
// src/lint/anti-slop/shared/lexical-type-parameters.ts
|
|
3
|
+
function collectInferTypeParameterNames(node, visitorKeys, names) {
|
|
4
|
+
if (node.type === "TSInferType") names.add(node.typeParameter.name.name);
|
|
5
|
+
for (const child of childNodes(node, visitorKeys)) collectInferTypeParameterNames(child, visitorKeys, names);
|
|
6
|
+
}
|
|
7
|
+
/** Collect type binders that are in scope at a node and can shadow module aliases. */
|
|
8
|
+
function lexicalTypeParameterNames(node, visitorKeys) {
|
|
9
|
+
const names = /* @__PURE__ */ new Set();
|
|
10
|
+
let descendant = node;
|
|
11
|
+
let current = node;
|
|
12
|
+
while (current !== null && current.type !== "Program") {
|
|
13
|
+
if ("typeParameters" in current) for (const parameter of current.typeParameters?.params ?? []) names.add(parameter.name.name);
|
|
14
|
+
if (current.type === "TSMappedType" && (descendant === current.nameType || descendant === current.typeAnnotation)) names.add(current.key.name);
|
|
15
|
+
if (current.type === "TSConditionalType" && descendant === current.trueType) collectInferTypeParameterNames(current.extendsType, visitorKeys, names);
|
|
16
|
+
descendant = current;
|
|
17
|
+
current = current.parent;
|
|
18
|
+
}
|
|
19
|
+
return names;
|
|
20
|
+
}
|
|
21
|
+
export { lexicalTypeParameterNames };
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { resolveVariable } from "./scope.mjs";
|
|
2
|
+
// src/lint/anti-slop/shared/reflect-method.ts
|
|
3
|
+
function isGlobalReflect(sourceCode, expression) {
|
|
4
|
+
if (expression.type !== "Identifier" || expression.name !== "Reflect") return false;
|
|
5
|
+
if (sourceCode.isGlobalReference(expression)) return true;
|
|
6
|
+
const variable = resolveVariable(sourceCode, expression);
|
|
7
|
+
return variable === null || variable.defs.length === 0;
|
|
8
|
+
}
|
|
9
|
+
/** Reports whether a call target names one method on the global Reflect object. */
|
|
10
|
+
function isGlobalReflectMethodCall(sourceCode, callee, methodName) {
|
|
11
|
+
if (!("property" in callee) || !("object" in callee) || !("computed" in callee)) return false;
|
|
12
|
+
if (!isGlobalReflect(sourceCode, callee.object)) return false;
|
|
13
|
+
const property = callee.property;
|
|
14
|
+
return callee.computed ? property.type === "Literal" && property.value === methodName : property.type === "Identifier" && property.name === methodName;
|
|
15
|
+
}
|
|
16
|
+
export { isGlobalReflectMethodCall };
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
// src/lint/anti-slop/shared/scope.ts
|
|
2
|
+
/** Resolve an identifier to its binding by walking lexical scopes upward. */
|
|
3
|
+
function resolveVariable(sourceCode, identifier) {
|
|
4
|
+
let scope = sourceCode.getScope(identifier);
|
|
5
|
+
while (scope !== null) {
|
|
6
|
+
const variable = scope.set.get(identifier.name);
|
|
7
|
+
if (variable !== void 0) return variable;
|
|
8
|
+
scope = scope.upper;
|
|
9
|
+
}
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
export { resolveVariable };
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import * as Schema from "effect/Schema";
|
|
2
|
+
// src/lint/anti-slop/shared/string-literal.ts
|
|
3
|
+
const isString = Schema.is(Schema.String);
|
|
4
|
+
/** Whether `node` is a `Literal` whose value is a string. */
|
|
5
|
+
const isStringLiteral = (node) => node?.type === "Literal" && isString(node.value);
|
|
6
|
+
export { isStringLiteral };
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { childNodes } from "./child-nodes.mjs";
|
|
2
|
+
import { lexicalTypeParameterNames } from "./lexical-type-parameters.mjs";
|
|
3
|
+
// src/lint/anti-slop/shared/type-alias-resolution.ts
|
|
4
|
+
const environmentsByProgram = /* @__PURE__ */ new WeakMap();
|
|
5
|
+
function enclosingTypeScope(node) {
|
|
6
|
+
let current = node.parent;
|
|
7
|
+
while (current !== null) {
|
|
8
|
+
if (current.type === "Program" || current.type === "BlockStatement" || current.type === "TSModuleBlock" || current.type === "StaticBlock" || current.type === "SwitchStatement") return current;
|
|
9
|
+
current = current.parent;
|
|
10
|
+
}
|
|
11
|
+
return node;
|
|
12
|
+
}
|
|
13
|
+
function declaredTypeBinding(node) {
|
|
14
|
+
if (node.type === "TSTypeAliasDeclaration") return {
|
|
15
|
+
alias: node,
|
|
16
|
+
name: node.id.name
|
|
17
|
+
};
|
|
18
|
+
if (node.type === "TSInterfaceDeclaration" || node.type === "TSEnumDeclaration" || node.type === "ClassDeclaration" || node.type === "ClassExpression") return node.id === null ? null : {
|
|
19
|
+
alias: null,
|
|
20
|
+
name: node.id.name
|
|
21
|
+
};
|
|
22
|
+
if (node.type === "ImportSpecifier" || node.type === "ImportDefaultSpecifier" || node.type === "ImportNamespaceSpecifier") return {
|
|
23
|
+
alias: null,
|
|
24
|
+
name: node.local.name
|
|
25
|
+
};
|
|
26
|
+
return null;
|
|
27
|
+
}
|
|
28
|
+
function collectTypeBindings(node, visitorKeys, bindingsByName, aliases) {
|
|
29
|
+
const declared = declaredTypeBinding(node);
|
|
30
|
+
if (declared !== null) {
|
|
31
|
+
const bindings = bindingsByName.get(declared.name) ?? [];
|
|
32
|
+
bindings.push({
|
|
33
|
+
...declared,
|
|
34
|
+
scope: enclosingTypeScope(node)
|
|
35
|
+
});
|
|
36
|
+
bindingsByName.set(declared.name, bindings);
|
|
37
|
+
if (declared.alias !== null) aliases.push(declared.alias);
|
|
38
|
+
}
|
|
39
|
+
for (const child of childNodes(node, visitorKeys)) collectTypeBindings(child, visitorKeys, bindingsByName, aliases);
|
|
40
|
+
}
|
|
41
|
+
/** Collect every lexical type alias and competing type binding in a program. */
|
|
42
|
+
function createTypeAliasEnvironment(program, visitorKeys) {
|
|
43
|
+
const cached = environmentsByProgram.get(program);
|
|
44
|
+
if (cached !== void 0) return cached;
|
|
45
|
+
const bindingsByName = /* @__PURE__ */ new Map();
|
|
46
|
+
const aliases = [];
|
|
47
|
+
collectTypeBindings(program, visitorKeys, bindingsByName, aliases);
|
|
48
|
+
const environment = {
|
|
49
|
+
aliases,
|
|
50
|
+
bindingsByName,
|
|
51
|
+
visitorKeys
|
|
52
|
+
};
|
|
53
|
+
environmentsByProgram.set(program, environment);
|
|
54
|
+
return environment;
|
|
55
|
+
}
|
|
56
|
+
function ancestorDistance(ancestor, node) {
|
|
57
|
+
let current = node;
|
|
58
|
+
let distance = 0;
|
|
59
|
+
while (current !== null) {
|
|
60
|
+
if (current === ancestor) return distance;
|
|
61
|
+
current = current.parent;
|
|
62
|
+
distance += 1;
|
|
63
|
+
}
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
function nearestTypeBindings(name, use, environment) {
|
|
67
|
+
const candidates = environment.bindingsByName.get(name) ?? [];
|
|
68
|
+
let nearestDistance = Number.POSITIVE_INFINITY;
|
|
69
|
+
let nearest = [];
|
|
70
|
+
for (const candidate of candidates) {
|
|
71
|
+
const distance = ancestorDistance(candidate.scope, use);
|
|
72
|
+
if (distance === null || distance > nearestDistance) continue;
|
|
73
|
+
if (distance === nearestDistance) {
|
|
74
|
+
nearest.push(candidate);
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
nearestDistance = distance;
|
|
78
|
+
nearest = [candidate];
|
|
79
|
+
}
|
|
80
|
+
return nearest;
|
|
81
|
+
}
|
|
82
|
+
/** Resolve the nearest visible alias with this name, respecting lexical shadowing. */
|
|
83
|
+
function visibleTypeAlias(name, use, environment) {
|
|
84
|
+
if (lexicalTypeParameterNames(use, environment.visitorKeys).has(name)) return null;
|
|
85
|
+
const bindings = nearestTypeBindings(name, use, environment);
|
|
86
|
+
return bindings.length === 1 ? bindings[0]?.alias ?? null : null;
|
|
87
|
+
}
|
|
88
|
+
/** Return whether a local declaration shadows a built-in type at this use. */
|
|
89
|
+
function hasVisibleTypeBinding(name, use, environment) {
|
|
90
|
+
return lexicalTypeParameterNames(use, environment.visitorKeys).has(name) || nearestTypeBindings(name, use, environment).length > 0;
|
|
91
|
+
}
|
|
92
|
+
function typeReferenceName(type) {
|
|
93
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
94
|
+
}
|
|
95
|
+
function aliasSubstitutions(alias, reference, base) {
|
|
96
|
+
const parameters = alias.typeParameters?.params ?? [];
|
|
97
|
+
const arguments_ = reference.typeArguments?.params ?? [];
|
|
98
|
+
const next = new Map(base);
|
|
99
|
+
for (const [index, parameter] of parameters.entries()) {
|
|
100
|
+
const explicitArgument = arguments_[index];
|
|
101
|
+
const argument = explicitArgument ?? parameter.default;
|
|
102
|
+
if (argument === null || argument === void 0) return null;
|
|
103
|
+
const argumentSubstitutions = explicitArgument === void 0 ? next : base;
|
|
104
|
+
next.set(parameter.name.name, {
|
|
105
|
+
type: argument,
|
|
106
|
+
substitutions: new Map(argumentSubstitutions)
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return next;
|
|
110
|
+
}
|
|
111
|
+
/** Match a type after resolving visible aliases and substituting their type parameters. */
|
|
112
|
+
function resolvedTypeMatches(type, environment, matcher) {
|
|
113
|
+
const evaluate = (current, substitutions, resolvingAliases) => {
|
|
114
|
+
if (current.type === "TSTypeReference") {
|
|
115
|
+
const name = typeReferenceName(current);
|
|
116
|
+
if (name !== null) {
|
|
117
|
+
const substitution = substitutions.get(name);
|
|
118
|
+
if (substitution !== void 0 && !current.typeArguments?.params.length) return evaluate(substitution.type, substitution.substitutions, resolvingAliases);
|
|
119
|
+
const alias = visibleTypeAlias(name, current, environment);
|
|
120
|
+
if (alias !== null && !resolvingAliases.has(alias)) {
|
|
121
|
+
const nextSubstitutions = aliasSubstitutions(alias, current, substitutions);
|
|
122
|
+
if (nextSubstitutions !== null) {
|
|
123
|
+
const nextResolving = new Set(resolvingAliases);
|
|
124
|
+
nextResolving.add(alias);
|
|
125
|
+
return evaluate(alias.typeAnnotation, nextSubstitutions, nextResolving);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
return matcher(current, (child) => evaluate(child, substitutions, resolvingAliases));
|
|
131
|
+
};
|
|
132
|
+
return evaluate(type, /* @__PURE__ */ new Map(), /* @__PURE__ */ new Set());
|
|
133
|
+
}
|
|
134
|
+
export { createTypeAliasEnvironment, hasVisibleTypeBinding, resolvedTypeMatches, visibleTypeAlias };
|