From f62bec1b9055159873fab357d3a32388fe307b2d Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Tue, 7 Apr 2026 20:04:19 +0200 Subject: [PATCH] Fix sync for `syntax_suggest` It is a bit special in that is uses `spec` for tests. Since https://github.com/ruby/ruby/pull/15015 those did not get synced anymore. This fixes the mapping to what is was before and pulls in missing changes --- .../integration/ruby_command_line_spec.rb | 4 --- .../integration/syntax_suggest_spec.rb | 10 ++---- spec/syntax_suggest/spec_helper.rb | 10 ++++++ spec/syntax_suggest/unit/api_spec.rb | 10 ------ spec/syntax_suggest/unit/code_block_spec.rb | 2 +- spec/syntax_suggest/unit/code_line_spec.rb | 2 -- spec/syntax_suggest/unit/core_ext_spec.rb | 2 -- .../unit/explain_syntax_spec.rb | 32 +++++++++++++++++-- spec/syntax_suggest/unit/lex_all_spec.rb | 8 ++--- tool/sync_default_gems.rb | 8 ++++- 10 files changed, 54 insertions(+), 34 deletions(-) diff --git a/spec/syntax_suggest/integration/ruby_command_line_spec.rb b/spec/syntax_suggest/integration/ruby_command_line_spec.rb index c1ec4be54e2c2f..02354ceff00eea 100644 --- a/spec/syntax_suggest/integration/ruby_command_line_spec.rb +++ b/spec/syntax_suggest/integration/ruby_command_line_spec.rb @@ -94,8 +94,6 @@ module SyntaxSuggest end it "gem can be tested when executing on Ruby with default gem included" do - skip if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.2") - out = `#{ruby} -I#{lib_dir} -rsyntax_suggest -e "puts SyntaxError.instance_method(:detailed_message).source_location" 2>&1` expect($?.success?).to be_truthy @@ -103,8 +101,6 @@ module SyntaxSuggest end it "annotates a syntax error in Ruby 3.2+ when require is not used" do - skip if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.2") - Dir.mktmpdir do |dir| tmpdir = Pathname(dir) script = tmpdir.join("script.rb") diff --git a/spec/syntax_suggest/integration/syntax_suggest_spec.rb b/spec/syntax_suggest/integration/syntax_suggest_spec.rb index 015d088c92229d..79bfdc1b577d43 100644 --- a/spec/syntax_suggest/integration/syntax_suggest_spec.rb +++ b/spec/syntax_suggest/integration/syntax_suggest_spec.rb @@ -4,10 +4,6 @@ module SyntaxSuggest RSpec.describe "Integration tests that don't spawn a process (like using the cli)" do - before(:each) do - skip "Benchmark is not available" unless defined?(::Benchmark) - end - it "does not timeout on massive files" do next unless ENV["SYNTAX_SUGGEST_TIMEOUT"] @@ -17,7 +13,7 @@ module SyntaxSuggest io = StringIO.new - benchmark = Benchmark.measure do + benchmark_measure do debug_perf do SyntaxSuggest.call( io: io, @@ -28,7 +24,6 @@ module SyntaxSuggest end debug_display(io.string) - debug_display(benchmark) expect(io.string).to include(<<~EOM) 6 class SyntaxTree < Ripper @@ -46,7 +41,7 @@ module SyntaxSuggest io = StringIO.new debug_perf do - benchmark = Benchmark.measure do + benchmark_measure do SyntaxSuggest.call( io: io, source: file.read, @@ -54,7 +49,6 @@ module SyntaxSuggest ) end debug_display(io.string) - debug_display(benchmark) end expect(io.string).to_not include("def ruby_install_binstub_path") diff --git a/spec/syntax_suggest/spec_helper.rb b/spec/syntax_suggest/spec_helper.rb index b5d2924e698615..c0aaf0f4f7bcfc 100644 --- a/spec/syntax_suggest/spec_helper.rb +++ b/spec/syntax_suggest/spec_helper.rb @@ -85,6 +85,16 @@ def debug_perf end end +def benchmark_measure + raise "No block given" unless block_given? + + if defined?(::Benchmark) + debug_display(Benchmark.measure { yield }) + else + yield + end +end + def run!(cmd, raise_on_nonzero_exit: true) out = `#{cmd} 2>&1` raise "Command: #{cmd} failed: #{out}" if !$?.success? && raise_on_nonzero_exit diff --git a/spec/syntax_suggest/unit/api_spec.rb b/spec/syntax_suggest/unit/api_spec.rb index e900b9e10b8ef4..9299a17beeacf1 100644 --- a/spec/syntax_suggest/unit/api_spec.rb +++ b/spec/syntax_suggest/unit/api_spec.rb @@ -8,12 +8,6 @@ module SyntaxSuggest RSpec.describe "Top level SyntaxSuggest api" do - it "doesn't load prism if env var is set" do - skip("SYNTAX_SUGGEST_DISABLE_PRISM not set") unless ENV["SYNTAX_SUGGEST_DISABLE_PRISM"] - - expect(SyntaxSuggest.use_prism_parser?).to be_falsey - end - it "has a `handle_error` interface" do fake_error = Object.new def fake_error.message @@ -69,8 +63,6 @@ def fake_error.message end it "respects highlight API" do - skip if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.2") - core_ext_file = lib_dir.join("syntax_suggest").join("core_ext.rb") require_relative core_ext_file @@ -91,8 +83,6 @@ def detailed_message(**kwargs) end it "can be disabled via falsey kwarg" do - skip if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.2") - core_ext_file = lib_dir.join("syntax_suggest").join("core_ext.rb") require_relative core_ext_file diff --git a/spec/syntax_suggest/unit/code_block_spec.rb b/spec/syntax_suggest/unit/code_block_spec.rb index 3ab2751b271597..baf1c63b25ae29 100644 --- a/spec/syntax_suggest/unit/code_block_spec.rb +++ b/spec/syntax_suggest/unit/code_block_spec.rb @@ -33,7 +33,7 @@ def foo array = [block_2, block_1, block_0].sort expect(array.last).to eq(block_2) - block = CodeBlock.new(lines: CodeLine.new(line: " " * 8 + "foo", index: 4, lex: [])) + block = CodeBlock.new(lines: CodeLine.new(line: " " * 8 + "foo", index: 4, tokens: [])) array.prepend(block) expect(array.max).to eq(block) end diff --git a/spec/syntax_suggest/unit/code_line_spec.rb b/spec/syntax_suggest/unit/code_line_spec.rb index 5b62cc2757be8a..761c46038510a6 100644 --- a/spec/syntax_suggest/unit/code_line_spec.rb +++ b/spec/syntax_suggest/unit/code_line_spec.rb @@ -17,8 +17,6 @@ def to_json(*opts) end it "supports endless method definitions" do - skip("Unsupported ruby version") unless Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3") - line = CodeLine.from_source(<<~EOM).first def square(x) = x * x EOM diff --git a/spec/syntax_suggest/unit/core_ext_spec.rb b/spec/syntax_suggest/unit/core_ext_spec.rb index 499c38a240b49a..d579cc8dc4484d 100644 --- a/spec/syntax_suggest/unit/core_ext_spec.rb +++ b/spec/syntax_suggest/unit/core_ext_spec.rb @@ -3,8 +3,6 @@ module SyntaxSuggest RSpec.describe "Core extension" do it "SyntaxError monkepatch ensures there is a newline to the end of the file" do - skip if Gem::Version.new(RUBY_VERSION) < Gem::Version.new("3.2") - Dir.mktmpdir do |dir| tmpdir = Pathname(dir) file = tmpdir.join("file.rb") diff --git a/spec/syntax_suggest/unit/explain_syntax_spec.rb b/spec/syntax_suggest/unit/explain_syntax_spec.rb index c62a42b925ed6b..7ddb32b8ea930a 100644 --- a/spec/syntax_suggest/unit/explain_syntax_spec.rb +++ b/spec/syntax_suggest/unit/explain_syntax_spec.rb @@ -17,9 +17,23 @@ module SyntaxSuggest expect(explain.errors.join.strip).to_not be_empty end - it "handles %w[]" do + %w[w W i I].each do |type| + it "handles %#{type}-style array" do + source = <<~EOM + node.is_a?(Op) && %#{type}[| ||].include?(node.value) && + EOM + + explain = ExplainSyntax.new( + code_lines: CodeLine.from_source(source) + ).call + + expect(explain.missing).to eq([]) + end + end + + it "handles %r-style regexp" do source = <<~EOM - node.is_a?(Op) && %w[| ||].include?(node.value) && + node.is_a?(Op) && %r{| ||}.include?(node.value) && EOM explain = ExplainSyntax.new( @@ -29,6 +43,20 @@ module SyntaxSuggest expect(explain.missing).to eq([]) end + ["", "q", "Q"].each do |type| + it "handles %#{type}-style string" do + source = <<~EOM + node.is_a?(Op) && %#{type}(| ||).include?(node.value) && + EOM + + explain = ExplainSyntax.new( + code_lines: CodeLine.from_source(source) + ).call + + expect(explain.missing).to eq([]) + end + end + it "doesn't falsely identify strings or symbols as critical chars" do source = <<~EOM a = ['(', '{', '[', '|'] diff --git a/spec/syntax_suggest/unit/lex_all_spec.rb b/spec/syntax_suggest/unit/lex_all_spec.rb index 9621c9ecec9968..b88ae65344264f 100644 --- a/spec/syntax_suggest/unit/lex_all_spec.rb +++ b/spec/syntax_suggest/unit/lex_all_spec.rb @@ -17,10 +17,10 @@ module SyntaxSuggest end # 9 EOM - lex = LexAll.new(source: source) - expect(lex.map(&:token).to_s).to include("dog") - expect(lex.first.line).to eq(1) - expect(lex.last.line).to eq(9) + tokens = LexAll.new(source: source) + expect(tokens.map(&:value)).to include("dog") + expect(tokens.first.line).to eq(1) + expect(tokens.last.line).to eq(9) end end end diff --git a/tool/sync_default_gems.rb b/tool/sync_default_gems.rb index 4ef99d16e4dc35..ad03e554f88f5d 100755 --- a/tool/sync_default_gems.rb +++ b/tool/sync_default_gems.rb @@ -295,7 +295,13 @@ def lib((upstream, branch), gemspec_in_subdir: false) "ext/strscan/regint.h", "ext/strscan/lib/strscan/truffleruby.rb", ]), - syntax_suggest: lib(["ruby/syntax_suggest", "main"], gemspec_in_subdir: true), + syntax_suggest: repo("ruby/syntax_suggest", [ + ["lib/syntax_suggest.rb", "lib/syntax_suggest.rb"], + ["lib/syntax_suggest", "lib/syntax_suggest"], + ["syntax_suggest.gemspec", "lib/syntax_suggest/syntax_suggest.gemspec"], + ["exe/syntax_suggest", "libexec/syntax_suggest"], + ["spec", "spec/syntax_suggest"], + ]), tempfile: lib("ruby/tempfile"), time: lib("ruby/time"), timeout: lib("ruby/timeout"),