Skip to content

Commit

Permalink
[[FIX]] Restrict "name" of strict mode functions
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike authored and rwaldron committed Jan 22, 2018
1 parent 8f3f880 commit a554c89
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/jshint.js
Expand Up @@ -1750,6 +1750,8 @@ var JSHINT = (function() {
}
directives();

state.funct["(isStrict)"] = state.isStrict();

if (state.option.strict && state.funct["(context)"]["(global)"]) {
if (!m["use strict"] && !state.isStrict()) {
warning("E007");
Expand Down Expand Up @@ -2864,6 +2866,12 @@ var JSHINT = (function() {
"(name)" : name,
"(breakage)" : 0,
"(loopage)" : 0,
// The strictness of the function body is tracked via a dedicated
// property (as opposed to via the global `state` object) so that the
// value can be referenced after the body has been fully parsed (i.e.
// when validating the identifier used in function declarations and
// function expressions).
"(isStrict)" : "unknown",

"(global)" : false,

Expand Down Expand Up @@ -3047,6 +3055,11 @@ var JSHINT = (function() {
warning("W124", state.tokens.curr);
}

if (state.funct["(isStrict)"] === true &&
(name === "arguments" || name === "eval")) {
error('E008', token);
}

state.funct["(metrics)"].verifyMaxStatementsPerFunction();
state.funct["(metrics)"].verifyMaxComplexityPerFunction();
state.funct["(unusedOption)"] = state.option.unused;
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/parser.js
Expand Up @@ -3666,6 +3666,38 @@ exports["strict violation - use of arguments and eval"] = function (test) {
"function f2(eval) { 'use strict'; }"
]);

TestRun(test, "as function binding (valid)")
.test([
"function arguments() {}",
"function eval() {}",
"void function arguments() {};",
"void function eval() {};"
]);

TestRun(test, "as function bindings for expressions with inferred names (valid)")
.test([
"var arguments = function() {};",
"(function() {",
" var eval = function() {};",
"}());"
]);

TestRun(test, "as function declaration binding (invalid)")
.addError(1, 10, "Strict violation.")
.addError(2, 10, "Strict violation.")
.test([
"function arguments() { 'use strict'; }",
"function eval() { 'use strict'; }"
]);

TestRun(test, "as function expression binding (invalid)")
.addError(1, 15, "Strict violation.")
.addError(2, 15, "Strict violation.")
.test([
"void function arguments() { 'use strict'; };",
"void function eval() { 'use strict'; };"
]);

test.done();
};

Expand Down

0 comments on commit a554c89

Please sign in to comment.