Skip to content

Commit

Permalink
[[FIX]] Templates can not be directives
Browse files Browse the repository at this point in the history
Return a different kind of lexer token for full template strings such that they are not consumed by string directives.
  • Loading branch information
leebyron authored and caitp committed Feb 6, 2015
1 parent 4f08b74 commit 20ff670
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
4 changes: 4 additions & 0 deletions src/jshint.js
Expand Up @@ -1992,6 +1992,10 @@ var JSHINT = (function() {
return this;
});

type("(no subst template)", function() {
return this;
});

type("(regexp)", function() {
return this;
});
Expand Down
16 changes: 13 additions & 3 deletions src/lex.js
Expand Up @@ -31,7 +31,8 @@ var Token = {
RegExp: 9,
TemplateHead: 10,
TemplateMiddle: 11,
TemplateTail: 12
TemplateTail: 12,
NoSubstTemplate: 13
};

var Context = {
Expand Down Expand Up @@ -1060,8 +1061,8 @@ Lexer.prototype = {
}
}

// Final value is either StringLiteral or TemplateTail
tokenType = tokenType === Token.TemplateHead ? Token.StringLiteral : Token.TemplateTail;
// Final value is either NoSubstTemplate or TemplateTail
tokenType = tokenType === Token.TemplateHead ? Token.NoSubstTemplate : Token.TemplateTail;
this.skip(1);
this.context.pop();

Expand Down Expand Up @@ -1668,6 +1669,15 @@ Lexer.prototype = {
});
return create("(template tail)", token.value);

case Token.NoSubstTemplate:
this.trigger("NoSubstTemplate", {
line: this.line,
char: this.char,
from: this.from,
value: token.value
});
return create("(no subst template)", token.value);

case Token.Identifier:
this.trigger("Identifier", {
line: this.line,
Expand Down
26 changes: 26 additions & 0 deletions tests/unit/core.js
Expand Up @@ -889,6 +889,32 @@ exports.testES6TemplateLiteralMultiline = function (test) {
test.done();
};

exports.testES6TemplateLiteralsAreNotDirectives = function (test) {
var src = [
"function fn() {",
"`use strict`;",
"return \"\\077\";",
"}"
];

TestRun(test)
.addError(2, "Expected an assignment or function call and instead saw an expression.")
.test(src, { esnext: true });

var src2 = [
"function fn() {",
"`${\"use strict\"}`;",
"return \"\\077\";",
"}"
];

TestRun(test)
.addError(2, "Expected an assignment or function call and instead saw an expression.")
.test(src2, { esnext: true });

test.done();
};

exports.testES6ExportStarFrom = function (test) {
var src = fs.readFileSync(__dirname + "/fixtures/es6-export-star-from.js", "utf8");
TestRun(test)
Expand Down

0 comments on commit 20ff670

Please sign in to comment.