Skip to content
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

Merged
merged 2 commits into from
Sep 10, 2015
Merged

Fix inline tag with code #11

merged 2 commits into from
Sep 10, 2015

Conversation

TimothyGu
Copy link
Member

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.

Depends on ForbesLindesay/character-parser#12.

This should be merged in conjunction with pugjs/pug#2049.

var parsed, shortened = 0;
if (this.interpolated) {
try {
parsed = characterParser.parseUntil(code, ']');
Copy link
Member

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 ]?

Copy link
Member Author

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.

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.
ForbesLindesay added a commit that referenced this pull request Sep 10, 2015
@ForbesLindesay ForbesLindesay merged commit 222c0ae into master Sep 10, 2015
@ForbesLindesay ForbesLindesay deleted the inline-tag-code branch September 10, 2015 15:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mismatched brackets work with interpolation
2 participants