Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177// Line-terminator fuzz for contextual keywords: joins the first tokens of statement heads, class and object// members with random separators (space, \n, U+2028, U+2029, comments holding a line break), keeps the inputs// TypeScript parses cleanly, and judges them the way test/erase.mjs does (length, JS AST, errors, streaming).// Usage: node test/fuzz-heads.mjs [count] [seed]import ts from "typescript";import { blankSourceFile } from "../vendor/ts-blank-space/out/index.js";import { transformSync } from "../src/index.js";const SK = ts.SyntaxKind;
const heads = `export default abstract class C { m() {} }declare abstract class C { }export declare const x: T;abstract interface I { }declare namespace N { }export type X = 1;import type X from 'y';export type { A } from 'x';async function f() { }export async function f() { }declare module 'x' { }declare global { }using x = y;await using x = y;export import x = y;export as namespace N;declare function f(): void;abstract class C { }export enum E { }const enum E { }declare const enum E { }export declare type T = 1;declare type T = 1;export default interface I { }export default async function f() { }accessor class C { }public interface I { }static class C { }module N { }namespace N { }global { }interface I { }type T = 1;export * from 'x';export { a };import x from 'y';import { type A } from 'y';declare instanceof C;declare var v: T;export declare namespace N { }export abstract class C { }declare export function f(): void;export default abstract class { }readonly interface I { }protected class C { }private namespace N { }let x: T = 1;let [a]: T = b;type: x;declare: x;abstract: x;namespace.x = 1;module.exports = 1;type = 1;declare = 1;async(x);async x => x;async (x: T) => x;interface;export const enum E { }declare enum E { }export declare class C { }export default class C { }export default function f() { }import y = require('y');import type z = require('z');export type * from 'x';export type * as ns from 'x';class K { declare x: T; }class K { abstract m(): void; }class K { static x = 1; }class K { readonly x: T; }class K { accessor x = 1; }class K { public static async *m() {} }class K { get x() { return 1 } }class K { set x(v) {} }class K { static { } }class K { private [k]: T; }class K { override m() {} }class K { declare static x: T; }class K { async m() {} }class K { static get [k]() { return 1 } }class K { public x?: T; }class K { m?(): void; }class K { constructor(public x: T) {} }o = { async m() {}, };o = { get x() { return 1 }, };o = { set x(v) {}, };o = { async *h() {}, };o = { get: 1, set: 2, async: 3, };f(async (x: T) => x);f(async x => x);x = a ? (b) : c => d;x = y as T;x = y satisfies T;x = y!;x = f<T>(y);x = new C<T>();for (using x of y) {}for (await using x of y) {}for (let x: T of y) {}label: abstract;declare type;abstract interface;export abstract interface I {}`.split("\n");
const prelude = "var o: any, k: any, d: any, declare: any, abstract: any, type: any, interface: any, namespace: any, module: any, global: any, async: any, using: any, accessor: any, x: any, y: any, a: any, b: any, v: any, f: any;\n";const seps = [" ", " ", " ", "\n", "\u2028", "\u2029", "/*\n*/", "//c\n", "/* */"];let seed = Number(process.argv[3] ?? 1) >>> 0 || 1;const rnd = () => { seed ^= seed << 13; seed >>>= 0; seed ^= seed >>> 17; seed ^= seed << 5; seed >>>= 0; return seed / 4294967296; };const pick = (a) => a[Math.floor(rnd() * a.length)];
function jsAst(code) { const sf = ts.createSourceFile("out.js", code, ts.ScriptTarget.ESNext, false, ts.ScriptKind.JS); const out = []; (function visit(n) { if (n.kind === SK.EmptyStatement || n.kind === SK.SemicolonClassElement) return; out.push(n.kind); if (ts.isIdentifier(n) || ts.isPrivateIdentifier(n) || ts.isLiteralExpression(n)) out.push(JSON.stringify(n.text)); ts.forEachChild(n, visit); out.push(-1); })(sf); return out.join(",");}
const count = Number(process.argv[2] ?? 5000);let tested = 0, fails = 0;const seen = new Set();for (let i = 0; i < count; i++) { const toks = pick(heads).split(" "); let body = toks[0]; for (let j = 1; j < toks.length; j++) body += (j < 4 ? pick(seps) : " ") + toks[j]; const src = prelude + body + "\nlet zz: T = 1;\n"; const sf = ts.createSourceFile("input.ts", src, ts.ScriptTarget.ESNext, false, ts.ScriptKind.TS); if (sf.parseDiagnostics.length) continue; tested++; const we = []; const want = blankSourceFile(sf, (n) => we.push(n.getStart(sf))); const ge = []; let got, problem = null; try { got = transformSync(src, { onError: (e) => ge.push(e.start) }); } catch (e) { problem = "threw"; } if (!problem && got.length !== src.length) problem = "length"; if (!problem && jsAst(want) !== jsAst(got)) problem = "AST"; if (!problem && we.join() !== ge.join()) problem = `errors want [${we}] got [${ge}]`; if (!problem) { for (const n of [1, 2, 5]) { let out = ""; try { function* parts() { for (let k = 0; k < src.length; k += n) yield src.slice(k, k + n); } for (const piece of transformSync(parts(), { onError() {} })) out += piece; } catch { out = null; } if (out !== got) { problem = `streamed(${n})`; break; } } } if (problem) { fails++; if (!seen.has(body)) { seen.add(body); if (seen.size <= 25) { console.log(`FAIL ${problem}: ${JSON.stringify(body)}`); if (got) console.log(` want ${JSON.stringify(want.slice(prelude.length))}\n got ${JSON.stringify(got.slice(prelude.length))}`); } } }}console.log(`fuzz-heads: ${tested} parse-clean inputs of ${count}, ${fails} failed (${seen.size} distinct)`);process.exitCode = fails ? 1 : 0;