Skip to content

Commit

Permalink
[[FIX]] Correct location reported for directive
Browse files Browse the repository at this point in the history
Extend the internal data structure used to track directives so that it
can be used to correctly report location information. Document the type
of this structure in the source file where it is initialized.
  • Loading branch information
jugglinmike authored and rwaldron committed Feb 8, 2021
1 parent c18a6e4 commit ee6aa68
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
5 changes: 2 additions & 3 deletions src/jshint.js
Expand Up @@ -2054,8 +2054,7 @@ var JSHINT = (function() {
error("E065", state.tokens.curr);
}

// there's no directive negation, so always set to true
state.directive[directive] = true;
state.directive[directive] = state.tokens.curr;

parseFinalSemicolon(current);
}
Expand Down Expand Up @@ -6509,7 +6508,7 @@ var JSHINT = (function() {

if (state.directive["use strict"]) {
if (!state.allowsGlobalUsd()) {
warning("W097", state.tokens.prev);
warning("W097", state.directive["use strict"]);
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/state.js
Expand Up @@ -10,7 +10,7 @@ var state = {
* @returns {boolean}
*/
isStrict: function() {
return this.directive["use strict"] || this.inClassBody ||
return !!this.directive["use strict"] || this.inClassBody ||
this.option.module || this.option.strict === "implied";
},

Expand Down Expand Up @@ -177,6 +177,10 @@ var state = {
this.esVersion = 5;
this.funct = null;
this.ignored = {};
/**
* A lookup table for active directives whose keys are the value of the
* directives and whose values are the tokens which enabled the directives.
*/
this.directive = Object.create(null);
this.jsonMode = false;
this.lines = [];
Expand Down
13 changes: 13 additions & 0 deletions tests/unit/options.js
Expand Up @@ -2046,6 +2046,19 @@ exports.strict = function (test) {
.addError(1, 6, "Missing \"use strict\" statement.")
.test("a = 2;", { strict: "global" });

TestRun(test, "Warning location, missing semicolon (gh-3528)")
.addError(1, 1, "Use the function form of \"use strict\".")
.addError(1, 13, "Missing semicolon.")
.test("'use strict'\n");

TestRun(test, "Warning location among other directives")
.addError(2, 1, "Use the function form of \"use strict\".")
.test([
"'use another-directive';",
"'use strict';",
"'use a-third-directive';"
]);

test.done();
};

Expand Down

0 comments on commit ee6aa68

Please sign in to comment.