-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix inline tag with code #11
Conversation
ffbf921
to
0a44ab6
Compare
var parsed, shortened = 0; | ||
if (this.interpolated) { | ||
try { | ||
parsed = characterParser.parseUntil(code, ']'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How does this handle code like:
- var index = Math.floor(Math.random() * 3);
p #[strong= ['a', 'b', 'c'][index]];
We could use characterParser.parseMax(code)
and then check that the next character is ]
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did not want to use parseMax
, since consider this case:
p #[strong= ['a', 'b', 'c'][index]};
We could throw an error about "bracket mismatch," but that doesn't feel intuitive. One would expect the lexer to throw "no ending bracket found" since that is the most direct error.
And for this reason, I wrote ForbesLindesay/character-parser#12, which will allow ['a', 'b', 'c'][index]};
to be the source as long as there is a trailing ]
, and throw the correct error if not.
0a44ab6
to
0fc272f
Compare
The cause of this problem is in how inline tags work: you encounter #[, then you take the rest of the line and feeds it into a new lexer. Using a special option `this.interpolated` and a flag `this.ended`, the child lexer returns whatever it can lex (i.e. the interpolated part) and the rest (after the interpolation) is fed back to the parent lexer from `child.input`. For text, it works well, since can't really have ] as a valid part of the text, so one can just use indexOf(']') for the ending index of the interpolation. Right now for code, it just feeds the **entire** rest of the line to the token, which in many cases will not be valid JS and caught in the assertExpression below. Plus, with code one never knows when code starts or ends, if the `']'` is in quotes or not. So one has to be cautious when consuming the strings, not using the crude `indexOf` check, which is what I am doing here. This also fixes pugjs/pug#1871.
0fc272f
to
1705034
Compare
Fix inline tag with code
The cause of this problem is in how inline tags work: you encounter
#[
, then you take the rest of the line and feeds it into a new lexer. Using a special optionthis.interpolated
and a flagthis.ended
, the child lexer returns whatever it can lex (i.e. the interpolated part) and the rest (after the interpolation) is fed back to the parent lexer fromchild.input
.For text, it works well, since can't really have
]
as a valid part of the text, so one can just useindexOf(']')
for the ending index of the interpolation.Right now for code, it just feeds the entire rest of the line to the token, which in many cases will not be valid JS and caught in the
assertExpression
below. Plus, with code one never knows when codestarts or ends, if the
']'
is in quotes or not. So one has to be cautious when consuming the strings, not using the crudeindexOf
check, which is what I am doing here.This also fixes pugjs/pug#1871.
Depends on ForbesLindesay/character-parser#12.
This should be merged in conjunction with pugjs/pug#2049.