Skip to content

Commit

Permalink
[[FIX]] Allow object-literals within template strings
Browse files Browse the repository at this point in the history
Uses context introduced in previous diff to let lexer be contextually aware when parsing a "}" character.

Closes #2082
  • Loading branch information
leebyron authored and caitp committed Feb 6, 2015
1 parent 3da1eaf commit 4f08b74
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 6 deletions.
24 changes: 21 additions & 3 deletions src/lex.js
Expand Up @@ -35,7 +35,8 @@ var Token = {
};

var Context = {
Template: 1
Block: 1,
Template: 2
};

// Object that handles postponed lexing verifications that checks the parsed
Expand Down Expand Up @@ -224,8 +225,6 @@ Lexer.prototype = {
case ")":
case ";":
case ",":
case "{":
case "}":
case "[":
case "]":
case ":":
Expand All @@ -236,6 +235,25 @@ Lexer.prototype = {
value: ch1
};

// A block/object opener
case "{":
this.context.push(Context.Block);
return {
type: Token.Punctuator,
value: ch1
};

// A block/object closer
case "}":
if (!this.inContext(Context.Block)) {
return null;
}
this.context.pop();
return {
type: Token.Punctuator,
value: ch1
};

// A pound sign (for Node shebangs)
case "#":
return {
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/core.js
Expand Up @@ -846,9 +846,9 @@ exports.testES6TemplateLiterals = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/es6-template-literal.js", "utf8");
TestRun(test)
.addError(14, "Octal literals are not allowed in strict mode.")
.addError(19, "Unclosed template literal.")
.addError(20, "Expected an identifier and instead saw '(end)'.")
.addError(20, "Missing semicolon.")
.addError(21, "Unclosed template literal.")
.addError(22, "Expected an identifier and instead saw '(end)'.")
.addError(22, "Missing semicolon.")
.test(src, { esnext: true });
test.done();
};
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/fixtures/es6-template-literal.js
Expand Up @@ -16,4 +16,6 @@ function octal_strictmode() {

var nested = `Look and ${ `Nested ${ `whoaaa` } template` } listen`;

var innerobj = `Template with ${ {obj: "literal"} } inside`;

var unterminated = `${one}

0 comments on commit 4f08b74

Please sign in to comment.