Skip to content

Commit 5c0c3a7

Browse files
authored
Merge pull request #105 from zombocom/schneems/quicker-picker-upper
~1.32x Faster checking with CleanDocument regex
2 parents 58a8d74 + c088263 commit 5c0c3a7

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

lib/dead_end/clean_document.rb

Lines changed: 19 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -85,17 +85,16 @@ module DeadEnd
8585
#
8686
class CleanDocument
8787
def initialize(source:)
88-
@source = source
88+
@source = clean_sweep(source: source)
8989
@document = CodeLine.from_source(@source)
9090
end
9191

9292
# Call all of the document "cleaners"
9393
# and return self
9494
def call
95-
clean_sweep
96-
.join_trailing_slash!
97-
.join_consecutive!
98-
.join_heredoc!
95+
join_trailing_slash!
96+
join_consecutive!
97+
join_heredoc!
9998

10099
self
101100
end
@@ -122,17 +121,15 @@ def to_s
122121
# puts "world"
123122
# EOM
124123
#
125-
# lines = CleanDocument.new(source: source).clean_sweep.lines
124+
# lines = CleanDocument.new(source: source).lines
126125
# expect(lines[0].to_s).to eq("\n")
127126
# expect(lines[1].to_s).to eq("puts "hello")
128127
# expect(lines[2].to_s).to eq("\n")
129128
# expect(lines[3].to_s).to eq("puts "world")
130129
#
131-
# WARNING:
132-
# If you run this after any of the "join" commands, they
133-
# will be un-joined.
130+
# Important: This must be done before lexing.
134131
#
135-
# After this change is made, we re-lex the document because
132+
# After this change is made, we lex the document because
136133
# removing comments can change how the doc is parsed.
137134
#
138135
# For example:
@@ -142,7 +139,9 @@ def to_s
142139
# # comment
143140
# where(name: 'schneems')
144141
# EOM
145-
# expect(values.count {|v| v.type == :on_ignored_nl}).to eq(1)
142+
# expect(
143+
# values.count {|v| v.type == :on_ignored_nl}
144+
# ).to eq(1)
146145
#
147146
# After the comment is removed:
148147
#
@@ -151,26 +150,18 @@ def to_s
151150
#
152151
# where(name: 'schneems')
153152
# EOM
154-
# expect(values.count {|v| v.type == :on_ignored_nl}).to eq(2)
153+
# expect(
154+
# values.count {|v| v.type == :on_ignored_nl}
155+
# ).to eq(2)
155156
#
156-
def clean_sweep
157-
source = @document.map do |code_line|
158-
# Clean trailing whitespace on empty line
159-
if code_line.line.strip.empty?
160-
next CodeLine.new(line: "\n", index: code_line.index, lex: [])
157+
def clean_sweep(source:)
158+
source.lines.map do |line|
159+
if line.match?(/^\s*(#[^{].*)?$/) # https://rubular.com/r/LLE10D8HKMkJvs
160+
$/
161+
else
162+
line
161163
end
162-
163-
# Remove comments
164-
if code_line.lex.detect { |lex| lex.type != :on_sp }&.type == :on_comment
165-
next CodeLine.new(line: "\n", index: code_line.index, lex: [])
166-
end
167-
168-
code_line
169164
end.join
170-
171-
@source = source
172-
@document = CodeLine.from_source(source)
173-
self
174165
end
175166

176167
# Smushes all heredoc lines into one line

spec/unit/clean_document_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module DeadEnd
66
RSpec.describe CleanDocument do
7-
it "heredoc: blerg" do
7+
it "heredocs" do
88
source = fixtures_dir.join("this_project_extra_def.rb.txt").read
99
code_lines = CleanDocument.new(source: source).call.lines
1010

@@ -92,7 +92,7 @@ module DeadEnd
9292
# yolo
9393
EOM
9494

95-
out = CleanDocument.new(source: source).clean_sweep
95+
out = CleanDocument.new(source: source).lines.join
9696
expect(out.to_s).to eq(<<~EOM)
9797
9898
puts "what"
@@ -105,7 +105,7 @@ module DeadEnd
105105
puts "what"
106106
EOM
107107

108-
out = CleanDocument.new(source: source).clean_sweep
108+
out = CleanDocument.new(source: source).lines.join
109109
expect(out.to_s).to eq(<<~EOM)
110110
111111
puts "what"

0 commit comments

Comments
 (0)