Skip to content
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
21 changes: 20 additions & 1 deletion src/extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,26 @@ void herb_extract_ruby_to_buffer_with_semicolons(const char* source, hb_buffer_T

case TOKEN_ERB_CONTENT: {
if (skip_erb_content == false) {
hb_buffer_append(output, token->value);
bool is_inline_comment = false;

if (!is_comment_tag && token->value != NULL) {
const char* content = token->value;

while (*content == ' ' || *content == '\t') {
content++;
}

if (*content == '#' && token->location.start.line == token->location.end.line) {
is_comment_tag = true;
is_inline_comment = true;
}
}

if (is_inline_comment) {
hb_buffer_append_whitespace(output, range_length(token->range));
} else {
hb_buffer_append(output, token->value);
}
} else {
hb_buffer_append_whitespace(output, range_length(token->range));
}
Expand Down
62 changes: 62 additions & 0 deletions test/c/test_extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,62 @@ TEST(extract_ruby_issue_135_if_without_condition)
free(result);
END

TEST(extract_ruby_inline_comment_same_line)
char* source = "<% if true %><% # Comment here %><% end %>";
char* result = herb_extract_ruby_with_semicolons(source);

ck_assert_str_eq(result, " if true ; end ");

free(result);
END

TEST(extract_ruby_inline_comment_with_newline)
char* source = "<% if true %><% # Comment here %>\n<% end %>";
char* result = herb_extract_ruby_with_semicolons(source);

char expected[] = " if true ; \n end ";
ck_assert_str_eq(result, expected);

free(result);
END

TEST(extract_ruby_inline_comment_with_spaces)
char* source = "<% # Comment %> <% code %>";
char* result = herb_extract_ruby_with_semicolons(source);

ck_assert_str_eq(result, " code ");

free(result);
END

TEST(extract_ruby_inline_comment_multiline)
char* source = "<% # Comment\nmore %> <% code %>";
char* result = herb_extract_ruby_with_semicolons(source);

char expected[] = " # Comment\nmore ; code ";
ck_assert_str_eq(result, expected);

free(result);
END

TEST(extract_ruby_inline_comment_between_code)
char* source = "<% if true %><% # Comment here %><%= hello %><% end %>";
char* result = herb_extract_ruby_with_semicolons(source);

ck_assert_str_eq(result, " if true ; hello ; end ");

free(result);
END

TEST(extract_ruby_inline_comment_complex)
char* source = "<% # Comment here %><% if true %><% # Comment here %><%= hello %><% end %>";
char* result = herb_extract_ruby_with_semicolons(source);

ck_assert_str_eq(result, " if true ; hello ; end ");

free(result);
END

TCase *extract_tests(void) {
TCase *extract = tcase_create("Extract");

Expand All @@ -101,6 +157,12 @@ TCase *extract_tests(void) {
tcase_add_test(extract, extract_ruby_empty_erb_same_line);
tcase_add_test(extract, extract_ruby_comments_skipped);
tcase_add_test(extract, extract_ruby_issue_135_if_without_condition);
tcase_add_test(extract, extract_ruby_inline_comment_same_line);
tcase_add_test(extract, extract_ruby_inline_comment_with_newline);
tcase_add_test(extract, extract_ruby_inline_comment_with_spaces);
tcase_add_test(extract, extract_ruby_inline_comment_multiline);
tcase_add_test(extract, extract_ruby_inline_comment_between_code);
tcase_add_test(extract, extract_ruby_inline_comment_complex);

return extract;
}
30 changes: 30 additions & 0 deletions test/parser/erb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -228,5 +228,35 @@ class ERBTest < Minitest::Spec
<% end %>
HTML
end

test "inline ruby comment on same line" do
assert_parsed_snapshot(%(<% if true %><% # Comment here %><% end %>))
end

test "inline ruby comment with newline" do
assert_parsed_snapshot(<<~HTML)
<% if true %><% # Comment here %>
<% end %>
HTML
end

test "inline ruby comment between code" do
assert_parsed_snapshot(%(<% if true %><% # Comment here %><%= hello %><% end %>))
end

test "inline ruby comment before and between code" do
assert_parsed_snapshot(%(<% # Comment here %><% if true %><% # Comment here %><%= hello %><% end %>))
end

test "inline ruby comment with spaces" do
assert_parsed_snapshot(%(<% # Comment %> <% code %>))
end

test "inline ruby comment multiline" do
assert_parsed_snapshot(<<~HTML)
<% # Comment
more %> <% code %>
HTML
end
end
end

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading