Skip to content

[wip] experimental typo checker #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in language_server.gemspec
gemspec

gem 'did_you_mean', git: 'https://github.com/yuki24/did_you_mean', branch: 'static-typo-checker'
6 changes: 5 additions & 1 deletion lib/language_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
require "language_server/logger"
require "language_server/protocol"
require "language_server/linter/ruby_wc"
require "language_server/linter/did_you_mean"
require "language_server/completion_provider/rcodetools"
require "language_server/completion_provider/ad_hoc"
require "language_server/definition_provider/ad_hoc"
Expand Down Expand Up @@ -94,7 +95,10 @@ def on(method, &callback)
file_store.cache(uri, text)
project.recalculate_result(uri)

diagnostics = Linter::RubyWC.new(text).call.map do |error|
diagnostics = [
Linter::RubyWC.new(text).call,
Linter::DidYouMean.new(text).call
].flatten.map do |error|
Protocol::Interface::Diagnostic.new(
message: error.message,
severity: error.warning? ? Protocol::Constant::DiagnosticSeverity::WARNING : Protocol::Constant::DiagnosticSeverity::ERROR,
Expand Down
20 changes: 20 additions & 0 deletions lib/language_server/linter/did_you_mean.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require "open3"
require "language_server/linter/error"

module LanguageServer
module Linter
class DidYouMean
def initialize(source)
@source = source
end

def call
out, _, _ = Open3.capture3("ruby -r bundler/setup -r did_you_mean/static", stdin_data: @source)

(JSON.parse(out, symbolize_names: true)[:undefined_names] || {}).values.flatten.map do |undefined_name:, symbol_type:, path:, lineno:, suggestions:|
Error.new(line_num: lineno - 1, message: "Undefined method #{undefined_name}. Did you mean? #{suggestions.join("\n")}", type: 'warning')
end
end
end
end
end
23 changes: 23 additions & 0 deletions lib/language_server/linter/error.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require "open3"

module LanguageServer
module Linter
class Error
attr_reader :line_num, :message, :type

def initialize(line_num:, message:, type:)
@line_num = line_num
@message = message
@type = type
end

def warning?
@type == "warning"
end

def ==(other)
line_num == other.line_num && message == other.message
end
end
end
end
17 changes: 17 additions & 0 deletions test/language_server/linter/did_you_mean_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require 'test_helper'

module LanguageServer::Linter
class DidYouMeanTest < Minitest::Test
def test_error
linter = DidYouMean.new(<<-EOS.strip_heredoc)
def hi
foo
end
EOS

assert {
linter.call == [Error.new(line_num: 1, message: "Undefined method foo. Did you mean? for", type: "warning")]
}
end
end
end