From 861499b2567c3df504f33bc9ba2bf88e3112691e Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 10 Nov 2026 03:28:50 +0000 Subject: [PATCH 1/2] Avoid polynomial regex when detecting the end of the routes annotation Co-Authored-By: adam@nexl.cloud --- lib/annotate/annotate_routes/helpers.rb | 2 +- .../annotate/annotate_routes/helpers_spec.rb | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 spec/lib/annotate/annotate_routes/helpers_spec.rb diff --git a/lib/annotate/annotate_routes/helpers.rb b/lib/annotate/annotate_routes/helpers.rb index 1dba65b..0fa66cc 100644 --- a/lib/annotate/annotate_routes/helpers.rb +++ b/lib/annotate/annotate_routes/helpers.rb @@ -16,7 +16,7 @@ def strip_annotations(content) header_position = 0 content.split(/\n/, -1).each_with_index do |line, line_number| - if mode == :header && line !~ /\s*#/ + if mode == :header && !line.include?('#') mode = :content real_content << line unless line.blank? elsif mode == :content diff --git a/spec/lib/annotate/annotate_routes/helpers_spec.rb b/spec/lib/annotate/annotate_routes/helpers_spec.rb new file mode 100644 index 0000000..5e45a27 --- /dev/null +++ b/spec/lib/annotate/annotate_routes/helpers_spec.rb @@ -0,0 +1,26 @@ +require_relative '../../../spec_helper' +require 'annotate/annotate_routes' + +RSpec.describe AnnotateRoutes::Helpers do + describe '.strip_annotations' do + it 'removes the route annotation block and keeps the content after it' do + content = "# == Route Map\n#\n# root GET / home#index\n\nRails.application.routes.draw do\nend\n" + + real_content, header_position = described_class.strip_annotations(content) + + expect(real_content).to eq(['Rails.application.routes.draw do', 'end', '']) + expect(header_position).to eq(:before) + end + + it 'handles a long whitespace-only line in the annotation block in linear time' do + content = "# == Route Map\n#{' ' * 100_000}\nRails.application.routes.draw do\nend\n" + + started = Process.clock_gettime(Process::CLOCK_MONOTONIC) + real_content, = described_class.strip_annotations(content) + elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started + + expect(real_content).to eq(['Rails.application.routes.draw do', 'end', '']) + expect(elapsed).to be < 1 + end + end +end From a2e2605a75b4ca41a462b77fb99d45a8b790ca5a Mon Sep 17 00:00:00 2001 From: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Date: Tue, 10 Nov 2026 03:30:36 +0000 Subject: [PATCH 2/2] Keep the linear-time routes spec within RSpec/ExampleLength Co-Authored-By: adam@nexl.cloud --- spec/lib/annotate/annotate_routes/helpers_spec.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/spec/lib/annotate/annotate_routes/helpers_spec.rb b/spec/lib/annotate/annotate_routes/helpers_spec.rb index 5e45a27..cee334a 100644 --- a/spec/lib/annotate/annotate_routes/helpers_spec.rb +++ b/spec/lib/annotate/annotate_routes/helpers_spec.rb @@ -15,12 +15,16 @@ it 'handles a long whitespace-only line in the annotation block in linear time' do content = "# == Route Map\n#{' ' * 100_000}\nRails.application.routes.draw do\nend\n" - started = Process.clock_gettime(Process::CLOCK_MONOTONIC) - real_content, = described_class.strip_annotations(content) - elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - started + (real_content,), elapsed = timed { described_class.strip_annotations(content) } expect(real_content).to eq(['Rails.application.routes.draw do', 'end', '']) expect(elapsed).to be < 1 end + + def timed + started = Process.clock_gettime(Process::CLOCK_MONOTONIC) + result = yield + [result, Process.clock_gettime(Process::CLOCK_MONOTONIC) - started] + end end end