Skip to content

~1.32x Faster checking with CleanDocument regex #105

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

Merged
merged 1 commit into from
Nov 4, 2021
Merged
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
47 changes: 19 additions & 28 deletions lib/dead_end/clean_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,17 +85,16 @@ module DeadEnd
#
class CleanDocument
def initialize(source:)
@source = source
@source = clean_sweep(source: source)
@document = CodeLine.from_source(@source)
end

# Call all of the document "cleaners"
# and return self
def call
clean_sweep
.join_trailing_slash!
.join_consecutive!
.join_heredoc!
join_trailing_slash!
join_consecutive!
join_heredoc!

self
end
Expand All @@ -122,17 +121,15 @@ def to_s
# puts "world"
# EOM
#
# lines = CleanDocument.new(source: source).clean_sweep.lines
# lines = CleanDocument.new(source: source).lines
# expect(lines[0].to_s).to eq("\n")
# expect(lines[1].to_s).to eq("puts "hello")
# expect(lines[2].to_s).to eq("\n")
# expect(lines[3].to_s).to eq("puts "world")
#
# WARNING:
# If you run this after any of the "join" commands, they
# will be un-joined.
# Important: This must be done before lexing.
#
# After this change is made, we re-lex the document because
# After this change is made, we lex the document because
# removing comments can change how the doc is parsed.
#
# For example:
Expand All @@ -142,7 +139,9 @@ def to_s
# # comment
# where(name: 'schneems')
# EOM
# expect(values.count {|v| v.type == :on_ignored_nl}).to eq(1)
# expect(
# values.count {|v| v.type == :on_ignored_nl}
# ).to eq(1)
#
# After the comment is removed:
#
Expand All @@ -151,26 +150,18 @@ def to_s
#
# where(name: 'schneems')
# EOM
# expect(values.count {|v| v.type == :on_ignored_nl}).to eq(2)
# expect(
# values.count {|v| v.type == :on_ignored_nl}
# ).to eq(2)
#
def clean_sweep
source = @document.map do |code_line|
# Clean trailing whitespace on empty line
if code_line.line.strip.empty?
next CodeLine.new(line: "\n", index: code_line.index, lex: [])
def clean_sweep(source:)
source.lines.map do |line|
if line.match?(/^\s*(#[^{].*)?$/) # https://rubular.com/r/LLE10D8HKMkJvs
$/
else
line
end

# Remove comments
if code_line.lex.detect { |lex| lex.type != :on_sp }&.type == :on_comment
next CodeLine.new(line: "\n", index: code_line.index, lex: [])
end

code_line
end.join

@source = source
@document = CodeLine.from_source(source)
self
end

# Smushes all heredoc lines into one line
Expand Down
6 changes: 3 additions & 3 deletions spec/unit/clean_document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

module DeadEnd
RSpec.describe CleanDocument do
it "heredoc: blerg" do
it "heredocs" do
source = fixtures_dir.join("this_project_extra_def.rb.txt").read
code_lines = CleanDocument.new(source: source).call.lines

Expand Down Expand Up @@ -92,7 +92,7 @@ module DeadEnd
# yolo
EOM

out = CleanDocument.new(source: source).clean_sweep
out = CleanDocument.new(source: source).lines.join
expect(out.to_s).to eq(<<~EOM)

puts "what"
Expand All @@ -105,7 +105,7 @@ module DeadEnd
puts "what"
EOM

out = CleanDocument.new(source: source).clean_sweep
out = CleanDocument.new(source: source).lines.join
expect(out.to_s).to eq(<<~EOM)

puts "what"
Expand Down