forked from rubocop/rubocop-ast
/
processed_source.rb
208 lines (167 loc) · 5.41 KB
/
processed_source.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# frozen_string_literal: true
require 'digest/sha1'
# rubocop:disable Metrics/ClassLength
module RuboCop
module AST
# 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.
class ProcessedSource
STRING_SOURCE_NAME = '(string)'
attr_reader :path, :buffer, :ast, :comments, :tokens, :diagnostics,
:parser_error, :raw_source, :ruby_version
def self.from_file(path, ruby_version)
file = File.read(path, mode: 'rb')
new(file, ruby_version, path)
end
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
def ast_with_comments
return if !ast || !comments
@ast_with_comments ||= Parser::Source::Comment.associate(ast, comments)
end
# Returns the source lines, line break characters removed, excluding a
# possible __END__ and everything that comes after.
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
def [](*args)
lines[*args]
end
def valid_syntax?
return false if @parser_error
@diagnostics.none? { |d| %i[error fatal].include?(d.level) }
end
# Raw source checksum for tracking infinite loops.
def checksum
Digest::SHA1.hexdigest(@raw_source)
end
def each_comment
comments.each { |comment| yield comment }
end
def find_comment
comments.find { |comment| yield comment }
end
def each_token
tokens.each { |token| yield token }
end
def find_token
tokens.find { |token| yield token }
end
def file_path
buffer.name
end
def blank?
ast.nil?
end
def commented?(source_range)
comment_lines.include?(source_range.line)
end
def comments_before_line(line)
comments.select { |c| c.location.line <= line }
end
def start_with?(string)
return false if self[0].nil?
self[0].start_with?(string)
end
def preceding_line(token)
lines[token.line - 2]
end
def current_line(token)
lines[token.line - 1]
end
def following_line(token)
lines[token.line]
end
def line_indentation(line_number)
lines[line_number - 1]
.match(/^(\s*)/)[1]
.to_s
.length
end
private
def comment_lines
@comment_lines ||= comments.map { |c| c.location.line }
end
def parse(source, ruby_version)
buffer_name = @path || STRING_SOURCE_NAME
@buffer = Parser::Source::Buffer.new(buffer_name, 1)
begin
@buffer.source = source
rescue EncodingError => e
@parser_error = e
return
end
@ast, @comments, @tokens = tokenize(create_parser(ruby_version))
end
def tokenize(parser)
begin
ast, comments, tokens = parser.tokenize(@buffer)
ast.respond_to?(:complete!) && ast.complete!
rescue Parser::SyntaxError
# All errors are in diagnostics. No need to handle exception.
end
tokens = tokens.map { |t| Token.from_parser_token(t) } if tokens
[ast, comments, tokens]
end
# rubocop:disable Metrics/MethodLength
def parser_class(ruby_version)
case ruby_version
when 2.4
require 'parser/ruby24'
Parser::Ruby24
when 2.5
require 'parser/ruby25'
Parser::Ruby25
when 2.6
require 'parser/ruby26'
Parser::Ruby26
when 2.7
require 'parser/ruby27'
Parser::Ruby27
when 2.8
require 'parser/ruby28'
Parser::Ruby28
else
raise ArgumentError,
"RuboCop found unknown Ruby version: #{ruby_version.inspect}"
end
end
# rubocop:enable Metrics/MethodLength
def create_parser(ruby_version)
builder = RuboCop::AST::Builder.new
parser_class(ruby_version).new(builder).tap do |parser|
# On JRuby there's a risk that we hang in tokenize() if we
# don't set the all errors as fatal flag. The problem is caused by a bug
# in Racc that is discussed in issue #93 of the whitequark/parser
# project on GitHub.
parser.diagnostics.all_errors_are_fatal = (RUBY_ENGINE != 'ruby')
parser.diagnostics.ignore_warnings = false
parser.diagnostics.consumer = lambda do |diagnostic|
@diagnostics << diagnostic
end
end
end
end
end
end
# rubocop:enable Metrics/ClassLength