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
83 changes: 83 additions & 0 deletions alias_undef_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package parser

import (
"testing"

"github.com/go-ruby-parser/parser/ast"
)

// `alias` accepts symbol, operator-symbol, bare-name, and global-variable items.
func TestAlias(t *testing.T) {
cases := []struct {
src string
newN, oldName string
}{
{`alias :== :eql?`, "==", "eql?"},
{`alias foo bar`, "foo", "bar"},
{`alias == eql?`, "==", "eql?"},
{`alias new_name old_name`, "new_name", "old_name"},
{`alias $x $y`, "$x", "$y"},
}
for _, c := range cases {
prog, err := Parse(c.src)
if err != nil {
t.Fatalf("Parse(%q) error: %v", c.src, err)
}
a, ok := prog.Body[0].(*ast.Alias)
if !ok {
t.Fatalf("%q: expected *ast.Alias, got %T", c.src, prog.Body[0])
}
if a.NewName != c.newN || a.OldName != c.oldName {
t.Fatalf("%q: got (%q, %q), want (%q, %q)", c.src, a.NewName, a.OldName, c.newN, c.oldName)
}
}
}

// `undef` accepts one or more comma-separated method names.
func TestUndef(t *testing.T) {
prog, err := Parse(`undef :foo, :bar, baz`)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
u, ok := prog.Body[0].(*ast.Undef)
if !ok {
t.Fatalf("expected *ast.Undef, got %T", prog.Body[0])
}
want := []string{"foo", "bar", "baz"}
if len(u.Names) != len(want) {
t.Fatalf("names=%v, want %v", u.Names, want)
}
for i, w := range want {
if u.Names[i] != w {
t.Fatalf("name %d=%q, want %q", i, u.Names[i], w)
}
}
}

// `alias_method` (a plain identifier, not the keyword) is unaffected.
func TestAliasMethodIdentifierUnaffected(t *testing.T) {
if _, err := Parse(`alias_method :a, :b`); err != nil {
t.Fatalf("Parse error: %v", err)
}
}

// A chained multiple-assignment right-hand side (`a, b = c = expr`) is a further
// assignment whose value is then destructured.
func TestChainedMasgnRhs(t *testing.T) {
for _, src := range []string{
`a, b = c = [1, 2]`,
`_, headers, _ = response = call(x)`,
} {
prog, err := Parse(src)
if err != nil {
t.Fatalf("Parse(%q) error: %v", src, err)
}
ma, ok := prog.Body[0].(*ast.MultiAssign)
if !ok {
t.Fatalf("%q: expected *ast.MultiAssign, got %T", src, prog.Body[0])
}
if _, ok := ma.Values[0].(*ast.Assign); !ok {
t.Fatalf("%q: expected value0 *ast.Assign, got %T", src, ma.Values[0])
}
}
}
22 changes: 22 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,13 @@ type ConstAssign struct {
Value Node
}

// ScopedConstAssign assigns to a scope-resolved constant: `A::B = value` or the
// top-level `::A = value`. Target is the *ScopedConst that names the constant.
type ScopedConstAssign struct {
Target Node
Value Node
}

// IvarRef reads an instance variable (@name) of self.
type IvarRef struct{ Name string }

Expand Down Expand Up @@ -384,6 +391,18 @@ type FindPattern struct {
PostName string
}

// Alias is `alias NewName OldName` — it makes NewName an alias of an existing
// method (or global variable). Each name is the bare method/symbol/global text
// without a leading colon.
type Alias struct {
NewName string
OldName string
}

// Undef is `undef name [, name…]` — it removes the named method(s) from the
// current class/module. Names hold the bare method names.
type Undef struct{ Names []string }

// Retry restarts the enclosing begin body from inside a rescue clause.
type Retry struct{}

Expand Down Expand Up @@ -442,6 +461,7 @@ func (*MethodDef) node() {}
func (*Return) node() {}
func (*ConstRef) node() {}
func (*ConstAssign) node() {}
func (*ScopedConstAssign) node() {}
func (*GVarRef) node() {}
func (*GVarAssign) node() {}
func (*CVarRef) node() {}
Expand All @@ -462,6 +482,8 @@ func (*Begin) node() {}
func (*StrInterp) node() {}
func (*Case) node() {}
func (*Retry) node() {}
func (*Alias) node() {}
func (*Undef) node() {}
func (*SplatArg) node() {}
func (*BlockPass) node() {}
func (*ForwardArgs) node() {}
Expand Down
73 changes: 73 additions & 0 deletions block_comment_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package parser

import (
"testing"

"github.com/go-ruby-parser/parser/ast"
)

// `=begin` … `=end` block comments (at column 0) are skipped wholesale, with the
// surrounding code parsing normally.
func TestBlockComment(t *testing.T) {
cases := []string{
"=begin\nthis is a comment\nmore\n=end\nputs 1",
"x = 1\n=begin\ncomment\n=end\ny = 2",
"=begin doc\nfoo\n=end",
"=begin\nunterminated comment to EOF",
}
for _, src := range cases {
if _, err := Parse(src); err != nil {
t.Fatalf("Parse(%q) error: %v", src, err)
}
}
}

// A `=begin` not at column 0 is not a block comment: `x = begin … end` is an
// ordinary begin expression assigned to x.
func TestBlockCommentNotAtLineStart(t *testing.T) {
prog, err := Parse("x = begin\n 1\nend")
if err != nil {
t.Fatalf("Parse error: %v", err)
}
asn, ok := prog.Body[0].(*ast.Assign)
if !ok {
t.Fatalf("expected *ast.Assign, got %T", prog.Body[0])
}
if _, ok := asn.Value.(*ast.Begin); !ok {
t.Fatalf("expected RHS *ast.Begin, got %T", asn.Value)
}
}

// An identifier that merely starts with "begin" is unaffected.
func TestBlockCommentIdentifierPrefix(t *testing.T) {
if _, err := Parse("result = beginning"); err != nil {
t.Fatalf("Parse error: %v", err)
}
}

// The bitwise op-assignments |=, &=, ^=, >>= now lex (they were previously
// missing, so `mode |= x` failed). Confirm each produces an OpAssign.
func TestBitwiseOpAssign(t *testing.T) {
for _, c := range []struct {
src string
op string
}{
{"mode |= x", "|"},
{"a &= b", "&"},
{"c ^= d", "^"},
{"x >>= 2", ">>"},
{"y <<= 1", "<<"},
} {
prog, err := Parse(c.src)
if err != nil {
t.Fatalf("Parse(%q) error: %v", c.src, err)
}
oa, ok := prog.Body[0].(*ast.OpAssign)
if !ok {
t.Fatalf("%q: expected *ast.OpAssign, got %T", c.src, prog.Body[0])
}
if oa.Op != c.op {
t.Fatalf("%q: op=%q, want %q", c.src, oa.Op, c.op)
}
}
}
54 changes: 54 additions & 0 deletions command_do_block_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package parser

import (
"testing"

"github.com/go-ruby-parser/parser/ast"
)

// A `do…end` block following a paren-less command call on a receiver binds to
// that command call, mirroring MRI (`obj.foo bar do … end`). Previously the
// receiver command-call path returned before it could attach the block.
func TestReceiverCommandDoBlock(t *testing.T) {
cases := []string{
`recv.set_callback :work, prepend: true do |x| end`,
`Capybara.register_driver name do |app| end`,
`obj.instrument "x", y do end`,
`ActiveSupport.on_load(:action_cable) do; end`,
}
for _, src := range cases {
if _, err := Parse(src); err != nil {
t.Fatalf("Parse(%q) error: %v", src, err)
}
}
}

// The block-bearing receiver command call can itself be chained (the block
// attaches to the command, then the postfix chain continues).
func TestReceiverCommandDoBlockChained(t *testing.T) {
prog, err := Parse(`app.foo bar do end.baz`)
if err != nil {
t.Fatalf("Parse error: %v", err)
}
outer, ok := prog.Body[0].(*ast.Call)
if !ok || outer.Name != "baz" {
t.Fatalf("expected outer .baz call, got %T", prog.Body[0])
}
inner, ok := outer.Recv.(*ast.Call)
if !ok || inner.Name != "foo" || inner.Block == nil {
t.Fatalf("expected inner foo call with block, got %#v", outer.Recv)
}
}

// A braced block, by contrast, binds tighter (to the nearest call/arg), and a
// `do` inside a while/until condition still belongs to the loop, not the call.
func TestReceiverCommandBlockBoundaries(t *testing.T) {
for _, src := range []string{
`while obj.foo bar do end`,
`recv.each x { |y| y }`,
} {
if _, err := Parse(src); err != nil {
t.Fatalf("Parse(%q) error: %v", src, err)
}
}
}
83 changes: 83 additions & 0 deletions def_extras_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package parser

import (
"testing"

"github.com/go-ruby-parser/parser/ast"
)

// A `def` may name an explicit receiver that is an instance/class/global
// variable, not just self/local/constant.
func TestDefVariableReceiver(t *testing.T) {
cases := []struct {
src string
kind string
}{
{"def @controller.foo(&b)\nend", "ivar"},
{"def @@reg.foo\nend", "cvar"},
{"def $g.foo\nend", "gvar"},
}
for _, c := range cases {
prog, err := Parse(c.src)
if err != nil {
t.Fatalf("Parse(%q) error: %v", c.src, err)
}
md := prog.Body[0].(*ast.MethodDef)
switch c.kind {
case "ivar":
if _, ok := md.Recv.(*ast.IvarRef); !ok {
t.Fatalf("%q: recv=%T, want *ast.IvarRef", c.src, md.Recv)
}
case "cvar":
if _, ok := md.Recv.(*ast.CVarRef); !ok {
t.Fatalf("%q: recv=%T, want *ast.CVarRef", c.src, md.Recv)
}
case "gvar":
if _, ok := md.Recv.(*ast.GVarRef); !ok {
t.Fatalf("%q: recv=%T, want *ast.GVarRef", c.src, md.Recv)
}
}
}
}

// A `def` is a value-producing expression, so it can be a command argument:
// `module_function def server; end`, `private def bar; end`.
func TestDefAsCommandArgument(t *testing.T) {
for _, src := range []string{
"module_function def server\n 1\nend",
"private def bar; end",
} {
prog, err := Parse(src)
if err != nil {
t.Fatalf("Parse(%q) error: %v", src, err)
}
call, ok := prog.Body[0].(*ast.Call)
if !ok {
t.Fatalf("%q: expected *ast.Call, got %T", src, prog.Body[0])
}
if _, ok := call.Args[0].(*ast.MethodDef); !ok {
t.Fatalf("%q: arg0=%T, want *ast.MethodDef", src, call.Args[0])
}
}
}

// A string interpolation body is a full statement, so it admits a trailing
// modifier and several statements.
func TestInterpolationBodyStatements(t *testing.T) {
for _, src := range []string{
`"x#{'s' if n > 1}y"`,
`"v#{a; b}"`,
`"#{}"`, // empty interpolation
} {
if _, err := Parse(src); err != nil {
t.Fatalf("Parse(%q) error: %v", src, err)
}
}
}

// A bare incomplete `def` is still a clean parse error (not an internal panic).
func TestBareDefError(t *testing.T) {
if _, err := Parse("def"); err == nil {
t.Fatalf("expected error for bare def")
}
}
Loading
Loading