@ref0/workspace 0.0.0-stage → 0.2.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 +169 -2
- package/dist/bin.d.mts +1 -0
- package/dist/bin.mjs +113 -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/dist/package-exports.mjs +157 -0
- package/dist/symbols/check.mjs +209 -0
- package/dist/symbols/find.mjs +63 -0
- package/dist/symbols/symbols.mjs +390 -0
- package/dist/type-coverage.mjs +68 -0
- package/dist/types/cache.mjs +157 -0
- package/dist/types/inputs.mjs +71 -0
- package/dist/types/projects.mjs +65 -0
- package/dist/types/types.mjs +175 -0
- package/package.json +68 -3
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
2
|
+
// src/lint/anti-slop/rules/no-widen-then-assert.ts
|
|
3
|
+
const functionBoundaryTypes = /* @__PURE__ */ new Set([
|
|
4
|
+
"ArrowFunctionExpression",
|
|
5
|
+
"FunctionDeclaration",
|
|
6
|
+
"FunctionExpression",
|
|
7
|
+
"TSDeclareFunction",
|
|
8
|
+
"TSEmptyBodyFunctionExpression"
|
|
9
|
+
]);
|
|
10
|
+
function unwrapExpressionParentheses(expression) {
|
|
11
|
+
let current = expression;
|
|
12
|
+
while (current.type === "ParenthesizedExpression") current = current.expression;
|
|
13
|
+
return current;
|
|
14
|
+
}
|
|
15
|
+
function unwrapTypeParentheses(type) {
|
|
16
|
+
let current = type;
|
|
17
|
+
while (current.type === "TSParenthesizedType") current = current.typeAnnotation;
|
|
18
|
+
return current;
|
|
19
|
+
}
|
|
20
|
+
function typeReferenceName(type) {
|
|
21
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
22
|
+
}
|
|
23
|
+
function isUnknownOrAnyType(type) {
|
|
24
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
25
|
+
return unwrapped.type === "TSUnknownKeyword" || unwrapped.type === "TSAnyKeyword";
|
|
26
|
+
}
|
|
27
|
+
function isBroadRecordKeyType(type) {
|
|
28
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
29
|
+
if (unwrapped.type === "TSStringKeyword" || unwrapped.type === "TSNumberKeyword" || unwrapped.type === "TSSymbolKeyword") return true;
|
|
30
|
+
if (unwrapped.type === "TSUnionType") return unwrapped.types.every(isBroadRecordKeyType);
|
|
31
|
+
return unwrapped.type === "TSTypeReference" && typeReferenceName(unwrapped) === "PropertyKey";
|
|
32
|
+
}
|
|
33
|
+
function isBroadRecordType(type) {
|
|
34
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
35
|
+
if (unwrapped.type === "TSTypeReference") {
|
|
36
|
+
if (typeReferenceName(unwrapped) === "Readonly") {
|
|
37
|
+
const [inner] = unwrapped.typeArguments?.params ?? [];
|
|
38
|
+
return inner !== void 0 && isBroadRecordType(inner);
|
|
39
|
+
}
|
|
40
|
+
if (typeReferenceName(unwrapped) !== "Record") return false;
|
|
41
|
+
const parameters = unwrapped.typeArguments?.params ?? [];
|
|
42
|
+
return parameters.length === 2 && parameters[0] !== void 0 && parameters[1] !== void 0 && isBroadRecordKeyType(parameters[0]) && isUnknownOrAnyType(parameters[1]);
|
|
43
|
+
}
|
|
44
|
+
if (unwrapped.type !== "TSTypeLiteral" || unwrapped.members.length !== 1) return false;
|
|
45
|
+
const [member] = unwrapped.members;
|
|
46
|
+
const [parameter] = member?.type === "TSIndexSignature" ? member.parameters : [];
|
|
47
|
+
return member?.type === "TSIndexSignature" && member.parameters.length === 1 && parameter !== void 0 && isBroadRecordKeyType(parameter.typeAnnotation.typeAnnotation) && isUnknownOrAnyType(member.typeAnnotation.typeAnnotation);
|
|
48
|
+
}
|
|
49
|
+
function broadTypeKind(type) {
|
|
50
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
51
|
+
if (unwrapped.type === "TSUnknownKeyword" || unwrapped.type === "TSAnyKeyword") return "top";
|
|
52
|
+
if (unwrapped.type === "TSObjectKeyword") return "object";
|
|
53
|
+
return isBroadRecordType(unwrapped) ? "record" : null;
|
|
54
|
+
}
|
|
55
|
+
function assertedExpression(node) {
|
|
56
|
+
return unwrapExpressionParentheses(node.expression);
|
|
57
|
+
}
|
|
58
|
+
function assertionFromExpression(expression) {
|
|
59
|
+
const unwrapped = unwrapExpressionParentheses(expression);
|
|
60
|
+
return unwrapped.type === "TSAsExpression" || unwrapped.type === "TSTypeAssertion" ? unwrapped : null;
|
|
61
|
+
}
|
|
62
|
+
function normalizedTypeText(sourceText, type) {
|
|
63
|
+
return sourceText.slice(type.start, type.end).replaceAll(/\s+/gu, "");
|
|
64
|
+
}
|
|
65
|
+
function typesHaveSameSyntax(sourceText, left, right) {
|
|
66
|
+
return left !== null && normalizedTypeText(sourceText, unwrapTypeParentheses(left)) === normalizedTypeText(sourceText, unwrapTypeParentheses(right));
|
|
67
|
+
}
|
|
68
|
+
function isDefinitelyObjectType(type) {
|
|
69
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
70
|
+
switch (unwrapped.type) {
|
|
71
|
+
case "TSArrayType":
|
|
72
|
+
case "TSConstructorType":
|
|
73
|
+
case "TSFunctionType":
|
|
74
|
+
case "TSMappedType":
|
|
75
|
+
case "TSObjectKeyword":
|
|
76
|
+
case "TSTupleType": return true;
|
|
77
|
+
case "TSTypeLiteral": return unwrapped.members.length > 0;
|
|
78
|
+
case "TSIntersectionType": return unwrapped.types.every(isDefinitelyObjectType);
|
|
79
|
+
case "TSTypeOperator": return unwrapped.operator === "readonly" && isDefinitelyObjectType(unwrapped.typeAnnotation);
|
|
80
|
+
default: return false;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
function isDefinitelyNarrowerRecordType(type) {
|
|
84
|
+
const unwrapped = unwrapTypeParentheses(type);
|
|
85
|
+
if (unwrapped.type === "TSTypeLiteral") return unwrapped.members.some((member) => member.type !== "TSIndexSignature");
|
|
86
|
+
if (unwrapped.type !== "TSTypeReference") return false;
|
|
87
|
+
if (typeReferenceName(unwrapped) === "Readonly") {
|
|
88
|
+
const [inner] = unwrapped.typeArguments?.params ?? [];
|
|
89
|
+
return inner !== void 0 && isDefinitelyNarrowerRecordType(inner);
|
|
90
|
+
}
|
|
91
|
+
if (typeReferenceName(unwrapped) !== "Record") return false;
|
|
92
|
+
const parameters = unwrapped.typeArguments?.params ?? [];
|
|
93
|
+
return parameters.length === 2 && parameters[1] !== void 0 && !isUnknownOrAnyType(parameters[1]);
|
|
94
|
+
}
|
|
95
|
+
function functionBoundary(node) {
|
|
96
|
+
let current = node.parent;
|
|
97
|
+
while (current !== null && current.type !== "Program") {
|
|
98
|
+
if (functionBoundaryTypes.has(current.type)) return current;
|
|
99
|
+
current = current.parent;
|
|
100
|
+
}
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
function resolvedVariableForIdentifier(scopes, identifier) {
|
|
104
|
+
for (const scope of scopes) {
|
|
105
|
+
const reference = scope.references.find((candidate) => candidate.identifier.start === identifier.start && candidate.identifier.end === identifier.end);
|
|
106
|
+
if (reference !== void 0) return reference.resolved;
|
|
107
|
+
}
|
|
108
|
+
return null;
|
|
109
|
+
}
|
|
110
|
+
function variableDeclarator(variable) {
|
|
111
|
+
for (const definition of variable.defs) if (definition.type === "Variable" && definition.node.type === "VariableDeclarator") return definition.node;
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
function knownValueEvidence(expression, scopes, boundary, visitedVariables) {
|
|
115
|
+
const unwrapped = unwrapExpressionParentheses(expression);
|
|
116
|
+
if (unwrapped.type === "TSAsExpression" || unwrapped.type === "TSTypeAssertion") {
|
|
117
|
+
if (broadTypeKind(unwrapped.typeAnnotation) !== null) return null;
|
|
118
|
+
return { type: unwrapped.typeAnnotation };
|
|
119
|
+
}
|
|
120
|
+
if (unwrapped.type === "Literal" || unwrapped.type === "TemplateLiteral") return { type: null };
|
|
121
|
+
if (unwrapped.type === "ArrayExpression" || unwrapped.type === "ArrowFunctionExpression" || unwrapped.type === "ClassExpression" || unwrapped.type === "FunctionExpression" || unwrapped.type === "NewExpression" || unwrapped.type === "ObjectExpression") return { type: null };
|
|
122
|
+
if (unwrapped.type !== "Identifier") return null;
|
|
123
|
+
const variable = resolvedVariableForIdentifier(scopes, unwrapped);
|
|
124
|
+
if (variable === null || visitedVariables.has(variable)) return null;
|
|
125
|
+
const annotatedIdentifier = variable.identifiers.find((identifier) => identifier.typeAnnotation !== null && identifier.typeAnnotation !== void 0);
|
|
126
|
+
const annotation = annotatedIdentifier?.typeAnnotation?.typeAnnotation;
|
|
127
|
+
if (annotation !== void 0 && annotatedIdentifier !== void 0) {
|
|
128
|
+
if (functionBoundary(annotatedIdentifier) !== boundary || broadTypeKind(annotation) !== null) return null;
|
|
129
|
+
return { type: annotation };
|
|
130
|
+
}
|
|
131
|
+
const declarator = variableDeclarator(variable);
|
|
132
|
+
if (declarator === null || declarator.parent.type !== "VariableDeclaration" || declarator.parent.kind !== "const" || declarator.init === null || variable.references.some((reference) => reference.isWrite() && !reference.init) || functionBoundary(declarator) !== boundary) return null;
|
|
133
|
+
return knownValueEvidence(declarator.init, scopes, boundary, /* @__PURE__ */ new Set([...visitedVariables, variable]));
|
|
134
|
+
}
|
|
135
|
+
function widenedBinding(variable, scopes) {
|
|
136
|
+
const declarator = variableDeclarator(variable);
|
|
137
|
+
if (declarator === null || declarator.parent.type !== "VariableDeclaration" || declarator.parent.kind !== "const" || declarator.id.type !== "Identifier" || declarator.init === null || variable.references.some((reference) => reference.isWrite() && !reference.init)) return null;
|
|
138
|
+
const boundary = functionBoundary(declarator);
|
|
139
|
+
const declaredType = declarator.id.typeAnnotation?.typeAnnotation;
|
|
140
|
+
const initializerAssertion = assertionFromExpression(declarator.init);
|
|
141
|
+
const initializerBroadKind = initializerAssertion === null ? null : broadTypeKind(initializerAssertion.typeAnnotation);
|
|
142
|
+
const broadKind = (declaredType === void 0 ? null : broadTypeKind(declaredType)) ?? initializerBroadKind;
|
|
143
|
+
if (broadKind === null) return null;
|
|
144
|
+
const evidence = knownValueEvidence(initializerAssertion !== null && initializerBroadKind !== null ? assertedExpression(initializerAssertion) : declarator.init, scopes, boundary, /* @__PURE__ */ new Set([variable]));
|
|
145
|
+
return evidence === null ? null : {
|
|
146
|
+
broadKind,
|
|
147
|
+
evidence,
|
|
148
|
+
declaredAt: declarator.end,
|
|
149
|
+
boundary
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
function assertionIsNarrower(sourceText, broadKind, evidence, assertedType) {
|
|
153
|
+
if (broadTypeKind(assertedType) !== null) return false;
|
|
154
|
+
if (broadKind === "top") return true;
|
|
155
|
+
if (typesHaveSameSyntax(sourceText, evidence.type, assertedType)) return true;
|
|
156
|
+
if (broadKind === "object") return isDefinitelyObjectType(assertedType);
|
|
157
|
+
return isDefinitelyNarrowerRecordType(assertedType);
|
|
158
|
+
}
|
|
159
|
+
/** Detect immutable local bindings that erase a known type and are later asserted back to a narrower type. */
|
|
160
|
+
const noWidenThenAssertRule = defineRule({
|
|
161
|
+
meta: {
|
|
162
|
+
type: "problem",
|
|
163
|
+
docs: { description: "Disallow local const flows that explicitly widen a known value before asserting the widened binding to a narrower type." },
|
|
164
|
+
messages: { widenThenAssert: "Binding \"{{name}}\" discards type evidence and later recreates it with an assertion. Keep the precise type from initialization through use; parse boundary input once." }
|
|
165
|
+
},
|
|
166
|
+
createOnce(context) {
|
|
167
|
+
let scopes = null;
|
|
168
|
+
const checkAssertion = (node) => {
|
|
169
|
+
const expression = assertedExpression(node);
|
|
170
|
+
if (expression.type !== "Identifier") return;
|
|
171
|
+
scopes ??= context.sourceCode.scopeManager.scopes;
|
|
172
|
+
const variable = resolvedVariableForIdentifier(scopes, expression);
|
|
173
|
+
if (variable === null) return;
|
|
174
|
+
const widened = widenedBinding(variable, scopes);
|
|
175
|
+
if (widened === null || node.start <= widened.declaredAt || functionBoundary(node) !== widened.boundary || !assertionIsNarrower(context.sourceCode.text, widened.broadKind, widened.evidence, node.typeAnnotation)) return;
|
|
176
|
+
context.report({
|
|
177
|
+
node,
|
|
178
|
+
messageId: "widenThenAssert",
|
|
179
|
+
data: { name: expression.name }
|
|
180
|
+
});
|
|
181
|
+
};
|
|
182
|
+
return {
|
|
183
|
+
Program() {
|
|
184
|
+
scopes = null;
|
|
185
|
+
},
|
|
186
|
+
TSAsExpression: checkAssertion,
|
|
187
|
+
TSTypeAssertion: checkAssertion
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
export { noWidenThenAssertRule };
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import createPaddingLineRule from "../vendor/eslint-stylistic/padding-line-between-statements.mjs";
|
|
2
|
+
// src/lint/anti-slop/rules/require-readable-spacing.ts
|
|
3
|
+
const paddingRule = createPaddingLineRule([
|
|
4
|
+
{
|
|
5
|
+
blankLine: "always",
|
|
6
|
+
prev: "import",
|
|
7
|
+
next: "*"
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
blankLine: "always",
|
|
11
|
+
prev: "*",
|
|
12
|
+
next: { selector: "Program > :not(ImportDeclaration)" }
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
blankLine: "always",
|
|
16
|
+
prev: { selector: "Program > :not(ImportDeclaration)" },
|
|
17
|
+
next: "*"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
blankLine: "always",
|
|
21
|
+
prev: "*",
|
|
22
|
+
next: [
|
|
23
|
+
"function",
|
|
24
|
+
"class",
|
|
25
|
+
"interface",
|
|
26
|
+
"type"
|
|
27
|
+
]
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
blankLine: "always",
|
|
31
|
+
prev: [
|
|
32
|
+
"function",
|
|
33
|
+
"class",
|
|
34
|
+
"interface",
|
|
35
|
+
"type"
|
|
36
|
+
],
|
|
37
|
+
next: "*"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
blankLine: "always",
|
|
41
|
+
prev: "*",
|
|
42
|
+
next: [
|
|
43
|
+
"return",
|
|
44
|
+
"if",
|
|
45
|
+
"switch",
|
|
46
|
+
"try",
|
|
47
|
+
"for",
|
|
48
|
+
"while",
|
|
49
|
+
"do"
|
|
50
|
+
]
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
blankLine: "always",
|
|
54
|
+
prev: "block-like",
|
|
55
|
+
next: "*"
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
blankLine: "never",
|
|
59
|
+
prev: [
|
|
60
|
+
"const",
|
|
61
|
+
"let",
|
|
62
|
+
"var"
|
|
63
|
+
],
|
|
64
|
+
next: { selector: "IfStatement:not([consequent.type=\"BlockStatement\"])" }
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
blankLine: "any",
|
|
68
|
+
prev: "import",
|
|
69
|
+
next: "import"
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
blankLine: "any",
|
|
73
|
+
prev: { selector: ":matches(TSDeclareFunction, ExportNamedDeclaration[declaration.type=\"TSDeclareFunction\"])" },
|
|
74
|
+
next: { selector: ":matches(TSDeclareFunction, FunctionDeclaration, ExportNamedDeclaration[declaration.type=\"TSDeclareFunction\"], ExportNamedDeclaration[declaration.type=\"FunctionDeclaration\"])" }
|
|
75
|
+
}
|
|
76
|
+
]);
|
|
77
|
+
/** Restore structural blank lines without treating wrapped bindings as separate semantic phases. */
|
|
78
|
+
const requireReadableSpacingRule = {
|
|
79
|
+
...paddingRule,
|
|
80
|
+
meta: {
|
|
81
|
+
...paddingRule.meta,
|
|
82
|
+
docs: { description: "Require readable spacing between declarations and logical statement groups." },
|
|
83
|
+
schema: []
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
export { requireReadableSpacingRule };
|
|
@@ -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 };
|