@jfalava/ts-standard 0.0.0-stage → 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 +43 -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/reference/ci.yml +64 -0
- package/src/check.ts +62 -0
- package/src/cli.ts +34 -0
- package/src/config.ts +41 -0
- package/src/finding.ts +8 -0
- package/src/lint.ts +101 -0
- package/src/pins.ts +264 -0
- package/src/repo.ts +87 -0
- package/src/scripts.ts +597 -0
- package/src/shell.ts +165 -0
- package/src/workflows.ts +317 -0
- package/README.md +0 -3
|
@@ -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/reference/ci.yml
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
branches: ["**"]
|
|
6
|
+
pull_request:
|
|
7
|
+
|
|
8
|
+
concurrency:
|
|
9
|
+
group: ci-${{ github.ref }}
|
|
10
|
+
cancel-in-progress: true
|
|
11
|
+
|
|
12
|
+
permissions:
|
|
13
|
+
contents: read
|
|
14
|
+
|
|
15
|
+
jobs:
|
|
16
|
+
# Decides whether `check` runs. A skipped `check` still satisfies the branch rule, which a
|
|
17
|
+
# workflow skipped by `paths-ignore` would not. Every path counts as code except the ones
|
|
18
|
+
# negated below. On a pull request the diff is against the base branch, on a push to the
|
|
19
|
+
# default branch against the previous push, and on any other branch against its merge
|
|
20
|
+
# base with the default branch.
|
|
21
|
+
#
|
|
22
|
+
# `check` is skipped only when this job reports `code` as false. If this job fails, its
|
|
23
|
+
# output is empty and `check` runs anyway, so the branch rule needs only `check`.
|
|
24
|
+
changes:
|
|
25
|
+
runs-on: ubuntu-latest
|
|
26
|
+
timeout-minutes: 5
|
|
27
|
+
permissions:
|
|
28
|
+
contents: read
|
|
29
|
+
pull-requests: read
|
|
30
|
+
outputs:
|
|
31
|
+
code: ${{ steps.filter.outputs.code }}
|
|
32
|
+
steps:
|
|
33
|
+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
34
|
+
- uses: dorny/paths-filter@ceb8a2b8f2d89434be7ff52d3de7ec3738c5cc9d # v4.0.3
|
|
35
|
+
id: filter
|
|
36
|
+
with:
|
|
37
|
+
predicate-quantifier: some-with-excludes
|
|
38
|
+
filters: |
|
|
39
|
+
code:
|
|
40
|
+
- "**"
|
|
41
|
+
- "!**/README.md"
|
|
42
|
+
- "!**/AGENTS.md"
|
|
43
|
+
- "!**/CLAUDE.md"
|
|
44
|
+
- "!LICENSE*"
|
|
45
|
+
- "!.agents/**"
|
|
46
|
+
- "!.zed/**"
|
|
47
|
+
- "!.vscode/**"
|
|
48
|
+
- "!.github/dependabot.yml"
|
|
49
|
+
|
|
50
|
+
check:
|
|
51
|
+
needs: changes
|
|
52
|
+
if: ${{ !cancelled() && needs.changes.outputs.code != 'false' }}
|
|
53
|
+
runs-on: ubuntu-latest
|
|
54
|
+
timeout-minutes: 15
|
|
55
|
+
steps:
|
|
56
|
+
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
|
|
57
|
+
- uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
|
|
58
|
+
- uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
|
|
59
|
+
with:
|
|
60
|
+
path: ~/.bun/install/cache
|
|
61
|
+
key: bun-${{ runner.os }}-${{ runner.arch }}-${{ hashFiles('bun.lock') }}
|
|
62
|
+
restore-keys: bun-${{ runner.os }}-${{ runner.arch }}-
|
|
63
|
+
- run: bun install --frozen-lockfile
|
|
64
|
+
- run: bun run check
|
package/src/check.ts
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import type { Exception, StandardConfig } from "./config.ts";
|
|
2
|
+
import type { Finding } from "./finding.ts";
|
|
3
|
+
import { lintRules } from "./lint.ts";
|
|
4
|
+
import { pinRules } from "./pins.ts";
|
|
5
|
+
import { loadRepo } from "./repo.ts";
|
|
6
|
+
import { scriptRules } from "./scripts.ts";
|
|
7
|
+
import { workflowRules } from "./workflows.ts";
|
|
8
|
+
|
|
9
|
+
export type Severity = "error" | "warn";
|
|
10
|
+
|
|
11
|
+
/** Rules that print but do not fail. Every other rule is an error. */
|
|
12
|
+
const WARN_RULES: ReadonlySet<string> = new Set(["workflows/reference-pins"]);
|
|
13
|
+
|
|
14
|
+
export const severityOf = (rule: string): Severity => (WARN_RULES.has(rule) ? "warn" : "error");
|
|
15
|
+
|
|
16
|
+
export interface Result {
|
|
17
|
+
readonly repo: string;
|
|
18
|
+
/** Findings no exception covers, plus exceptions that cover nothing. */
|
|
19
|
+
readonly errors: readonly Finding[];
|
|
20
|
+
readonly warnings: readonly Finding[];
|
|
21
|
+
/** How many findings an exception covered. */
|
|
22
|
+
readonly excepted: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const matches = (e: Exception, f: Finding): boolean =>
|
|
26
|
+
e.rule === f.rule &&
|
|
27
|
+
(e.where === undefined || e.where === f.where || f.where.startsWith(`${e.where}:`)) &&
|
|
28
|
+
(e.script === undefined || e.script === f.script);
|
|
29
|
+
|
|
30
|
+
/** Runs every rule against the repo at `root`. Reads files only. */
|
|
31
|
+
export const check = (root: string, config: StandardConfig): Result => {
|
|
32
|
+
const repo = loadRepo(root, config);
|
|
33
|
+
const findings = [
|
|
34
|
+
...scriptRules(repo),
|
|
35
|
+
...pinRules(repo),
|
|
36
|
+
...lintRules(repo),
|
|
37
|
+
...workflowRules(repo),
|
|
38
|
+
];
|
|
39
|
+
const exceptions = config.exceptions ?? [];
|
|
40
|
+
const used = new Set<Exception>();
|
|
41
|
+
const open = findings.filter((f) => {
|
|
42
|
+
const e = exceptions.find((x) => matches(x, f));
|
|
43
|
+
if (e) {
|
|
44
|
+
used.add(e);
|
|
45
|
+
}
|
|
46
|
+
return !e;
|
|
47
|
+
});
|
|
48
|
+
const stale: Finding[] = exceptions
|
|
49
|
+
.filter((e) => !used.has(e))
|
|
50
|
+
.map((e) => ({
|
|
51
|
+
rule: "standard/stale-exception",
|
|
52
|
+
where: e.where ?? "*",
|
|
53
|
+
script: e.script,
|
|
54
|
+
message: `exception for \`${e.rule}\` matches nothing; remove it`,
|
|
55
|
+
}));
|
|
56
|
+
return {
|
|
57
|
+
repo: repo.name,
|
|
58
|
+
errors: [...open.filter((f) => severityOf(f.rule) === "error"), ...stale],
|
|
59
|
+
warnings: open.filter((f) => severityOf(f.rule) === "warn"),
|
|
60
|
+
excepted: findings.length - open.length,
|
|
61
|
+
};
|
|
62
|
+
};
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
#!/usr/bin/env bun
|
|
2
|
+
// `ts-standard [dir] [--config file] [--json]`: read-only. Exits 1 when an error is left after the
|
|
3
|
+
// repo's exceptions, including an exception that matches nothing. Warnings print and pass.
|
|
4
|
+
import { existsSync } from "node:fs";
|
|
5
|
+
import { join, resolve } from "node:path";
|
|
6
|
+
|
|
7
|
+
import { check } from "./check.ts";
|
|
8
|
+
import type { StandardConfig } from "./config.ts";
|
|
9
|
+
import type { Finding } from "./finding.ts";
|
|
10
|
+
|
|
11
|
+
const args = process.argv.slice(2);
|
|
12
|
+
const configIdx = args.indexOf("--config");
|
|
13
|
+
const dirArg = args.find((a, i) => !a.startsWith("--") && (configIdx < 0 || i !== configIdx + 1));
|
|
14
|
+
const root = resolve(dirArg ?? ".");
|
|
15
|
+
const configArg = configIdx >= 0 ? args[configIdx + 1] : undefined;
|
|
16
|
+
const configPath = configArg === undefined ? join(root, "standard.config.ts") : resolve(configArg);
|
|
17
|
+
const config: StandardConfig = existsSync(configPath) ? (await import(configPath)).default : {};
|
|
18
|
+
|
|
19
|
+
const result = check(root, config);
|
|
20
|
+
|
|
21
|
+
const line = (level: string, f: Finding) =>
|
|
22
|
+
`${level} ${f.rule} ${f.where}${f.script === undefined ? "" : ` [${f.script}]`} ${f.message}`;
|
|
23
|
+
|
|
24
|
+
if (args.includes("--json")) {
|
|
25
|
+
process.stdout.write(`${JSON.stringify(result, null, 2)}\n`);
|
|
26
|
+
} else {
|
|
27
|
+
const out = [
|
|
28
|
+
...result.errors.map((f) => line("error", f)),
|
|
29
|
+
...result.warnings.map((f) => line("warn ", f)),
|
|
30
|
+
`${result.repo}: ${result.errors.length} error(s), ${result.warnings.length} warning(s), ${result.excepted} excepted`,
|
|
31
|
+
];
|
|
32
|
+
process.stdout.write(`${out.join("\n")}\n`);
|
|
33
|
+
}
|
|
34
|
+
process.exit(result.errors.length > 0 ? 1 : 0);
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// The public entry point: what a repo's `standard.config.ts` imports. It has no imports of its
|
|
2
|
+
// own, so a consumer's typecheck reads only this file, whatever its tsconfig flags are.
|
|
3
|
+
|
|
4
|
+
/** Which column of the standard's script table a package follows. */
|
|
5
|
+
export type Toolchain = "oxc" | "astro" | "pulumi" | "vite-plus";
|
|
6
|
+
|
|
7
|
+
/** A documented exception. It must match a finding, or it is reported as stale. */
|
|
8
|
+
export interface Exception {
|
|
9
|
+
/** Rule id, such as "scripts/check-runs-own-checks". */
|
|
10
|
+
readonly rule: string;
|
|
11
|
+
/** Package directory ("." for the root) or repo-relative file. Omit to match anywhere. */
|
|
12
|
+
readonly where?: string;
|
|
13
|
+
/** Script name, for script rules. Omit to match every script. */
|
|
14
|
+
readonly script?: string;
|
|
15
|
+
/** Why the repo differs. Keep it in step with the README's exceptions table. */
|
|
16
|
+
readonly why: string;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface StandardConfig {
|
|
20
|
+
/** Public repos gate on `ci.yml`; private ones gate on Workers Builds and have none. Default "public". */
|
|
21
|
+
readonly visibility?: "public" | "private";
|
|
22
|
+
/** Package directory to toolchain. Packages not listed are "oxc". */
|
|
23
|
+
readonly toolchains?: Readonly<Record<string, Toolchain>>;
|
|
24
|
+
/** What this repo's `ci.yml` adds to the reference. */
|
|
25
|
+
readonly ci?: {
|
|
26
|
+
/** Extra negations in the `code` filter, without the leading "!". */
|
|
27
|
+
readonly ignoreAdd?: readonly string[];
|
|
28
|
+
/** Standard negations this repo counts as code, without the leading "!". */
|
|
29
|
+
readonly ignoreRemove?: readonly string[];
|
|
30
|
+
/** Extra filters in the `changes` step, beside `code`. */
|
|
31
|
+
readonly filters?: readonly string[];
|
|
32
|
+
/** Extra jobs beside `changes` and `check`. */
|
|
33
|
+
readonly jobs?: readonly string[];
|
|
34
|
+
/** Extra top-level `env` keys. */
|
|
35
|
+
readonly env?: readonly string[];
|
|
36
|
+
};
|
|
37
|
+
readonly exceptions?: readonly Exception[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** Identity function that types a `standard.config.ts`. */
|
|
41
|
+
export const defineStandard = (config: StandardConfig): StandardConfig => config;
|
package/src/finding.ts
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** One way a repo differs from the standard. Severity comes from the rule (`severityOf`). */
|
|
2
|
+
export interface Finding {
|
|
3
|
+
readonly rule: string;
|
|
4
|
+
/** Package directory or repo-relative file. */
|
|
5
|
+
readonly where: string;
|
|
6
|
+
readonly script?: string | undefined;
|
|
7
|
+
readonly message: string;
|
|
8
|
+
}
|
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
|
+
};
|