syntax_tree 2.0.1 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -124,7 +124,7 @@ module SyntaxTree
124
124
  start = Time.now
125
125
 
126
126
  formatted = handler.format(source)
127
- File.write(filepath, formatted)
127
+ File.write(filepath, formatted) if filepath != :stdin
128
128
 
129
129
  color = source == formatted ? Color.gray(filepath) : filepath
130
130
  delta = ((Time.now - start) * 1000).round
@@ -191,11 +191,6 @@ module SyntaxTree
191
191
  return 0
192
192
  end
193
193
 
194
- if arguments.empty?
195
- warn(HELP)
196
- return 1
197
- end
198
-
199
194
  action =
200
195
  case name
201
196
  when "a", "ast"
@@ -215,6 +210,13 @@ module SyntaxTree
215
210
  return 1
216
211
  end
217
212
 
213
+ # If we're not reading from stdin and the user didn't supply and
214
+ # filepaths to be read, then we exit with the usage message.
215
+ if STDIN.tty? && arguments.empty?
216
+ warn(HELP)
217
+ return 1
218
+ end
219
+
218
220
  # If there are any plugins specified on the command line, then load them
219
221
  # by requiring them here. We do this by transforming something like
220
222
  #
@@ -224,40 +226,34 @@ module SyntaxTree
224
226
  #
225
227
  # require "syntax_tree/haml"
226
228
  #
227
- if arguments.first.start_with?("--plugins=")
229
+ if arguments.first&.start_with?("--plugins=")
228
230
  plugins = arguments.shift[/^--plugins=(.*)$/, 1]
229
231
  plugins.split(",").each { |plugin| require "syntax_tree/#{plugin}" }
230
232
  end
231
233
 
234
+ # Track whether or not there are any errors from any of the files that
235
+ # we take action on so that we can properly clean up and exit.
232
236
  errored = false
233
- arguments.each do |pattern|
234
- Dir.glob(pattern).each do |filepath|
235
- next unless File.file?(filepath)
236
-
237
- handler = HANDLERS[File.extname(filepath)]
238
- source = handler.read(filepath)
239
-
240
- begin
241
- action.run(handler, filepath, source)
242
- rescue Parser::ParseError => error
243
- warn("Error: #{error.message}")
244
-
245
- if error.lineno
246
- highlight_error(error, source)
247
- else
248
- warn(error.message)
249
- warn(error.backtrace)
250
- end
251
-
252
- errored = true
253
- rescue Check::UnformattedError, Debug::NonIdempotentFormatError
254
- errored = true
255
- rescue => error
256
- warn(error.message)
257
- warn(error.backtrace)
258
- errored = true
259
- end
237
+
238
+ each_file(arguments) do |handler, filepath, source|
239
+ action.run(handler, filepath, source)
240
+ rescue Parser::ParseError => error
241
+ warn("Error: #{error.message}")
242
+
243
+ if error.lineno
244
+ highlight_error(error, source)
245
+ else
246
+ warn(error.message)
247
+ warn(error.backtrace)
260
248
  end
249
+
250
+ errored = true
251
+ rescue Check::UnformattedError, Debug::NonIdempotentFormatError
252
+ errored = true
253
+ rescue => error
254
+ warn(error.message)
255
+ warn(error.backtrace)
256
+ errored = true
261
257
  end
262
258
 
263
259
  if errored
@@ -271,6 +267,22 @@ module SyntaxTree
271
267
 
272
268
  private
273
269
 
270
+ def each_file(arguments)
271
+ if STDIN.tty?
272
+ arguments.each do |pattern|
273
+ Dir.glob(pattern).each do |filepath|
274
+ next unless File.file?(filepath)
275
+
276
+ handler = HANDLERS[File.extname(filepath)]
277
+ source = handler.read(filepath)
278
+ yield handler, filepath, source
279
+ end
280
+ end
281
+ else
282
+ yield HANDLERS[".rb"], :stdin, STDIN.read
283
+ end
284
+ end
285
+
274
286
  # Highlights a snippet from a source and parse error.
275
287
  def highlight_error(error, source)
276
288
  lines = source.lines
@@ -27,20 +27,6 @@ module SyntaxTree
27
27
  after[location.start_char + "rescue".length] << " StandardError"
28
28
  end
29
29
 
30
- # Adds the implicitly referenced value (local variable or method call)
31
- # that is added into a hash when the value of a key-value pair is omitted.
32
- # For example,
33
- #
34
- # { value: }
35
- #
36
- # becomes
37
- #
38
- # { value: value }
39
- #
40
- def missing_hash_value(key, location)
41
- after[location.end_char] << " #{key}"
42
- end
43
-
44
30
  # Adds implicit parentheses around certain expressions to make it clear
45
31
  # which subexpression will be evaluated first. For example,
46
32
  #
@@ -69,8 +55,6 @@ module SyntaxTree
69
55
  case [parent_node, child_node]
70
56
  in _, Rescue[exception: nil, location:]
71
57
  inlay_hints.bare_rescue(location)
72
- in _, Assoc[key: Label[value: key], value: nil, location:]
73
- inlay_hints.missing_hash_value(key[0...-1], location)
74
58
  in Assign | Binary | IfOp | OpAssign, IfOp[location:]
75
59
  inlay_hints.precedence_parentheses(location)
76
60
  in Assign | OpAssign, Binary[location:]