From aa2bea5b8cb8f64f4d88c8e0ccfc4573259f0e11 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 | 15 +++++++-------- lib/reek/source/source_code.rb | 8 ++++---- 4 files changed, 14 insertions(+), 14 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..ffacd4124 100644 --- a/lib/reek/examiner.rb +++ b/lib/reek/examiner.rb @@ -11,6 +11,7 @@ module Reek # Applies all available smell detectors to a source. # # @public + # :reek:TooManyInstanceVariables { max_instance_variables: 5 } class Examiner # Handles no errors class NullHandler @@ -33,14 +34,16 @@ def handle(_exception) # The configuration for this Examiner. # # @public + # :reek:ControlParameter # :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 +53,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..f4d388c8f 100644 --- a/lib/reek/source/source_code.rb +++ b/lib/reek/source/source_code.rb @@ -59,13 +59,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