Issue #14938 has been updated by mtsmfm (Fumiaki Matsushima).


Hmm, I tried `warn` but it doesn't work in some case:

~~~ruby
#!/usr/bin/ruby
require 'ripper'
require 'tempfile'

class Lint < Ripper
  def warn(*s)
    @results << [:warning, lineno, column, *s]
  end
  alias warning warn

  def results
    unless defined?(@results) and @results
      @results = []
      parse
    end
    @results
  end
end

data = DATA.read
result1 = nil

Tempfile.create do |file|
  file.write(data)
  file.flush
  result1 = `ruby -wc #{file.path}`
end

$VERBOSE = true
result2 = Lint.new(data).results.first

p result1
#=> /tmp/20180729-457-smihk6:1: warning: ambiguous first argument; put parentheses or a space even after `/' operator
#=> "Syntax OK\n"
p result2
#=> nil
__END__
a /#{

}/
~~~

In addition,

- How can I get the range of code instead of lineno and column only?
- Is there any way to detect syntax error?
  - It seems Ripper#compile_error doesn't work

~~~ruby
#!/usr/bin/ruby
require 'ripper'
require 'tempfile'

class Lint < Ripper
  def compile_error(*s)
    @results << [:error, lineno, column, *s]
  end

  def results
    unless defined?(@results) and @results
      @results = []
      parse
    end
    @results
  end
end

data = DATA.read
result1 = nil

Tempfile.create do |file|
  file.write(data)
  file.flush
  result1 = `ruby -wc #{file.path}`
end

$VERBOSE = true
result2 = Lint.new(data).results.first

p result1
#=> /tmp/20180729-461-1wcbfr4:1: syntax error, unexpected end-of-input
#=> ""
p result2
#=> nil
__END__
def
~~~

----------------------------------------
Feature #14938: Provide API to get same result as ruby -wc
https://bugs.ruby-lang.org/issues/14938#change-73193

* Author: mtsmfm (Fumiaki Matsushima)
* Status: Open
* Priority: Normal
* Assignee: 
* Target version: 
----------------------------------------
I'm the author of an implementation of Ruby Language Server.
Currently, it uses `RubyVM::InstructionSequence.compile` to get the result of `ruby -wc` and parses message and location by regexp.

https://github.com/mtsmfm/language_server-ruby/blob/v0.11.0/lib/language_server/linter/ruby_wc.rb#L30

Do you have any plan to provide API?



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>