-
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.
This commit adds a new tag named `#` that behaves like a comment. Therefore it behaves as you'd expect any tag would work. The difference with the comment tag is that the comment is in the tag markup and that there is no block delimiter. What it looks like in practice: ```liquid {%- # this is an inline comment -%} {% # this too is an inline comment %} {% liquid # required args: assign product = product # optional args: assign should_show_border = should_show_border | default: true assign should_show_cursor = should_show_cursor | default: true %} {% liquid # This is a very long comment that spans multiple lines. # It looks very similar to what it would look like if you wrote # ruby code instead of liquid. But it doesn't have all the clunk # of having an open tag and a close tag with so many characters. %} ``` Co-authored-by: Dylan Thacker-Smith <Dylan.Smith@shopify.com>
- Loading branch information
1 parent
db3999a
commit 4deaa31
Showing
6 changed files
with
108 additions
and
8 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 to be a backward incompatible changes. | ||
# | ||
# 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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
# 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 |