From 47f566fae74a8ad26361336a48a1cd61169e095b Mon Sep 17 00:00:00 2001 From: Paul Sadauskas Date: Sun, 24 Jun 2018 20:14:21 -0600 Subject: [PATCH] Change Examiner to always expect a SourceCode Also allow `SourceCode.from` to noop if the provided source is already a `SourceCode` --- features/command_line_interface/stdin.feature | 1 + lib/reek/cli/command/report_command.rb | 4 ++-- lib/reek/examiner.rb | 13 +++++-------- lib/reek/source/source_code.rb | 7 ++++--- 4 files changed, 12 insertions(+), 13 deletions(-) diff --git a/features/command_line_interface/stdin.feature b/features/command_line_interface/stdin.feature index 18359ce67..6f91d38de 100644 --- a/features/command_line_interface/stdin.feature +++ b/features/command_line_interface/stdin.feature @@ -67,6 +67,7 @@ Feature: Reek reads from $stdin when no files are given end """ Then it succeeds + And it reports nothing diff --git a/lib/reek/cli/command/report_command.rb b/lib/reek/cli/command/report_command.rb index 2fdd529b0..7a0c0ad48 100644 --- a/lib/reek/cli/command/report_command.rb +++ b/lib/reek/cli/command/report_command.rb @@ -23,8 +23,8 @@ def execute def populate_reporter_with_smells sources.each do |source| - reporter.add_examiner Examiner.new(source, - stdin_filename: options.stdin_filename, + reporter.add_examiner Examiner.new(Source::SourceCode.from(source), + origin: options.stdin_filename, filter_by_smells: smell_names, configuration: configuration, error_handler: LoggingErrorHandler.new) diff --git a/lib/reek/examiner.rb b/lib/reek/examiner.rb index ee6f9c190..52f3f6b39 100644 --- a/lib/reek/examiner.rb +++ b/lib/reek/examiner.rb @@ -35,12 +35,13 @@ def handle(_exception) # @public # :reek:LongParameterList { max_params: 6 } def initialize(source, - stdin_filename: nil, + origin: nil, filter_by_smells: [], configuration: Configuration::AppConfiguration.default, detector_repository_class: DetectorRepository, error_handler: NullHandler.new) - @source = Source::SourceCode.from(source, filename: stdin_filename) + @source = Source::SourceCode.from(source) + @origin = origin || @source.origin @smell_types = detector_repository_class.eligible_smell_types(filter_by_smells) @detector_repository = detector_repository_class.new(smell_types: @smell_types, configuration: configuration.directive_for(description)) @@ -50,17 +51,13 @@ def initialize(source, # @return [String] origin of the source being analysed # # @public - def origin - @origin ||= source.origin - end + attr_reader :origin # @return [String] description of the source being analysed # # @public # @deprecated Use origin - def description - origin - end + alias description origin # # @return [Array] the smells found in the source diff --git a/lib/reek/source/source_code.rb b/lib/reek/source/source_code.rb index b0d0341b9..8cc21c01f 100644 --- a/lib/reek/source/source_code.rb +++ b/lib/reek/source/source_code.rb @@ -60,12 +60,13 @@ def initialize(code:, origin:, parser: self.class.default_parser) # @return an instance of SourceCode # :reek:DuplicateMethodCall { max_calls: 2 } # :reek:ControlParameter - def self.from(source, filename: nil) + def self.from(source) case source + when self then source when File then new(code: source.read, origin: source.path) - when IO then new(code: source.readlines.join, origin: filename || IO_IDENTIFIER) + when IO then new(code: source.readlines.join, origin: IO_IDENTIFIER) when Pathname then new(code: source.read, origin: source.to_s) - when String then new(code: source, origin: filename || STRING_IDENTIFIER) + when String then new(code: source, origin: STRING_IDENTIFIER) end end