Skip to content

Commit 474b1a0

Browse files
committed
Use RubyVM::InstructionSequence.compile instead of open3
1 parent b5b15ac commit 474b1a0

File tree

1 file changed

+47
-4
lines changed

1 file changed

+47
-4
lines changed

lib/language_server/linter/ruby_wc.rb

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require "open3"
1+
require "stringio"
22

33
module LanguageServer
44
module Linter
@@ -26,12 +26,55 @@ def initialize(source)
2626
end
2727

2828
def call
29-
_, err, _ = Open3.capture3("ruby -wc", stdin_data: @source)
30-
31-
err.scan(/.+:(\d+):\s*(.+?)[,:]\s(.+)/).map do |line_num, type, message|
29+
error_message.scan(/.+:(\d+):\s*(.+?)[,:]\s(.+)/).map do |line_num, type, message|
3230
Error.new(line_num: line_num.to_i - 1, message: message, type: type)
3331
end
3432
end
33+
34+
private
35+
36+
# Since Ruby 2.4, syntax error information is outputted to Exception#message instead of stderr
37+
if begin; stderr = $stderr; $stderr = StringIO.new; RubyVM::InstructionSequence.compile('='); rescue SyntaxError => e; e.message != 'compile error'; ensure; $stderr = stderr; end
38+
def error_message
39+
with_verbose do
40+
begin
41+
capture_stderr { RubyVM::InstructionSequence.compile(@source) }
42+
rescue SyntaxError => e
43+
e.message
44+
end
45+
end
46+
end
47+
else
48+
def error_message
49+
with_verbose do
50+
capture_stderr do
51+
begin
52+
RubyVM::InstructionSequence.compile(@source)
53+
rescue SyntaxError
54+
end
55+
end
56+
end
57+
end
58+
end
59+
60+
def with_verbose
61+
origin = $VERBOSE
62+
$VERBOSE = true
63+
yield
64+
ensure
65+
$VERBOSE = origin
66+
end
67+
68+
def capture_stderr
69+
origin = $stderr
70+
$stderr = StringIO.new
71+
72+
yield
73+
74+
$stderr.string
75+
ensure
76+
$stderr = origin
77+
end
3578
end
3679
end
3780
end

0 commit comments

Comments
 (0)