Skip to content

parser: ArrayLiteral as computed property key rejected in class bodies #22157

@dvrd

Description

@dvrd

parser: ArrayLiteral as computed property key rejected in class bodies

class C { [[]]() {} } is rejected with "Unexpected token", but the grammar is valid per ECMA-262 and every other major parser accepts it.

Reproduction

Playground link

Steps:

mkdir /tmp/oxc-repro && cd /tmp/oxc-repro
npm init -y
npm install oxc-parser
node -e "
  const { parseSync } = require('oxc-parser');

  // ✗ Rejected in class body
  const r1 = parseSync('input.js', 'class C { [[]]() {} }');
  console.log('class [[]]():', r1.errors.length ? 'ERROR: ' + r1.errors[0].message : 'OK');

  // ✗ Also rejected with non-empty inner array
  const r2 = parseSync('input.js', 'class C { [[1]]() {} }');
  console.log('class [[1]]():', r2.errors.length ? 'ERROR: ' + r2.errors[0].message : 'OK');

  // ✗ Also rejected as field
  const r3 = parseSync('input.js', 'class C { [[]] = 1 }');
  console.log('class [[]] = 1:', r3.errors.length ? 'ERROR: ' + r3.errors[0].message : 'OK');

  // ✓ Accepted in object literal
  const r4 = parseSync('input.js', 'const o = { [[]]() {} }');
  console.log('obj [[]]():', r4.errors.length ? 'ERROR: ' + r4.errors[0].message : 'OK');
"

Expected output:

class [[]](): OK
class [[1]](): OK
class [[]] = 1: OK
obj [[]](): OK

Actual output (oxc-parser 0.129.0):

class [[]](): ERROR: Unexpected token
class [[1]](): ERROR: Unexpected token
class [[]] = 1: ERROR: Unexpected token
obj [[]](): OK

V8 cross-check (confirming it's valid JS):

echo 'class C { [[]]() {} }' > /tmp/test.js
node --check /tmp/test.js && echo "V8: OK" || echo "V8: ERROR"
# → V8: OK

Spec reference

ECMA-262 §13.2.5.2 ComputedPropertyName: [ AssignmentExpression ]

AssignmentExpression → … → PrimaryExpressionArrayLiteral[ ElementList_opt ]

An empty ArrayLiteral ([]) is a valid AssignmentExpression, so [[]] as a class element's computed key is grammatically correct. The same grammar applies to both object literals and class bodies — there is no restriction that excludes ArrayLiteral from class ComputedPropertyName.

Parser matrix

Parser Version class { [[]]() {} } { [[]]() {} } (obj)
V8 (node --check) v25.9.0 ✅ accepts ✅ accepts
Acorn latest ✅ accepts ✅ accepts
Babel 7.29.3 ✅ accepts ✅ accepts
TypeScript 6.0.3 ✅ accepts ✅ accepts
SWC 1.15.33 ✅ accepts ✅ accepts
esbuild 0.28.0 ✅ accepts ✅ accepts
OXC 0.129.0 ❌ rejects ✅ accepts

OXC is the only parser that rejects this syntax, and only in class bodies.

Metadata

Metadata

Assignees

Labels

Type

Priority

None yet

Effort

None yet

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions