Skip to content

Commit

Permalink
[[FEAT]] Implement regexpu option
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike authored and rwaldron committed Jan 29, 2019
1 parent 1af5930 commit 962dced
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ var JSHINT = (function() {
}
}

if (state.option.regexpu) {
/**
* TODO: Extend this restriction to *all* ES6-specific options.
*/
if (!state.inES6()) {
warning("W134", state.tokens.next, "regexpu", 6);
}
}

if (state.option.couch) {
combine(predefined, vars.couch);
}
Expand Down
8 changes: 8 additions & 0 deletions src/lex.js
Original file line number Diff line number Diff line change
Expand Up @@ -1717,6 +1717,14 @@ Lexer.prototype = {
index += 1;
}

if (flags.indexOf("u") === -1) {
this.triggerAsync("warning", {
code: "W147",
line: this.line,
character: this.char
}, checks, function() { return state.option.regexpu; });
}

// Check regular expression for correctness.

try {
Expand Down
3 changes: 2 additions & 1 deletion src/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ var warnings = {
"unexpected changes to formal parameters.",
W144: "'{a}' is a non-standard language feature. Enable it using the '{b}' unstable option.",
W145: "Superfluous 'case' clause.",
W146: "Unnecessary `await` expression."
W146: "Unnecessary `await` expression.",
W147: "Regular expressions should include the 'u' flag."
};

var info = {
Expand Down
12 changes: 11 additions & 1 deletion src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,15 @@ exports.bool = {
*/
noreturnawait: true,

/**
* This option enables warnings for regular expressions which do not
* include the "u" flag. The "u" flag extends support for Unicode and also
* enables more strict parsing rules. JSHint will enforce these rules even
* if it is executed in a JavaScript engine which does not support the "u"
* flag.
*/
regexpu : true,

/**
* This option prohibits the use of explicitly undeclared variables. This
* option is very useful for spotting leaking and mistyped variables.
Expand Down Expand Up @@ -1088,5 +1097,6 @@ exports.removed = {
// `enforceall`.
exports.noenforceall = {
varstmt: true,
strict: true
strict: true,
regexpu: true
};
48 changes: 48 additions & 0 deletions tests/unit/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -2788,6 +2788,9 @@ exports.enforceall = function (test) {
TestRun(test)
.test(src, { enforceall: true, nonbsp: false, bitwise: false, sub: true, undef: false, unused: false, asi:true });

TestRun(test, "Does not enable 'regexpu'.")
.test('void /./;', { enforceall: true });

test.done();
};

Expand Down Expand Up @@ -4425,5 +4428,50 @@ exports.noreturnawait = function(test) {
.addError(5, 19, "Unnecessary `await` expression.")
.test(code, { esversion: 8, noreturnawait: true });


test.done();
};

exports.regexpu = function (test) {
TestRun(test, "restricted outside of ES6 - via API")
.addError(0, 0, "The 'regexpu' option is only available when linting ECMAScript 6 code.")
.test("void 0;", { regexpu: true })

TestRun(test, "restricted outside of ES6 - via directive")
.addError(1, 1, "The 'regexpu' option is only available when linting ECMAScript 6 code.")
.test([
"// jshint regexpu: true",
"void 0;"
]);

TestRun(test, "missing")
.addError(1, 6, "Regular expressions should include the 'u' flag.")
.addError(2, 6, "Regular expressions should include the 'u' flag.")
.addError(3, 6, "Regular expressions should include the 'u' flag.")
.test([
"void /./;",
"void /./g;",
"void /./giym;",
], { regexpu: true, esversion: 6 });

TestRun(test, "in use")
.test([
"void /./u;",
"void /./ugiym;",
"void /./guiym;",
"void /./giuym;",
"void /./giyum;",
"void /./giymu;"
], { esversion: 6 });

TestRun(test, "missing - option set when parsing precedes option enablement")
.addError(3, 8, "Regular expressions should include the 'u' flag.")
.test([
"(function() {",
" // jshint regexpu: true",
" void /./;",
"}());"
], { esversion: 6 });

test.done();
};

0 comments on commit 962dced

Please sign in to comment.