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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,9 @@ Lexer.prototype = {

eos: function() {
if (this.input.length) return;
if (this.interpolated) {
this.error('NO_END_BRACKET', 'End of line was reached with no closing bracket for interpolation.');
}
for (var i = 0; i < this.indentStack.length; i++) {
this.tokens.push(this.tok('outdent'));
}
Expand Down Expand Up @@ -326,12 +329,7 @@ Lexer.prototype = {
startingLine: this.lineno
});
var interpolated = child.getTokens();
for (var i = 0; i < interpolated.length; i++) {
this.tokens.push(interpolated[i]);
if (interpolated[i].type === 'eos') {
this.error('NO_END_BRACKET', 'End of line was reached with no closing bracket for interpolation.');
}
}
this.tokens.push.apply(this.tokens, interpolated);
this.tokens.push(this.tok('end-jade-interpolation'));
this.addText(child.input);
return;
Expand Down Expand Up @@ -690,13 +688,24 @@ Lexer.prototype = {
code: function() {
var captures;
if (captures = /^(!?=|-)[ \t]*([^\n]+)/.exec(this.input)) {
this.consume(captures[0].length);
var flags = captures[1];
captures[1] = captures[2];
var tok = this.tok('code', captures[1]);
var code = captures[2];
var shortened = 0;
if (this.interpolated) {
var parsed;
try {
parsed = characterParser.parseMaxBracket(code, ']');
} catch (err) {
this.error('NO_END_BRACKET', 'End of line was reached with no closing bracket for interpolation.');
}
shortened = code.length - parsed.end;
code = parsed.src;
}
this.consume(captures[0].length - shortened);
var tok = this.tok('code', code);
tok.escape = flags.charAt(0) === '=';
tok.buffer = flags.charAt(0) === '=' || flags.charAt(1) === '=';
if (tok.buffer) this.assertExpression(captures[1]);
if (tok.buffer) this.assertExpression(code);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this.interpolated is true we should probably add an assertion to check that the code is a valid statement/sequence of statements. e.g. we should probably prevent the following (admittedly currently valid) jade.

p #[- items.forEach(function (item) {]item#[- });]

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 think the better question is if we should support #[- 'code'] syntax at all. Tag interpolation isn't designed for control flow.

If we decide to keep supporting #[-, then I see no reason why we should only support complete statements.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets drop support all together then

this.tokens.push(tok);
return true;
}
Expand Down
78 changes: 52 additions & 26 deletions test/cases/inline-tag.expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,60 @@
{"type":"end-jade-interpolation","line":5}
{"type":"text","line":5,"val":""}
{"type":"newline","line":6}
{"type":"text","line":6,"val":"bong"}
{"type":"text","line":6,"val":""}
{"type":"start-jade-interpolation","line":6}
{"type":"tag","line":6,"val":"strong","selfClosing":false}
{"type":"code","line":6,"val":"'[foo]'","escape":true,"buffer":true}
{"type":"end-jade-interpolation","line":6}
{"type":"text","line":6,"val":""}
{"type":"newline","line":7}
{"type":"text","line":7,"val":""}
{"type":"end-pipeless-text","line":7}
{"type":"start-jade-interpolation","line":7}
{"type":"code","line":7,"val":"var foo = 'foo]'","escape":false,"buffer":false}
{"type":"end-jade-interpolation","line":7}
{"type":"text","line":7,"val":""}
{"type":"newline","line":8}
{"type":"tag","line":8,"val":"p","selfClosing":false}
{"type":"indent","line":9,"val":2}
{"type":"text","line":9,"val":"bing"}
{"type":"text","line":8,"val":"bong"}
{"type":"newline","line":9}
{"type":"text","line":9,"val":""}
{"type":"end-pipeless-text","line":9}
{"type":"newline","line":10}
{"type":"text","line":10,"val":""}
{"type":"start-jade-interpolation","line":10}
{"type":"tag","line":10,"val":"strong","selfClosing":false}
{"type":"text","line":10,"val":"foo"}
{"type":"end-jade-interpolation","line":10}
{"type":"text","line":10,"val":""}
{"type":"newline","line":11}
{"type":"text","line":11,"val":"bong"}
{"type":"outdent","line":13}
{"type":"tag","line":13,"val":"p","selfClosing":false}
{"type":"dot","line":13}
{"type":"start-pipeless-text","line":13}
{"type":"text","line":14,"val":"#[strong escaped]"}
{"type":"tag","line":10,"val":"p","selfClosing":false}
{"type":"indent","line":11,"val":2}
{"type":"text","line":11,"val":"bing"}
{"type":"newline","line":12}
{"type":"text","line":12,"val":""}
{"type":"start-jade-interpolation","line":12}
{"type":"tag","line":12,"val":"strong","selfClosing":false}
{"type":"text","line":12,"val":"foo"}
{"type":"end-jade-interpolation","line":12}
{"type":"text","line":12,"val":""}
{"type":"newline","line":13}
{"type":"text","line":13,"val":""}
{"type":"start-jade-interpolation","line":13}
{"type":"tag","line":13,"val":"strong","selfClosing":false}
{"type":"code","line":13,"val":"'[foo]'","escape":true,"buffer":true}
{"type":"end-jade-interpolation","line":13}
{"type":"text","line":13,"val":""}
{"type":"newline","line":14}
{"type":"text","line":14,"val":""}
{"type":"start-jade-interpolation","line":14}
{"type":"code","line":14,"val":"var foo = 'foo]'","escape":false,"buffer":false}
{"type":"end-jade-interpolation","line":14}
{"type":"text","line":14,"val":""}
{"type":"newline","line":15}
{"type":"text","line":15,"val":"#["}
{"type":"start-jade-interpolation","line":15}
{"type":"tag","line":15,"val":"strong","selfClosing":false}
{"type":"text","line":15,"val":"escaped"}
{"type":"end-jade-interpolation","line":15}
{"type":"text","line":15,"val":""}
{"type":"end-pipeless-text","line":15}
{"type":"eos","line":15}
{"type":"text","line":15,"val":"bong"}
{"type":"outdent","line":17}
{"type":"tag","line":17,"val":"p","selfClosing":false}
{"type":"dot","line":17}
{"type":"start-pipeless-text","line":17}
{"type":"text","line":18,"val":"#[strong escaped]"}
{"type":"newline","line":19}
{"type":"text","line":19,"val":"#["}
{"type":"start-jade-interpolation","line":19}
{"type":"tag","line":19,"val":"strong","selfClosing":false}
{"type":"text","line":19,"val":"escaped"}
{"type":"end-jade-interpolation","line":19}
{"type":"text","line":19,"val":""}
{"type":"end-pipeless-text","line":19}
{"type":"eos","line":19}
6 changes: 5 additions & 1 deletion test/cases/inline-tag.jade
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ p bing #[strong foo] bong
p.
bing
#[strong foo]
#[strong= '[foo]']
#[- var foo = 'foo]']
bong

p
| bing
| #[strong foo]
| #[strong= '[foo]']
| #[- var foo = 'foo]']
| bong

p.
\#[strong escaped]
\#[#[strong escaped]
\#[#[strong escaped]
2 changes: 2 additions & 0 deletions test/errors/mismatched-inline-tag.jade
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//- #1871
p #[strong a}
5 changes: 5 additions & 0 deletions test/errors/mismatched-inline-tag.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"msg": "End of line was reached with no closing bracket for interpolation.",
"code": "JADE:NO_END_BRACKET",
"line": 2
}