diff --git a/lib/packwerk_yard/parser.rb b/lib/packwerk_yard/parser.rb index 0e6ac27..2e45822 100644 --- a/lib/packwerk_yard/parser.rb +++ b/lib/packwerk_yard/parser.rb @@ -14,12 +14,16 @@ def initialize(ruby_parser: Packwerk::Parsers::Ruby.new) def call(io:, file_path: "") 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:) @@ -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) @@ -45,8 +48,16 @@ def extract_from_yard_to_types(source_code) types.uniq end + def to_evaluable_type(type) + # "Array" => "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 diff --git a/packwerk_yard.gemspec b/packwerk_yard.gemspec index 0ba2411..8db57e8 100644 --- a/packwerk_yard.gemspec +++ b/packwerk_yard.gemspec @@ -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