ts: the no-any rule is a check now, and the check passes on itself #2474

Merged
jaylouisw merged 1 commit from build/no-any into main 2026-11-23 22:09:02 +00:00
Owner

First new TypeScript code in the repository, and the first thing it does is enforce the rule that produced it: new code is TypeScript, and any appears nowhere.

What this adds

  • scripts/check-no-any.ts — scans scripts/ engine/ server/ web/ test/ types/ (plus any top-level *.ts) for an explicit any in a type position and reports file:line:column for every occurrence, exit 1 on a hit, 0 when clean, 2 on a usage error. --list prints every file it read, and an optional path argument scopes it to one directory or one file (the tests use that).
  • test/no-any.test.ts — builds fixtures in a temp directory (os.tmpdir(), no network, no repository writes, removed in a finally) and asserts the checker can both fail and stay quiet. 28 assertions; prints 28 passed, 0 failed.

The spellings it catches

written reported as
: any type annotation
as any type assertion
<any> type argument
any[] array of any
Array<any>, Promise<any> type argument
Record<string, any> generic argument
<T = any> generic default (the worst of them: every call site that omits the argument silently types as any)
| any union member
extends any generic constraint

What it deliberately does not catch, and why

  • A comment. The tokenizer drops // and /* */ before anything is judged, so the documentation above — which spells out every banned form — cannot fail the file it describes.
  • A string or template literal. Same reason: the fixture code in test/no-any.test.ts is strings and template literals, so the guard is blind to it by construction; if that ever broke, this PR's own self-scan would go red. ${...} hands the scanner back to code, so a cast inside an interpolation is still caught. A /regex/ is text too, and a quote character inside one can no longer open a string and swallow the rest of the file.
  • unknown. A different word, and the answer to nearly every real any — the fixture narrows it with typeof guards and passes untouched.
  • A property, member or variable named any — row.any, { any: 1 }, interface Row { any: number }. The rules key on what surrounds the word (:, as, <, ,/= inside a type argument list, |, extends, [ ]), never on the word alone.
  • A file or directory name containing any — discovery is by extension.

It is a lexical check, not a parser, and the header says so: < opens a type-argument stretch when the token before it can be a type name, and the count is dropped at ;, { and }. A comparison can inflate the count, which only matters if a comma inside that stretch is followed by the bare word any.

Verified

node scripts/check-no-any.ts        # 0 occurrences in 0 files (2 scanned) - its own two files
node test/no-any.test.ts            # 28 passed, 0 failed
node scripts/syntax-check.js        # 545 files: all OK (it checks .js only; .ts does not choke it)
node scripts/check-node-surface.js  # no API newer than the Node 18 floor

Both files also typecheck under --strict --noUncheckedIndexedAccess --isolatedModules --verbatimModuleSyntax, with both --module nodenext and --module commonjs.

The self-scan is the nicest proof here: the checker is inside its own scan path, and the test asserts that it is (the scan reaches the checker itself, and this test file).

Both files are CommonJS at run time — the repository is "type": "commonjs", so ESM syntax cannot run (Cannot use import statement outside a module, verified). Each carries one export type ... = never, which makes TypeScript treat the file as a module instead of a script; without it, every .ts script in the program shares one global scope and the first two of them collide on const fs (TS2451, verified). The runtime strips that line because it is a type: node scripts/check-no-any.ts runs.

Wiring for the TypeScript toolchain PR to absorb

Everything below touches package.json / tsconfig.json / the workflow, which this PR deliberately does not edit:

  1. Discovery of test/*.test.ts in scripts/run-tests.js. Until that lands, the suite is run directly — node test/no-any.test.ts — which works today (Node 26.13.1 strips types natively).
  2. Node types. Both files use require, process and __dirname, so the typecheck job needs @types/node as a devDependency and "types": ["node"] in tsconfig.json. Without them these two files do not typecheck — and no .ts file that touches fs will.
  3. The gate, one line in package.json:
    "check:no-any": "node scripts/check-no-any.ts"
    
    with npm run check:no-any appended to the test chain. It needs a runtime that can load .ts (22.18+ / 26), so on the Node 18 leg it has to be skipped rather than failing for the language — the test announces that case loudly (skipped: no TypeScript support in node v18...) instead of going red for the wrong reason.
  4. tsconfig.json's include should cover the same directories the checker scans. moduleDetection: "force" is not needed, because of the in-file type export.

No issue to close: this is the first slice of the standing rule, not a fix for a report.

First new TypeScript code in the repository, and the first thing it does is enforce the rule that produced it: **new code is TypeScript, and `any` appears nowhere.** ## What this adds - **`scripts/check-no-any.ts`** — scans `scripts/ engine/ server/ web/ test/ types/` (plus any top-level `*.ts`) for an explicit `any` in a type position and reports `file:line:column` for every occurrence, exit 1 on a hit, 0 when clean, 2 on a usage error. `--list` prints every file it read, and an optional path argument scopes it to one directory or one file (the tests use that). - **`test/no-any.test.ts`** — builds fixtures in a temp directory (`os.tmpdir()`, no network, no repository writes, removed in a `finally`) and asserts the checker can both fail and stay quiet. 28 assertions; prints `28 passed, 0 failed`. ### The spellings it catches | written | reported as | |---|---| | `: any` | type annotation | | `as any` | type assertion | | `<any>` | type argument | | `any[]` | array of any | | `Array<any>`, `Promise<any>` | type argument | | `Record<string, any>` | generic argument | | `<T = any>` | generic default (the worst of them: every call site that omits the argument silently types as `any`) | | `\| any` | union member | | `extends any` | generic constraint | ### What it deliberately does not catch, and why - **A comment.** The tokenizer drops `//` and `/* */` before anything is judged, so the documentation above — which spells out every banned form — cannot fail the file it describes. - **A string or template literal.** Same reason: the fixture code in `test/no-any.test.ts` is strings and template literals, so the guard is blind to it by construction; if that ever broke, this PR's own self-scan would go red. `${...}` hands the scanner back to code, so a cast inside an interpolation is still caught. A `/regex/` is text too, and a quote character inside one can no longer open a string and swallow the rest of the file. - **`unknown`.** A different word, and the answer to nearly every real `any` — the fixture narrows it with `typeof` guards and passes untouched. - **A property, member or variable named `any`** — `row.any`, `{ any: 1 }`, `interface Row { any: number }`. The rules key on what surrounds the word (`:`, `as`, `<`, `,`/`=` inside a type argument list, `|`, `extends`, `[` `]`), never on the word alone. - **A file or directory *name* containing `any`** — discovery is by extension. It is a lexical check, not a parser, and the header says so: `<` opens a type-argument stretch when the token before it can be a type name, and the count is dropped at `;`, `{` and `}`. A comparison can inflate the count, which only matters if a comma inside that stretch is followed by the bare word `any`. ## Verified ``` node scripts/check-no-any.ts # 0 occurrences in 0 files (2 scanned) - its own two files node test/no-any.test.ts # 28 passed, 0 failed node scripts/syntax-check.js # 545 files: all OK (it checks .js only; .ts does not choke it) node scripts/check-node-surface.js # no API newer than the Node 18 floor ``` Both files also typecheck under `--strict --noUncheckedIndexedAccess --isolatedModules --verbatimModuleSyntax`, with both `--module nodenext` and `--module commonjs`. The self-scan is the nicest proof here: the checker is inside its own scan path, and the test asserts that it is (`the scan reaches the checker itself`, `and this test file`). Both files are CommonJS at run time — the repository is `"type": "commonjs"`, so ESM syntax cannot run (`Cannot use import statement outside a module`, verified). Each carries one `export type ... = never`, which makes TypeScript treat the file as a module instead of a script; without it, every `.ts` script in the program shares one global scope and the first two of them collide on `const fs` (`TS2451`, verified). The runtime strips that line because it is a type: `node scripts/check-no-any.ts` runs. ## Wiring for the TypeScript toolchain PR to absorb Everything below touches `package.json` / `tsconfig.json` / the workflow, which this PR deliberately does not edit: 1. **Discovery** of `test/*.test.ts` in `scripts/run-tests.js`. Until that lands, the suite is run directly — `node test/no-any.test.ts` — which works today (Node 26.13.1 strips types natively). 2. **Node types.** Both files use `require`, `process` and `__dirname`, so the typecheck job needs `@types/node` as a devDependency and `"types": ["node"]` in `tsconfig.json`. Without them these two files do not typecheck — and no `.ts` file that touches `fs` will. 3. **The gate, one line** in `package.json`: ```json "check:no-any": "node scripts/check-no-any.ts" ``` with `npm run check:no-any` appended to the `test` chain. It needs a runtime that can load `.ts` (22.18+ / 26), so on the Node 18 leg it has to be skipped rather than failing for the language — the test announces that case loudly (`skipped: no TypeScript support in node v18...`) instead of going red for the wrong reason. 4. `tsconfig.json`'s `include` should cover the same directories the checker scans. `moduleDetection: "force"` is *not* needed, because of the in-file type export. No issue to close: this is the first slice of the standing rule, not a fix for a report.
ts: the no-any rule is a check now, and the check passes on itself
All checks were successful
CI / Server smoke and Docker build (pull_request) Successful in 1m4s
CI / Test (Node 18) (pull_request) Successful in 1m0s
CI / Test (Node 20) (pull_request) Successful in 1m2s
PR rebase / Rebase onto main and arm auto-merge (pull_request) Successful in 6s
CI / Test (Node 22) (pull_request) Successful in 41s
CI / Arm auto-merge (pull_request) Successful in 4s
c511e03306
New code is TypeScript and `any` appears nowhere, but a rule that lives only
in a document is a habit. `scripts/check-no-any.ts` reports every explicit
`any` in a type position with the `file:line:column` of the word, and
`test/no-any.test.ts` proves each banned spelling is caught while comments,
string and template literals, `unknown` narrowed with a guard, a property
named `any` and a filename containing the word all stay silent - including
the checker's own two files, which are in its scan path.

`tsc --noEmit` cannot answer this question (it says nothing about an `any`
you wrote yourself) and ESLint would break the zero-dependency promise, so
the check reads the source: comments, strings, template literals and regex
literals are dropped before anything is judged, then each bare `any` is
judged by its neighbours - `:`, `as`, `<`, `,`/`=` inside type arguments,
`|`, `extends`, `[` `]`.
forgejo-actions scheduled this pull request to auto merge when all checks succeed 2026-11-23 22:03:17 +00:00
forgejo-actions canceled auto merging this pull request when all checks succeed 2026-11-23 22:06:10 +00:00
Sign in to join this conversation.
No description provided.