Skip to content
Merged
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
2 changes: 2 additions & 0 deletions operator_def_xstr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ func TestOperatorMethodDef(t *testing.T) {
{"def |(o); end", "|"},
{"def ^(o); end", "^"},
{"def =~(o); end", "=~"},
{"def !~(o); end", "!~"},
{"def !=(o); end", "!="},
{"def [](i); end", "[]"},
{"def []=(i, v); end", "[]="},
{"def +(o); end", "+"},
Expand Down
6 changes: 4 additions & 2 deletions parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,9 @@ func (p *Parser) parseClass() ast.Node {
// `class << target` opens target's singleton (metaclass). A SHOVEL here is
// the singleton-class form, not a constant path.
if p.accept(token.SHOVEL) {
target := p.parseTernary()
// The target is a full expression, including an assignment — MRI allows
// `class << @x = Object.new` (assign, then open the result's singleton).
target := p.parseExprOrAssign()
p.pushScope() // the singleton-class body has its own local scope
body := p.parseBodyWithRescue()
p.popScope()
Expand Down Expand Up @@ -618,7 +620,7 @@ func (p *Parser) parseDefName() (string, bool) {
return name, true
case token.CONST,
token.SPACESHIP, token.LT, token.GT, token.LE, token.GE,
token.EQ, token.EQQ, token.NEQ, token.MATCH, token.SHOVEL, token.RSHIFT,
token.EQ, token.EQQ, token.NEQ, token.MATCH, token.NMATCH, token.SHOVEL, token.RSHIFT,
token.STAR, token.POW, token.SLASH, token.PERCENT,
token.AMPER, token.PIPE, token.CARET:
return p.advance().Lit, true
Expand Down
14 changes: 14 additions & 0 deletions singleton_class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,20 @@ func TestSingletonClassExprTarget(t *testing.T) {
}
}

// TestSingletonClassAssignTarget covers `class << @x = expr` — MRI allows the
// singleton target to be an assignment (assign, then open the result's singleton
// class), which needs a full expression, not just a ternary, after the `<<`.
func TestSingletonClassAssignTarget(t *testing.T) {
sc := topSingleton(t, "class << @receiver = Object.new\n def m; end\nend")
asn, ok := sc.Target.(*ast.IvarAssign)
if !ok || asn.Name != "@receiver" {
t.Fatalf("Target = %T (%+v), want *ast.IvarAssign @receiver", sc.Target, sc.Target)
}
if len(sc.Body) != 1 {
t.Fatalf("Body has %d nodes, want 1", len(sc.Body))
}
}

// TestSingletonClassEmptyBody covers a singleton class with no body statements.
func TestSingletonClassEmptyBody(t *testing.T) {
sc := topSingleton(t, "class << self\nend")
Expand Down