Skip to content

Commit

Permalink
[[FIX]] Make strict: func have precedence over env options.
Browse files Browse the repository at this point in the history
Now it is possible to ovverride node, phantom, module and browserify's `strict: global` using
`strict: func`, so that JSHint warns if `"use strict";` isn't in a function.
Example:
  /* jshint node: true, strict: func */
  "use strict"; // Warning: Use the function form of "use strict".

Env options still have precedence over `strict: true`.
  • Loading branch information
nicolo-ribaudo authored and lukeapage committed Jul 26, 2015
1 parent ae48966 commit d138db8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
19 changes: 14 additions & 5 deletions src/jshint.js
Expand Up @@ -198,6 +198,9 @@ var JSHINT = (function() {
}

if (state.option.module) {
if (state.option.strict === true) {
state.option.strict = "global";
}
/**
* TODO: Extend this restriction to *all* ES6-specific options.
*/
Expand Down Expand Up @@ -228,6 +231,9 @@ var JSHINT = (function() {

if (state.option.phantom) {
combine(predefined, vars.phantom);
if (state.option.strict === true) {
state.option.strict = "global";
}
}

if (state.option.prototypejs) {
Expand All @@ -237,6 +243,9 @@ var JSHINT = (function() {
if (state.option.node) {
combine(predefined, vars.node);
combine(predefined, vars.typed);
if (state.option.strict === true) {
state.option.strict = "global";
}
}

if (state.option.devel) {
Expand All @@ -256,6 +265,9 @@ var JSHINT = (function() {
combine(predefined, vars.browser);
combine(predefined, vars.typed);
combine(predefined, vars.browserify);
if (state.option.strict === true) {
state.option.strict = "global";
}
}

if (state.option.nonstandard) {
Expand Down Expand Up @@ -607,12 +619,12 @@ var JSHINT = (function() {
if (key === "strict") {
switch (val) {
case "true":
case "func":
state.option.strict = true;
break;
case "false":
state.option.strict = false;
break;
case "func":
case "global":
case "implied":
state.option.strict = val;
Expand Down Expand Up @@ -5168,10 +5180,7 @@ var JSHINT = (function() {

if (state.directive["use strict"]) {
if (state.option.strict !== "global") {
if (!(state.option.module || state.option.node || state.option.phantom ||
state.option.browserify)) {
warning("W097", state.tokens.prev);
}
warning("W097", state.tokens.prev);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/options.js
Expand Up @@ -881,7 +881,9 @@ exports.val = {
* - "global" - there must be a `"use strict";` directive at global level
* - "implied" - lint the code as if there is the `"use strict";` directive
* - false - disable warnings about strict mode
* - true - same as `"func"`
* - true - same as `"func"`, but environment options have precedence over
* this (e.g. `node`, `module`, `browserify` and `phantomjs` can
* set `strict: global`)
*/
strict : true,

Expand Down
11 changes: 8 additions & 3 deletions tests/unit/options.js
Expand Up @@ -1579,17 +1579,22 @@ exports.strict = function (test) {
run.addError(1, 'Unnecessary directive "use strict".')
.test(code3, { strict: "implied" });

[ true, false, "global", "implied" ].forEach(function(val) {
[ true, false, "global", "func", "implied" ].forEach(function(val) {
JSHINT("/*jshint strict: " + val + " */");
test.strictEqual(JSHINT.data().options.strict, val);
});
JSHINT("/*jshint strict: func */");
test.strictEqual(JSHINT.data().options.strict, true);

TestRun(test)
.addError(1, "Bad option value.")
.test("/*jshint strict: foo */");

TestRun(test, "environments have precedence over 'strict: true'")
.test(code3, { strict: true, node: true });

TestRun(test, "environments don't have precedence over 'strict: func'")
.addError(1, 'Use the function form of "use strict".')
.test(code3, { strict: "func", node: true });

test.done();
};

Expand Down

0 comments on commit d138db8

Please sign in to comment.