Skip to content

Commit b39f8bf

Browse files
committed
Review comments
1 parent 4337d1d commit b39f8bf

File tree

3 files changed

+79
-14
lines changed

3 files changed

+79
-14
lines changed

lib/ruby_ast_gen.rb

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
require_relative 'ruby_ast_gen/version'
77
require_relative 'ruby_ast_gen/node_handling'
8-
require_relative 'ruby_ast_gen/ErbToRubyTransformer'
8+
require_relative 'ruby_ast_gen/erb_to_ruby_transformer'
99

1010
module RubyAstGen
1111

@@ -122,18 +122,7 @@ def self.parse_file(file_path, relative_input_path)
122122
File.read(file_path)
123123
else
124124
file_content = File.read(file_path)
125-
begin
126-
transformer = ErbToRubyTransformer.new
127-
transformer.call(file_content)
128-
rescue Error
129-
# Wrap the file_content in HEREDOC so the AST parser gives a String output of the ERB file
130-
# in case the transformation fell over
131-
<<~RUBY
132-
<<~HEREDOC
133-
#{file_content}
134-
HEREDOC
135-
RUBY
136-
end
125+
get_erb_content(file_content)
137126
end
138127

139128
buffer = Parser::Source::Buffer.new(file_path)
@@ -159,4 +148,19 @@ def self.erb_file?(file_path)
159148
ext = File.extname(file_path)
160149
['.erb'].include?(ext) || file_path.end_with?('.erb')
161150
end
151+
152+
def self.get_erb_content(file_content)
153+
begin
154+
transformer = ErbToRubyTransformer.new
155+
transformer.transform(file_content)
156+
rescue Error
157+
# Wrap the file_content in HEREDOC so the AST parser gives a String output of the ERB file
158+
# in case the transformation fell over
159+
<<~RUBY
160+
<<~HEREDOC
161+
#{file_content}
162+
HEREDOC
163+
RUBY
164+
end
165+
end
162166
end

lib/ruby_ast_gen/ErbToRubyTransformer.rb renamed to lib/ruby_ast_gen/erb_to_ruby_transformer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ def initialize
1010
@control_block_content = []
1111
end
1212

13-
def call(input)
13+
def transform(input)
1414
ast = @parser.call(input)
1515
content = visit(ast)
1616
# Wrap everything in a HEREDOC

spec/ruby_ast_gen_spec.rb

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@
1010
file
1111
}
1212

13+
let(:temp_erb_file) {
14+
file = Tempfile.new('test_erb.erb')
15+
temp_name = File.basename(file.path)
16+
file
17+
}
18+
1319
after(:each) do
1420
temp_file.close
1521
temp_file.unlink
@@ -20,6 +26,11 @@ def code(s)
2026
temp_file.rewind
2127
end
2228

29+
def erb_code(s)
30+
temp_erb_file.write(s)
31+
temp_erb_file.rewind
32+
end
33+
2334
it "should parse a class successfully" do
2435
code(<<-CODE)
2536
class Foo
@@ -90,4 +101,54 @@ def foo(a, bar: "default")
90101
ast = RubyAstGen::parse_file(temp_file.path, temp_name)
91102
expect(ast).not_to be_nil
92103
end
104+
105+
it "should parse ERB structure with no ruby expressions" do
106+
code(<<-CODE)
107+
app_name: <%= ENV['APP_NAME'] %>
108+
version: <%= ENV['APP_VERSION'] %>
109+
110+
database:
111+
host: <%= ENV['DB_HOST'] %>
112+
port: <%= ENV['DB_PORT'] %>
113+
CODE
114+
ast = RubyAstGen::parse_file(temp_erb_file.path, temp_name)
115+
expect(ast).not_to be_nil
116+
end
117+
118+
it "should parse ERB structure with expressions" do
119+
code(<<-CODE)
120+
app_name: <%= ENV['APP_NAME'] %>
121+
version: <%= ENV['APP_VERSION'] %>
122+
123+
database:
124+
host: <%= ENV['DB_HOST'] %>
125+
port: <%= ENV['DB_PORT'] %>
126+
127+
<% if ENV['USE_REDIS'] == 'true' %>
128+
redis:
129+
host: <%= ENV['REDIS_HOST'] %>
130+
port: <%= ENV['REDIS_PORT'] %>
131+
<% end %>
132+
CODE
133+
ast = RubyAstGen::parse_file(temp_erb_file.path, temp_name)
134+
expect(ast).not_to be_nil
135+
end
136+
137+
it "should still return some AST even if the ERB is invalid" do
138+
code(<<-CODE)
139+
app_name: <%= ENV['APP_NAME'] %>
140+
version: <%= ENV['APP_VERSION'] %>
141+
142+
database:
143+
host: <%= ENV['DB_HOST'] %>
144+
port: <%= ENV['DB_PORT'] %>
145+
146+
<% if ENV['USE_REDIS'] == 'true' %>
147+
redis:
148+
host: <%= ENV['REDIS_HOST'] %>
149+
port: <%= ENV['REDIS_PORT'] %>
150+
CODE
151+
ast = RubyAstGen::parse_file(temp_erb_file.path, temp_name)
152+
expect(ast).not_to be_nil
153+
end
93154
end

0 commit comments

Comments
 (0)