Description
In theme-check, we're going through excessive lengths to attempt to figure out what the true tag.raw
value is.
TL;DR few issues:
tag.raw
needs work.line_number
is not enough to identify the location of a tag in a file. We'd need the col as well, or better thestart_index,end_index
range in the string.
The source of tag.raw right now is the following:
def raw
"#{tag_name} #{markup}"
end
This is wrong for any of the following tags:
// missing newline and spaces between 'render' and '"mysnippet"'
{%
render
"mysnippet"
%}
// tag.raw is "schema ", there's no space in the source
{%schema%}
To further complicate things on our end, the line_number
is the line_number
of the start of the markup (not the tag_name
!).
// line number is 3 for this tag
1 {%
2 render
3 'snippet'
4 %}
So any attempt at finding the true location of the tag has to do the following
- Find the start of the line for the markup
- Find the markup on the line, backtrack on whitespace till you find the tag
- Return the markup with the correct whitespace, and remember the offset in line_number.
Which causes more problem, because it could be that the markup exists outside of the tag on the same line. Consider the following:
1 {%
2 comment
3 %}comment{% endcomment %}
We have a couple of issues:
- Searching for "" on line 3 returns the start of line. Could either backtrack or look ahead.
- If looking ahead, we will find comment outside of the tag.
Also, consider the following situation:
1 {%
2 comment
3 %}{% endcomment %}{%comment%}{% endcomment %}
More issues
- Both comments have
line_number=3
, both comments havemarkup == ""
- markup="" is found on the beginning of the line could either look for
tag_name
backwards or forward till end of line
As you can see by now, trying to account for all those edge cases is not only painful on our end, it also makes everything slower because we need to recalculate the true location of everything.
My life would be a lot easier if we had the correct value for tag.raw
(with appropriate whitespace), better if we also had col numbers, and even better if we had start_index/end_index
ranges inside the template.