forked from lsegal/yard
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for
private_constant
class method calls to recognize pr…
…ivate class, module and constant definitions (proposed for Ruby 1.9.3). Closes lsegalgh-219
- Loading branch information
Showing
7 changed files
with
130 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# (see Ruby::PrivateConstantHandler) | ||
class YARD::Handlers::Ruby::Legacy::PrivateConstantHandler < YARD::Handlers::Ruby::Legacy::Base | ||
namespace_only | ||
handles /\Aprivate_constant(\s|\(|$)/ | ||
|
||
process do | ||
tokval_list(statement.tokens[2..-1], :attr, TkCONSTANT).each do |name| | ||
privatize_constant name | ||
end | ||
end | ||
|
||
private | ||
|
||
def privatize_constant(name) | ||
const = Proxy.new(namespace, name) | ||
ensure_loaded!(const) | ||
const.visibility = :private | ||
rescue NamespaceMissingError | ||
raise UndocumentableError, "private visibility set on unrecognized constant: #{name}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Sets visibility of a constant (class, module, const) | ||
class YARD::Handlers::Ruby::PrivateConstantHandler < YARD::Handlers::Ruby::Base | ||
namespace_only | ||
handles method_call(:private_constant) | ||
|
||
process do | ||
errors = [] | ||
statement.parameters.each do |param| | ||
next unless param.respond_to?(:type) | ||
begin | ||
privatize_constant(param) | ||
rescue UndocumentableError => err | ||
errors << err.message | ||
end | ||
end | ||
if errors.size > 0 | ||
msg = errors.size == 1 ? ": #{errors[0]}" : "s: #{errors.join(", ")}" | ||
raise UndocumentableError, "private constant#{msg} for #{namespace.path}" | ||
end | ||
end | ||
|
||
private | ||
|
||
def privatize_constant(node) | ||
if node.literal? || (node.type == :var_ref && node[0].type == :const) | ||
node = node.jump(:tstring_content, :const) | ||
const = Proxy.new(namespace, node[0]) | ||
ensure_loaded!(const) | ||
const.visibility = :private | ||
else | ||
raise UndocumentableError, "invalid argument to private_constant: #{node.source}" | ||
end | ||
rescue NamespaceMissingError | ||
raise UndocumentableError, "private visibility set on unrecognized constant: #{node[0]}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module A | ||
Foo = 1 | ||
class B; end | ||
module C; end | ||
module D; end | ||
|
||
private_constant :Foo, 'B', C | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
require File.dirname(__FILE__) + '/spec_helper' | ||
|
||
describe "YARD::Handlers::Ruby::#{RUBY18 ? "Legacy::" : ""}PrivateConstantHandler" do | ||
before(:all) { parse_file :private_constant_handler_001, __FILE__ } | ||
|
||
it "should handle private_constant statement" do | ||
Registry.at('A::Foo').visibility.should == :private | ||
Registry.at('A::B').visibility.should == :private | ||
Registry.at('A::C').visibility.should == :private | ||
end | ||
|
||
it "should make all other constants public" do | ||
Registry.at('A::D').visibility.should == :public | ||
end | ||
|
||
it "should fail if parameter is not String, Symbol or Constant" do | ||
undoc_error 'class Foo; private_constant x; end' | ||
undoc_error 'class Foo; X = 1; private_constant X.new("hi"); end' | ||
end if RUBY19 | ||
|
||
it "should fail if constant can't be recognized" do | ||
undoc_error 'class Foo2; private_constant :X end' | ||
end | ||
end |