Skip to content

Commit 68a81d2

Browse files
authored
Merge pull request #7 from go-ruby-parser/fix-operator-method-name
fix: operator-method call names (regression from Round 4)
2 parents 266559c + 2971862 commit 68a81d2

2 files changed

Lines changed: 47 additions & 5 deletions

File tree

‎parser.go‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,13 +2536,16 @@ func (p *Parser) methodName() string {
25362536
return t.Lit
25372537
}
25382538
switch t.Type {
2539-
// Operator methods called explicitly: 1.+(2), a.<=>(b), obj.&(x), …
2539+
// Operator methods called explicitly: 1.+(2), a.<=>(b), obj.&(x), … Each
2540+
// operator token names the method by its own spelling ("+", "<=>", "&", …).
25402541
case token.SPACESHIP, token.LT, token.GT, token.LE, token.GE, token.EQ, token.EQQ, token.NEQ,
25412542
token.SHOVEL, token.RSHIFT, token.PLUS, token.MINUS, token.STAR, token.SLASH, token.PERCENT, token.POW,
2542-
token.AMPER, token.PIPE, token.CARET, token.TILDE, token.MATCH, token.NMATCH, token.BANG,
2543-
token.XSTRING:
2544-
// XSTRING here is an empty backtick literal `` produced when `` ` `` names the
2545-
// backtick method (`def \`(cmd); end`, `obj.\``).
2543+
token.AMPER, token.PIPE, token.CARET, token.TILDE, token.MATCH, token.NMATCH, token.BANG:
2544+
p.advance()
2545+
return t.Type.String()
2546+
case token.XSTRING:
2547+
// An empty backtick literal `` produced when `` ` `` names the backtick
2548+
// method (`def \`(cmd); end`, `obj.\``).
25462549
p.advance()
25472550
return "`"
25482551
}

‎round4_features_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,45 @@ func TestUnicodeIdentifiers(t *testing.T) {
188188
parsesOK(t, "p ?é\n")
189189
}
190190

191+
// TestOperatorMethodName asserts that an explicitly-called operator method
192+
// (`recv.OP(arg)`) names the method by the operator's own spelling — not the
193+
// backtick “ ` “ that only the empty-XSTRING backtick-method case yields.
194+
func TestOperatorMethodName(t *testing.T) {
195+
cases := []struct{ src, name string }{
196+
{"1.+(2)", "+"},
197+
{"1.-(2)", "-"},
198+
{"1.*(2)", "*"},
199+
{"1.**(2)", "**"},
200+
{"a.<=>(b)", "<=>"},
201+
{"a.<(b)", "<"},
202+
{"a.>(b)", ">"},
203+
{"a.<=(b)", "<="},
204+
{"a.>=(b)", ">="},
205+
{"a.==(b)", "=="},
206+
{"a.===(b)", "==="},
207+
{"a.!=(b)", "!="},
208+
{"a.<<(b)", "<<"},
209+
{"a.>>(b)", ">>"},
210+
{"a.&(b)", "&"},
211+
{"a.|(b)", "|"},
212+
{"a.^(b)", "^"},
213+
{"a.=~(b)", "=~"},
214+
{"a.!~(b)", "!~"},
215+
{"a.~", "~"},
216+
{"a.!", "!"},
217+
{"a.``(c)", "`"}, // the backtick method (empty XSTRING literal), still "`"
218+
}
219+
for _, c := range cases {
220+
call, ok := parseOne(t, c.src).(*ast.Call)
221+
if !ok {
222+
t.Fatalf("Parse(%q): top node = %T, want *ast.Call", c.src, parseOne(t, c.src))
223+
}
224+
if call.Name != c.name {
225+
t.Errorf("Parse(%q): method name = %q, want %q", c.src, call.Name, c.name)
226+
}
227+
}
228+
}
229+
191230
// --- Feature 8: safe-navigation with an operator method ---
192231

193232
func TestSafeNavOperatorMethod(t *testing.T) {

0 commit comments

Comments
 (0)