@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,40 @@
|
|
|
1
|
+
import { isStringLiteral } from "../../shared/string-literal.mjs";
|
|
2
|
+
// src/lint/anti-slop/effect/shared/tagged-values.ts
|
|
3
|
+
const equalityOperators = /* @__PURE__ */ new Set([
|
|
4
|
+
"==",
|
|
5
|
+
"===",
|
|
6
|
+
"!=",
|
|
7
|
+
"!=="
|
|
8
|
+
]);
|
|
9
|
+
const broadEffectCatchMethods = /* @__PURE__ */ new Set([
|
|
10
|
+
"catch",
|
|
11
|
+
"catchAll",
|
|
12
|
+
"catchIf"
|
|
13
|
+
]);
|
|
14
|
+
const isTagMember = (node) => node?.type === "MemberExpression" && (!node.computed && node.property.type === "Identifier" && node.property.name === "_tag" || node.computed && isStringLiteral(node.property) && node.property.value === "_tag");
|
|
15
|
+
const tagMemberFromComparison = (node) => {
|
|
16
|
+
if (!equalityOperators.has(node.operator)) return void 0;
|
|
17
|
+
if (isTagMember(node.left) && isStringLiteral(node.right)) return node.left;
|
|
18
|
+
if (isTagMember(node.right) && isStringLiteral(node.left)) return node.right;
|
|
19
|
+
};
|
|
20
|
+
const isBroadEffectCatchCall = (node) => node?.type === "CallExpression" && node.callee.type === "MemberExpression" && node.callee.object.type === "Identifier" && node.callee.object.name === "Effect" && !node.callee.computed && node.callee.property.type === "Identifier" && broadEffectCatchMethods.has(node.callee.property.name);
|
|
21
|
+
const isInsideBroadEffectHandler = (node) => {
|
|
22
|
+
let current = node.parent;
|
|
23
|
+
while (current !== null && current !== void 0) {
|
|
24
|
+
if (current.type === "ArrowFunctionExpression" || current.type === "FunctionExpression") return isBroadEffectCatchCall(current.parent) && current.parent.arguments.includes(current);
|
|
25
|
+
current = current.parent;
|
|
26
|
+
}
|
|
27
|
+
return false;
|
|
28
|
+
};
|
|
29
|
+
const isReasonTagMember = (node) => node.object.type === "MemberExpression" && (!node.object.computed && node.object.property.type === "Identifier" && node.object.property.name === "reason" || node.object.computed && isStringLiteral(node.object.property) && node.object.property.value === "reason");
|
|
30
|
+
const propertyName = (property) => {
|
|
31
|
+
if (!property.computed && property.key.type === "Identifier") return property.key.name;
|
|
32
|
+
if (isStringLiteral(property.key)) return property.key.value;
|
|
33
|
+
};
|
|
34
|
+
const isMatchPatternObject = (node) => {
|
|
35
|
+
const call = node.parent;
|
|
36
|
+
if (call?.type !== "CallExpression" || !call.arguments.includes(node)) return false;
|
|
37
|
+
const callee = call.callee;
|
|
38
|
+
return callee.type === "MemberExpression" && callee.object.type === "Identifier" && callee.object.name === "Match" && !callee.computed && callee.property.type === "Identifier" && (callee.property.name === "when" || callee.property.name === "not");
|
|
39
|
+
};
|
|
40
|
+
export { isInsideBroadEffectHandler, isMatchPatternObject, isReasonTagMember, isTagMember, propertyName, tagMemberFromComparison };
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { noArrayFilterMapRule } from "./rules/no-array-filter-map.mjs";
|
|
2
|
+
import { noReduceAccumulatorCopyRule } from "./rules/no-reduce-accumulator-copy.mjs";
|
|
3
|
+
import { noChainedTypeAssertionsRule } from "./rules/no-chained-type-assertions.mjs";
|
|
4
|
+
import { noConditionalEmptyObjectSpreadRule } from "./rules/no-conditional-empty-object-spread.mjs";
|
|
5
|
+
import { noKnownValueWideningRule } from "./rules/no-known-value-widening.mjs";
|
|
6
|
+
import { noModuleMockingRule } from "./rules/no-module-mocking.mjs";
|
|
7
|
+
import { noObjectParametersRule } from "./rules/no-object-parameters.mjs";
|
|
8
|
+
import { noReflectApplyRule } from "./rules/no-reflect-apply.mjs";
|
|
9
|
+
import { noReflectGetRule } from "./rules/no-reflect-get.mjs";
|
|
10
|
+
import { noRuntimeTypeofRule } from "./rules/no-runtime-typeof.mjs";
|
|
11
|
+
import { noForbiddenTermInSymbolNamesRule } from "./rules/no-shape-in-symbol-names.mjs";
|
|
12
|
+
import { noUnknownParametersRule } from "./rules/no-unknown-parameters.mjs";
|
|
13
|
+
import { noUnknownReturnsRule } from "./rules/no-unknown-returns.mjs";
|
|
14
|
+
import { noUnknownTypeAliasesRule } from "./rules/no-unknown-type-aliases.mjs";
|
|
15
|
+
import { noUnsafeDictionaryTypeRule } from "./rules/no-unsafe-dictionary-type.mjs";
|
|
16
|
+
import { noWidenThenAssertRule } from "./rules/no-widen-then-assert.mjs";
|
|
17
|
+
import { requireReadableSpacingRule } from "./rules/require-readable-spacing.mjs";
|
|
18
|
+
import { requireSafetyCommentForTypeAssertionRule } from "./rules/require-safety-comment-for-type-assertion.mjs";
|
|
19
|
+
import { eslintCompatPlugin } from "vite-plus/lint/plugins";
|
|
20
|
+
// src/lint/anti-slop/plugin.ts
|
|
21
|
+
/** Generic Oxlint rules that reject low-evidence and low-signal implementation patterns. */
|
|
22
|
+
const antiSlopPlugin = eslintCompatPlugin({
|
|
23
|
+
meta: { name: "anti-slop" },
|
|
24
|
+
rules: {
|
|
25
|
+
"no-array-filter-map": noArrayFilterMapRule,
|
|
26
|
+
"no-reduce-accumulator-copy": noReduceAccumulatorCopyRule,
|
|
27
|
+
"no-chained-type-assertions": noChainedTypeAssertionsRule,
|
|
28
|
+
"no-conditional-empty-object-spread": noConditionalEmptyObjectSpreadRule,
|
|
29
|
+
"no-known-value-widening": noKnownValueWideningRule,
|
|
30
|
+
"no-module-mocking": noModuleMockingRule,
|
|
31
|
+
"no-object-parameters": noObjectParametersRule,
|
|
32
|
+
"no-reflect-apply": noReflectApplyRule,
|
|
33
|
+
"no-reflect-get": noReflectGetRule,
|
|
34
|
+
"no-runtime-typeof": noRuntimeTypeofRule,
|
|
35
|
+
"no-unsafe-dictionary-type": noUnsafeDictionaryTypeRule,
|
|
36
|
+
"no-shape-in-symbol-names": noForbiddenTermInSymbolNamesRule,
|
|
37
|
+
"no-unknown-parameters": noUnknownParametersRule,
|
|
38
|
+
"no-unknown-returns": noUnknownReturnsRule,
|
|
39
|
+
"no-unknown-type-aliases": noUnknownTypeAliasesRule,
|
|
40
|
+
"no-widen-then-assert": noWidenThenAssertRule,
|
|
41
|
+
"require-readable-spacing": requireReadableSpacingRule,
|
|
42
|
+
"require-safety-comment-for-type-assertion": requireSafetyCommentForTypeAssertionRule
|
|
43
|
+
}
|
|
44
|
+
});
|
|
45
|
+
export { antiSlopPlugin as default };
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { arrayMethodTarget, isKnownArrayExpression, unwrapArrayExpression } from "../shared/array-method.mjs";
|
|
2
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
3
|
+
// src/lint/anti-slop/rules/no-array-filter-map.ts
|
|
4
|
+
/** Reject eager array filter/map pipelines; lazy iterator helpers remain allowed. */
|
|
5
|
+
const noArrayFilterMapRule = defineRule({
|
|
6
|
+
meta: {
|
|
7
|
+
type: "suggestion",
|
|
8
|
+
docs: { description: "Disallow adjacent array filter/map passes in favor of lazy iterator helpers or a single transformation." },
|
|
9
|
+
messages: { arrayFilterMap: "Avoid consecutive array `{{first}}` and `{{second}}` passes. Prefer `.values().{{first}}(...).{{second}}(...).toArray()` where iterator helpers are supported, or a single `flatMap`/mutating reducer. Preserve callback ordering, indexes, and filtering semantics." }
|
|
10
|
+
},
|
|
11
|
+
createOnce(context) {
|
|
12
|
+
return { CallExpression(node) {
|
|
13
|
+
const outer = arrayMethodTarget(node.callee);
|
|
14
|
+
if (outer === null || outer.name !== "map" && outer.name !== "filter") return;
|
|
15
|
+
const innerCall = unwrapArrayExpression(outer.object);
|
|
16
|
+
if (innerCall.type !== "CallExpression") return;
|
|
17
|
+
const inner = arrayMethodTarget(innerCall.callee);
|
|
18
|
+
if (inner === null || inner.name !== (outer.name === "map" ? "filter" : "map")) return;
|
|
19
|
+
if (!isKnownArrayExpression(context.sourceCode, inner.object)) return;
|
|
20
|
+
context.report({
|
|
21
|
+
node,
|
|
22
|
+
messageId: "arrayFilterMap",
|
|
23
|
+
data: {
|
|
24
|
+
first: inner.name,
|
|
25
|
+
second: outer.name
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
} };
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
export { noArrayFilterMapRule };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
2
|
+
// src/lint/anti-slop/rules/no-chained-type-assertions.ts
|
|
3
|
+
function isTypeAssertionExpression(node) {
|
|
4
|
+
return node.type === "TSAsExpression" || node.type === "TSTypeAssertion";
|
|
5
|
+
}
|
|
6
|
+
function unwrapParenthesizedExpression(expression) {
|
|
7
|
+
let current = expression;
|
|
8
|
+
while (current.type === "ParenthesizedExpression") current = current.expression;
|
|
9
|
+
return current;
|
|
10
|
+
}
|
|
11
|
+
function isConstAssertion(node) {
|
|
12
|
+
const { typeAnnotation } = node;
|
|
13
|
+
return typeAnnotation.type === "TSTypeReference" && typeAnnotation.typeName.type === "Identifier" && typeAnnotation.typeName.name === "const";
|
|
14
|
+
}
|
|
15
|
+
function isOutermostAssertionInChain(node) {
|
|
16
|
+
let current = node;
|
|
17
|
+
let parent = node.parent;
|
|
18
|
+
while (parent.type === "ParenthesizedExpression" && parent.expression === current) {
|
|
19
|
+
current = parent;
|
|
20
|
+
parent = parent.parent;
|
|
21
|
+
}
|
|
22
|
+
return !isTypeAssertionExpression(parent) || parent.expression !== current;
|
|
23
|
+
}
|
|
24
|
+
function isForbiddenAssertionChain(node) {
|
|
25
|
+
let assertionCount = 0;
|
|
26
|
+
let hasNonConstAssertion = false;
|
|
27
|
+
let current = node;
|
|
28
|
+
while (isTypeAssertionExpression(current)) {
|
|
29
|
+
assertionCount += 1;
|
|
30
|
+
hasNonConstAssertion ||= !isConstAssertion(current);
|
|
31
|
+
current = unwrapParenthesizedExpression(current.expression);
|
|
32
|
+
}
|
|
33
|
+
return assertionCount > 1 && hasNonConstAssertion;
|
|
34
|
+
}
|
|
35
|
+
/** Disallow nested TypeScript type assertions, while permitting chains made only of const assertions. */
|
|
36
|
+
const noChainedTypeAssertionsRule = defineRule({
|
|
37
|
+
meta: {
|
|
38
|
+
type: "problem",
|
|
39
|
+
docs: { description: "Disallow chained TypeScript as and angle-bracket assertions, including parenthesized chains." },
|
|
40
|
+
messages: { chained: "This assertion chain discards type evidence. Keep the original precise type, or parse untrusted input at its boundary before narrowing it." }
|
|
41
|
+
},
|
|
42
|
+
createOnce(context) {
|
|
43
|
+
const checkTypeAssertion = (node) => {
|
|
44
|
+
if (!isOutermostAssertionInChain(node) || !isForbiddenAssertionChain(node)) return;
|
|
45
|
+
context.report({
|
|
46
|
+
node,
|
|
47
|
+
messageId: "chained"
|
|
48
|
+
});
|
|
49
|
+
};
|
|
50
|
+
return {
|
|
51
|
+
TSAsExpression: checkTypeAssertion,
|
|
52
|
+
TSTypeAssertion: checkTypeAssertion
|
|
53
|
+
};
|
|
54
|
+
}
|
|
55
|
+
});
|
|
56
|
+
export { noChainedTypeAssertionsRule };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
2
|
+
// src/lint/anti-slop/rules/no-conditional-empty-object-spread.ts
|
|
3
|
+
function unwrapParentheses(node) {
|
|
4
|
+
let current = node;
|
|
5
|
+
while (current.type === "ParenthesizedExpression") current = current.expression;
|
|
6
|
+
return current;
|
|
7
|
+
}
|
|
8
|
+
function isEmptyObjectExpression(node) {
|
|
9
|
+
return node.type === "ObjectExpression" && node.properties.length === 0;
|
|
10
|
+
}
|
|
11
|
+
function isConditionalEmptyObjectSpread(node) {
|
|
12
|
+
const conditional = unwrapParentheses(node);
|
|
13
|
+
return conditional.type === "ConditionalExpression" && (isEmptyObjectExpression(conditional.consequent) || isEmptyObjectExpression(conditional.alternate));
|
|
14
|
+
}
|
|
15
|
+
/** Ban conditional empty-object spreads without changing their omission semantics. */
|
|
16
|
+
const noConditionalEmptyObjectSpreadRule = defineRule({
|
|
17
|
+
meta: {
|
|
18
|
+
type: "suggestion",
|
|
19
|
+
docs: { description: "Disallow object spreads that conditionally spread an empty object to omit fields." },
|
|
20
|
+
messages: { avoid: "This conditional spread hides property omission behind an empty object. Build the object in separate statements and add the property only when present." }
|
|
21
|
+
},
|
|
22
|
+
createOnce(context) {
|
|
23
|
+
return { SpreadElement(node) {
|
|
24
|
+
if (node.parent.type !== "ObjectExpression") return;
|
|
25
|
+
if (isConditionalEmptyObjectSpread(node.argument)) context.report({
|
|
26
|
+
node,
|
|
27
|
+
messageId: "avoid"
|
|
28
|
+
});
|
|
29
|
+
} };
|
|
30
|
+
}
|
|
31
|
+
});
|
|
32
|
+
export { noConditionalEmptyObjectSpreadRule };
|
|
@@ -0,0 +1,236 @@
|
|
|
1
|
+
import { classifyUnsafeDictionaryValue, classifyWideningTarget, createTypeEnvironment, isKnownEvidenceExpression } from "../shared/dictionary-types.mjs";
|
|
2
|
+
import { containsUnknownType, functionParameterBindingName, functionParameterTypeAnnotation } from "../shared/function-parameters.mjs";
|
|
3
|
+
import { resolveVariable } from "../shared/scope.mjs";
|
|
4
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
5
|
+
// src/lint/anti-slop/rules/no-known-value-widening.ts
|
|
6
|
+
function unwrapExpression(expression) {
|
|
7
|
+
let current = expression;
|
|
8
|
+
while (current.type === "ParenthesizedExpression" || current.type === "TSAsExpression" || current.type === "TSSatisfiesExpression" || current.type === "TSTypeAssertion" || current.type === "TSNonNullExpression") current = current.expression;
|
|
9
|
+
return current;
|
|
10
|
+
}
|
|
11
|
+
function variableDeclarator(variable) {
|
|
12
|
+
if (variable.defs.length !== 1) return null;
|
|
13
|
+
const [definition] = variable.defs;
|
|
14
|
+
return definition?.type === "Variable" && definition.node.type === "VariableDeclarator" ? definition.node : null;
|
|
15
|
+
}
|
|
16
|
+
function isStableConstVariable(variable, declarator) {
|
|
17
|
+
return declarator.parent.type === "VariableDeclaration" && declarator.parent.kind === "const" && variable.references.every((reference) => reference.init || !reference.isWrite());
|
|
18
|
+
}
|
|
19
|
+
function hasKnownEvidence(sourceCode, expression, visitedVariables = /* @__PURE__ */ new Set()) {
|
|
20
|
+
if (isKnownEvidenceExpression(expression)) return true;
|
|
21
|
+
const unwrapped = unwrapExpression(expression);
|
|
22
|
+
if (unwrapped.type !== "Identifier") return false;
|
|
23
|
+
const variable = resolveVariable(sourceCode, unwrapped);
|
|
24
|
+
if (variable === null || visitedVariables.has(variable)) return false;
|
|
25
|
+
const declarator = variableDeclarator(variable);
|
|
26
|
+
if (declarator === null || declarator.init === null || !isStableConstVariable(variable, declarator)) return false;
|
|
27
|
+
visitedVariables.add(variable);
|
|
28
|
+
return hasKnownEvidence(sourceCode, declarator.init, visitedVariables);
|
|
29
|
+
}
|
|
30
|
+
function isFunctionExpression(node) {
|
|
31
|
+
return node.type === "ArrowFunctionExpression" || node.type === "FunctionDeclaration" || node.type === "FunctionExpression" || node.type === "TSDeclareFunction" || node.type === "TSEmptyBodyFunctionExpression";
|
|
32
|
+
}
|
|
33
|
+
function localFunctionForCall(sourceCode, callee) {
|
|
34
|
+
const unwrapped = unwrapExpression(callee);
|
|
35
|
+
if (isFunctionExpression(unwrapped)) return unwrapped;
|
|
36
|
+
if (unwrapped.type !== "Identifier") return null;
|
|
37
|
+
const variable = resolveVariable(sourceCode, unwrapped);
|
|
38
|
+
if (variable === null || variable.defs.length !== 1) return null;
|
|
39
|
+
const [definition] = variable.defs;
|
|
40
|
+
if (definition === void 0) return null;
|
|
41
|
+
if (definition.type === "FunctionName" && isFunctionExpression(definition.node)) return definition.node;
|
|
42
|
+
if (definition.type !== "Variable" || definition.node.type !== "VariableDeclarator") return null;
|
|
43
|
+
const initializer = definition.node.init;
|
|
44
|
+
if (initializer === null) return null;
|
|
45
|
+
const unwrappedInitializer = unwrapExpression(initializer);
|
|
46
|
+
return isFunctionExpression(unwrappedInitializer) ? unwrappedInitializer : null;
|
|
47
|
+
}
|
|
48
|
+
function variableTypeAnnotation(sourceCode, variable) {
|
|
49
|
+
if (variable.defs.length !== 1) return null;
|
|
50
|
+
const [definition] = variable.defs;
|
|
51
|
+
if (definition === void 0) return null;
|
|
52
|
+
if (definition.type === "Variable" && definition.node.type === "VariableDeclarator" && definition.node.id.type === "Identifier") return definition.node.id.typeAnnotation ?? null;
|
|
53
|
+
if (definition.type !== "Parameter" || !isFunctionExpression(definition.node)) return null;
|
|
54
|
+
const parameter = definition.node.params.find((candidate) => functionParameterBindingName(candidate, sourceCode) === variable.name);
|
|
55
|
+
return parameter === void 0 ? null : functionParameterTypeAnnotation(parameter) ?? null;
|
|
56
|
+
}
|
|
57
|
+
function hasInformativeType(type, environment) {
|
|
58
|
+
return classifyUnsafeDictionaryValue(type, environment) === null;
|
|
59
|
+
}
|
|
60
|
+
function hasKnownCallArgumentEvidence(sourceCode, expression, environment, visitedVariables = /* @__PURE__ */ new Set()) {
|
|
61
|
+
if (expression.type === "ParenthesizedExpression" || expression.type === "TSNonNullExpression") return hasKnownCallArgumentEvidence(sourceCode, expression.expression, environment, visitedVariables);
|
|
62
|
+
if (expression.type === "TSAsExpression" || expression.type === "TSTypeAssertion") return hasInformativeType(expression.typeAnnotation, environment);
|
|
63
|
+
if (expression.type === "TSSatisfiesExpression") return hasKnownCallArgumentEvidence(sourceCode, expression.expression, environment, visitedVariables);
|
|
64
|
+
if (expression.type === "CallExpression") {
|
|
65
|
+
const returnType = localFunctionForCall(sourceCode, expression.callee)?.returnType?.typeAnnotation;
|
|
66
|
+
return returnType !== void 0 && hasInformativeType(returnType, environment);
|
|
67
|
+
}
|
|
68
|
+
if (expression.type !== "Identifier") return isKnownEvidenceExpression(expression);
|
|
69
|
+
const variable = resolveVariable(sourceCode, expression);
|
|
70
|
+
if (variable === null || visitedVariables.has(variable)) return false;
|
|
71
|
+
const annotation = variableTypeAnnotation(sourceCode, variable);
|
|
72
|
+
if (annotation !== null) return hasInformativeType(annotation.typeAnnotation, environment);
|
|
73
|
+
const declarator = variableDeclarator(variable);
|
|
74
|
+
if (declarator === null || declarator.init === null || !isStableConstVariable(variable, declarator)) return false;
|
|
75
|
+
visitedVariables.add(variable);
|
|
76
|
+
return hasKnownCallArgumentEvidence(sourceCode, declarator.init, environment, visitedVariables);
|
|
77
|
+
}
|
|
78
|
+
function typePredicateSubjectIndex(sourceCode, owner) {
|
|
79
|
+
const predicate = owner.returnType?.typeAnnotation;
|
|
80
|
+
if (predicate?.type !== "TSTypePredicate" || predicate.parameterName.type !== "Identifier") return null;
|
|
81
|
+
const predicateParameterName = predicate.parameterName.name;
|
|
82
|
+
const index = owner.params.findIndex((parameter) => functionParameterBindingName(parameter, sourceCode) === predicateParameterName);
|
|
83
|
+
return index === -1 ? null : index;
|
|
84
|
+
}
|
|
85
|
+
function annotationTarget(annotation, environment) {
|
|
86
|
+
return annotation === null || annotation === void 0 ? null : classifyWideningTarget(annotation.typeAnnotation, environment);
|
|
87
|
+
}
|
|
88
|
+
function enclosingFunction(node) {
|
|
89
|
+
let current = node.parent;
|
|
90
|
+
while (current !== null && current.type !== "Program") {
|
|
91
|
+
if (current.type === "ArrowFunctionExpression" || current.type === "FunctionDeclaration" || current.type === "FunctionExpression") return current;
|
|
92
|
+
current = current.parent;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
function sourceKeyName(sourceCode, key) {
|
|
97
|
+
if (key.type === "Identifier" || key.type === "PrivateIdentifier") return key.name;
|
|
98
|
+
if (key.type === "Literal") return String(key.value);
|
|
99
|
+
return sourceCode.getText(key);
|
|
100
|
+
}
|
|
101
|
+
function functionName(sourceCode, owner) {
|
|
102
|
+
if (owner === null) return "anonymous function";
|
|
103
|
+
if (owner.id !== null) return owner.id.name;
|
|
104
|
+
const parent = owner.parent;
|
|
105
|
+
if (parent.type === "VariableDeclarator" && parent.id.type === "Identifier") return parent.id.name;
|
|
106
|
+
if (parent.type === "MethodDefinition") return sourceKeyName(sourceCode, parent.key);
|
|
107
|
+
return "anonymous function";
|
|
108
|
+
}
|
|
109
|
+
function isEmptyObjectExpression(expression) {
|
|
110
|
+
const unwrapped = unwrapExpression(expression);
|
|
111
|
+
return unwrapped.type === "ObjectExpression" && unwrapped.properties.length === 0;
|
|
112
|
+
}
|
|
113
|
+
function isDictionaryAccumulatorTarget(destination) {
|
|
114
|
+
return destination.kind === "open dictionary" || destination.kind === "generic container";
|
|
115
|
+
}
|
|
116
|
+
function hasParentAssertion(node) {
|
|
117
|
+
return node.parent?.type === "TSAsExpression" || node.parent?.type === "TSTypeAssertion";
|
|
118
|
+
}
|
|
119
|
+
/** Detect sound syntactic cases where a known value is explicitly widened and loses evidence. */
|
|
120
|
+
const noKnownValueWideningRule = defineRule({
|
|
121
|
+
meta: {
|
|
122
|
+
type: "problem",
|
|
123
|
+
docs: { description: "Disallow syntactically established values from flowing into explicitly broad or anonymous target types that discard useful evidence." },
|
|
124
|
+
messages: { widening: "The explicit {{target}} type on {{subject}} discards known type evidence. Keep inference, validate with `satisfies`, or use a named owner contract." }
|
|
125
|
+
},
|
|
126
|
+
createOnce(context) {
|
|
127
|
+
let environment = null;
|
|
128
|
+
let calls = [];
|
|
129
|
+
let assignments = [];
|
|
130
|
+
let declaresPredicate = false;
|
|
131
|
+
let declaresWideningBinding = false;
|
|
132
|
+
const notePredicate = (node) => {
|
|
133
|
+
if (node.returnType?.typeAnnotation.type === "TSTypePredicate") declaresPredicate = true;
|
|
134
|
+
};
|
|
135
|
+
const reportFlow = (expression, destination, subject) => {
|
|
136
|
+
if (destination === null) return;
|
|
137
|
+
if (isDictionaryAccumulatorTarget(destination) && isEmptyObjectExpression(expression)) return;
|
|
138
|
+
if (!hasKnownEvidence(context.sourceCode, expression)) return;
|
|
139
|
+
context.report({
|
|
140
|
+
node: expression,
|
|
141
|
+
messageId: "widening",
|
|
142
|
+
data: {
|
|
143
|
+
subject,
|
|
144
|
+
target: destination.kind
|
|
145
|
+
}
|
|
146
|
+
});
|
|
147
|
+
};
|
|
148
|
+
const targetFromAnnotation = (annotation) => environment === null ? null : annotationTarget(annotation, environment);
|
|
149
|
+
const checkCall = (node) => {
|
|
150
|
+
if (environment === null) return;
|
|
151
|
+
const owner = localFunctionForCall(context.sourceCode, node.callee);
|
|
152
|
+
if (owner === null) return;
|
|
153
|
+
const parameterIndex = typePredicateSubjectIndex(context.sourceCode, owner);
|
|
154
|
+
if (parameterIndex === null) return;
|
|
155
|
+
const parameter = owner.params[parameterIndex];
|
|
156
|
+
const argument = node.arguments[parameterIndex];
|
|
157
|
+
if (parameter === void 0 || argument === void 0 || argument.type === "SpreadElement") return;
|
|
158
|
+
const parameterAnnotation = functionParameterTypeAnnotation(parameter);
|
|
159
|
+
if (parameterAnnotation === null || parameterAnnotation === void 0 || !containsUnknownType(parameterAnnotation.typeAnnotation)) return;
|
|
160
|
+
if (!hasKnownCallArgumentEvidence(context.sourceCode, argument, environment)) return;
|
|
161
|
+
context.report({
|
|
162
|
+
node: argument,
|
|
163
|
+
messageId: "widening",
|
|
164
|
+
data: {
|
|
165
|
+
subject: `argument for parameter \`${functionParameterBindingName(parameter, context.sourceCode)}\` of \`${functionName(context.sourceCode, owner)}\``,
|
|
166
|
+
target: "unknown"
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
};
|
|
170
|
+
const checkAssignment = (node) => {
|
|
171
|
+
if (node.left.type !== "Identifier") return;
|
|
172
|
+
const variable = resolveVariable(context.sourceCode, node.left);
|
|
173
|
+
if (variable === null) return;
|
|
174
|
+
const declarator = variableDeclarator(variable);
|
|
175
|
+
if (declarator === null || declarator.id.type !== "Identifier") return;
|
|
176
|
+
reportFlow(node.right, targetFromAnnotation(declarator.id.typeAnnotation), `binding \`${declarator.id.name}\``);
|
|
177
|
+
};
|
|
178
|
+
return {
|
|
179
|
+
Program(node) {
|
|
180
|
+
calls = [];
|
|
181
|
+
assignments = [];
|
|
182
|
+
declaresPredicate = false;
|
|
183
|
+
declaresWideningBinding = false;
|
|
184
|
+
environment = createTypeEnvironment(node, context.sourceCode.visitorKeys);
|
|
185
|
+
},
|
|
186
|
+
VariableDeclarator(node) {
|
|
187
|
+
if (node.id.type !== "Identifier") return;
|
|
188
|
+
const target = targetFromAnnotation(node.id.typeAnnotation);
|
|
189
|
+
if (target !== null) declaresWideningBinding = true;
|
|
190
|
+
if (node.init === null) return;
|
|
191
|
+
reportFlow(node.init, target, `binding \`${node.id.name}\``);
|
|
192
|
+
},
|
|
193
|
+
PropertyDefinition(node) {
|
|
194
|
+
if (node.value === null) return;
|
|
195
|
+
reportFlow(node.value, targetFromAnnotation(node.typeAnnotation), `property \`${sourceKeyName(context.sourceCode, node.key)}\``);
|
|
196
|
+
},
|
|
197
|
+
AccessorProperty(node) {
|
|
198
|
+
if (node.value === null) return;
|
|
199
|
+
reportFlow(node.value, targetFromAnnotation(node.typeAnnotation), `property \`${sourceKeyName(context.sourceCode, node.key)}\``);
|
|
200
|
+
},
|
|
201
|
+
AssignmentExpression(node) {
|
|
202
|
+
if (node.operator === "=" && node.left.type === "Identifier") assignments.push(node);
|
|
203
|
+
},
|
|
204
|
+
CallExpression(node) {
|
|
205
|
+
calls.push(node);
|
|
206
|
+
},
|
|
207
|
+
"Program:exit"() {
|
|
208
|
+
if (declaresPredicate) for (const call of calls) checkCall(call);
|
|
209
|
+
if (declaresWideningBinding) for (const assignment of assignments) checkAssignment(assignment);
|
|
210
|
+
},
|
|
211
|
+
FunctionDeclaration: notePredicate,
|
|
212
|
+
FunctionExpression: notePredicate,
|
|
213
|
+
TSDeclareFunction: notePredicate,
|
|
214
|
+
TSEmptyBodyFunctionExpression: notePredicate,
|
|
215
|
+
ReturnStatement(node) {
|
|
216
|
+
if (node.argument === null) return;
|
|
217
|
+
const owner = enclosingFunction(node);
|
|
218
|
+
reportFlow(node.argument, targetFromAnnotation(owner?.returnType), `return value of \`${functionName(context.sourceCode, owner)}\``);
|
|
219
|
+
},
|
|
220
|
+
ArrowFunctionExpression(node) {
|
|
221
|
+
notePredicate(node);
|
|
222
|
+
if (node.body.type === "BlockStatement") return;
|
|
223
|
+
reportFlow(node.body, targetFromAnnotation(node.returnType), `return value of \`${functionName(context.sourceCode, node)}\``);
|
|
224
|
+
},
|
|
225
|
+
TSAsExpression(node) {
|
|
226
|
+
if (environment === null || hasParentAssertion(node)) return;
|
|
227
|
+
reportFlow(node.expression, classifyWideningTarget(node.typeAnnotation, environment), "assertion");
|
|
228
|
+
},
|
|
229
|
+
TSTypeAssertion(node) {
|
|
230
|
+
if (environment === null || hasParentAssertion(node)) return;
|
|
231
|
+
reportFlow(node.expression, classifyWideningTarget(node.typeAnnotation, environment), "assertion");
|
|
232
|
+
}
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
});
|
|
236
|
+
export { noKnownValueWideningRule };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { resolveVariable } from "../shared/scope.mjs";
|
|
2
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
3
|
+
// src/lint/anti-slop/rules/no-module-mocking.ts
|
|
4
|
+
const moduleMockMethods = /* @__PURE__ */ new Set([
|
|
5
|
+
"doMock",
|
|
6
|
+
"mock",
|
|
7
|
+
"unstable_mockModule"
|
|
8
|
+
]);
|
|
9
|
+
function importedName(node) {
|
|
10
|
+
if (node.type !== "ImportSpecifier") return null;
|
|
11
|
+
return node.imported.type === "Identifier" ? node.imported.name : node.imported.value;
|
|
12
|
+
}
|
|
13
|
+
function isTestFrameworkObject(sourceCode, expression) {
|
|
14
|
+
if (expression.type !== "Identifier") return false;
|
|
15
|
+
if ((expression.name === "vi" || expression.name === "jest") && sourceCode.isGlobalReference(expression)) return true;
|
|
16
|
+
const variable = resolveVariable(sourceCode, expression);
|
|
17
|
+
if (variable === null || variable.defs.length === 0) return expression.name === "vi" || expression.name === "jest";
|
|
18
|
+
return variable.defs.some((definition) => {
|
|
19
|
+
if (definition.type !== "ImportBinding" || definition.parent?.type !== "ImportDeclaration") return false;
|
|
20
|
+
const source = definition.parent.source.value;
|
|
21
|
+
const name = importedName(definition.node);
|
|
22
|
+
return source === "vitest" && name === "vi" || source === "@jest/globals" && name === "jest";
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
function moduleMockCall(sourceCode, callee) {
|
|
26
|
+
if (!("property" in callee) || !("object" in callee) || !("computed" in callee)) return false;
|
|
27
|
+
const property = callee.property;
|
|
28
|
+
const method = callee.computed ? property.type === "Literal" && (property.value === "doMock" || property.value === "mock" || property.value === "unstable_mockModule") ? property.value : null : property.type === "Identifier" ? property.name : null;
|
|
29
|
+
return method !== null && moduleMockMethods.has(method) && isTestFrameworkObject(sourceCode, callee.object);
|
|
30
|
+
}
|
|
31
|
+
/** Ban test framework module mocking in favor of real dependency seams. */
|
|
32
|
+
const noModuleMockingRule = defineRule({
|
|
33
|
+
meta: {
|
|
34
|
+
type: "problem",
|
|
35
|
+
docs: { description: "Disallow Vitest and Jest module mocking; tests must replace dependencies through real interfaces." },
|
|
36
|
+
messages: { moduleMock: "Replace module mocking with dependency injection through a real interface, service layer, or faithful test implementation." }
|
|
37
|
+
},
|
|
38
|
+
createOnce(context) {
|
|
39
|
+
return { CallExpression(node) {
|
|
40
|
+
if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression") return;
|
|
41
|
+
if (moduleMockCall(context.sourceCode, node.callee)) context.report({
|
|
42
|
+
node,
|
|
43
|
+
messageId: "moduleMock"
|
|
44
|
+
});
|
|
45
|
+
} };
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
export { noModuleMockingRule };
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { functionParameterBindingName, functionParameterTypeAnnotation } from "../shared/function-parameters.mjs";
|
|
2
|
+
import { createTypeAliasEnvironment, resolvedTypeMatches } from "../shared/type-alias-resolution.mjs";
|
|
3
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
4
|
+
// src/lint/anti-slop/rules/no-object-parameters.ts
|
|
5
|
+
/** Ban the broad object type on function inputs, including local aliases to object. */
|
|
6
|
+
const noObjectParametersRule = defineRule({
|
|
7
|
+
meta: {
|
|
8
|
+
type: "problem",
|
|
9
|
+
docs: { description: "Disallow object function parameters; inputs must use an owner-provided type and be parsed at their boundary." },
|
|
10
|
+
messages: { objectParameter: "Parameter `{{parameter}}` uses the broad `object` type. Accept a named owner type; parse external input at its boundary before calling this function." }
|
|
11
|
+
},
|
|
12
|
+
createOnce(context) {
|
|
13
|
+
let environment = null;
|
|
14
|
+
const resolvesToObject = (type) => environment !== null && resolvedTypeMatches(type, environment, (resolved, matches) => {
|
|
15
|
+
if (resolved.type === "TSObjectKeyword") return true;
|
|
16
|
+
if (resolved.type === "TSParenthesizedType") return matches(resolved.typeAnnotation);
|
|
17
|
+
return resolved.type === "TSUnionType" && resolved.types.some(matches);
|
|
18
|
+
});
|
|
19
|
+
const checkParameters = (node) => {
|
|
20
|
+
for (const parameter of node.params) {
|
|
21
|
+
const annotation = functionParameterTypeAnnotation(parameter);
|
|
22
|
+
if (annotation === null || annotation === void 0) continue;
|
|
23
|
+
if (!resolvesToObject(annotation.typeAnnotation)) continue;
|
|
24
|
+
context.report({
|
|
25
|
+
node: annotation.typeAnnotation,
|
|
26
|
+
messageId: "objectParameter",
|
|
27
|
+
data: { parameter: functionParameterBindingName(parameter, context.sourceCode) }
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
return {
|
|
32
|
+
Program(node) {
|
|
33
|
+
environment = createTypeAliasEnvironment(node, context.sourceCode.visitorKeys);
|
|
34
|
+
},
|
|
35
|
+
ArrowFunctionExpression: checkParameters,
|
|
36
|
+
FunctionDeclaration: checkParameters,
|
|
37
|
+
FunctionExpression: checkParameters,
|
|
38
|
+
TSCallSignatureDeclaration: checkParameters,
|
|
39
|
+
TSConstructSignatureDeclaration: checkParameters,
|
|
40
|
+
TSConstructorType: checkParameters,
|
|
41
|
+
TSDeclareFunction: checkParameters,
|
|
42
|
+
TSEmptyBodyFunctionExpression: checkParameters,
|
|
43
|
+
TSFunctionType: checkParameters,
|
|
44
|
+
TSMethodSignature: checkParameters
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
export { noObjectParametersRule };
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { arrayMethodTarget, isKnownArrayExpression, resolveArrayBinding, unwrapArrayExpression } from "../shared/array-method.mjs";
|
|
2
|
+
import { defineRule } from "vite-plus/lint/plugins";
|
|
3
|
+
// src/lint/anti-slop/rules/no-reduce-accumulator-copy.ts
|
|
4
|
+
function enclosingReducer(node) {
|
|
5
|
+
let parent = node.parent;
|
|
6
|
+
while (parent !== null) {
|
|
7
|
+
if (parent.type === "FunctionDeclaration") return null;
|
|
8
|
+
if (parent.type === "ArrowFunctionExpression" || parent.type === "FunctionExpression") {
|
|
9
|
+
const callback = parent;
|
|
10
|
+
let owner = callback.parent;
|
|
11
|
+
while (owner !== null && unwrapArrayExpression(owner) === callback) owner = owner.parent;
|
|
12
|
+
if (owner?.type !== "CallExpression") return null;
|
|
13
|
+
const method = arrayMethodTarget(owner.callee);
|
|
14
|
+
const firstArgument = owner.arguments[0];
|
|
15
|
+
if (method === null || method.name !== "reduce" && method.name !== "reduceRight" || owner.arguments.length > 2 || firstArgument === void 0 || unwrapArrayExpression(firstArgument) !== callback) return null;
|
|
16
|
+
const firstParameter = callback.params[0];
|
|
17
|
+
const accumulator = firstParameter?.type === "AssignmentPattern" ? firstParameter.left : firstParameter;
|
|
18
|
+
if (accumulator?.type !== "Identifier") return null;
|
|
19
|
+
return {
|
|
20
|
+
callback,
|
|
21
|
+
accumulator,
|
|
22
|
+
initialValue: owner.arguments[1]
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
parent = parent.parent;
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
function referencesAccumulator(sourceCode, node, accumulator, visited = /* @__PURE__ */ new Set()) {
|
|
30
|
+
const variable = resolveArrayBinding(sourceCode, node);
|
|
31
|
+
if (variable === null || visited.has(variable)) return false;
|
|
32
|
+
if (variable === accumulator) return true;
|
|
33
|
+
visited.add(variable);
|
|
34
|
+
if (variable.references.some((reference) => reference.isWrite() && !reference.init)) return false;
|
|
35
|
+
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 referencesAccumulator(sourceCode, definition.node.init, accumulator, visited);
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
function isGlobalCopyOwner(sourceCode, node, name) {
|
|
39
|
+
node = unwrapArrayExpression(node);
|
|
40
|
+
if (node.type !== "Identifier" || node.name !== name) return false;
|
|
41
|
+
const variable = resolveArrayBinding(sourceCode, node);
|
|
42
|
+
return variable === null || variable.defs.length === 0;
|
|
43
|
+
}
|
|
44
|
+
/** Reject non-spread copies of reducer accumulators; pair with oxc/no-accumulating-spread. */
|
|
45
|
+
const noReduceAccumulatorCopyRule = defineRule({
|
|
46
|
+
meta: {
|
|
47
|
+
type: "problem",
|
|
48
|
+
docs: { description: "Disallow copying growing reducer accumulators with Object.assign, Array.from, or array copy methods." },
|
|
49
|
+
messages: { accumulatorCopy: "Do not copy the reducer accumulator on every iteration; growing copies can cause quadratic work. Mutate a fresh, locally owned accumulator and return it, or use an iterator pipeline/flatMap." }
|
|
50
|
+
},
|
|
51
|
+
createOnce(context) {
|
|
52
|
+
return { CallExpression(node) {
|
|
53
|
+
const method = arrayMethodTarget(node.callee);
|
|
54
|
+
if (method === null) return;
|
|
55
|
+
const reducer = enclosingReducer(node);
|
|
56
|
+
if (reducer === null) return;
|
|
57
|
+
const accumulator = context.sourceCode.getDeclaredVariables(reducer.callback).find((variable) => variable.identifiers.some((identifier) => identifier.start === reducer.accumulator.start));
|
|
58
|
+
if (accumulator === void 0) return;
|
|
59
|
+
const isAccumulator = (expression) => referencesAccumulator(context.sourceCode, expression, accumulator);
|
|
60
|
+
let copiesAccumulator = false;
|
|
61
|
+
if (method.name === "assign" && isGlobalCopyOwner(context.sourceCode, method.object, "Object")) {
|
|
62
|
+
const target = node.arguments[0];
|
|
63
|
+
copiesAccumulator = target !== void 0 && unwrapArrayExpression(target).type === "ObjectExpression" && node.arguments.slice(1).some(isAccumulator);
|
|
64
|
+
} else if (method.name === "from" && isGlobalCopyOwner(context.sourceCode, method.object, "Array")) {
|
|
65
|
+
const source = node.arguments[0];
|
|
66
|
+
copiesAccumulator = source !== void 0 && isAccumulator(source);
|
|
67
|
+
} else if ([
|
|
68
|
+
"concat",
|
|
69
|
+
"slice",
|
|
70
|
+
"toSpliced",
|
|
71
|
+
"toSorted",
|
|
72
|
+
"toReversed",
|
|
73
|
+
"with"
|
|
74
|
+
].includes(method.name)) {
|
|
75
|
+
const initialValue = reducer.initialValue;
|
|
76
|
+
copiesAccumulator = initialValue !== void 0 && isKnownArrayExpression(context.sourceCode, initialValue) && isAccumulator(method.object);
|
|
77
|
+
}
|
|
78
|
+
if (copiesAccumulator) context.report({
|
|
79
|
+
node,
|
|
80
|
+
messageId: "accumulatorCopy"
|
|
81
|
+
});
|
|
82
|
+
} };
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
export { noReduceAccumulatorCopyRule };
|
|
@@ -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-apply.ts
|
|
4
|
+
/** Ban Reflect.apply, which bypasses ordinary typed function calls. */
|
|
5
|
+
const noReflectApplyRule = defineRule({
|
|
6
|
+
meta: {
|
|
7
|
+
type: "problem",
|
|
8
|
+
docs: { description: "Disallow Reflect.apply; call typed functions directly or model dynamic dispatch behind an interface." },
|
|
9
|
+
messages: { reflectApply: "Replace `Reflect.apply` with a typed function call. Model dynamic dispatch behind a named interface." }
|
|
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, "apply")) context.report({
|
|
15
|
+
node,
|
|
16
|
+
messageId: "reflectApply"
|
|
17
|
+
});
|
|
18
|
+
} };
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
export { noReflectApplyRule };
|