@@ -85,17 +85,16 @@ module DeadEnd
85
85
#
86
86
class CleanDocument
87
87
def initialize ( source :)
88
- @source = source
88
+ @source = clean_sweep ( source : source )
89
89
@document = CodeLine . from_source ( @source )
90
90
end
91
91
92
92
# Call all of the document "cleaners"
93
93
# and return self
94
94
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!
99
98
100
99
self
101
100
end
@@ -122,17 +121,15 @@ def to_s
122
121
# puts "world"
123
122
# EOM
124
123
#
125
- # lines = CleanDocument.new(source: source).clean_sweep. lines
124
+ # lines = CleanDocument.new(source: source).lines
126
125
# expect(lines[0].to_s).to eq("\n")
127
126
# expect(lines[1].to_s).to eq("puts "hello")
128
127
# expect(lines[2].to_s).to eq("\n")
129
128
# expect(lines[3].to_s).to eq("puts "world")
130
129
#
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.
134
131
#
135
- # After this change is made, we re- lex the document because
132
+ # After this change is made, we lex the document because
136
133
# removing comments can change how the doc is parsed.
137
134
#
138
135
# For example:
@@ -142,7 +139,9 @@ def to_s
142
139
# # comment
143
140
# where(name: 'schneems')
144
141
# 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)
146
145
#
147
146
# After the comment is removed:
148
147
#
@@ -151,26 +150,18 @@ def to_s
151
150
#
152
151
# where(name: 'schneems')
153
152
# 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)
155
156
#
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
161
163
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
169
164
end . join
170
-
171
- @source = source
172
- @document = CodeLine . from_source ( source )
173
- self
174
165
end
175
166
176
167
# Smushes all heredoc lines into one line
0 commit comments