-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add handler for abstract!/interface! modules (#50)
* Add abstract handler * Update documentation * test interfaces and preserving existing tags * Rename to AbstractDSLHandler; add class-specific text
- Loading branch information
Showing
10 changed files
with
143 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
--main README.md | ||
--private | ||
--plugin sorbet | ||
lib/**/*.rb | ||
|
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,26 @@ | ||
# typed: strict | ||
# frozen_string_literal: true | ||
|
||
# Apllies an +@abstract+ tag to +abstract!+/+interface!+ modules (if not alerady present). | ||
class YARDSorbet::Handlers::AbstractDSLHandler < YARD::Handlers::Ruby::Base | ||
extend T::Sig | ||
|
||
handles method_call(:abstract!), method_call(:interface!) | ||
namespace_only | ||
|
||
# The text accompanying the `@abstract` tag. | ||
# @see https://github.com/lsegal/yard/blob/main/templates/default/docstring/html/abstract.erb | ||
# The `@abstract` tag template | ||
TAG_TEXT = 'Subclasses must implement the `abstract` methods below.' | ||
# Extra text for class namespaces | ||
CLASS_TAG_TEXT = T.let("This class cannont be directly instantiated. #{TAG_TEXT}", String) | ||
|
||
sig { void } | ||
def process | ||
return if namespace.has_tag?(:abstract) | ||
|
||
text = namespace.is_a?(YARD::CodeObjects::ClassObject) ? CLASS_TAG_TEXT : TAG_TEXT | ||
tag = YARD::Tags::Tag.new(:abstract, text) | ||
namespace.add_tag(tag) | ||
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
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,35 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
# An abstract class | ||
# @note this class is abstract | ||
class MyAbstractClass | ||
extend T::Helpers | ||
extend T::Sig | ||
|
||
abstract! | ||
|
||
sig { abstract.void } | ||
def abstract_method; end | ||
end | ||
|
||
|
||
module MyInterface | ||
extend T::Helpers | ||
extend T::Sig | ||
|
||
interface! | ||
sig { abstract.returns(T::Boolean) } | ||
def ibool; end | ||
end | ||
|
||
|
||
# @abstract Existing abstract tag | ||
module MyTaggedAbstractModule | ||
extend T::Helpers | ||
extend T::Sig | ||
|
||
abstract! | ||
sig { abstract.returns(T::Boolean) } | ||
def ibool; 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,34 @@ | ||
# typed: true | ||
# frozen_string_literal: true | ||
|
||
|
||
module MyInterface | ||
extend T::Helpers | ||
extend T::Sig | ||
|
||
interface! | ||
sig { abstract.returns(T::Boolean) } | ||
def ibool; end | ||
end | ||
|
||
# An abstract class | ||
# @note this class is abstract | ||
class MyAbstractClass | ||
extend T::Helpers | ||
extend T::Sig | ||
|
||
abstract! | ||
|
||
sig { abstract.void } | ||
def abstract_method; end | ||
end | ||
|
||
# @abstract Existing abstract tag | ||
module MyTaggedAbstractModule | ||
extend T::Helpers | ||
extend T::Sig | ||
|
||
abstract! | ||
sig { abstract.returns(T::Boolean) } | ||
def ibool; 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,41 @@ | ||
# typed: false | ||
# frozen_string_literal: true | ||
|
||
require 'yard' | ||
|
||
RSpec.describe YARDSorbet::Handlers::AbstractDSLHandler do | ||
path = File.join( | ||
File.expand_path('../../data', __dir__), | ||
'abstract_dsl_handler.rb' | ||
) | ||
|
||
before do | ||
YARD::Registry.clear | ||
YARD::Parser::SourceParser.parse(path) | ||
end | ||
|
||
describe 'modules with abstract!/interface! declarations' do | ||
it('apply @abstract tags') do | ||
node = YARD::Registry.at('MyInterface') | ||
expect(node.tags.size).to eq(1) | ||
expect(node.has_tag?(:abstract)).to be(true) | ||
expect(node.tags.first.text).to eq(YARDSorbet::Handlers::AbstractDSLHandler::TAG_TEXT) | ||
end | ||
|
||
it('apply class text to abstract classes') do | ||
node = YARD::Registry.at('MyAbstractClass') | ||
expect(node.docstring).to eq('An abstract class') | ||
expect(node.tags.size).to eq(2) | ||
expect(node.has_tag?(:abstract)).to be(true) | ||
abstract_tag = node.tags.find { |tag| tag.tag_name == 'abstract' } | ||
expect(abstract_tag.text).to eq(YARDSorbet::Handlers::AbstractDSLHandler::CLASS_TAG_TEXT) | ||
end | ||
|
||
it('keep existing @abstract tags') do | ||
node = YARD::Registry.at('MyTaggedAbstractModule') | ||
expect(node.has_tag?(:abstract)).to be(true) | ||
abstract_tag = node.tags.find { |tag| tag.tag_name == 'abstract' } | ||
expect(abstract_tag.text).to eq('Existing abstract tag') | ||
end | ||
end | ||
end |