Skip to content

Commit

Permalink
[[FIX]] Don't warn when RegExp() is used without 'new'. (#3529)
Browse files Browse the repository at this point in the history
https://262.ecma-international.org/#sec-regexp-constructor
...
function call RegExp(…) is equivalent
to the object creation expression new RegExp(…)
with the same arguments
...
  • Loading branch information
lauriro committed Feb 6, 2021
1 parent 2d4a3d1 commit c18a6e4
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
19 changes: 11 additions & 8 deletions src/jshint.js
Expand Up @@ -3046,14 +3046,17 @@ var JSHINT = (function() {

if (left) {
if (left.type === "(identifier)") {
if (left.value.match(/^[A-Z]([A-Z0-9_$]*[a-z][A-Za-z0-9_$]*)?$/)) {
if ("Array Number String Boolean Date Object Error Symbol".indexOf(left.value) === -1) {
if (left.value === "Math") {
/* istanbul ignore next */
warning("W063", left);
} else if (state.option.newcap) {
warning("W064", left);
}
var newcapRe = /^[A-Z]([A-Z0-9_$]*[a-z][A-Za-z0-9_$]*)?$/;
var newcapIgnore = [
"Array", "Boolean", "Date", "Error", "Number",
"Object", "RegExp", "String", "Symbol"
];
if (newcapRe.test(left.value) && newcapIgnore.indexOf(left.value) === -1) {
if (left.value === "Math") {
/* istanbul ignore next */
warning("W063", left);
} else if (state.option.newcap) {
warning("W064", left);
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion tests/unit/fixtures/newcap.js
Expand Up @@ -19,8 +19,13 @@ bat = myAnimal();

// Make sure we don't warn on Error, Number, etc.
Array();
Boolean();
Date();
Error();
Number();
/*jshint -W010 */
Object();
/*jshint +W010 */
RegExp();
String();
Boolean();
Symbol();

0 comments on commit c18a6e4

Please sign in to comment.