diff options
author | schneems <[email protected]> | 2022-07-26 15:21:09 -0500 |
---|---|---|
committer | Hiroshi SHIBATA <[email protected]> | 2022-08-19 10:02:24 +0900 |
commit | 490af8dbdb66263f29d0b4e43752fbb298b94862 (patch) | |
tree | 5f161e99d27a1417f446e8b1516263fd76d6f0bc /lib/syntax_suggest/pathname_from_message.rb | |
parent | a50df1ab0eb312e5cdcf010d2c1b362ec41f3c59 (diff) |
Sync SyntaxSuggest
```
$ tool/sync_default_gems.rb syntax_suggest
```
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/5859
Diffstat (limited to 'lib/syntax_suggest/pathname_from_message.rb')
-rw-r--r-- | lib/syntax_suggest/pathname_from_message.rb | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/lib/syntax_suggest/pathname_from_message.rb b/lib/syntax_suggest/pathname_from_message.rb new file mode 100644 index 0000000000..ea1a90856e --- /dev/null +++ b/lib/syntax_suggest/pathname_from_message.rb @@ -0,0 +1,59 @@ +# frozen_string_literal: true + +module SyntaxSuggest + # Converts a SyntaxError message to a path + # + # Handles the case where the filename has a colon in it + # such as on a windows file system: https://github.com/zombocom/syntax_suggest/issues/111 + # + # Example: + # + # message = "/tmp/scratch:2:in `require_relative': /private/tmp/bad.rb:1: syntax error, unexpected `end' (SyntaxError)" + # puts PathnameFromMessage.new(message).call.name + # # => "/tmp/scratch.rb" + # + class PathnameFromMessage + EVAL_RE = /^\(eval\):\d+/ + STREAMING_RE = /^-:\d+/ + attr_reader :name + + def initialize(message, io: $stderr) + @line = message.lines.first + @parts = @line.split(":") + @guess = [] + @name = nil + @io = io + end + + def call + if skip_missing_file_name? + if ENV["SYNTAX_SUGGEST_DEBUG"] + @io.puts "SyntaxSuggest: Could not find filename from #{@line.inspect}" + end + else + until stop? + @guess << @parts.shift + @name = Pathname(@guess.join(":")) + end + + if @parts.empty? + @io.puts "SyntaxSuggest: Could not find filename from #{@line.inspect}" + @name = nil + end + end + + self + end + + def stop? + return true if @parts.empty? + return false if @guess.empty? + + @name&.exist? + end + + def skip_missing_file_name? + @line.match?(EVAL_RE) || @line.match?(STREAMING_RE) + end + end +end |