Skip to content

Commit

Permalink
Change Examiner to always expect a SourceCode
Browse files Browse the repository at this point in the history
Also allow `SourceCode.from` to noop if the provided source is already a
`SourceCode`
  • Loading branch information
paul committed Jun 25, 2018
1 parent 2ca1882 commit 47f566f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 13 deletions.
1 change: 1 addition & 0 deletions features/command_line_interface/stdin.feature
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ Feature: Reek reads from $stdin when no files are given
end
"""
Then it succeeds
And it reports nothing



4 changes: 2 additions & 2 deletions lib/reek/cli/command/report_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 5 additions & 8 deletions lib/reek/examiner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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<SmellWarning>] the smells found in the source
Expand Down
7 changes: 4 additions & 3 deletions lib/reek/source/source_code.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 47f566f

Please sign in to comment.