Skip to content

Commit

Permalink
Fix parse logic
Browse files Browse the repository at this point in the history
  • Loading branch information
euglena1215 committed Jan 5, 2024
1 parent 6c4a5fc commit f1b2da8
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
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

0 comments on commit f1b2da8

Please sign in to comment.