Skip to content

Commit b3478aa

Browse files
committed
Strip HEREDOC
Heredocs in ruby are common sources of indentation even though they cannot cause either an "unmatched" or "unexpected" end syntax errors. To help avoid these false positives when parsing source, we first find and parse all heredocs. If the lines that contain the heredocs are valid, they'll be commented-out/hidden early on. This drastically decreases the chance that heredoc based indentation will lead to false positives.
1 parent 7bac04a commit b3478aa

File tree

9 files changed

+678
-3
lines changed

9 files changed

+678
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## HEAD (unreleased)
22

3+
- Strip out heredocs in documents first (https://github.com/zombocom/syntax_search/pull/19)
4+
35
## 0.1.4
46

57
- Parser gem replaced with Ripper (https://github.com/zombocom/syntax_search/pull/17)

lib/syntax_search.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,3 +141,4 @@ def self.invalid_type(source)
141141

142142
require_relative "syntax_search/code_search"
143143
require_relative "syntax_search/who_dis_syntax_error"
144+
require_relative "syntax_search/heredoc_block_parse"

lib/syntax_search/code_block.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ def is_end?
3131
to_s.strip == "end"
3232
end
3333

34+
def hidden?
35+
@lines.all?(&:hidden?)
36+
end
37+
3438
def starts_at
3539
@starts_at ||= @lines.first&.line_number
3640
end

lib/syntax_search/code_search.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ class CodeSearch
2828
private; attr_reader :frontier; public
2929
public; attr_reader :invalid_blocks, :record_dir, :code_lines
3030

31-
def initialize(string, record_dir: ENV["SYNTAX_SEARCH_RECORD_DIR"])
31+
def initialize(source, record_dir: ENV["SYNTAX_SEARCH_RECORD_DIR"])
32+
@source = source
3233
if record_dir
3334
@time = Time.now.strftime('%Y-%m-%d-%H-%M-%s-%N')
3435
@record_dir = Pathname(record_dir).join(@time).tap {|p| p.mkpath }
3536
@write_count = 0
3637
end
37-
@code_lines = string.lines.map.with_index do |line, i|
38+
@code_lines = source.lines.map.with_index do |line, i|
3839
CodeLine.new(line: line, index: i)
3940
end
4041
@frontier = CodeFrontier.new(code_lines: @code_lines)
@@ -107,8 +108,19 @@ def expand_invalid_block
107108
push(block, name: "expand")
108109
end
109110

111+
112+
def sweep_heredocs
113+
HeredocBlockParse.new(
114+
source: @source,
115+
code_lines: @code_lines
116+
).call.each do |block|
117+
push(block, name: "heredoc")
118+
end
119+
end
120+
110121
# Main search loop
111122
def call
123+
sweep_heredocs
112124
until frontier.holds_all_syntax_errors?
113125
@tick += 1
114126

lib/syntax_search/display_invalid_blocks.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def initialize(code_lines: ,blocks:, io: $stderr, filename: nil, terminal: false
2020
end
2121

2222
def call
23-
if @blocks.any?
23+
if @blocks.any? { |b| !b.hidden? }
2424
found_invalid_blocks
2525
else
2626
@io.puts "Syntax OK"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
module SyntaxErrorSearch
4+
# Takes in a source, and returns blocks containing each heredoc
5+
class HeredocBlockParse
6+
private; attr_reader :code_lines, :lex; public
7+
8+
def initialize(source:, code_lines: )
9+
@code_lines = code_lines
10+
@lex = Ripper.lex(source)
11+
end
12+
13+
def call
14+
blocks = []
15+
beginning = []
16+
@lex.each do |(line, col), event, *_|
17+
case event
18+
when :on_heredoc_beg
19+
beginning << line
20+
when :on_heredoc_end
21+
start_index = beginning.pop - 1
22+
end_index = line - 1
23+
blocks << CodeBlock.new(lines: code_lines[start_index..end_index])
24+
end
25+
end
26+
27+
blocks
28+
end
29+
end
30+
end

0 commit comments

Comments
 (0)