@@ -3218,16 +3218,21 @@ pub const Parser = struct {
32183218 return m ;
32193219 }
32203220
3221+ fn parseMemberName (self : * Parser ) ParseError ! []const u8 {
3222+ const name = self .advance ();
3223+ if (name .kind != .identifier and name .kind != .private_name ) return ParseError .UnexpectedToken ;
3224+ if (name .kind == .private_name and ! self .in_class ) return ParseError .UnexpectedToken ;
3225+ return name .text ;
3226+ }
3227+
32213228 /// Consume a chain of `.prop`, `[expr]`, `?.…`, and `(args)` operators on `e`.
32223229 fn parseMemberTail (self : * Parser , start : * Node ) ParseError ! * Node {
32233230 var e = start ;
32243231 var has_optional = false ;
32253232 while (true ) {
32263233 if (self .match (.dot )) {
3227- const name = self .advance ();
3228- if (name .kind != .identifier and name .kind != .private_name ) return ParseError .UnexpectedToken ;
3229- if (name .kind == .private_name and ! self .in_class ) return ParseError .UnexpectedToken ;
3230- e = try self .alloc (.{ .member = .{ .object = e , .property = name .text } });
3234+ const name = try self .parseMemberName ();
3235+ e = try self .alloc (.{ .member = .{ .object = e , .property = name } });
32313236 } else if (self .match (.question_dot )) {
32323237 has_optional = true ;
32333238 if (self .check (.lparen )) {
@@ -3241,10 +3246,8 @@ pub const Parser = struct {
32413246 try self .expect (.rbracket );
32423247 e = try self .alloc (.{ .member = .{ .object = e , .computed = idx , .optional = true } });
32433248 } else {
3244- const name = self .advance ();
3245- if (name .kind != .identifier and name .kind != .private_name ) return ParseError .UnexpectedToken ;
3246- if (name .kind == .private_name and ! self .in_class ) return ParseError .UnexpectedToken ;
3247- e = try self .alloc (.{ .member = .{ .object = e , .property = name .text , .optional = true } });
3249+ const name = try self .parseMemberName ();
3250+ e = try self .alloc (.{ .member = .{ .object = e , .property = name , .optional = true } });
32483251 }
32493252 } else if (self .match (.lbracket )) {
32503253 const saved_no_in = self .no_in ; // a computed key is `[+In]`
@@ -3323,9 +3326,10 @@ pub const Parser = struct {
33233326 if (! parenthesized_callee and callee .* == .import_call ) return ParseError .UnexpectedToken ;
33243327 while (true ) {
33253328 if (self .match (.dot )) {
3326- const name = self .advance ();
3327- if (name .kind != .identifier ) return ParseError .UnexpectedToken ;
3328- callee = try self .alloc (.{ .member = .{ .object = callee , .property = name .text } });
3329+ // `new MemberExpression Arguments` includes private property
3330+ // access; it has the same lexical-name gate as ordinary access.
3331+ const name = try self .parseMemberName ();
3332+ callee = try self .alloc (.{ .member = .{ .object = callee , .property = name } });
33293333 } else if (self .check (.question_dot )) {
33303334 // `new o?.C()` / `new C?.()` is syntactically invalid; callers
33313335 // must parenthesize the optional chain (`new (o?.C)()`).
@@ -5866,6 +5870,61 @@ test "parser validates class private name uses" {
58665870 try std .testing .expectEqual (@as (usize , 1 ), nested_prog .program .len );
58675871}
58685872
5873+ test "parser private constructor members preserve lexical name validation" {
5874+ const valid = [_ ][]const u8 {
5875+ "class C { #Ctor; make() { return new this.#Ctor(); } }" ,
5876+ "class C { #Ctor; make() { return new this.#Ctor; } }" ,
5877+ "class C { #holder; make() { return new this.#holder.Ctor(7).value; } }" ,
5878+ "class C { get #Ctor() {} make(value = new this.#Ctor()) {} }" ,
5879+ "class C { #Ctor; make(value = new (this.#Ctor)()) {} }" ,
5880+ "class C { #Ctor; make() { return class D { run(c) { return new c.#Ctor(); } }; } }" ,
5881+ };
5882+ const Probe = struct {
5883+ fn alloc (raw : * anyopaque , len : usize , alignment : std.mem.Alignment , ra : usize ) ? [* ]u8 {
5884+ const backing : * std.mem.Allocator = @ptrCast (@alignCast (raw ));
5885+ return backing .rawAlloc (len , alignment , ra );
5886+ }
5887+
5888+ fn free (raw : * anyopaque , memory : []u8 , alignment : std.mem.Alignment , ra : usize ) void {
5889+ const backing : * std.mem.Allocator = @ptrCast (@alignCast (raw ));
5890+ backing .rawFree (memory , alignment , ra );
5891+ }
5892+
5893+ fn run (allocator : std.mem.Allocator , source : []const u8 ) ! void {
5894+ var arena = std .heap .ArenaAllocator .init (allocator );
5895+ defer arena .deinit ();
5896+ var parser = try Parser .init (arena .allocator (), source );
5897+ // Freeze the keyed parse input for deterministic allocation replay.
5898+ parser .secure_hash_state .context = .{ .seed = 0x807_7061_7273_65 };
5899+ const program = try parser .parseProgram ();
5900+ try std .testing .expectEqual (@as (usize , 1 ), program .program .len );
5901+ }
5902+ };
5903+ var backing = std .testing .allocator ;
5904+ // Arena growth must allocate during replay: whether the host can resize a
5905+ // particular mapping in place is address-dependent, not parser behavior.
5906+ const non_resizing : std.mem.Allocator = .{ .ptr = & backing , .vtable = &.{
5907+ .alloc = Probe .alloc ,
5908+ .resize = std .mem .Allocator .noResize ,
5909+ .remap = std .mem .Allocator .noRemap ,
5910+ .free = Probe .free ,
5911+ } };
5912+ for (valid ) | source | try std .testing .checkAllAllocationFailures (non_resizing , Probe .run , .{source });
5913+ const invalid = [_ ][]const u8 {
5914+ "new value.#Ctor()" ,
5915+ "class C { make() { return new this.#Ctor(); } }" ,
5916+ "class C { #Ctor; make() { return new this?.#Ctor(); } }" ,
5917+ "class C { #Ctor; make() { return new this.#Ctor?.(); } }" ,
5918+ "class C extends Base { #Ctor; make() { return new super.#Ctor(); } }" ,
5919+ };
5920+ for (invalid ) | source | {
5921+ var arena = std .heap .ArenaAllocator .init (std .testing .allocator );
5922+ defer arena .deinit ();
5923+ var parser = try Parser .init (arena .allocator (), source );
5924+ try std .testing .expectError (ParseError .UnexpectedToken , parser .parseProgram ());
5925+ }
5926+ }
5927+
58695928test "private eval contexts validate exact enclosing names" {
58705929 var arena = std .heap .ArenaAllocator .init (std .testing .allocator );
58715930 defer arena .deinit ();
0 commit comments