Skip to content

Commit

Permalink
Switch syntax checking over to Pry's trick
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshCheek committed Jan 9, 2015
1 parent c85d55a commit f114918
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions lib/seeing_is_believing/binary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def self.call(argv, stdin, stdout, stderr)
return SUCCESS_STATUS
end

syntax_error_notice = syntax_error_notice_for(options.body)
syntax_error_notice = syntax_error_notice_for(options.filename, options.body)
if syntax_error_notice
stderr.puts syntax_error_notice
return NONDISPLAYABLE_ERROR_STATUS
Expand Down Expand Up @@ -88,17 +88,12 @@ def self.call(argv, stdin, stdout, stderr)

private

def self.syntax_error_notice_for(body)
require 'open3'
out, err, syntax_status = Open3.capture3 RbConfig.ruby, '-c', stdin_data: body
return err unless syntax_status.success?

# The stdin_data may still be getting written when the pipe closes
# This is because Ruby will stop reading from stdin if everything left is in the DATA segment, and the data segment is not referenced.
# In this case, the Syntax is fine
# https://bugs.ruby-lang.org/issues/9583
rescue Errno::EPIPE
return nil
# brilliant idea from pry https://github.com/banister/method_source/blob/5e5c55642662c248e721282cc287b41a49778ee8/lib/method_source/code_helpers.rb#L58-72
def self.syntax_error_notice_for(filename, body)
catch(:valid) { eval "BEGIN{throw :valid}\n#{body}", binding, filename.to_s }
nil
rescue SyntaxError
return $!.message.sub(/:(\d):/) { ":#{$1.to_i-1}:" }
end

def self.evaluate_program(body, options)
Expand Down

0 comments on commit f114918

Please sign in to comment.