test: stop the server-state guard counting empty as-const tuples as state

This commit is contained in:
Linus Rath
2026-11-03 02:35:42 +01:00
parent bf19f21eaf
commit 34e016f1c0
2 changed files with 5 additions and 3 deletions
@@ -50,6 +50,8 @@ describe('the scan', () => {
'const sessions = new Map<string, number>();',
'let counter = 0;',
'const queue: string[] = [];',
'export const NONE = [] as const;',
'const later = [] as string[];',
"const budget = createFailureBudget({ name: 'x' });",
"const pattern = new RegExp('x');",
'function f() {',
@@ -59,7 +61,7 @@ describe('the scan', () => {
"(globalThis as any)[KEY] ??= new Map();",
].join('\n'));
const names = findStateSites(dir, file).map((s) => s.name);
expect(names).toEqual(['sessions', 'counter', 'queue', 'budget', 'globalThis:KEY']);
expect(names).toEqual(['sessions', 'counter', 'queue', 'later', 'budget', 'globalThis:KEY']);
} finally {
rmSync(dir, { recursive: true, force: true });
}
@@ -13,7 +13,7 @@ import path from 'node:path';
* - top-level `const x = new Something(`, except constructors of values that
* can't change (RegExp, URL, TextEncoder/TextDecoder, Intl.*, Error types);
* - top-level `const x = []` / `{}` / `new Map()`… (empty containers, which
* only exist to be filled);
* only exist to be filled; `[] as const` is a readonly tuple and can't be);
* - top-level `const x = createSomethingBudget|Cache|Store|Limiter(`;
* - any `globalThis` access that stores something (`??=`, `= new`, `[KEY] =`).
*
@@ -130,7 +130,7 @@ export function findStateSites(root: string, file: string): StateSite[] {
// SCREAMING_CASE tables built once from a literal list are constants by convention.
const constantTable = /^[A-Z][A-Z0-9_]*$/.test(name) && /^new (Set|Map)(<.*?>)?\(\s*\S/.test(value) && !/^new (Set|Map)(<.*?>)?\(\s*\)/.test(value);
const isNew = /^new\s/.test(value) && !IMMUTABLE_CONSTRUCTORS.test(value) && !constantTable;
const isEmpty = /^(\[\s*\]|\{\s*\})\s*(as\s.+)?;?$/.test(value);
const isEmpty = /^(\[\s*\]|\{\s*\})\s*(as\s.+)?;?$/.test(value) && !/^(\[\s*\]|\{\s*\})\s*as\s+const\s*;?$/.test(value);
const isFactory = /^create\w*(Budget|Cache|Store|Limiter|Queue)\s*[(<]/.test(value);
if (isNew || isEmpty || isFactory) sites.push({ file: rel, name, line: i + 1, text: raw.trim() });
return;