Class: RuboCop::ProcessedSource

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/processed_source.rb

Overview

ProcessedSource contains objects which are generated by Parser and other information such as disabled lines for cops. It also provides a convenient way to access source lines.

Constant Summary collapse

STRING_SOURCE_NAME =
'(string)'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, path = nil) ⇒ ProcessedSource

Returns a new instance of ProcessedSource.



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rubocop/processed_source.rb', line 24

def initialize(source, path = nil)
  # In Ruby 2, source code encoding defaults to UTF-8. We follow the same
  # principle regardless of which Ruby version we're running under.
  # Encoding comments will override this setting.
  source.force_encoding(Encoding::UTF_8)

  @raw_source = source
  @path = path
  @diagnostics = []
  parse(source)
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



12
13
14
# File 'lib/rubocop/processed_source.rb', line 12

def ast
  @ast
end

#bufferObject (readonly)

Returns the value of attribute buffer.



12
13
14
# File 'lib/rubocop/processed_source.rb', line 12

def buffer
  @buffer
end

#commentsObject (readonly)

Returns the value of attribute comments.



12
13
14
# File 'lib/rubocop/processed_source.rb', line 12

def comments
  @comments
end

#diagnosticsObject (readonly)

Returns the value of attribute diagnostics.



12
13
14
# File 'lib/rubocop/processed_source.rb', line 12

def diagnostics
  @diagnostics
end

#parser_errorObject (readonly)

Returns the value of attribute parser_error.



12
13
14
# File 'lib/rubocop/processed_source.rb', line 12

def parser_error
  @parser_error
end

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/rubocop/processed_source.rb', line 12

def path
  @path
end

#raw_sourceObject (readonly)

Returns the value of attribute raw_source.



12
13
14
# File 'lib/rubocop/processed_source.rb', line 12

def raw_source
  @raw_source
end

#tokensObject (readonly)

Returns the value of attribute tokens.



12
13
14
# File 'lib/rubocop/processed_source.rb', line 12

def tokens
  @tokens
end

Class Method Details

.from_file(path) ⇒ Object



15
16
17
18
19
20
21
22
# File 'lib/rubocop/processed_source.rb', line 15

def self.from_file(path)
  file = File.read(path)
  new(file, path)
rescue Errno::ENOENT
  abort("#{Rainbow('rubocop: No such file or directory').red} -- #{path}")
rescue => ex
  abort("#{Rainbow("rubocop: #{ex.message}").red} -- #{path}")
end

Instance Method Details

#[](*args) ⇒ Object



59
60
61
# File 'lib/rubocop/processed_source.rb', line 59

def [](*args)
  lines[*args]
end

#checksumObject

Raw source checksum for tracking infinite loops.



69
70
71
# File 'lib/rubocop/processed_source.rb', line 69

def checksum
  Digest::MD5.hexdigest(@raw_source)
end

#comment_configObject



36
37
38
# File 'lib/rubocop/processed_source.rb', line 36

def comment_config
  @comment_config ||= CommentConfig.new(self)
end

#disabled_line_rangesObject



40
41
42
# File 'lib/rubocop/processed_source.rb', line 40

def disabled_line_ranges
  comment_config.cop_disabled_line_ranges
end

#linesObject

Returns the source lines, line break characters removed, excluding a possible END and everything that comes after.



46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/rubocop/processed_source.rb', line 46

def lines
  @lines ||= begin
    all_lines = raw_source.lines.map(&:chomp)
    last_token_line = tokens.any? ? tokens.last.pos.line : all_lines.size
    result = []
    all_lines.each_with_index do |line, ix|
      break if ix >= last_token_line && line == '__END__'
      result << line
    end
    result
  end
end

#valid_syntax?Boolean

Returns:

  • (Boolean)


63
64
65
66
# File 'lib/rubocop/processed_source.rb', line 63

def valid_syntax?
  return false if @parser_error
  @diagnostics.none? { |d| [:error, :fatal].include?(d.level) }
end