Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions feature/accessor.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,13 @@ const accessor = (kind) => a => {
token('get', ASSIGN - 1, accessor('get'));
token('set', ASSIGN - 1, accessor('set'));

// Method shorthand: { foo() {} } → [':', 'foo', ['=>', ['()', params], body]]
// Method shorthand: { foo() {} } / { "foo"() {} } → [':', 'foo', ['=>', ['()', params], body]]
// Uses token() infix handler - returns undefined to fall through to function call
token('(', ASSIGN - 1, a => {
// Only handle infix position with plain identifier in low-precedence context (object literal)
if (!a || typeof a !== 'string') return;
// Only handle infix position with a static property key in low-precedence context (object literal)
if (!a) return;
if (Array.isArray(a) && a[0] === undefined) a = a[1];
if (typeof a !== 'string') return;
const params = expr(0, CPAREN) || null;
space();
// Not followed by { - not method shorthand, fall through
Expand Down
18 changes: 16 additions & 2 deletions feature/class.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
// Class declarations and expressions - parse half
// class A extends B { ... }
import { binary, unary, token, expr, space, next, parse, keyword, word, skip } from '../parse.js';
import { binary, token, expr, space, next, parse, keyword, word, skip, cur, idx } from '../parse.js';
import { block } from './if.js';

const TOKEN = 200, PREFIX = 140, COMP = 90;
const OPAREN = 40, CPAREN = 41, OBRACE = 123, CBRACE = 125;

// static member → ['static', member]
unary('static', PREFIX);
token('static', PREFIX, a => {
Copy link
Copy Markdown
Owner

@dy dy May 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can it be kept as unary call - we tend to keep token for low-level primitives outside of normal syntax rules.
@copilot

if (a) return;
space();
const name = next(parse.id);
if (!name) return;
space();
if (cur.charCodeAt(idx) !== OPAREN) return ['static', name];
skip();
const params = expr(0, CPAREN) || null;
Comment on lines +10 to +18
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot apply changes based on this feedback
I would try using unary still for static - token is for low-level primitives

space();
if (cur.charCodeAt(idx) !== OBRACE) return ['static', ['()', name, params]];
skip();
return ['static', [':', name, ['=>', ['()', params], expr(0, CBRACE) || null]]];
});

// instanceof: object instanceof Constructor
binary('instanceof', COMP);
Expand Down
8 changes: 8 additions & 0 deletions test/feature/async-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ test('async/class: anonymous class', () => {
test('async/class: static', () => {
is(parse('static x'), ['static', 'x']);
is(parse('static x = 1'), ['=', ['static', 'x'], [, 1]]);
is(parse('class A { static m(a) { return a } }'), [
'class', 'A', null,
['static', [':', 'm', ['=>', ['()', 'a'], ['return', 'a']]]]
]);
});

test('async/class: super', () => {
Expand Down Expand Up @@ -130,6 +134,10 @@ test('meta: new.target', () => {
test('object: method shorthand', () => {
is(parse('{ foo() {} }'), ['{}', [':', 'foo', ['=>', ['()', null], null]]]);
is(parse('{ add(a, b) { a + b } }'), ['{}', [':', 'add', ['=>', ['()', [',', 'a', 'b']], ['+', 'a', 'b']]]]);
is(parse('{ "x/y.js"(exports, module) { module.exports = {} } }'), [
'{}',
[':', 'x/y.js', ['=>', ['()', [',', 'exports', 'module']], ['=', ['.', 'module', 'exports'], ['{}', null]]]]
]);
// Evaluation
const obj = compile(parse('{ double(x) { x * 2 } }'))();
is(obj.double(5), 10);
Expand Down
Loading