diff --git a/CHANGELOG.md b/CHANGELOG.md index e9cba91..d03a970 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## HEAD (unreleased) +- "Unmatched end" and "missing end" not generate different error text instructions (https://github.com/zombocom/syntax_search/pull/10) + ## 0.1.1 - Fire search on both unexpected end-of-input and unexected end (https://github.com/zombocom/syntax_search/pull/8) diff --git a/lib/syntax_search.rb b/lib/syntax_search.rb index 39e5877..86912c4 100644 --- a/lib/syntax_search.rb +++ b/lib/syntax_search.rb @@ -23,7 +23,7 @@ def self.handle_error(e, search_source_on_error: SEARCH_SOURCE_ON_ERROR_DEFAULT) self.call( source: Pathname(filename).read, filename: filename, - terminal: true + terminal: true, ) end @@ -40,6 +40,7 @@ def self.call(source: , filename: , terminal: false, record_dir: nil) blocks: blocks, filename: filename, terminal: terminal, + invalid_type: invalid_type(source), io: $stderr ).call end @@ -122,11 +123,30 @@ def self.valid?(source) Parser::CurrentRuby.parse(source) true - rescue Parser::SyntaxError + rescue Parser::SyntaxError => e + yield e if block_given? false ensure $stderr = stderr if stderr end + + def self.invalid_type(source) + message = nil + self.valid?(source) do |error| + message = error.message + end + + case message + when nil + :none + when /token kEND/ + :unmatched_end + when /token \$end/ # + :missing_end + else + raise "Unexpected message #{message}" + end + end end require_relative "syntax_search/code_line" diff --git a/lib/syntax_search/display_invalid_blocks.rb b/lib/syntax_search/display_invalid_blocks.rb index 1a5db93..b380a80 100644 --- a/lib/syntax_search/display_invalid_blocks.rb +++ b/lib/syntax_search/display_invalid_blocks.rb @@ -5,7 +5,7 @@ module SyntaxErrorSearch class DisplayInvalidBlocks attr_reader :filename - def initialize(blocks:, io: $stderr, filename: nil, terminal: false) + def initialize(blocks:, io: $stderr, filename: nil, terminal: false, invalid_type: :unmatched_end) @terminal = terminal @filename = filename @io = io @@ -16,6 +16,7 @@ def initialize(blocks:, io: $stderr, filename: nil, terminal: false) @digit_count = @code_lines.last&.line_number.to_s.length @invalid_line_hash = @lines.each_with_object({}) {|line, h| h[line] = true } + @invalid_type = invalid_type end def call @@ -33,15 +34,28 @@ def call end private def found_invalid_blocks - @io.puts <<~EOM + case @invalid_type + when :missing_end + @io.puts <<~EOM - SyntaxErrorSearch: A syntax error was detected + SyntaxSearch: Missing `end` detected - This code has an unmatched `end` this is caused by either - missing a syntax keyword (`def`, `do`, etc.) or inclusion - of an extra `end` line + This code has a missing `end`. Ensure that all + syntax keywords (`def`, `do`, etc.) have a matching `end`. + + EOM + when :unmatched_end + @io.puts <<~EOM + + SyntaxSearch: Unmatched `end` detected + + This code has an unmatched `end`. Ensure that all `end` lines + in your code have a matching syntax keyword (`def`, `do`, etc.) + and that you don't have any extra `end` lines. + + EOM + end - EOM @io.puts("file: #{filename}") if filename @io.puts <<~EOM simplified: diff --git a/spec/unit/display_invalid_blocks_spec.rb b/spec/unit/display_invalid_blocks_spec.rb index ba2afa4..6155ca9 100644 --- a/spec/unit/display_invalid_blocks_spec.rb +++ b/spec/unit/display_invalid_blocks_spec.rb @@ -42,7 +42,7 @@ def hai ) display.call expect(io.string).to include("❯ 2 def hello") - expect(io.string).to include("A syntax error was detected") + expect(io.string).to include("SyntaxSearch") end it " wraps code with github style codeblocks" do diff --git a/spec/unit/exe_spec.rb b/spec/unit/exe_spec.rb index fcb47d2..2f23f14 100644 --- a/spec/unit/exe_spec.rb +++ b/spec/unit/exe_spec.rb @@ -22,7 +22,7 @@ def exe(cmd) ruby_file = fixtures_dir.join("this_project_extra_def.rb.txt") out = exe("#{ruby_file} --no-terminal") - expect(out.strip).to include("A syntax error was detected") + expect(out.strip).to include("Missing `end` detected") expect(out.strip).to include("❯ 36 def filename") end @@ -37,7 +37,7 @@ def exe(cmd) out = exe("#{ruby_file} --record #{tmp_dir}") - expect(out.strip).to include("A syntax error was detected") + expect(out.strip).to include("Unmatched `end` detected") expect(tmp_dir).to_not be_empty end end diff --git a/spec/unit/syntax_search_spec.rb b/spec/unit/syntax_search_spec.rb index 97e6e98..5bb01ee 100644 --- a/spec/unit/syntax_search_spec.rb +++ b/spec/unit/syntax_search_spec.rb @@ -32,7 +32,7 @@ module SyntaxErrorSearch out = `ruby -I#{lib_dir} -rsyntax_search/auto #{require_rb} 2>&1` - expect(out).to include("This code has an unmatched") + expect(out).to include("Unmatched `end` detected") expect(out).to include("Run `$ syntax_search") expect($?.success?).to be_falsey end