@timmo001/oxlint-rules 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (51) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +102 -0
  3. package/THIRD_PARTY_NOTICES.md +10 -0
  4. package/dist/cli.js +78 -0
  5. package/dist/configs/effect.js +52 -0
  6. package/dist/configs/recommended.js +35 -0
  7. package/dist/effect/index.js +106 -0
  8. package/dist/upstream/anti-slop.js +1838 -0
  9. package/dist/upstream/effect.js +59 -0
  10. package/package.json +61 -0
  11. package/skills/add-oxlint-rule/SKILL.md +31 -0
  12. package/skills/install-timmo-oxlint-rules/SKILL.md +45 -0
  13. package/skills/install-timmo-oxlint-rules/scripts/copy.mjs +30 -0
  14. package/src/cli.ts +35 -0
  15. package/src/configs/effect.ts +20 -0
  16. package/src/configs/recommended.ts +29 -0
  17. package/src/effect/index.ts +12 -0
  18. package/src/effect/rules/no-try-catch-in-effect-generators.ts +140 -0
  19. package/src/install/copy.ts +77 -0
  20. package/src/jsr/effect-config.ts +7 -0
  21. package/src/jsr/effect.ts +7 -0
  22. package/src/jsr/recommended.ts +7 -0
  23. package/src/jsr/upstream-anti-slop.ts +7 -0
  24. package/src/jsr/upstream-effect.ts +7 -0
  25. package/src/upstream/anti-slop.ts +1 -0
  26. package/src/upstream/effect.ts +1 -0
  27. package/vendor/anti-slop/LICENSE +21 -0
  28. package/vendor/anti-slop/README.md +299 -0
  29. package/vendor/anti-slop/src/effect/index.ts +13 -0
  30. package/vendor/anti-slop/src/effect/rules/no-service-constructor-imports.ts +52 -0
  31. package/vendor/anti-slop/src/index.ts +41 -0
  32. package/vendor/anti-slop/src/rules/no-chained-type-assertions.ts +77 -0
  33. package/vendor/anti-slop/src/rules/no-conditional-empty-object-spread.ts +49 -0
  34. package/vendor/anti-slop/src/rules/no-known-value-widening.ts +427 -0
  35. package/vendor/anti-slop/src/rules/no-module-mocking.ts +91 -0
  36. package/vendor/anti-slop/src/rules/no-object-parameters.ts +83 -0
  37. package/vendor/anti-slop/src/rules/no-reflect-apply.ts +28 -0
  38. package/vendor/anti-slop/src/rules/no-reflect-get.ts +28 -0
  39. package/vendor/anti-slop/src/rules/no-runtime-typeof.ts +77 -0
  40. package/vendor/anti-slop/src/rules/no-shape-in-symbol-names.ts +46 -0
  41. package/vendor/anti-slop/src/rules/no-unknown-parameters.ts +69 -0
  42. package/vendor/anti-slop/src/rules/no-unknown-returns.ts +82 -0
  43. package/vendor/anti-slop/src/rules/no-unknown-type-aliases.ts +54 -0
  44. package/vendor/anti-slop/src/rules/no-unsafe-dictionary-type.ts +154 -0
  45. package/vendor/anti-slop/src/rules/no-widen-then-assert.ts +366 -0
  46. package/vendor/anti-slop/src/rules/require-safety-comment-for-type-assertion.ts +131 -0
  47. package/vendor/anti-slop/src/shared/dictionary-types.ts +515 -0
  48. package/vendor/anti-slop/src/shared/function-parameters.ts +49 -0
  49. package/vendor/anti-slop/src/shared/lexical-type-parameters.ts +61 -0
  50. package/vendor/anti-slop/src/shared/reflect-method.ts +35 -0
  51. package/vendor/anti-slop/src/shared/type-alias-resolution.ts +250 -0
@@ -0,0 +1,77 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import type { ESTree } from "@oxlint/plugins";
4
+
5
+ type RuntimeFunction = ESTree.ArrowFunctionExpression | ESTree.Function;
6
+
7
+ function isRuntimeFunction(node: ESTree.Node): node is RuntimeFunction {
8
+ return (
9
+ node.type === "ArrowFunctionExpression" ||
10
+ node.type === "FunctionDeclaration" ||
11
+ node.type === "FunctionExpression"
12
+ );
13
+ }
14
+
15
+ function isInsideTypeGuard(node: ESTree.Node): boolean {
16
+ let current: ESTree.Node | null = node.parent;
17
+ while (current !== null && current.type !== "Program") {
18
+ if (isRuntimeFunction(current)) {
19
+ return current.returnType?.typeAnnotation.type === "TSTypePredicate";
20
+ }
21
+ current = current.parent;
22
+ }
23
+ return false;
24
+ }
25
+
26
+ /** Return whether typeof safely probes for the existence of a possibly absent binding. */
27
+ function isExistenceProbe(node: ESTree.UnaryExpression): boolean {
28
+ const parent = node.parent;
29
+ if (parent.type !== "BinaryExpression") return false;
30
+ if (!["===", "!==", "==", "!="].includes(parent.operator)) return false;
31
+ const other = parent.left === node ? parent.right : parent.left;
32
+ return other.type === "Literal" && other.value === "undefined";
33
+ }
34
+
35
+ /** Disallow runtime typeof checks that narrow unparsed values instead of decoding them. */
36
+ export const noRuntimeTypeofRule = defineRule({
37
+ meta: {
38
+ type: "problem",
39
+ docs: {
40
+ description:
41
+ "Disallow runtime typeof checks; external values must be decoded into meaningful types at their I/O boundary.",
42
+ },
43
+ messages: {
44
+ runtimeTypeof:
45
+ "A `typeof` check narrows a representation without establishing its contract. Parse input at its I/O boundary, then branch on the domain value.",
46
+ },
47
+ schema: [
48
+ {
49
+ type: "object",
50
+ properties: {
51
+ allowInTypeGuards: { type: "boolean" },
52
+ },
53
+ additionalProperties: false,
54
+ },
55
+ ],
56
+ defaultOptions: [{ allowInTypeGuards: false }],
57
+ },
58
+ createOnce(context) {
59
+ return {
60
+ UnaryExpression(node) {
61
+ const option = context.options?.[0];
62
+ const allowInTypeGuards =
63
+ typeof option === "object" &&
64
+ option !== null &&
65
+ !Array.isArray(option) &&
66
+ option.allowInTypeGuards === true;
67
+ if (
68
+ node.operator === "typeof" &&
69
+ !isExistenceProbe(node) &&
70
+ (!allowInTypeGuards || !isInsideTypeGuard(node))
71
+ ) {
72
+ context.report({ node, messageId: "runtimeTypeof" });
73
+ }
74
+ },
75
+ };
76
+ },
77
+ });
@@ -0,0 +1,46 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+ import type { ESTree } from "@oxlint/plugins";
3
+
4
+ const FORBIDDEN_SYMBOL_NAME = "shape";
5
+
6
+ function containsForbiddenSymbolName(name: string): boolean {
7
+ return name.toLowerCase().includes(FORBIDDEN_SYMBOL_NAME);
8
+ }
9
+
10
+ /** Return whether an identifier names a statically accessed member owned by another value. */
11
+ function isBorrowedMemberName(node: ESTree.Node): boolean {
12
+ const parent = node.parent;
13
+ if (parent === null || parent.type !== "MemberExpression") return false;
14
+ return parent.property === node && parent.computed === false;
15
+ }
16
+
17
+ /** Ban the case-insensitive substring "shape" in every JavaScript and TypeScript symbol name. */
18
+ export const noForbiddenTermInSymbolNamesRule = defineRule({
19
+ meta: {
20
+ type: "problem",
21
+ docs: {
22
+ description:
23
+ 'Disallow the case-insensitive substring "shape" in JavaScript, TypeScript, private, and JSX symbol names.',
24
+ },
25
+ messages: {
26
+ forbiddenSymbolName:
27
+ 'Rename symbol "{{name}}" for its domain role; "shape" describes structure rather than ownership.',
28
+ },
29
+ },
30
+ createOnce(context) {
31
+ const reportForbiddenSymbolName = (node: ESTree.Node & { name: string }) => {
32
+ if (!containsForbiddenSymbolName(node.name) || isBorrowedMemberName(node)) return;
33
+ context.report({
34
+ node,
35
+ messageId: "forbiddenSymbolName",
36
+ data: { name: node.name },
37
+ });
38
+ };
39
+
40
+ return {
41
+ Identifier: reportForbiddenSymbolName,
42
+ PrivateIdentifier: reportForbiddenSymbolName,
43
+ JSXIdentifier: reportForbiddenSymbolName,
44
+ };
45
+ },
46
+ });
@@ -0,0 +1,69 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+ import type { ESTree } from "@oxlint/plugins";
3
+
4
+ import {
5
+ containsUnknownType,
6
+ functionParameterBindingName,
7
+ functionParameterTypeAnnotation,
8
+ } from "../shared/function-parameters.ts";
9
+ type ParameterOwner =
10
+ | ESTree.ArrowFunctionExpression
11
+ | ESTree.Function
12
+ | ESTree.TSCallSignatureDeclaration
13
+ | ESTree.TSConstructSignatureDeclaration
14
+ | ESTree.TSConstructorType
15
+ | ESTree.TSFunctionType
16
+ | ESTree.TSMethodSignature;
17
+
18
+ function isTypePredicateSubject(owner: ParameterOwner, parameterName: string): boolean {
19
+ const predicate = owner.returnType?.typeAnnotation;
20
+ return (
21
+ predicate?.type === "TSTypePredicate" &&
22
+ predicate.parameterName.type === "Identifier" &&
23
+ predicate.parameterName.name === parameterName
24
+ );
25
+ }
26
+
27
+ /** Disallow unknown inputs except explicitly named error-cause enrichment. */
28
+ export const noUnknownParametersRule = defineRule({
29
+ meta: {
30
+ type: "problem",
31
+ docs: {
32
+ description:
33
+ "Disallow explicitly unknown function parameters except `cause` and type-predicate subjects; decode unknown input at its I/O boundary instead.",
34
+ },
35
+ messages: {
36
+ unknownParameter:
37
+ "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.",
38
+ },
39
+ },
40
+ createOnce(context) {
41
+ const checkParameters = (node: ParameterOwner) => {
42
+ for (const parameter of node.params) {
43
+ const annotation = functionParameterTypeAnnotation(parameter);
44
+ if (annotation === null || annotation === undefined) continue;
45
+ if (!containsUnknownType(annotation.typeAnnotation)) continue;
46
+ const name = functionParameterBindingName(parameter, context.sourceCode);
47
+ if (name === "cause" || isTypePredicateSubject(node, name)) continue;
48
+ context.report({
49
+ node: annotation.typeAnnotation,
50
+ messageId: "unknownParameter",
51
+ data: { parameter: name },
52
+ });
53
+ }
54
+ };
55
+
56
+ return {
57
+ ArrowFunctionExpression: checkParameters,
58
+ FunctionDeclaration: checkParameters,
59
+ FunctionExpression: checkParameters,
60
+ TSCallSignatureDeclaration: checkParameters,
61
+ TSConstructSignatureDeclaration: checkParameters,
62
+ TSConstructorType: checkParameters,
63
+ TSDeclareFunction: checkParameters,
64
+ TSEmptyBodyFunctionExpression: checkParameters,
65
+ TSFunctionType: checkParameters,
66
+ TSMethodSignature: checkParameters,
67
+ };
68
+ },
69
+ });
@@ -0,0 +1,82 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import type { ESTree } from "@oxlint/plugins";
4
+
5
+ import {
6
+ createTypeAliasEnvironment,
7
+ resolvedTypeMatches,
8
+ type TypeAliasEnvironment,
9
+ } from "../shared/type-alias-resolution.ts";
10
+
11
+ type FunctionWithReturnType =
12
+ | ESTree.ArrowFunctionExpression
13
+ | ESTree.Function
14
+ | ESTree.TSCallSignatureDeclaration
15
+ | ESTree.TSConstructSignatureDeclaration
16
+ | ESTree.TSConstructorType
17
+ | ESTree.TSFunctionType
18
+ | ESTree.TSMethodSignature;
19
+
20
+ /** Ban function contracts that return unknown instead of a parsed domain type. */
21
+ export const noUnknownReturnsRule = defineRule({
22
+ meta: {
23
+ type: "problem",
24
+ docs: {
25
+ description:
26
+ "Disallow functions whose explicit return contract is unknown or Promise<unknown>.",
27
+ },
28
+ messages: {
29
+ unknownReturn:
30
+ "This function exposes `unknown` to its caller. Parse the value at its boundary and return a named domain type.",
31
+ },
32
+ },
33
+ createOnce(context) {
34
+ let environment: TypeAliasEnvironment | null = null;
35
+
36
+ const resolvesToUnknown = (type: ESTree.TSType): boolean =>
37
+ environment !== null &&
38
+ resolvedTypeMatches(type, environment, (resolved, matches) => {
39
+ if (resolved.type === "TSUnknownKeyword") return true;
40
+ if (resolved.type === "TSParenthesizedType") {
41
+ return matches(resolved.typeAnnotation);
42
+ }
43
+ if (resolved.type === "TSUnionType") return resolved.types.some(matches);
44
+ if (
45
+ resolved.type !== "TSTypeReference" ||
46
+ resolved.typeName.type !== "Identifier" ||
47
+ (resolved.typeName.name !== "Promise" &&
48
+ resolved.typeName.name !== "PromiseLike")
49
+ ) {
50
+ return false;
51
+ }
52
+ const value = resolved.typeArguments?.params[0];
53
+ return value !== undefined && matches(value);
54
+ });
55
+
56
+ const checkReturnType = (node: FunctionWithReturnType) => {
57
+ const annotation = node.returnType;
58
+ if (annotation === null || annotation === undefined) return;
59
+ if (!resolvesToUnknown(annotation.typeAnnotation)) return;
60
+ context.report({ node: annotation.typeAnnotation, messageId: "unknownReturn" });
61
+ };
62
+
63
+ return {
64
+ Program(node) {
65
+ environment = createTypeAliasEnvironment(
66
+ node,
67
+ context.sourceCode.visitorKeys,
68
+ );
69
+ },
70
+ ArrowFunctionExpression: checkReturnType,
71
+ FunctionDeclaration: checkReturnType,
72
+ FunctionExpression: checkReturnType,
73
+ TSCallSignatureDeclaration: checkReturnType,
74
+ TSConstructSignatureDeclaration: checkReturnType,
75
+ TSConstructorType: checkReturnType,
76
+ TSDeclareFunction: checkReturnType,
77
+ TSEmptyBodyFunctionExpression: checkReturnType,
78
+ TSFunctionType: checkReturnType,
79
+ TSMethodSignature: checkReturnType,
80
+ };
81
+ },
82
+ });
@@ -0,0 +1,54 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import type { ESTree } from "@oxlint/plugins";
4
+
5
+ import {
6
+ createTypeAliasEnvironment,
7
+ resolvedTypeMatches,
8
+ type TypeAliasEnvironment,
9
+ } from "../shared/type-alias-resolution.ts";
10
+
11
+ /** Ban named aliases that merely conceal TypeScript's unknown top type. */
12
+ export const noUnknownTypeAliasesRule = defineRule({
13
+ meta: {
14
+ type: "problem",
15
+ docs: {
16
+ description:
17
+ "Disallow type aliases whose resolved type is unknown; unknown must remain visible at an allowed boundary.",
18
+ },
19
+ messages: {
20
+ unknownAlias:
21
+ "Type alias `{{alias}}` hides `unknown`. Keep `unknown` explicit at the parsing boundary or on an allowed `cause` field; otherwise use the parsed owner type.",
22
+ },
23
+ },
24
+ createOnce(context) {
25
+ let environment: TypeAliasEnvironment | null = null;
26
+
27
+ const resolvesToUnknown = (type: ESTree.TSType): boolean =>
28
+ environment !== null &&
29
+ resolvedTypeMatches(type, environment, (resolved, matches) => {
30
+ if (resolved.type === "TSUnknownKeyword") return true;
31
+ if (resolved.type === "TSParenthesizedType") {
32
+ return matches(resolved.typeAnnotation);
33
+ }
34
+ return resolved.type === "TSUnionType" && resolved.types.some(matches);
35
+ });
36
+
37
+ return {
38
+ Program(node) {
39
+ environment = createTypeAliasEnvironment(
40
+ node,
41
+ context.sourceCode.visitorKeys,
42
+ );
43
+ },
44
+ TSTypeAliasDeclaration(node) {
45
+ if (!resolvesToUnknown(node.typeAnnotation)) return;
46
+ context.report({
47
+ node: node.id,
48
+ messageId: "unknownAlias",
49
+ data: { alias: node.id.name },
50
+ });
51
+ },
52
+ };
53
+ },
54
+ });
@@ -0,0 +1,154 @@
1
+ import { defineRule } from "@oxlint/plugins";
2
+
3
+ import {
4
+ classifyUnsafeDictionary,
5
+ classifyUnsafeDictionaryValue,
6
+ createTypeEnvironment,
7
+ type TypeEnvironment,
8
+ } from "../shared/dictionary-types.ts";
9
+ import { visibleTypeAlias } from "../shared/type-alias-resolution.ts";
10
+
11
+ import type { ESTree } from "@oxlint/plugins";
12
+
13
+ const typeNodeKinds: ReadonlySet<string> = new Set([
14
+ "JSDocNonNullableType",
15
+ "JSDocNullableType",
16
+ "JSDocUnknownType",
17
+ "TSAnyKeyword",
18
+ "TSArrayType",
19
+ "TSBigIntKeyword",
20
+ "TSBooleanKeyword",
21
+ "TSConditionalType",
22
+ "TSConstructorType",
23
+ "TSFunctionType",
24
+ "TSImportType",
25
+ "TSIndexedAccessType",
26
+ "TSInferType",
27
+ "TSIntersectionType",
28
+ "TSIntrinsicKeyword",
29
+ "TSLiteralType",
30
+ "TSMappedType",
31
+ "TSNamedTupleMember",
32
+ "TSNeverKeyword",
33
+ "TSNullKeyword",
34
+ "TSNumberKeyword",
35
+ "TSObjectKeyword",
36
+ "TSParenthesizedType",
37
+ "TSStringKeyword",
38
+ "TSSymbolKeyword",
39
+ "TSTemplateLiteralType",
40
+ "TSThisType",
41
+ "TSTupleType",
42
+ "TSTypeLiteral",
43
+ "TSTypeOperator",
44
+ "TSTypePredicate",
45
+ "TSTypeQuery",
46
+ "TSTypeReference",
47
+ "TSUndefinedKeyword",
48
+ "TSUnionType",
49
+ "TSUnknownKeyword",
50
+ "TSVoidKeyword",
51
+ ]);
52
+
53
+ function isTypeNode(node: ESTree.Node): node is ESTree.TSType {
54
+ return typeNodeKinds.has(node.type);
55
+ }
56
+
57
+ function typeReferenceName(type: ESTree.TSTypeReference): string | null {
58
+ return type.typeName.type === "Identifier" ? type.typeName.name : null;
59
+ }
60
+
61
+ function isInsideTypeAliasDeclaration(node: ESTree.Node): boolean {
62
+ let current: ESTree.Node | null = node.parent;
63
+ while (current !== null && current.type !== "Program") {
64
+ if (current.type === "TSTypeAliasDeclaration") return true;
65
+ current = current.parent;
66
+ }
67
+ return false;
68
+ }
69
+
70
+ function isPlainAliasConsumerUse(node: ESTree.TSType, environment: TypeEnvironment): boolean {
71
+ if (node.type !== "TSTypeReference" || node.typeArguments?.params.length) return false;
72
+ const name = typeReferenceName(node);
73
+ return (
74
+ name !== null &&
75
+ visibleTypeAlias(name, node, environment.typeAliases) !== null &&
76
+ !isInsideTypeAliasDeclaration(node)
77
+ );
78
+ }
79
+
80
+ function isInsideTypeParameterConstraint(node: ESTree.TSType): boolean {
81
+ let child: ESTree.Node = node;
82
+ let parent: ESTree.Node | null = child.parent;
83
+ while (parent !== null && parent.type !== "Program") {
84
+ if (parent.type === "TSTypeParameter" && parent.constraint === child) return true;
85
+ child = parent;
86
+ parent = child.parent;
87
+ }
88
+ return false;
89
+ }
90
+
91
+ function shouldReportType(node: ESTree.TSType, environment: TypeEnvironment): boolean {
92
+ if (isInsideTypeParameterConstraint(node)) return false;
93
+ if (isPlainAliasConsumerUse(node, environment)) return false;
94
+ if (classifyUnsafeDictionary(node, environment) === null) return false;
95
+ let current: ESTree.Node | null = node.parent;
96
+ while (current !== null && current.type !== "Program") {
97
+ if (isTypeNode(current) && classifyUnsafeDictionary(current, environment) !== null)
98
+ return false;
99
+ current = current.parent;
100
+ }
101
+ return true;
102
+ }
103
+
104
+ /** Disallow object-dictionary contracts whose direct value type is an unsafe escape hatch. */
105
+ export const noUnsafeDictionaryTypeRule = defineRule({
106
+ meta: {
107
+ type: "problem",
108
+ docs: {
109
+ description:
110
+ "Disallow object-dictionary contracts whose direct value type is unknown, any, object, {}, or a union/alias containing one of those escape hatches.",
111
+ },
112
+ messages: {
113
+ unsafeDictionary:
114
+ "This dictionary's {{value}} value type gives callers no concrete value contract. Use an owner/schema-derived value type; parse external payloads before insertion.",
115
+ },
116
+ },
117
+ createOnce(context) {
118
+ let environment: TypeEnvironment | null = null;
119
+ const report = (node: ESTree.Node, value: string) => {
120
+ context.report({ node, messageId: "unsafeDictionary", data: { value } });
121
+ };
122
+ const reportIfUnsafe = (node: ESTree.TSType) => {
123
+ if (environment === null || !shouldReportType(node, environment)) return;
124
+ const unsafe = classifyUnsafeDictionary(node, environment);
125
+ if (unsafe === null) return;
126
+ report(node, unsafe.unsafeValue);
127
+ };
128
+
129
+ return {
130
+ Program(node) {
131
+ environment = createTypeEnvironment(
132
+ node,
133
+ context.sourceCode.visitorKeys,
134
+ );
135
+ },
136
+ TSTypeReference: reportIfUnsafe,
137
+ TSTypeLiteral: reportIfUnsafe,
138
+ TSMappedType: reportIfUnsafe,
139
+ TSIndexSignature(node) {
140
+ if (
141
+ environment === null ||
142
+ node.typeAnnotation === null ||
143
+ node.parent.type === "TSTypeLiteral"
144
+ )
145
+ return;
146
+ const unsafe = classifyUnsafeDictionaryValue(
147
+ node.typeAnnotation.typeAnnotation,
148
+ environment,
149
+ );
150
+ if (unsafe !== null) report(node, unsafe.unsafeValue);
151
+ },
152
+ };
153
+ },
154
+ });