-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1498 from Shopify/feature/new-comment-syntax
Add `#` inline comment tag.
- Loading branch information
Showing
7 changed files
with
113 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
module Liquid | ||
class InlineComment < Tag | ||
def initialize(tag_name, markup, options) | ||
super | ||
|
||
# Semantically, a comment should only ignore everything after it on the line. | ||
# Currently, this implementation doesn't support mixing a comment with another tag | ||
# but we need to reserve future support for this and prevent the introduction | ||
# of inline comments from being backward incompatible change. | ||
# | ||
# As such, we're forcing users to put a # symbol on every line otherwise this | ||
# tag will throw an error. | ||
if markup.match?(/\n\s*[^#\s]/) | ||
raise SyntaxError, options[:locale].t("errors.syntax.inline_comment_invalid") | ||
end | ||
end | ||
|
||
def render_to_output_buffer(_context, output) | ||
output | ||
end | ||
|
||
def blank? | ||
true | ||
end | ||
end | ||
|
||
Template.register_tag('#', InlineComment) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
# frozen_string_literal: true | ||
|
||
module Liquid | ||
VERSION = "5.3.0" | ||
VERSION = "5.4.0.alpha" | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
class InlineCommentTest < Minitest::Test | ||
include Liquid | ||
|
||
def test_inline_comment_returns_nothing | ||
assert_template_result('', '{%- # this is an inline comment -%}') | ||
assert_template_result('', '{%-# this is an inline comment -%}') | ||
assert_template_result('', '{% # this is an inline comment %}') | ||
assert_template_result('', '{%# this is an inline comment %}') | ||
end | ||
|
||
def test_inline_comment_does_not_require_a_space_after_the_pound_sign | ||
assert_template_result('', '{%#this is an inline comment%}') | ||
end | ||
|
||
def test_liquid_inline_comment_returns_nothing | ||
assert_template_result('Hey there, how are you doing today?', <<~LIQUID) | ||
{%- liquid | ||
# This is how you'd write a block comment in a liquid tag. | ||
# It looks a lot like what you'd have in ruby. | ||
# You can use it as inline documentation in your | ||
# liquid blocks to explain why you're doing something. | ||
echo "Hey there, " | ||
# It won't affect the output. | ||
echo "how are you doing today?" | ||
-%} | ||
LIQUID | ||
end | ||
|
||
def test_inline_comment_can_be_written_on_multiple_lines | ||
assert_template_result('', <<~LIQUID) | ||
{%- | ||
# That kind of block comment is also allowed. | ||
# It would only be a stylistic difference. | ||
# Much like JavaScript's /* */ comments and their | ||
# leading * on new lines. | ||
-%} | ||
LIQUID | ||
end | ||
|
||
def test_inline_comment_multiple_pound_signs | ||
assert_template_result('', <<~LIQUID) | ||
{%- liquid | ||
###################################### | ||
# We support comments like this too. # | ||
###################################### | ||
-%} | ||
LIQUID | ||
end | ||
|
||
def test_inline_comments_require_the_pound_sign_on_every_new_line | ||
assert_match_syntax_error("Each line of comments must be prefixed by the '#' character", <<~LIQUID) | ||
{%- | ||
# some comment | ||
echo 'hello world' | ||
-%} | ||
LIQUID | ||
end | ||
|
||
def test_inline_comment_does_not_support_nested_tags | ||
assert_template_result(' -%}', "{%- # {% echo 'hello world' %} -%}") | ||
end | ||
end |