Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Handle singleton classes.

  • Loading branch information...
commit fa68163f9c03b45eba2bef7f0c114fdcb0f46ff2 1 parent 7ac7e3e
@mvz mvz authored
View
4 lib/ripper_ruby_parser/commenting_sexp_builder.rb
@@ -35,6 +35,10 @@ def on_class *args
commentize(:class, super)
end
+ def on_sclass *args
+ commentize(:class, super)
+ end
+
def on_def *args
commentize(:def, super)
end
View
5 lib/ripper_ruby_parser/sexp_processor.rb
@@ -71,6 +71,11 @@ def process_class exp
s(:class, const, parent, class_or_module_body(body)))
end
+ def process_sclass exp
+ _, klass, block = exp.shift 3
+ s(:sclass, process(klass), class_or_module_body(block))
+ end
+
def process_var_ref exp
_, contents = exp.shift 2
process(contents)
View
16 test/unit/parser_test.rb
@@ -29,6 +29,10 @@
nil,
s(:scope))
end
+
+ it "works for singleton classes" do
+ "class << self; end".must_be_parsed_as s(:sclass, s(:self), s(:scope))
+ end
end
describe "for a module declaration" do
@@ -1686,6 +1690,18 @@
s(:scope, s(:block, s(:lit, :class))))
result.comments.must_equal "# Foo\n"
end
+
+ it "handles use of singleton class inside methods" do
+ result = parser.parse "# Foo\ndef bar\nclass << self\nbaz\nend\nend"
+ result.must_equal s(:defn,
+ :bar,
+ s(:args),
+ s(:scope,
+ s(:block,
+ s(:sclass, s(:self),
+ s(:scope, s(:call, nil, :baz, s(:arglist)))))))
+ result.comments.must_equal "# Foo\n"
+ end
end
# Note: differences in the handling of line numbers are not caught by
Please sign in to comment.
Something went wrong with that request. Please try again.