Class: RuboCop::AST::ProcessedSource

Inherits:
Object
  • Object
show all
Defined in:
lib/rubocop/ast/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, ruby_version, path = nil) ⇒ ProcessedSource

Returns a new instance of ProcessedSource.



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

def initialize(source, ruby_version, path = nil)
  # Defaults source encoding to UTF-8, regardless of the encoding it has
  # been read with, which could be non-utf8 depending on the default
  # external encoding.
  source.force_encoding(Encoding::UTF_8) unless source.encoding == Encoding::UTF_8

  @raw_source = source
  @path = path
  @diagnostics = []
  @ruby_version = ruby_version
  @parser_error = nil

  parse(source, ruby_version)
end

Instance Attribute Details

#astObject (readonly)

Returns the value of attribute ast.



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

def ast
  @ast
end

#bufferObject (readonly)

Returns the value of attribute buffer.



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

def buffer
  @buffer
end

#commentsObject (readonly)

Returns the value of attribute comments.



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

def comments
  @comments
end

#diagnosticsObject (readonly)

Returns the value of attribute diagnostics.



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

def diagnostics
  @diagnostics
end

#parser_errorObject (readonly)

Returns the value of attribute parser_error.



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

def parser_error
  @parser_error
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#raw_sourceObject (readonly)

Returns the value of attribute raw_source.



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

def raw_source
  @raw_source
end

#ruby_versionObject (readonly)

Returns the value of attribute ruby_version.



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

def ruby_version
  @ruby_version
end

#tokensObject (readonly)

Returns the value of attribute tokens.



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

def tokens
  @tokens
end

Class Method Details

.from_file(path, ruby_version) ⇒ Object



16
17
18
19
# File 'lib/rubocop/ast/processed_source.rb', line 16

def self.from_file(path, ruby_version)
  file = File.read(path, mode: 'rb')
  new(file, ruby_version, path)
end

Instance Method Details

#[](*args) ⇒ Object



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

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

#ast_with_commentsObject



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

def ast_with_comments
  return if !ast || !comments

  @ast_with_comments ||= Parser::Source::Comment.associate(ast, comments)
end

#blank?Boolean

Returns:

  • (Boolean)


93
94
95
# File 'lib/rubocop/ast/processed_source.rb', line 93

def blank?
  ast.nil?
end

#checksumObject

Raw source checksum for tracking infinite loops.



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

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

#commented?(source_range) ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'lib/rubocop/ast/processed_source.rb', line 97

def commented?(source_range)
  comment_lines.include?(source_range.line)
end

#comments_before_line(line) ⇒ Object



101
102
103
# File 'lib/rubocop/ast/processed_source.rb', line 101

def comments_before_line(line)
  comments.select { |c| c.location.line <= line }
end

#current_line(token) ⇒ Object



115
116
117
# File 'lib/rubocop/ast/processed_source.rb', line 115

def current_line(token)
  lines[token.line - 1]
end

#each_commentObject



73
74
75
# File 'lib/rubocop/ast/processed_source.rb', line 73

def each_comment
  comments.each { |comment| yield comment }
end

#each_tokenObject



81
82
83
# File 'lib/rubocop/ast/processed_source.rb', line 81

def each_token
  tokens.each { |token| yield token }
end

#file_pathObject



89
90
91
# File 'lib/rubocop/ast/processed_source.rb', line 89

def file_path
  buffer.name
end

#find_commentObject



77
78
79
# File 'lib/rubocop/ast/processed_source.rb', line 77

def find_comment
  comments.find { |comment| yield comment }
end

#find_tokenObject



85
86
87
# File 'lib/rubocop/ast/processed_source.rb', line 85

def find_token
  tokens.find { |token| yield token }
end

#following_line(token) ⇒ Object



119
120
121
# File 'lib/rubocop/ast/processed_source.rb', line 119

def following_line(token)
  lines[token.line]
end

#line_indentation(line_number) ⇒ Object



123
124
125
126
127
128
# File 'lib/rubocop/ast/processed_source.rb', line 123

def line_indentation(line_number)
  lines[line_number - 1]
    .match(/^(\s*)/)[1]
    .to_s
    .length
end

#linesObject

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



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

def lines
  @lines ||= begin
    all_lines = @buffer.source_lines
    last_token_line = tokens.any? ? tokens.last.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

#preceding_line(token) ⇒ Object



111
112
113
# File 'lib/rubocop/ast/processed_source.rb', line 111

def preceding_line(token)
  lines[token.line - 2]
end

#start_with?(string) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
# File 'lib/rubocop/ast/processed_source.rb', line 105

def start_with?(string)
  return false if self[0].nil?

  self[0].start_with?(string)
end

#valid_syntax?Boolean

Returns:

  • (Boolean)


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

def valid_syntax?
  return false if @parser_error

  @diagnostics.none? { |d| %i[error fatal].include?(d.level) }
end