@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,21 @@
|
|
|
1
|
+
import { isGlobalReflectMethodCall } from "../shared/reflect-method.mjs";
|
|
2
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
3
|
+
// src/lint/anti-slop/rules/no-reflect-get.ts
|
|
4
|
+
/** Ban Reflect.get, which bypasses ordinary property access and useful type evidence. */
|
|
5
|
+
const noReflectGetRule = defineRule({
|
|
6
|
+
meta: {
|
|
7
|
+
type: "problem",
|
|
8
|
+
docs: { description: "Disallow Reflect.get; use typed property access or parse dynamic input into a domain type." },
|
|
9
|
+
messages: { reflectGet: "Replace `Reflect.get` with typed property access. Parse dynamic input into a named domain type before reading it." }
|
|
10
|
+
},
|
|
11
|
+
createOnce(context) {
|
|
12
|
+
return { CallExpression(node) {
|
|
13
|
+
if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression") return;
|
|
14
|
+
if (isGlobalReflectMethodCall(context.sourceCode, node.callee, "get")) context.report({
|
|
15
|
+
node,
|
|
16
|
+
messageId: "reflectGet"
|
|
17
|
+
});
|
|
18
|
+
} };
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
export { noReflectGetRule };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
2
|
+
import * as Schema from "effect/Schema";
|
|
3
|
+
// src/lint/anti-slop/rules/no-runtime-typeof.ts
|
|
4
|
+
function isRuntimeFunction(node) {
|
|
5
|
+
return node.type === "ArrowFunctionExpression" || node.type === "FunctionDeclaration" || node.type === "FunctionExpression";
|
|
6
|
+
}
|
|
7
|
+
function isInsideTypeGuard(node) {
|
|
8
|
+
let current = node.parent;
|
|
9
|
+
while (current !== null && current.type !== "Program") {
|
|
10
|
+
if (isRuntimeFunction(current)) return current.returnType?.typeAnnotation.type === "TSTypePredicate";
|
|
11
|
+
current = current.parent;
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
/** Return whether typeof safely probes for the existence of a possibly absent binding. */
|
|
16
|
+
function isExistenceProbe(node) {
|
|
17
|
+
const parent = node.parent;
|
|
18
|
+
if (parent.type !== "BinaryExpression") return false;
|
|
19
|
+
if (![
|
|
20
|
+
"===",
|
|
21
|
+
"!==",
|
|
22
|
+
"==",
|
|
23
|
+
"!="
|
|
24
|
+
].includes(parent.operator)) return false;
|
|
25
|
+
const other = parent.left === node ? parent.right : parent.left;
|
|
26
|
+
return other.type === "Literal" && other.value === "undefined";
|
|
27
|
+
}
|
|
28
|
+
const allowsTypeGuards = Schema.is(Schema.Struct({ allowInTypeGuards: Schema.Literal(true) }));
|
|
29
|
+
/** Disallow runtime typeof checks that narrow unparsed values instead of decoding them. */
|
|
30
|
+
const noRuntimeTypeofRule = defineRule({
|
|
31
|
+
meta: {
|
|
32
|
+
type: "problem",
|
|
33
|
+
docs: { description: "Disallow runtime typeof checks; external values must be decoded into meaningful types at their I/O boundary." },
|
|
34
|
+
messages: { runtimeTypeof: "A `typeof` check narrows a representation without establishing its contract. Parse input at its I/O boundary, then branch on the domain value." },
|
|
35
|
+
schema: [{
|
|
36
|
+
type: "object",
|
|
37
|
+
properties: { allowInTypeGuards: { type: "boolean" } },
|
|
38
|
+
additionalProperties: false
|
|
39
|
+
}],
|
|
40
|
+
defaultOptions: [{ allowInTypeGuards: false }]
|
|
41
|
+
},
|
|
42
|
+
createOnce(context) {
|
|
43
|
+
return { UnaryExpression(node) {
|
|
44
|
+
const allowInTypeGuards = allowsTypeGuards(context.options?.[0]);
|
|
45
|
+
if (node.operator === "typeof" && !isExistenceProbe(node) && (!allowInTypeGuards || !isInsideTypeGuard(node))) context.report({
|
|
46
|
+
node,
|
|
47
|
+
messageId: "runtimeTypeof"
|
|
48
|
+
});
|
|
49
|
+
} };
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
export { noRuntimeTypeofRule };
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
2
|
+
// src/lint/anti-slop/rules/no-shape-in-symbol-names.ts
|
|
3
|
+
const FORBIDDEN_SYMBOL_NAME = "shape";
|
|
4
|
+
function containsForbiddenSymbolName(name) {
|
|
5
|
+
return name.toLowerCase().includes(FORBIDDEN_SYMBOL_NAME);
|
|
6
|
+
}
|
|
7
|
+
/** Return whether an identifier names a statically accessed member owned by another value. */
|
|
8
|
+
function isBorrowedMemberName(node) {
|
|
9
|
+
const parent = node.parent;
|
|
10
|
+
if (parent === null || parent.type !== "MemberExpression") return false;
|
|
11
|
+
return parent.property === node && parent.computed === false;
|
|
12
|
+
}
|
|
13
|
+
/** Ban the case-insensitive substring "shape" in every JavaScript and TypeScript symbol name. */
|
|
14
|
+
const noForbiddenTermInSymbolNamesRule = defineRule({
|
|
15
|
+
meta: {
|
|
16
|
+
type: "problem",
|
|
17
|
+
docs: { description: "Disallow the case-insensitive substring \"shape\" in JavaScript, TypeScript, private, and JSX symbol names." },
|
|
18
|
+
messages: { forbiddenSymbolName: "Rename symbol \"{{name}}\" for its domain role; \"shape\" describes structure rather than ownership." }
|
|
19
|
+
},
|
|
20
|
+
createOnce(context) {
|
|
21
|
+
const reportForbiddenSymbolName = (node) => {
|
|
22
|
+
if (!containsForbiddenSymbolName(node.name) || isBorrowedMemberName(node)) return;
|
|
23
|
+
context.report({
|
|
24
|
+
node,
|
|
25
|
+
messageId: "forbiddenSymbolName",
|
|
26
|
+
data: { name: node.name }
|
|
27
|
+
});
|
|
28
|
+
};
|
|
29
|
+
return {
|
|
30
|
+
Identifier: reportForbiddenSymbolName,
|
|
31
|
+
PrivateIdentifier: reportForbiddenSymbolName,
|
|
32
|
+
JSXIdentifier: reportForbiddenSymbolName
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
export { noForbiddenTermInSymbolNamesRule };
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { containsUnknownType, functionParameterBindingName, functionParameterTypeAnnotation } from "../shared/function-parameters.mjs";
|
|
2
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
3
|
+
// src/lint/anti-slop/rules/no-unknown-parameters.ts
|
|
4
|
+
function isTypePredicateSubject(owner, parameterName) {
|
|
5
|
+
const predicate = owner.returnType?.typeAnnotation;
|
|
6
|
+
return predicate?.type === "TSTypePredicate" && predicate.parameterName.type === "Identifier" && predicate.parameterName.name === parameterName;
|
|
7
|
+
}
|
|
8
|
+
/** Disallow unknown inputs except explicitly named error-cause enrichment. */
|
|
9
|
+
const noUnknownParametersRule = defineRule({
|
|
10
|
+
meta: {
|
|
11
|
+
type: "problem",
|
|
12
|
+
docs: { description: "Disallow explicitly unknown function parameters except `cause` and type-predicate subjects; decode unknown input at its I/O boundary instead." },
|
|
13
|
+
messages: { unknownParameter: "Parameter `{{parameter}}` leaves input unparsed. Accept a named domain type; run the expected schema or parser at the I/O boundary before calling this function." }
|
|
14
|
+
},
|
|
15
|
+
createOnce(context) {
|
|
16
|
+
const checkParameters = (node) => {
|
|
17
|
+
for (const parameter of node.params) {
|
|
18
|
+
const annotation = functionParameterTypeAnnotation(parameter);
|
|
19
|
+
if (annotation === null || annotation === void 0) continue;
|
|
20
|
+
if (!containsUnknownType(annotation.typeAnnotation)) continue;
|
|
21
|
+
const name = functionParameterBindingName(parameter, context.sourceCode);
|
|
22
|
+
if (name === "cause" || isTypePredicateSubject(node, name)) continue;
|
|
23
|
+
context.report({
|
|
24
|
+
node: annotation.typeAnnotation,
|
|
25
|
+
messageId: "unknownParameter",
|
|
26
|
+
data: { parameter: name }
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
ArrowFunctionExpression: checkParameters,
|
|
32
|
+
FunctionDeclaration: checkParameters,
|
|
33
|
+
FunctionExpression: checkParameters,
|
|
34
|
+
TSCallSignatureDeclaration: checkParameters,
|
|
35
|
+
TSConstructSignatureDeclaration: checkParameters,
|
|
36
|
+
TSConstructorType: checkParameters,
|
|
37
|
+
TSDeclareFunction: checkParameters,
|
|
38
|
+
TSEmptyBodyFunctionExpression: checkParameters,
|
|
39
|
+
TSFunctionType: checkParameters,
|
|
40
|
+
TSMethodSignature: checkParameters
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
export { noUnknownParametersRule };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { createTypeAliasEnvironment, resolvedTypeMatches } from "../shared/type-alias-resolution.mjs";
|
|
2
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
3
|
+
// src/lint/anti-slop/rules/no-unknown-returns.ts
|
|
4
|
+
/** Ban function contracts that return unknown instead of a parsed domain type. */
|
|
5
|
+
const noUnknownReturnsRule = defineRule({
|
|
6
|
+
meta: {
|
|
7
|
+
type: "problem",
|
|
8
|
+
docs: { description: "Disallow functions whose explicit return contract is unknown or Promise<unknown>." },
|
|
9
|
+
messages: { unknownReturn: "This function exposes `unknown` to its caller. Parse the value at its boundary and return a named domain type." }
|
|
10
|
+
},
|
|
11
|
+
createOnce(context) {
|
|
12
|
+
let environment = null;
|
|
13
|
+
const resolvesToUnknown = (type) => environment !== null && resolvedTypeMatches(type, environment, (resolved, matches) => {
|
|
14
|
+
if (resolved.type === "TSUnknownKeyword") return true;
|
|
15
|
+
if (resolved.type === "TSParenthesizedType") return matches(resolved.typeAnnotation);
|
|
16
|
+
if (resolved.type === "TSUnionType") return resolved.types.some(matches);
|
|
17
|
+
if (resolved.type !== "TSTypeReference" || resolved.typeName.type !== "Identifier" || resolved.typeName.name !== "Promise" && resolved.typeName.name !== "PromiseLike") return false;
|
|
18
|
+
const value = resolved.typeArguments?.params[0];
|
|
19
|
+
return value !== void 0 && matches(value);
|
|
20
|
+
});
|
|
21
|
+
const checkReturnType = (node) => {
|
|
22
|
+
const annotation = node.returnType;
|
|
23
|
+
if (annotation === null || annotation === void 0) return;
|
|
24
|
+
if (!resolvesToUnknown(annotation.typeAnnotation)) return;
|
|
25
|
+
context.report({
|
|
26
|
+
node: annotation.typeAnnotation,
|
|
27
|
+
messageId: "unknownReturn"
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
return {
|
|
31
|
+
Program(node) {
|
|
32
|
+
environment = createTypeAliasEnvironment(node, context.sourceCode.visitorKeys);
|
|
33
|
+
},
|
|
34
|
+
ArrowFunctionExpression: checkReturnType,
|
|
35
|
+
FunctionDeclaration: checkReturnType,
|
|
36
|
+
FunctionExpression: checkReturnType,
|
|
37
|
+
TSCallSignatureDeclaration: checkReturnType,
|
|
38
|
+
TSConstructSignatureDeclaration: checkReturnType,
|
|
39
|
+
TSConstructorType: checkReturnType,
|
|
40
|
+
TSDeclareFunction: checkReturnType,
|
|
41
|
+
TSEmptyBodyFunctionExpression: checkReturnType,
|
|
42
|
+
TSFunctionType: checkReturnType,
|
|
43
|
+
TSMethodSignature: checkReturnType
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
export { noUnknownReturnsRule };
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { createTypeAliasEnvironment, resolvedTypeMatches } from "../shared/type-alias-resolution.mjs";
|
|
2
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
3
|
+
// src/lint/anti-slop/rules/no-unknown-type-aliases.ts
|
|
4
|
+
/** Ban named aliases that merely conceal TypeScript's unknown top type. */
|
|
5
|
+
const noUnknownTypeAliasesRule = defineRule({
|
|
6
|
+
meta: {
|
|
7
|
+
type: "problem",
|
|
8
|
+
docs: { description: "Disallow type aliases whose resolved type is unknown; unknown must remain visible at an allowed boundary." },
|
|
9
|
+
messages: { unknownAlias: "Type alias `{{alias}}` hides `unknown`. Keep `unknown` explicit at the parsing boundary or on an allowed `cause` field; otherwise use the parsed owner type." }
|
|
10
|
+
},
|
|
11
|
+
createOnce(context) {
|
|
12
|
+
let environment = null;
|
|
13
|
+
const resolvesToUnknown = (type) => environment !== null && resolvedTypeMatches(type, environment, (resolved, matches) => {
|
|
14
|
+
if (resolved.type === "TSUnknownKeyword") return true;
|
|
15
|
+
if (resolved.type === "TSParenthesizedType") return matches(resolved.typeAnnotation);
|
|
16
|
+
return resolved.type === "TSUnionType" && resolved.types.some(matches);
|
|
17
|
+
});
|
|
18
|
+
return {
|
|
19
|
+
Program(node) {
|
|
20
|
+
environment = createTypeAliasEnvironment(node, context.sourceCode.visitorKeys);
|
|
21
|
+
},
|
|
22
|
+
TSTypeAliasDeclaration(node) {
|
|
23
|
+
if (!resolvesToUnknown(node.typeAnnotation)) return;
|
|
24
|
+
context.report({
|
|
25
|
+
node: node.id,
|
|
26
|
+
messageId: "unknownAlias",
|
|
27
|
+
data: { alias: node.id.name }
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
export { noUnknownTypeAliasesRule };
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { classifyUnsafeDictionary, classifyUnsafeDictionaryValue, createTypeEnvironment } from "../shared/dictionary-types.mjs";
|
|
2
|
+
import { visibleTypeAlias } from "../shared/type-alias-resolution.mjs";
|
|
3
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
4
|
+
// src/lint/anti-slop/rules/no-unsafe-dictionary-type.ts
|
|
5
|
+
const typeNodeKinds = /* @__PURE__ */ new Set([
|
|
6
|
+
"JSDocNonNullableType",
|
|
7
|
+
"JSDocNullableType",
|
|
8
|
+
"JSDocUnknownType",
|
|
9
|
+
"TSAnyKeyword",
|
|
10
|
+
"TSArrayType",
|
|
11
|
+
"TSBigIntKeyword",
|
|
12
|
+
"TSBooleanKeyword",
|
|
13
|
+
"TSConditionalType",
|
|
14
|
+
"TSConstructorType",
|
|
15
|
+
"TSFunctionType",
|
|
16
|
+
"TSImportType",
|
|
17
|
+
"TSIndexedAccessType",
|
|
18
|
+
"TSInferType",
|
|
19
|
+
"TSIntersectionType",
|
|
20
|
+
"TSIntrinsicKeyword",
|
|
21
|
+
"TSLiteralType",
|
|
22
|
+
"TSMappedType",
|
|
23
|
+
"TSNamedTupleMember",
|
|
24
|
+
"TSNeverKeyword",
|
|
25
|
+
"TSNullKeyword",
|
|
26
|
+
"TSNumberKeyword",
|
|
27
|
+
"TSObjectKeyword",
|
|
28
|
+
"TSParenthesizedType",
|
|
29
|
+
"TSStringKeyword",
|
|
30
|
+
"TSSymbolKeyword",
|
|
31
|
+
"TSTemplateLiteralType",
|
|
32
|
+
"TSThisType",
|
|
33
|
+
"TSTupleType",
|
|
34
|
+
"TSTypeLiteral",
|
|
35
|
+
"TSTypeOperator",
|
|
36
|
+
"TSTypePredicate",
|
|
37
|
+
"TSTypeQuery",
|
|
38
|
+
"TSTypeReference",
|
|
39
|
+
"TSUndefinedKeyword",
|
|
40
|
+
"TSUnionType",
|
|
41
|
+
"TSUnknownKeyword",
|
|
42
|
+
"TSVoidKeyword"
|
|
43
|
+
]);
|
|
44
|
+
function isTypeNode(node) {
|
|
45
|
+
return typeNodeKinds.has(node.type);
|
|
46
|
+
}
|
|
47
|
+
function typeReferenceName(type) {
|
|
48
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
49
|
+
}
|
|
50
|
+
function isInsideTypeAliasDeclaration(node) {
|
|
51
|
+
let current = node.parent;
|
|
52
|
+
while (current !== null && current.type !== "Program") {
|
|
53
|
+
if (current.type === "TSTypeAliasDeclaration") return true;
|
|
54
|
+
current = current.parent;
|
|
55
|
+
}
|
|
56
|
+
return false;
|
|
57
|
+
}
|
|
58
|
+
function isPlainAliasConsumerUse(node, environment) {
|
|
59
|
+
if (node.type !== "TSTypeReference" || node.typeArguments?.params.length) return false;
|
|
60
|
+
const name = typeReferenceName(node);
|
|
61
|
+
return name !== null && visibleTypeAlias(name, node, environment.typeAliases) !== null && !isInsideTypeAliasDeclaration(node);
|
|
62
|
+
}
|
|
63
|
+
function isInsideTypeParameterConstraint(node) {
|
|
64
|
+
let child = node;
|
|
65
|
+
let parent = child.parent;
|
|
66
|
+
while (parent !== null && parent.type !== "Program") {
|
|
67
|
+
if (parent.type === "TSTypeParameter" && parent.constraint === child) return true;
|
|
68
|
+
child = parent;
|
|
69
|
+
parent = child.parent;
|
|
70
|
+
}
|
|
71
|
+
return false;
|
|
72
|
+
}
|
|
73
|
+
function shouldReportType(node, environment) {
|
|
74
|
+
if (isInsideTypeParameterConstraint(node)) return false;
|
|
75
|
+
if (isPlainAliasConsumerUse(node, environment)) return false;
|
|
76
|
+
if (classifyUnsafeDictionary(node, environment) === null) return false;
|
|
77
|
+
let current = node.parent;
|
|
78
|
+
while (current !== null && current.type !== "Program") {
|
|
79
|
+
if (isTypeNode(current) && classifyUnsafeDictionary(current, environment) !== null) return false;
|
|
80
|
+
current = current.parent;
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
/** Disallow object-dictionary contracts whose direct value type is an unsafe escape hatch. */
|
|
85
|
+
const noUnsafeDictionaryTypeRule = defineRule({
|
|
86
|
+
meta: {
|
|
87
|
+
type: "problem",
|
|
88
|
+
docs: { description: "Disallow object-dictionary contracts whose direct value type is unknown, any, object, {}, or a union/alias containing one of those escape hatches." },
|
|
89
|
+
messages: { unsafeDictionary: "This dictionary's {{value}} value type gives callers no concrete value contract. Use an owner/schema-derived value type; parse external payloads before insertion." }
|
|
90
|
+
},
|
|
91
|
+
createOnce(context) {
|
|
92
|
+
let environment = null;
|
|
93
|
+
const report = (node, value) => {
|
|
94
|
+
context.report({
|
|
95
|
+
node,
|
|
96
|
+
messageId: "unsafeDictionary",
|
|
97
|
+
data: { value }
|
|
98
|
+
});
|
|
99
|
+
};
|
|
100
|
+
const reportIfUnsafe = (node) => {
|
|
101
|
+
if (environment === null || !shouldReportType(node, environment)) return;
|
|
102
|
+
const unsafe = classifyUnsafeDictionary(node, environment);
|
|
103
|
+
if (unsafe === null) return;
|
|
104
|
+
report(node, unsafe.unsafeValue);
|
|
105
|
+
};
|
|
106
|
+
return {
|
|
107
|
+
Program(node) {
|
|
108
|
+
environment = createTypeEnvironment(node, context.sourceCode.visitorKeys);
|
|
109
|
+
},
|
|
110
|
+
TSTypeReference: reportIfUnsafe,
|
|
111
|
+
TSTypeLiteral: reportIfUnsafe,
|
|
112
|
+
TSMappedType: reportIfUnsafe,
|
|
113
|
+
TSIndexSignature(node) {
|
|
114
|
+
if (environment === null || node.typeAnnotation === null || node.parent.type === "TSTypeLiteral") return;
|
|
115
|
+
const unsafe = classifyUnsafeDictionaryValue(node.typeAnnotation.typeAnnotation, environment);
|
|
116
|
+
if (unsafe !== null) report(node, unsafe.unsafeValue);
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
export { noUnsafeDictionaryTypeRule };
|
|
@@ -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 };
|