Class: RuboCop::ProcessedSource
- Inherits:
-
Object
- Object
- RuboCop::ProcessedSource
- 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
-
#ast ⇒ Object
readonly
Returns the value of attribute ast.
-
#buffer ⇒ Object
readonly
Returns the value of attribute buffer.
-
#comments ⇒ Object
readonly
Returns the value of attribute comments.
-
#diagnostics ⇒ Object
readonly
Returns the value of attribute diagnostics.
-
#parser_error ⇒ Object
readonly
Returns the value of attribute parser_error.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#raw_source ⇒ Object
readonly
Returns the value of attribute raw_source.
-
#ruby_version ⇒ Object
readonly
Returns the value of attribute ruby_version.
-
#tokens ⇒ Object
readonly
Returns the value of attribute tokens.
Class Method Summary collapse
Instance Method Summary collapse
- #[](*args) ⇒ Object
- #ast_with_comments ⇒ Object
- #blank? ⇒ Boolean
-
#checksum ⇒ Object
Raw source checksum for tracking infinite loops.
- #comment_config ⇒ Object
- #commented?(source_range) ⇒ Boolean
- #comments_before_line(line) ⇒ Object
- #disabled_line_ranges ⇒ Object
- #each_comment ⇒ Object
- #each_token ⇒ Object
- #file_path ⇒ Object
- #find_comment ⇒ Object
- #find_token ⇒ Object
- #following_line(token) ⇒ Object
-
#initialize(source, ruby_version, path = nil) ⇒ ProcessedSource
constructor
A new instance of ProcessedSource.
- #line_indentation(line_number) ⇒ Object
-
#lines ⇒ Object
Returns the source lines, line break characters removed, excluding a possible __END__ and everything that comes after.
- #preceding_line(token) ⇒ Object
- #start_with?(string) ⇒ Boolean
- #valid_syntax? ⇒ Boolean
Constructor Details
#initialize(source, ruby_version, path = nil) ⇒ ProcessedSource
Returns a new instance of ProcessedSource.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/rubocop/processed_source.rb', line 22 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. unless source.encoding == Encoding::UTF_8 source.force_encoding(Encoding::UTF_8) end @raw_source = source @path = path @diagnostics = [] @ruby_version = ruby_version @parser_error = nil parse(source, ruby_version) end |
Instance Attribute Details
#ast ⇒ Object (readonly)
Returns the value of attribute ast.
12 13 14 |
# File 'lib/rubocop/processed_source.rb', line 12 def ast @ast end |
#buffer ⇒ Object (readonly)
Returns the value of attribute buffer.
12 13 14 |
# File 'lib/rubocop/processed_source.rb', line 12 def buffer @buffer end |
#comments ⇒ Object (readonly)
Returns the value of attribute comments.
12 13 14 |
# File 'lib/rubocop/processed_source.rb', line 12 def comments @comments end |
#diagnostics ⇒ Object (readonly)
Returns the value of attribute diagnostics.
12 13 14 |
# File 'lib/rubocop/processed_source.rb', line 12 def diagnostics @diagnostics end |
#parser_error ⇒ Object (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 |
#path ⇒ Object (readonly)
Returns the value of attribute path.
12 13 14 |
# File 'lib/rubocop/processed_source.rb', line 12 def path @path end |
#raw_source ⇒ Object (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 |
#ruby_version ⇒ Object (readonly)
Returns the value of attribute ruby_version.
12 13 14 |
# File 'lib/rubocop/processed_source.rb', line 12 def ruby_version @ruby_version end |
#tokens ⇒ Object (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
Instance Method Details
#[](*args) ⇒ Object
69 70 71 |
# File 'lib/rubocop/processed_source.rb', line 69 def [](*args) lines[*args] end |
#ast_with_comments ⇒ Object
47 48 49 50 51 |
# File 'lib/rubocop/processed_source.rb', line 47 def ast_with_comments return if !ast || !comments @ast_with_comments ||= Parser::Source::Comment.associate(ast, comments) end |
#blank? ⇒ Boolean
104 105 106 |
# File 'lib/rubocop/processed_source.rb', line 104 def blank? ast.nil? end |
#checksum ⇒ Object
Raw source checksum for tracking infinite loops.
80 81 82 |
# File 'lib/rubocop/processed_source.rb', line 80 def checksum Digest::SHA1.hexdigest(@raw_source) end |
#comment_config ⇒ Object
39 40 41 |
# File 'lib/rubocop/processed_source.rb', line 39 def comment_config @comment_config ||= CommentConfig.new(self) end |
#commented?(source_range) ⇒ Boolean
108 109 110 |
# File 'lib/rubocop/processed_source.rb', line 108 def commented?(source_range) comment_lines.include?(source_range.line) end |
#comments_before_line(line) ⇒ Object
112 113 114 |
# File 'lib/rubocop/processed_source.rb', line 112 def comments_before_line(line) comments.select { |c| c.location.line <= line } end |
#disabled_line_ranges ⇒ Object
43 44 45 |
# File 'lib/rubocop/processed_source.rb', line 43 def disabled_line_ranges comment_config.cop_disabled_line_ranges end |
#each_comment ⇒ Object
84 85 86 |
# File 'lib/rubocop/processed_source.rb', line 84 def each_comment comments.each { |comment| yield comment } end |
#each_token ⇒ Object
92 93 94 |
# File 'lib/rubocop/processed_source.rb', line 92 def each_token tokens.each { |token| yield token } end |
#file_path ⇒ Object
100 101 102 |
# File 'lib/rubocop/processed_source.rb', line 100 def file_path buffer.name end |
#find_comment ⇒ Object
88 89 90 |
# File 'lib/rubocop/processed_source.rb', line 88 def find_comment comments.find { |comment| yield comment } end |
#find_token ⇒ Object
96 97 98 |
# File 'lib/rubocop/processed_source.rb', line 96 def find_token tokens.find { |token| yield token } end |
#following_line(token) ⇒ Object
126 127 128 |
# File 'lib/rubocop/processed_source.rb', line 126 def following_line(token) lines[token.line] end |
#line_indentation(line_number) ⇒ Object
130 131 132 133 134 135 |
# File 'lib/rubocop/processed_source.rb', line 130 def line_indentation(line_number) lines[line_number - 1] .match(/^(\s*)/)[1] .to_s .length end |
#lines ⇒ Object
Returns the source lines, line break characters removed, excluding a possible __END__ and everything that comes after.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/rubocop/processed_source.rb', line 55 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
122 123 124 |
# File 'lib/rubocop/processed_source.rb', line 122 def preceding_line(token) lines[token.line - 2] end |
#start_with?(string) ⇒ Boolean
116 117 118 119 120 |
# File 'lib/rubocop/processed_source.rb', line 116 def start_with?(string) return false if self[0].nil? self[0].start_with?(string) end |
#valid_syntax? ⇒ Boolean
73 74 75 76 77 |
# File 'lib/rubocop/processed_source.rb', line 73 def valid_syntax? return false if @parser_error @diagnostics.none? { |d| %i[error fatal].include?(d.level) } end |