Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix parse logic #1

Merged
merged 1 commit into from
Jan 5, 2024
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
19 changes: 15 additions & 4 deletions lib/packwerk_yard/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,16 @@ def initialize(ruby_parser: Packwerk::Parsers::Ruby.new)

def call(io:, file_path: "<unknown>")
source_code = io.read
file_path = file_path.gsub('.rb', '.yard.rb')
return to_ruby_ast(nil.inspect, file_path) if source_code.nil?

types = extract_from_yard_to_types(source_code)

to_ruby_ast(types.map { |type| to_constant(type) }.compact.inspect, file_path)
to_ruby_ast(
types.map { |type| to_evaluable_type(type) }
.reject { |type| to_constant(type).nil? }
.inspect.gsub('"', ""),
file_path
)
end

def match?(path:)
Expand All @@ -29,7 +33,6 @@ def match?(path:)
private

def extract_from_yard_to_types(source_code)
# TODO: parallel で packwerk が動作すると競合が発生する可能性があるため調査する
YARD::Registry.clear
YARD::Parser::SourceParser.parse_string(source_code)

Expand All @@ -45,8 +48,16 @@ def extract_from_yard_to_types(source_code)
types.uniq
end

def to_evaluable_type(type)
# "Array<Integer>" => "Integer"
if type =~ ARRAY_REGEXP
Regexp.last_match(1)
else
type
end
end

def to_constant(name)
name = Regexp.last_match(1) if name =~ ARRAY_REGEXP
Object.const_get(name)
rescue NameError
nil
Expand Down
2 changes: 1 addition & 1 deletion packwerk_yard.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "yard"
spec.add_dependency "parser"
spec.add_dependency "yard"
end
Loading