Repository navigation
Conversation
…block
After a modifier, or written with an escape, `static` before `{` is the
name of a method or of a field. The parser took it as a static block
and dropped what came before it, so programs that every engine rejects
ran:
class A { async static {} }
class A { static static {} }
class A { st\u0061tic {} }
In a TypeScript file `public`, `private`, `protected`, `readonly` and
`override` were dropped the same way, and `declare` and `abstract`
deleted the block.
`parse_class` stores the position of the first token of the class
element in `PropertyOpts::member_start`. It takes the slot of
`declare_range`, which nothing wrote. The static block arm of
`parse_property` accepts the block only if the word ends six characters
after that position: the word is then the first token and has no
escape. Otherwise it reports one error and stops:
- with a decorator: `Decorators are not valid here`, as before
- after `async`: `Expected "(" but found "{"`
- in a TypeScript file, after a modifier: `Modifiers cannot appear here`
- otherwise: `Expected ";" but found "{"`
This one test replaces the test for a decorator in the arm.
`bun check` is not changed: its checker reports these (1184, 1206,
1260).
|
Updated 1:43 AM PT - Oct 10th, 2026
✅ @robobun, your commit 6e415884d31d25f1415a1c19158558153af7a7f8 passed in 🧪 To try this PR locally: bunx bun-pr 44909That installs a local version of the PR into your bun-44909 --bun |
|
Status: ready for review. This PR is stacked on #44883. How I reproduced it, on main (e655c58) and on 1.4.3-canary. Each line is a whole class A { async static { console.log('block') } }
class A { static static { console.log('block') } }
class A { st\u0061tic { console.log('block') } }In a With this branch each file reports one syntax error and exits 1. The test is |
| if !p.is_tolerant() | ||
| && let Some(decorator) = opts.ts_decorators.slice().first() | ||
| && p.lexer.full_start().start | ||
| != opts.member_start.start + b"static".len() as i32 | ||
| { | ||
| p.log().add_error( | ||
| Some(p.source), | ||
| p.real_loc(decorator.loc), | ||
| b"Decorators are not valid here", | ||
| ); | ||
| p.static_block_is_not_first_token(opts, name_range.loc); | ||
| return Err(crate::Error::SyntaxError); |
There was a problem hiding this comment.
🟡 (optional) TypeScript users whose class has a field named public, private, protected, readonly or override on its own line, followed by a static block, now get a SyntaxError where the base ran the block. tsc accepts public\nstatic {} as a field plus a block (the keyword is a modifier only on the same line), but the TS keyword arm at parse_property.rs:654-658 skips its newline check outside tolerant mode, so the new position test at parse_property.rs:685 fails and reports "Modifiers cannot appear here". Fix: treat these five keywords as a member name when a line break follows them in every mode, as the declare and abstract arms already do at parse_property.rs:573 and :604, so the block is the next element and the field is kept. [also at: src/js_parser/parse/parse_property.rs:690 - TypeScript users whose class has a bare public (or private/protected/readonly/override) field on its own line right before static {} now get "Modifiers cannot appear here" where the base ran the block and tsc reports nothing.]
Why this was flagged
Input: a .ts/.tsx/.mts/.cts class body public\nstatic { ... } (or private/protected/readonly/override in place of public) through the module loader, Bun.Transpiler or bun build. tsc's nextTokenCanFollowModifier requires these keywords to be followed on the same line, so tsc parses a field public and then a ClassStaticBlock and emits public;\nstatic { ... }. In Bun the arm at src/js_parser/parse/parse_property.rs:654-658 only applies the newline check when p.is_tolerant(), so in a normal parse public is pushed as a modifier and the loop restarts at static. The static-block arm at src/js_parser/parse/parse_property.rs:684-689 now compares p.lexer.full_start().start with opts.member_start.start + 6; member_start is the public token, so the test fails and static_block_is_not_first_token at src/js_parser/parse/parse_property.rs:1227-1231 logs "Modifiers cannot appear here" and the parse returns Err(SyntaxError). On the base the arm accepted the block and printed static { ... } (the public field was dropped, but the block ran).
Verification: Outside tolerant mode the is_tolerant() term at src/js_parser/parse/parse_property.rs:654-658 is false, so the newline is ignored and public is consumed as a modifier (continue 'restart). So static_block_is_not_first_token runs and logs "Modifiers cannot appear here" at public (lines 1225-1230), and the arm returns Err(SyntaxError). Input that loaded on the base (and is valid per tsc) now fails with a hard SyntaxError.
|
The finding about The ordinary build reads The suggested fix is #38706: one condition in the keyword arm. #38706 merges with main as it is. With it, The condition is not in this PR for two reasons:
If a maintainer prefers one PR, I can add the condition here and close #38706. |
Stacked on #44883. The diff is against its branch.
Problem
staticbefore{is a member name. Bun runsclass A { async static {} },static static {}andst\u0061tic {}. V8 and JSC throwSyntaxError: Unexpected token '{'.public static {}runs the block anddeclare static {}deletes it.src/js_parser/parse/parse_property.rs:671) tests only{and the name.Fix
parse_classstores the start of each class member inPropertyOpts::member_start. The arm takes the block only ifstaticends six characters after it: first token, no escape.Expected "(" but found "{"afterasync,Modifiers cannot appear herein TypeScript,Expected ";" but found "{"otherwise. A decorator keeps the error of js_parser: reject a decorator on a class static block, keep decorators out of the body ofdeclare global#44883.test/bundler/transpiler/transpiler.test.js(28 tests fail on the base).bun checkoutput is byte-identical.declare_rangebeside the new field (+2 instructions per object property).Background
ClassStaticBlockisstatic { }with nothing before it. A modifier restarts theparse_propertyloop, so the arm cannot see it.declareand 5 keywords) and a flag per modifier arm (6 writers).Downsides
async static {}. node 26, tsc, Babel, SWC and oxc reject them all. No user reported this. 0 uses in 19,470 TypeScript files.public⏎ static {}is an error too: the keyword is a modifier there until Follow tsc's same-line rule for TS class member and parameter modifiers #38706.Notes
The decision this needs. For JavaScript every engine rejects these spellings, and esbuild rejects
async static {}. esbuild acceptsstatic static {},st\u0061tic {}and a TypeScript modifier beforestatic {. This PR rejects them. If that is too strict, the change shrinks toasync static {}.What each spelling reports. One error, then the parse stops. The block body is not parsed.
.js.tsasync static {},static async static {}Expected "(" but found "{"static static {},static⏎static {},static static⏎{}Expected ";" but found "{"Modifiers cannot appear herest\u0061tic {}Expected ";" but found "{"public/private/protected/readonly/override/declare/abstract static {}, and stacks of themModifiers cannot appear here, at the first modifierDecorators are not valid here(#44883)These stay valid, and
test/bundler/transpiler/transpiler.test.jspins their output:static⏎{},/* a */ static /* b */ {},static static⏎static {},async⏎static {},static async⏎static {},accessor⏎static {},st\u0061tic⏎static {},async static() {} static {}, and in TypeScriptdeclare⏎static {},abstract⏎static {},static?: T; static {}, and a block after an index signature or an overload.References.
ClassStaticBlock : static { ClassStaticBlockBody }has no modifier, and a keyword terminal has no escape.evalin Bun) throw for every JavaScript spelling above. JSC saysExpected an opening '(' before a async method's parameter listorExpected a ';' following a class field..ts:Modifiers cannot appear herefor 6 of the 7 TypeScript modifiers and forstatic static {}(another error foroverride),Expected '(', got '{'forasync static {}.Modifiers cannot appear here.from the checker, TS1260 for the escape. Its emit keeps the block.ts.transpileModulereports nothing for the modifiers.Static class blocks cannot have any modifier.SWC:TS1184. oxc:modifiers_cannot_appear_here.async static {}(Expected "(" but found "{"). It acceptsstatic static {}, the escape and the 5 TypeScript keywords, and deletes the block afterdeclareorabstract. It has no test for these.How it relates to #44883. That PR tests
opts.ts_decoratorsin the arm. A decorator is also a token beforestatic, so the position test covers it. This PR replaces the decorator test with the position test and moves theDecorators are not valid hereerror, with its position, into the cold function.scope-mismatch-panic.test.tspasses unchanged (87 pass, 6 todo). The cache version goes from 35 to 36.Measurements. Release builds of the base (#44883, 8d74cc4) and of this PR, same machine.
size_of::<PropertyOpts>(): 48 B -> 40 B (array length mismatch undercargo check -p bun_js_parser).PropertyOptsinit stores: class member 9 -> 9 (base: 8 into the struct and 1 for thefirst_decorator_locslot), object-literal property 7 -> 7 (llvm-objdump,P<false, false>).Instructions executed inside
parse_propertyandparse_class, per unit, base -> PR. Method:qemu-x86_6410.0.13 user mode, one instruction per translation block, the exec trace filtered to the address range of each function, (count for 2,000 units - count for 1,000 units) / 1,000. The count is the same on each run.parse_classparse_propertym() {}a: 1m() {}g = 1private g = 1class A { static {} }class A { }One transform executes about 3,600 instructions per object property and about 3,900 per TypeScript class member, so the TypeScript cost is under 0.1%.
node_modules/typescript/lib/typescript.js(9.1 MB), one transform:parse_propertyexecutes 5,392,834 instructions on both builds,parse_class147,562 -> 147,322. The whole process executes 1,129,326,350 -> 1,129,373,887 (+0.004%). Two runs of one binary differ by about 40,000 outside these two functions.Size:
parse_property+parse_classover 5 monomorphizations plus the cold function: 75,620 B -> 75,351 B (nm -S). Releasebun:.text65,659,989 B -> 65,659,733 B,.rodata20,603,628 B on both (size -A).Behaviour: a matrix of 30 prefixes by 22 members (660 JavaScript class bodies) against V8: bun-only accepts 14 -> 0, bun-only rejects 0 -> 0. 37 TypeScript prefix-before-block rows: accepted 30 -> 2 (
declare⏎static {}andabstract⏎static {}, a field and a block). The 5 decorated rows report the same error on both builds.bun check: 7 cases (3 fromtest/cli/check/differential-cases.json, TypeScript'sclassStaticBlock19andclassStaticBlock20, every modifier, the escape, the line-break forms), 4,383 bytes, 74 diagnostics: 0 bytes differ between the base and the PR.Transpiler cache: a 5 KiB module with
static static {}that the base cached exits 1 with the PR. After the version change the first run rewrites 41 of 41 cached modules of a test program, the second run 0.Existing TypeScript: an AST scan of the 19,470 TypeScript files on the machine (116 MB, 52 static blocks) finds 0 static blocks with a modifier.
Why
member_starttakes the place ofdeclare_range. Nothing writesdeclare_range, and its one reader (Class fields that use "declare" cannot be initialized) never runs. A build that keeps it besidemember_startexecutes 2 more instructions per object-literal property (parse_prefixstores one more constant) and 2 more per JavaScript class member (149 -> 151). #40817 uses the field and would add it again.Why the arm reads the lexer.
name_range.loc != opts.member_start || raw != b"static"is the same test. It keeps a local alive acrossLexer::next()for the arm, and the release build then executes 6 more instructions per object-literal property inparse_property(247 -> 253). Afternext()the lexer has the end of the previous token (full_start), so the arm needs no local.TypeScript keywords before a line break. The ordinary build reads
public,private,protected,readonlyandoverrideas modifiers also before a line break. Sopublic⏎static {}is a modifier before a static block, andstatic public⏎static {}(valid JavaScript) is an error in a.tsfile. The base accepts both and drops the word. tsc reads the word as a field name there, and #38706 makes the ordinary build do the same. With #38706 both spellings are a field and a block.Tests. In
test/bundler/transpiler/transpiler.test.js: 24 rejected spellings (each in a class declaration, a class expression andexport default class, with the message and the text the error points at), the scan pass, the valid orders, one arrow body that the parser reads twice, and two spawned processes (the module loader for.mjs,.cjs,.mtsand.cts, andbun index.js).test/cli/run/syntax.test.tstimes out with a debug build on main, so it has no new rows. Also run with the release build: all oftest/bundler/transpiler/,test/bundler/esbuild/ts.test.ts,default.test.ts,bundler_edgecase.test.ts,test/js/bun/transpiler/,test/cli/run/syntax.test.ts(the same results as the base) andtest/cli/check(1,062 pass. The 1 failure needs a package that is not installed, on main too).Not changed.
get static {},set static {},* static {}andaccessor static {}are errors already, with 2 to 4 messages.declareorabstracton a method, accessor or index signature still deletes the member (#40817).async public foo() {}is still accepted.[human-review] gate passed · iteration 0 · 5 files touched
fails on main (without fix)
passes on PR (with fix)
diff hotspot
gate history · 1 passed · 0 rejected · iteration 0
evidence per changed file