@jfalava/ts-standard 0.1.0 → 0.2.2
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/package.json +5 -4
- package/reference/anti-slop/UPSTREAM.md +8 -0
- package/reference/anti-slop/effect/index.ts +21 -0
- package/reference/anti-slop/effect/rules/no-manual-effect-error-tag.ts +52 -0
- package/reference/anti-slop/effect/rules/no-manual-tag-comparison.ts +45 -0
- package/reference/anti-slop/effect/rules/no-manual-tagged-construction.ts +37 -0
- package/reference/anti-slop/effect/rules/no-service-constructor-imports.ts +52 -0
- package/reference/anti-slop/effect/rules/prefer-effect-match.ts +54 -0
- package/reference/anti-slop/effect/shared/tagged-values.ts +97 -0
- package/reference/anti-slop/index.ts +24 -0
- package/reference/anti-slop/rules/no-module-mocking.ts +80 -0
- package/reference/anti-slop/rules/no-runtime-typeof.ts +77 -0
- package/reference/anti-slop/rules/no-shape-in-symbol-names.ts +46 -0
- package/reference/anti-slop/rules/no-unknown-parameters.ts +69 -0
- package/reference/anti-slop/rules/require-safety-comment-for-type-assertion.ts +131 -0
- package/reference/anti-slop/shared/function-parameters.ts +49 -0
- package/reference/anti-slop/shared/scope.ts +15 -0
- package/src/check.ts +10 -2
- package/src/lint.ts +101 -0
- package/src/pins.ts +55 -24
- package/src/repo.ts +5 -2
- package/src/scripts.ts +91 -29
- package/src/shell.ts +55 -21
- package/src/workflows.ts +66 -28
- package/README.md +0 -198
|
@@ -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,131 @@
|
|
|
1
|
+
import { defineRule } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
4
|
+
|
|
5
|
+
type TypeAssertion = ESTree.TSAsExpression | ESTree.TSTypeAssertion;
|
|
6
|
+
|
|
7
|
+
const DEFAULT_SAFETY_MARKERS = ["SAFETY"] as const;
|
|
8
|
+
|
|
9
|
+
const commentOwnerKinds = new Set([
|
|
10
|
+
"ExpressionStatement",
|
|
11
|
+
"PropertyDefinition",
|
|
12
|
+
"ReturnStatement",
|
|
13
|
+
"ThrowStatement",
|
|
14
|
+
"VariableDeclaration",
|
|
15
|
+
]);
|
|
16
|
+
|
|
17
|
+
function isConstAssertion(node: TypeAssertion): boolean {
|
|
18
|
+
return (
|
|
19
|
+
node.typeAnnotation.type === "TSTypeReference" &&
|
|
20
|
+
node.typeAnnotation.typeName.type === "Identifier" &&
|
|
21
|
+
node.typeAnnotation.typeName.name === "const"
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function configuredSafetyMarkers(option: unknown): readonly string[] {
|
|
26
|
+
if (typeof option !== "object" || option === null || !("markers" in option)) {
|
|
27
|
+
return DEFAULT_SAFETY_MARKERS;
|
|
28
|
+
}
|
|
29
|
+
const configured = option.markers;
|
|
30
|
+
if (!Array.isArray(configured)) return DEFAULT_SAFETY_MARKERS;
|
|
31
|
+
const markers = configured.flatMap((marker) =>
|
|
32
|
+
typeof marker === "string" && marker.trim().length > 0 ? [marker.trim()] : [],
|
|
33
|
+
);
|
|
34
|
+
return markers.length > 0 ? markers : DEFAULT_SAFETY_MARKERS;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function markerPattern(markers: readonly string[]): RegExp {
|
|
38
|
+
const alternation = markers
|
|
39
|
+
.map((marker) => marker.replaceAll(/[.*+?^${}()|[\]\\]/gu, String.raw`\$&`))
|
|
40
|
+
.join("|");
|
|
41
|
+
return new RegExp(
|
|
42
|
+
String.raw`(?:^|[^\p{L}\p{N}_])(?:${alternation})\s*:\s*\S`,
|
|
43
|
+
"u",
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function hasSafetyJustificationBefore(
|
|
48
|
+
sourceCode: SourceCode,
|
|
49
|
+
owner: ESTree.Node,
|
|
50
|
+
assertion: TypeAssertion,
|
|
51
|
+
pattern: RegExp,
|
|
52
|
+
): boolean {
|
|
53
|
+
return sourceCode
|
|
54
|
+
.getCommentsBefore(owner)
|
|
55
|
+
.some(
|
|
56
|
+
(comment) => comment.end <= assertion.start && pattern.test(comment.value),
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function hasSafetyComment(
|
|
61
|
+
sourceCode: SourceCode,
|
|
62
|
+
node: TypeAssertion,
|
|
63
|
+
pattern: RegExp,
|
|
64
|
+
): boolean {
|
|
65
|
+
let current: ESTree.Node = node;
|
|
66
|
+
while (true) {
|
|
67
|
+
if (hasSafetyJustificationBefore(sourceCode, current, node, pattern)) return true;
|
|
68
|
+
if (commentOwnerKinds.has(current.type)) {
|
|
69
|
+
const exportDeclaration = current.parent;
|
|
70
|
+
return (
|
|
71
|
+
exportDeclaration.type === "ExportNamedDeclaration" &&
|
|
72
|
+
exportDeclaration.declaration === current &&
|
|
73
|
+
hasSafetyJustificationBefore(sourceCode, exportDeclaration, node, pattern)
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
if (current.parent.type === "Program") return false;
|
|
77
|
+
current = current.parent;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Require every non-const type assertion to state the invariant TypeScript cannot express. */
|
|
82
|
+
export const requireSafetyCommentForTypeAssertionRule = defineRule({
|
|
83
|
+
meta: {
|
|
84
|
+
type: "problem",
|
|
85
|
+
docs: {
|
|
86
|
+
description:
|
|
87
|
+
"Require a nearby SAFETY comment for every TypeScript type assertion except const assertions.",
|
|
88
|
+
},
|
|
89
|
+
messages: {
|
|
90
|
+
missingSafetyComment:
|
|
91
|
+
"This type assertion has no `{{marker}}:` justification. State the checked invariant immediately before the assertion or its containing statement.",
|
|
92
|
+
},
|
|
93
|
+
schema: [
|
|
94
|
+
{
|
|
95
|
+
type: "object",
|
|
96
|
+
properties: {
|
|
97
|
+
markers: {
|
|
98
|
+
type: "array",
|
|
99
|
+
items: { type: "string", minLength: 1 },
|
|
100
|
+
minItems: 1,
|
|
101
|
+
uniqueItems: true,
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
additionalProperties: false,
|
|
105
|
+
},
|
|
106
|
+
],
|
|
107
|
+
defaultOptions: [{ markers: ["SAFETY"] }],
|
|
108
|
+
},
|
|
109
|
+
createOnce(context) {
|
|
110
|
+
const patterns = new Map<string, RegExp>();
|
|
111
|
+
|
|
112
|
+
const checkAssertion = (node: TypeAssertion) => {
|
|
113
|
+
if (isConstAssertion(node)) return;
|
|
114
|
+
const markers = configuredSafetyMarkers(context.options?.[0]);
|
|
115
|
+
const patternKey = markers.join("\u0000");
|
|
116
|
+
const pattern = patterns.get(patternKey) ?? markerPattern(markers);
|
|
117
|
+
patterns.set(patternKey, pattern);
|
|
118
|
+
if (hasSafetyComment(context.sourceCode, node, pattern)) return;
|
|
119
|
+
context.report({
|
|
120
|
+
node,
|
|
121
|
+
messageId: "missingSafetyComment",
|
|
122
|
+
data: { marker: markers[0] ?? DEFAULT_SAFETY_MARKERS[0] },
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
TSAsExpression: checkAssertion,
|
|
128
|
+
TSTypeAssertion: checkAssertion,
|
|
129
|
+
};
|
|
130
|
+
},
|
|
131
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
export type FunctionParameter = ESTree.ParamPattern;
|
|
4
|
+
|
|
5
|
+
/** Return whether a type is or contains TypeScript's absorbing unknown top type. */
|
|
6
|
+
export function containsUnknownType(type: ESTree.TSType): boolean {
|
|
7
|
+
if (type.type === "TSUnknownKeyword") return true;
|
|
8
|
+
if (type.type === "TSParenthesizedType") return containsUnknownType(type.typeAnnotation);
|
|
9
|
+
return type.type === "TSUnionType" && type.types.some(containsUnknownType);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** Return the TypeScript annotation attached to a function parameter or its wrapped binding. */
|
|
13
|
+
export function functionParameterTypeAnnotation(
|
|
14
|
+
parameter: FunctionParameter,
|
|
15
|
+
): ESTree.TSTypeAnnotation | null | undefined {
|
|
16
|
+
if (parameter.type === "TSParameterProperty") {
|
|
17
|
+
return functionParameterTypeAnnotation(parameter.parameter);
|
|
18
|
+
}
|
|
19
|
+
if (parameter.type === "RestElement") {
|
|
20
|
+
return parameter.typeAnnotation ?? functionParameterTypeAnnotation(parameter.argument);
|
|
21
|
+
}
|
|
22
|
+
if (parameter.type === "AssignmentPattern") {
|
|
23
|
+
return parameter.typeAnnotation ?? functionParameterTypeAnnotation(parameter.left);
|
|
24
|
+
}
|
|
25
|
+
return parameter.typeAnnotation;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Return only a function parameter's local binding, excluding its annotation and default value. */
|
|
29
|
+
export function functionParameterBindingName(
|
|
30
|
+
parameter: FunctionParameter,
|
|
31
|
+
sourceCode: SourceCode,
|
|
32
|
+
): string {
|
|
33
|
+
if (parameter.type === "TSParameterProperty") {
|
|
34
|
+
return functionParameterBindingName(parameter.parameter, sourceCode);
|
|
35
|
+
}
|
|
36
|
+
if (parameter.type === "AssignmentPattern") {
|
|
37
|
+
return functionParameterBindingName(parameter.left, sourceCode);
|
|
38
|
+
}
|
|
39
|
+
if (parameter.type === "RestElement") {
|
|
40
|
+
return functionParameterBindingName(parameter.argument, sourceCode);
|
|
41
|
+
}
|
|
42
|
+
if (parameter.type === "Identifier") return parameter.name;
|
|
43
|
+
|
|
44
|
+
const sourceText = sourceCode.getText(parameter);
|
|
45
|
+
const annotationStart = parameter.typeAnnotation?.start;
|
|
46
|
+
return annotationStart === undefined
|
|
47
|
+
? sourceText
|
|
48
|
+
: sourceText.slice(0, annotationStart - parameter.start).trimEnd();
|
|
49
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { ESTree, Scope, SourceCode, Variable } from "@oxlint/plugins";
|
|
2
|
+
|
|
3
|
+
/** Resolve an identifier to its binding by walking lexical scopes upward. */
|
|
4
|
+
export function resolveVariable(
|
|
5
|
+
sourceCode: SourceCode,
|
|
6
|
+
identifier: ESTree.IdentifierReference,
|
|
7
|
+
): Variable | null {
|
|
8
|
+
let scope: Scope | null = sourceCode.getScope(identifier);
|
|
9
|
+
while (scope !== null) {
|
|
10
|
+
const variable = scope.set.get(identifier.name);
|
|
11
|
+
if (variable !== undefined) return variable;
|
|
12
|
+
scope = scope.upper;
|
|
13
|
+
}
|
|
14
|
+
return null;
|
|
15
|
+
}
|
package/src/check.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Exception, StandardConfig } from "./config.ts";
|
|
2
2
|
import type { Finding } from "./finding.ts";
|
|
3
|
+
import { lintRules } from "./lint.ts";
|
|
3
4
|
import { pinRules } from "./pins.ts";
|
|
4
5
|
import { loadRepo } from "./repo.ts";
|
|
5
6
|
import { scriptRules } from "./scripts.ts";
|
|
@@ -29,12 +30,19 @@ const matches = (e: Exception, f: Finding): boolean =>
|
|
|
29
30
|
/** Runs every rule against the repo at `root`. Reads files only. */
|
|
30
31
|
export const check = (root: string, config: StandardConfig): Result => {
|
|
31
32
|
const repo = loadRepo(root, config);
|
|
32
|
-
const findings = [
|
|
33
|
+
const findings = [
|
|
34
|
+
...scriptRules(repo),
|
|
35
|
+
...pinRules(repo),
|
|
36
|
+
...lintRules(repo),
|
|
37
|
+
...workflowRules(repo),
|
|
38
|
+
];
|
|
33
39
|
const exceptions = config.exceptions ?? [];
|
|
34
40
|
const used = new Set<Exception>();
|
|
35
41
|
const open = findings.filter((f) => {
|
|
36
42
|
const e = exceptions.find((x) => matches(x, f));
|
|
37
|
-
if (e)
|
|
43
|
+
if (e) {
|
|
44
|
+
used.add(e);
|
|
45
|
+
}
|
|
38
46
|
return !e;
|
|
39
47
|
});
|
|
40
48
|
const stale: Finding[] = exceptions
|
package/src/lint.ts
ADDED
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join, relative } from "node:path";
|
|
4
|
+
|
|
5
|
+
import type { Finding } from "./finding.ts";
|
|
6
|
+
import type { Repo } from "./repo.ts";
|
|
7
|
+
|
|
8
|
+
const PRESET = "@cloudflare/oxlint-config";
|
|
9
|
+
const REMAINDER = "tools/oxlint/anti-slop";
|
|
10
|
+
/** The anti-slop rules @cloudflare/oxlint-config does not carry, as every repo vendors them. */
|
|
11
|
+
const REFERENCE_REMAINDER = join(import.meta.dir, "..", "reference", "anti-slop");
|
|
12
|
+
const OXFMT_CONFIGS = ["oxfmt.config.ts", ".oxfmtrc.json", ".oxfmtrc.jsonc"];
|
|
13
|
+
|
|
14
|
+
const filesUnder = (dir: string): string[] =>
|
|
15
|
+
readdirSync(dir, { recursive: true, withFileTypes: true })
|
|
16
|
+
.filter((entry) => entry.isFile())
|
|
17
|
+
.map((entry) => relative(dir, join(entry.parentPath, entry.name)))
|
|
18
|
+
.sort();
|
|
19
|
+
|
|
20
|
+
/** SHA-256 over every file's relative path and bytes, in path order. */
|
|
21
|
+
export const treeHash = (dir: string): string => {
|
|
22
|
+
const hash = createHash("sha256");
|
|
23
|
+
for (const file of filesUnder(dir)) {
|
|
24
|
+
hash.update(`${file}\0`);
|
|
25
|
+
hash.update(readFileSync(join(dir, file)));
|
|
26
|
+
hash.update("\0");
|
|
27
|
+
}
|
|
28
|
+
return hash.digest("hex");
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
const ignores = (text: string): boolean =>
|
|
32
|
+
/["'](tools\/\*\*|tools\/oxlint\/anti-slop\/\*\*)["']/.test(text);
|
|
33
|
+
|
|
34
|
+
export const lintRules = (repo: Repo): Finding[] => {
|
|
35
|
+
const out: Finding[] = [];
|
|
36
|
+
const f = (rule: string, where: string, message: string) => out.push({ rule, where, message });
|
|
37
|
+
|
|
38
|
+
const rootConfig = join(repo.root, "oxlint.config.ts");
|
|
39
|
+
if (!existsSync(rootConfig)) {
|
|
40
|
+
f("lint/preset", "oxlint.config.ts", "missing");
|
|
41
|
+
} else {
|
|
42
|
+
const text = readFileSync(rootConfig, "utf8");
|
|
43
|
+
if (!new RegExp(`from\\s+["']${PRESET}["']`).test(text)) {
|
|
44
|
+
f("lint/preset", "oxlint.config.ts", `does not import \`${PRESET}\``);
|
|
45
|
+
}
|
|
46
|
+
// `options` (type-aware, unused directives) is root-only: oxlint rejects it from a config
|
|
47
|
+
// that another config spreads, so it is split off the preset and spread back at the root.
|
|
48
|
+
const split = /const\s*\{\s*options\s*:\s*(\w+)\s*,\s*\.\.\.\s*\w+\s*\}\s*=/.exec(text);
|
|
49
|
+
if (!split) {
|
|
50
|
+
f(
|
|
51
|
+
"lint/preset",
|
|
52
|
+
"oxlint.config.ts",
|
|
53
|
+
"split the preset's root-only `options` off (`const { options: presetOptions, ...preset } = cloudflare`)",
|
|
54
|
+
);
|
|
55
|
+
} else if (!text.includes(`...${split[1]}`)) {
|
|
56
|
+
f("lint/preset", "oxlint.config.ts", `\`${split[1]}\` is never spread into \`options\``);
|
|
57
|
+
}
|
|
58
|
+
if (!/\bcorrectness\s*:/.test(text)) {
|
|
59
|
+
f(
|
|
60
|
+
"lint/preset",
|
|
61
|
+
"oxlint.config.ts",
|
|
62
|
+
"set `categories.correctness`; the preset turns the category off",
|
|
63
|
+
);
|
|
64
|
+
}
|
|
65
|
+
if (!ignores(text)) {
|
|
66
|
+
f(
|
|
67
|
+
"lint/anti-slop-remainder",
|
|
68
|
+
"oxlint.config.ts",
|
|
69
|
+
`\`ignorePatterns\` lacks \`${REMAINDER}\``,
|
|
70
|
+
);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
for (const pkg of [repo.rootPkg, ...repo.workspaces]) {
|
|
74
|
+
const path = join(pkg.abs, "oxlint.config.ts");
|
|
75
|
+
if (existsSync(path) && /\bextends\s*:/.test(readFileSync(path, "utf8"))) {
|
|
76
|
+
f(
|
|
77
|
+
"lint/preset",
|
|
78
|
+
relative(repo.root, path),
|
|
79
|
+
"uses `extends`, which drops env, globals and overrides and lets the preset's rule aliases win; spread the config instead",
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
const remainder = join(repo.root, REMAINDER);
|
|
85
|
+
if (!existsSync(remainder)) {
|
|
86
|
+
f("lint/anti-slop-remainder", REMAINDER, "missing");
|
|
87
|
+
} else if (treeHash(remainder) !== treeHash(REFERENCE_REMAINDER)) {
|
|
88
|
+
f(
|
|
89
|
+
"lint/anti-slop-remainder",
|
|
90
|
+
REMAINDER,
|
|
91
|
+
"differs from the remainder every repo shares; copy node_modules/@jfalava/ts-standard/reference/anti-slop",
|
|
92
|
+
);
|
|
93
|
+
}
|
|
94
|
+
const oxfmt = OXFMT_CONFIGS.find((name) => existsSync(join(repo.root, name)));
|
|
95
|
+
if (oxfmt === undefined) {
|
|
96
|
+
f("lint/anti-slop-remainder", ".", "no oxfmt config");
|
|
97
|
+
} else if (!ignores(readFileSync(join(repo.root, oxfmt), "utf8"))) {
|
|
98
|
+
f("lint/anti-slop-remainder", oxfmt, `\`ignorePatterns\` lacks \`${REMAINDER}\``);
|
|
99
|
+
}
|
|
100
|
+
return out;
|
|
101
|
+
};
|
package/src/pins.ts
CHANGED
|
@@ -2,12 +2,20 @@ import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
|
2
2
|
import { join, relative } from "node:path";
|
|
3
3
|
|
|
4
4
|
import type { Finding } from "./finding.ts";
|
|
5
|
-
import { catalogs,
|
|
5
|
+
import { catalogs, readJsonc } from "./repo.ts";
|
|
6
|
+
import type { Pkg, Repo } from "./repo.ts";
|
|
6
7
|
|
|
7
8
|
const EXACT = /^(npm:(@[\w.-]+\/)?[\w.-]+@)?\d+\.\d+\.\d+(-[0-9A-Za-z.-]+)?(\+[0-9A-Za-z.-]+)?$/;
|
|
8
9
|
const PINNED_GIT = /^(github:|git\+[a-z]+:\/\/|https:\/\/github\.com\/)[^#]+#[0-9a-f]{40}$/;
|
|
9
10
|
const PREPARE = "effect-tsgo patch --no-typescript --tsrs --oxlint";
|
|
10
|
-
const ROOT_TOOLS = [
|
|
11
|
+
const ROOT_TOOLS = [
|
|
12
|
+
"@cloudflare/oxlint-config",
|
|
13
|
+
"@effect/tsgo",
|
|
14
|
+
"@oxlint/plugins",
|
|
15
|
+
"oxfmt",
|
|
16
|
+
"oxlint",
|
|
17
|
+
"tsrs",
|
|
18
|
+
];
|
|
11
19
|
const TSGO_PLATFORMS = [
|
|
12
20
|
"darwin-arm64",
|
|
13
21
|
"darwin-x64",
|
|
@@ -37,26 +45,30 @@ export const pinRules = (repo: Repo): Finding[] => {
|
|
|
37
45
|
out.push({ rule, where, script, message });
|
|
38
46
|
|
|
39
47
|
const pm = root.json.packageManager;
|
|
40
|
-
if (typeof pm !== "string" || !/^bun@\d+\.\d+\.\d+$/.test(pm))
|
|
48
|
+
if (typeof pm !== "string" || !/^bun@\d+\.\d+\.\d+$/.test(pm)) {
|
|
41
49
|
f(
|
|
42
50
|
"pins/package-manager",
|
|
43
51
|
".",
|
|
44
52
|
`\`packageManager\` must be \`bun@<exact>\`; got ${JSON.stringify(pm)}`,
|
|
45
53
|
);
|
|
54
|
+
}
|
|
46
55
|
const bunVersion = typeof pm === "string" ? pm.slice(4) : undefined;
|
|
47
56
|
|
|
48
57
|
for (const [name, entries] of cats) {
|
|
49
58
|
for (const [dep, spec] of Object.entries(entries)) {
|
|
50
|
-
if (!EXACT.test(spec) && !PINNED_GIT.test(spec))
|
|
59
|
+
if (!EXACT.test(spec) && !PINNED_GIT.test(spec)) {
|
|
51
60
|
f(
|
|
52
61
|
"pins/exact",
|
|
53
62
|
".",
|
|
54
63
|
`catalog \`${name || "default"}\`: \`${dep}\` is \`${spec}\`, not an exact version`,
|
|
55
64
|
);
|
|
65
|
+
}
|
|
56
66
|
}
|
|
57
67
|
}
|
|
58
68
|
const resolve = (dep: string, spec: string): string | undefined => {
|
|
59
|
-
if (!spec.startsWith("catalog:"))
|
|
69
|
+
if (!spec.startsWith("catalog:")) {
|
|
70
|
+
return spec;
|
|
71
|
+
}
|
|
60
72
|
return cats.get(spec.slice(8))?.[dep];
|
|
61
73
|
};
|
|
62
74
|
const catalogued = new Set([...cats.values()].flatMap((e) => Object.keys(e)));
|
|
@@ -64,8 +76,9 @@ export const pinRules = (repo: Repo): Finding[] => {
|
|
|
64
76
|
for (const pkg of [root, ...repo.workspaces]) {
|
|
65
77
|
for (const [dep, spec] of depsOf(pkg)) {
|
|
66
78
|
if (spec.startsWith("catalog:")) {
|
|
67
|
-
if (resolve(dep, spec) === undefined)
|
|
79
|
+
if (resolve(dep, spec) === undefined) {
|
|
68
80
|
f("pins/exact", pkg.dir, `\`${dep}\`: \`${spec}\` has no entry for it`);
|
|
81
|
+
}
|
|
69
82
|
} else if (spec.startsWith("workspace:")) {
|
|
70
83
|
continue;
|
|
71
84
|
} else if (!EXACT.test(spec) && !PINNED_GIT.test(spec)) {
|
|
@@ -77,12 +90,13 @@ export const pinRules = (repo: Repo): Finding[] => {
|
|
|
77
90
|
`\`${dep}\` is in a root catalog; use \`catalog:\` instead of \`${spec}\``,
|
|
78
91
|
);
|
|
79
92
|
}
|
|
80
|
-
if (dep === "oxlint-tsgolint")
|
|
93
|
+
if (dep === "oxlint-tsgolint") {
|
|
81
94
|
f(
|
|
82
95
|
"pins/toolchain-deps",
|
|
83
96
|
pkg.dir,
|
|
84
97
|
"`oxlint-tsgolint` is not used: tsrs serves type-aware lint",
|
|
85
98
|
);
|
|
99
|
+
}
|
|
86
100
|
if (dep === "typescript" && pkg.toolchain !== "astro" && pkg.toolchain !== "pulumi") {
|
|
87
101
|
f(
|
|
88
102
|
"pins/toolchain-deps",
|
|
@@ -92,22 +106,25 @@ export const pinRules = (repo: Repo): Finding[] => {
|
|
|
92
106
|
}
|
|
93
107
|
if ((dep === "bun-types" || dep === "@types/bun") && bunVersion !== undefined) {
|
|
94
108
|
const v = resolve(dep, spec);
|
|
95
|
-
if (v !== bunVersion)
|
|
109
|
+
if (v !== bunVersion) {
|
|
96
110
|
f(
|
|
97
111
|
"pins/bun-types",
|
|
98
112
|
pkg.dir,
|
|
99
113
|
`\`${dep}\` is ${v}, \`packageManager\` is bun@${bunVersion}`,
|
|
100
114
|
);
|
|
115
|
+
}
|
|
101
116
|
}
|
|
102
117
|
}
|
|
103
118
|
}
|
|
104
119
|
const rootDev = root.json.devDependencies ?? {};
|
|
105
120
|
for (const tool of ROOT_TOOLS) {
|
|
106
|
-
if (rootDev[tool] === undefined)
|
|
121
|
+
if (rootDev[tool] === undefined) {
|
|
107
122
|
f("pins/toolchain-deps", ".", `root \`devDependencies\` lacks \`${tool}\``);
|
|
123
|
+
}
|
|
108
124
|
}
|
|
109
|
-
if (root.scripts.prepare !== PREPARE)
|
|
125
|
+
if (root.scripts.prepare !== PREPARE) {
|
|
110
126
|
f("pins/prepare", ".", `expected \`${PREPARE}\`; got \`${root.scripts.prepare}\``, "prepare");
|
|
127
|
+
}
|
|
111
128
|
|
|
112
129
|
// bunfig.toml
|
|
113
130
|
const bunfigPath = join(repo.root, "bunfig.toml");
|
|
@@ -115,7 +132,7 @@ export const pinRules = (repo: Repo): Finding[] => {
|
|
|
115
132
|
f("pins/bunfig", ".", "no `bunfig.toml`");
|
|
116
133
|
} else {
|
|
117
134
|
const install: any = (Bun.TOML.parse(readFileSync(bunfigPath, "utf8")) as any).install ?? {};
|
|
118
|
-
if (typeof install.minimumReleaseAge !== "number" || install.minimumReleaseAge <
|
|
135
|
+
if (typeof install.minimumReleaseAge !== "number" || install.minimumReleaseAge < 86_400) {
|
|
119
136
|
f(
|
|
120
137
|
"pins/bunfig",
|
|
121
138
|
"bunfig.toml",
|
|
@@ -125,10 +142,12 @@ export const pinRules = (repo: Repo): Finding[] => {
|
|
|
125
142
|
const excludes: string[] = install.minimumReleaseAgeExcludes ?? [];
|
|
126
143
|
const missing = ["@effect/tsgo", ...TSGO_PLATFORMS].filter((p) => !excludes.includes(p));
|
|
127
144
|
// This package ships from the same scope; without it a new release waits a day to install.
|
|
128
|
-
if (!excludes.includes("@jfalava/*") && !excludes.includes("@jfalava/ts-standard"))
|
|
145
|
+
if (!excludes.includes("@jfalava/*") && !excludes.includes("@jfalava/ts-standard")) {
|
|
129
146
|
missing.push("@jfalava/*");
|
|
130
|
-
|
|
147
|
+
}
|
|
148
|
+
if (missing.length > 0) {
|
|
131
149
|
f("pins/bunfig", "bunfig.toml", `\`minimumReleaseAgeExcludes\` lacks ${missing.join(", ")}`);
|
|
150
|
+
}
|
|
132
151
|
}
|
|
133
152
|
|
|
134
153
|
out.push(...tsconfigRules(repo));
|
|
@@ -158,27 +177,31 @@ const tsconfigRules = (repo: Repo): Finding[] => {
|
|
|
158
177
|
}
|
|
159
178
|
const base: any = readJsonc(basePath);
|
|
160
179
|
const co = base.compilerOptions ?? {};
|
|
161
|
-
if (base.$schema !== "./node_modules/@effect/tsgo/schema.json")
|
|
180
|
+
if (base.$schema !== "./node_modules/@effect/tsgo/schema.json") {
|
|
162
181
|
out.push({
|
|
163
182
|
rule: "tsconfig/base",
|
|
164
183
|
where: baseName,
|
|
165
184
|
message: "`$schema` must be `./node_modules/@effect/tsgo/schema.json`",
|
|
166
185
|
});
|
|
167
|
-
|
|
186
|
+
}
|
|
187
|
+
if (!pluginEntry(base)) {
|
|
168
188
|
out.push({
|
|
169
189
|
rule: "tsconfig/effect-plugin",
|
|
170
190
|
where: baseName,
|
|
171
191
|
message: "no `@effect/language-service` plugin entry; Effect diagnostics are off",
|
|
172
192
|
});
|
|
173
|
-
|
|
193
|
+
}
|
|
194
|
+
if (co.noEmit !== true) {
|
|
174
195
|
out.push({ rule: "tsconfig/no-emit", where: baseName, message: "`noEmit` must be true" });
|
|
196
|
+
}
|
|
175
197
|
for (const flag of ["strict", "noUncheckedIndexedAccess"]) {
|
|
176
|
-
if (co[flag] !== true)
|
|
198
|
+
if (co[flag] !== true) {
|
|
177
199
|
out.push({
|
|
178
200
|
rule: "tsconfig/strict-in-base",
|
|
179
201
|
where: baseName,
|
|
180
202
|
message: `\`${flag}\` must be true in the base`,
|
|
181
203
|
});
|
|
204
|
+
}
|
|
182
205
|
}
|
|
183
206
|
for (const path of tsconfigsOf(repo)) {
|
|
184
207
|
const rel = relative(repo.root, path);
|
|
@@ -191,43 +214,51 @@ const tsconfigRules = (repo: Repo): Finding[] => {
|
|
|
191
214
|
});
|
|
192
215
|
}
|
|
193
216
|
}
|
|
194
|
-
if (!repo.isWorkspace)
|
|
217
|
+
if (!repo.isWorkspace) {
|
|
218
|
+
return out;
|
|
219
|
+
}
|
|
195
220
|
for (const pkg of repo.workspaces) {
|
|
196
|
-
if (pkg.toolchain === "astro" || pkg.toolchain === "pulumi")
|
|
221
|
+
if (pkg.toolchain === "astro" || pkg.toolchain === "pulumi") {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
197
224
|
const p = join(pkg.abs, "tsconfig.json");
|
|
198
225
|
if (!existsSync(p)) {
|
|
199
226
|
out.push({ rule: "tsconfig/extends-base", where: pkg.dir, message: "no `tsconfig.json`" });
|
|
200
227
|
continue;
|
|
201
228
|
}
|
|
202
229
|
const ts: any = readJsonc(p);
|
|
203
|
-
const ext = [ts.extends ?? []].flat();
|
|
230
|
+
const ext = new Set([ts.extends ?? []].flat());
|
|
204
231
|
const want = relative(pkg.abs, basePath);
|
|
205
|
-
if (!ext.
|
|
232
|
+
if (!ext.has(want) && !ext.has(`./${want}`)) {
|
|
206
233
|
out.push({
|
|
207
234
|
rule: "tsconfig/extends-base",
|
|
208
235
|
where: `${pkg.dir}/tsconfig.json`,
|
|
209
236
|
message: `must extend \`${want}\``,
|
|
210
237
|
});
|
|
238
|
+
}
|
|
211
239
|
const set = STRICT_FLAGS.filter((k) => ts.compilerOptions?.[k] !== undefined);
|
|
212
|
-
if (set.length > 0)
|
|
240
|
+
if (set.length > 0) {
|
|
213
241
|
out.push({
|
|
214
242
|
rule: "tsconfig/strict-in-base",
|
|
215
243
|
where: `${pkg.dir}/tsconfig.json`,
|
|
216
244
|
message: `sets ${set.join(", ")}; strict flags belong in the base`,
|
|
217
245
|
});
|
|
218
|
-
|
|
246
|
+
}
|
|
247
|
+
if (ts.compilerOptions?.plugins !== undefined) {
|
|
219
248
|
out.push({
|
|
220
249
|
rule: "tsconfig/effect-plugin",
|
|
221
250
|
where: `${pkg.dir}/tsconfig.json`,
|
|
222
251
|
message: "overrides `plugins`; the Effect policy lives in the base",
|
|
223
252
|
});
|
|
253
|
+
}
|
|
224
254
|
const extra = readdirSync(pkg.abs).filter((n) => /^tsconfig\..+\.json$/.test(n));
|
|
225
|
-
if (extra.length > 0)
|
|
255
|
+
if (extra.length > 0) {
|
|
226
256
|
out.push({
|
|
227
257
|
rule: "tsconfig/one-per-package",
|
|
228
258
|
where: pkg.dir,
|
|
229
259
|
message: `${extra.join(", ")}: list tests in \`tsconfig.json\`'s \`include\` instead`,
|
|
230
260
|
});
|
|
261
|
+
}
|
|
231
262
|
}
|
|
232
263
|
return out;
|
|
233
264
|
};
|
package/src/repo.ts
CHANGED
|
@@ -77,8 +77,11 @@ export const loadRepo = (root: string, config: StandardConfig): Repo => {
|
|
|
77
77
|
export const catalogs = (repo: Repo): Map<string, Record<string, string>> => {
|
|
78
78
|
const ws = repo.rootPkg.json.workspaces ?? {};
|
|
79
79
|
const out = new Map<string, Record<string, string>>();
|
|
80
|
-
if (ws.catalog)
|
|
81
|
-
|
|
80
|
+
if (ws.catalog) {
|
|
81
|
+
out.set("", ws.catalog);
|
|
82
|
+
}
|
|
83
|
+
for (const [name, entries] of Object.entries(ws.catalogs ?? {})) {
|
|
82
84
|
out.set(name, entries as Record<string, string>);
|
|
85
|
+
}
|
|
83
86
|
return out;
|
|
84
87
|
};
|