syntax_tree 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c3684dd6ee3c63a0d12bd9688dcd5c31a508e5b56cbed0d9939843014dcb035c
4
- data.tar.gz: 7b229ace8aa66b761b7a2d2b9ad6560a819e953b67d787ac3c9dc2cc224a36fd
3
+ metadata.gz: 872e3355e589d7a18b916de52fc30d8ac7e52a9c746550082ed8fc503bd178b8
4
+ data.tar.gz: d27214567e9b0c9f9cf14b8874e839d7c90d7fec89d9a94c117e4e83028165a2
5
5
  SHA512:
6
- metadata.gz: 74efe38ca5ab0e3bfcd6fd8ac8809d88e0d4fc87d84178a188601c7afc50c06c18c85f2669f504f1d84635fb39a367fcbf3cdbdae1eb45a5bf79fc97f2447e78
7
- data.tar.gz: 7b186fc7133a6f2806d73de62c15de8ae39682f7f317a7368f9a3a5877dc1d2f5f69824b08a0f970c8983f92fce8f21c824db20f9f46eecbb6786deacb46560a
6
+ metadata.gz: 0d131267335b65e4ebd9c12c86275a64d556a9314a1d509b4a8c3629f553ed06e304abce59faab2b13a64bdd8a825101a3baf52e97e60cde8b5eeba2488621e7
7
+ data.tar.gz: 12f8ecf7e54688af0a2bac223320197113012c8b83a1db1135f51bf8192d8012462499f810c05bfb6d8d88f7c59551a467e4104cd12ec4094fd5722c3f940b64
data/CHANGELOG.md CHANGED
@@ -6,7 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
- ## [1.0.0]
9
+ ## [1.1.0] - 2021-12-08
10
+
11
+ ### Added
12
+
13
+ - Better handling for formatting files with errors.
14
+ - Colorize the output snippet using IRB.
15
+
16
+ ## [1.0.0] - 2021-12-08
10
17
 
11
18
  ### Added
12
19
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- syntax_tree (1.0.0)
4
+ syntax_tree (1.1.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -126,6 +126,9 @@ class SyntaxTree
126
126
  delta = ((Time.now - start) * 1000).round
127
127
 
128
128
  puts "\r#{color} #{delta}ms"
129
+ rescue
130
+ puts "\r#{filepath}"
131
+ raise
129
132
  end
130
133
  end
131
134
 
@@ -177,25 +180,12 @@ class SyntaxTree
177
180
  action.run(filepath, source)
178
181
  rescue ParseError => error
179
182
  warn("Error: #{error.message}")
180
- lines = source.lines
181
-
182
- maximum = [error.lineno + 3, lines.length].min
183
- digits = Math.log10(maximum).ceil
184
-
185
- ([error.lineno - 3, 0].max...maximum).each do |line_index|
186
- line_number = line_index + 1
187
183
 
188
- if line_number == error.lineno
189
- part1 = Color.red(">")
190
- part2 = Color.gray("%#{digits}d |" % line_number)
191
- warn("#{part1} #{part2} #{lines[line_index]}")
192
-
193
- part3 = Color.gray(" %#{digits}s |" % " ")
194
- warn("#{part3} #{" " * error.column}#{Color.red("^")}")
195
- else
196
- prefix = Color.gray(" %#{digits}d |" % line_number)
197
- warn("#{prefix} #{lines[line_index]}")
198
- end
184
+ if error.lineno
185
+ highlight_error(error, source)
186
+ else
187
+ warn(error.message)
188
+ warn(error.backtrace)
199
189
  end
200
190
 
201
191
  errored = true
@@ -232,6 +222,36 @@ class SyntaxTree
232
222
 
233
223
  File.read(filepath, encoding: encoding)
234
224
  end
225
+
226
+ # Highlights a snippet from a source and parse error.
227
+ def highlight_error(error, source)
228
+ lines = source.lines
229
+
230
+ maximum = [error.lineno + 3, lines.length].min
231
+ digits = Math.log10(maximum).ceil
232
+
233
+ ([error.lineno - 3, 0].max...maximum).each do |line_index|
234
+ line_number = line_index + 1
235
+
236
+ if line_number == error.lineno
237
+ part1 = Color.red(">")
238
+ part2 = Color.gray("%#{digits}d |" % line_number)
239
+ warn("#{part1} #{part2} #{colorize_line(lines[line_index])}")
240
+
241
+ part3 = Color.gray(" %#{digits}s |" % " ")
242
+ warn("#{part3} #{" " * error.column}#{Color.red("^")}")
243
+ else
244
+ prefix = Color.gray(" %#{digits}d |" % line_number)
245
+ warn("#{prefix} #{colorize_line(lines[line_index])}")
246
+ end
247
+ end
248
+ end
249
+
250
+ # Take a line of Ruby source and colorize the output.
251
+ def colorize_line(line)
252
+ require "irb"
253
+ IRB::Color.colorize_code(line, complete: false, ignore_error: true)
254
+ end
235
255
  end
236
256
  end
237
257
  end
@@ -3,5 +3,5 @@
3
3
  require "ripper"
4
4
 
5
5
  class SyntaxTree < Ripper
6
- VERSION = "1.0.0"
6
+ VERSION = "1.1.0"
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: syntax_tree
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kevin Newton