Repository: jshint/jshint
Pull request #3359 (2019-02-08 03:49:00)

[[FIX]] Allow built-in method names in classes
This is intended to resolve gh-3358.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/21523509/badge)](https://coveralls.io/builds/21523509)

Coverage increased (+0.07%) to 98.538% when pulling **a6c8805ab80f1c2a732f71e690b57ba37c7a50ac on jugglinmike:dup-class-names** into **0e85de5f33f3a4bb18833bbb87b4a08294f16899 on jshint:master**.

coveralls: 
[![Coverage Status](https://coveralls.io/builds/21523509/badge)](https://coveralls.io/builds/21523509)

Coverage increased (+0.07%) to 98.538% when pulling **a6c8805ab80f1c2a732f71e690b57ba37c7a50ac on jugglinmike:dup-class-names** into **0e85de5f33f3a4bb18833bbb87b4a08294f16899 on jshint:master**.


Patch:
diff --git a/src/jshint.js b/src/jshint.js
index 3b2f5871..55612643 100644
--- a/src/jshint.js
+++ b/src/jshint.js
@@ -2776,7 +2776,7 @@ var JSHINT = (function() {
   });
 
   function classBody(classToken, context) {
-    var props = {};
+    var props = Object.create(null);
     var name, accessorType, token, isStatic, inGenerator, hasConstructor;
 
     if (state.tokens.next.value === "{") {
diff --git a/src/lex.js b/src/lex.js
index 1ea296ae..1b1540c7 100644
--- a/src/lex.js
+++ b/src/lex.js
@@ -1582,6 +1582,11 @@ Lexer.prototype = {
         continue;
       }
 
+      if (isCharSet) {
+        index += 1;
+        continue;
+      }
+
       if (char === "{" && !hasInvalidQuantifier) {
         hasInvalidQuantifier = !checkQuantifier();
       }


Test patch:
diff --git a/tests/unit/parser.js b/tests/unit/parser.js
index c5e049b1..35ae1f70 100644
--- a/tests/unit/parser.js
+++ b/tests/unit/parser.js
@@ -866,6 +866,12 @@ exports.regexp.regressions = function (test) {
   TestRun(test).test("var exp = /\\[\\]/;", {esnext: true});
   TestRun(test).test("var exp = /\\[\\]/;", {moz: true});
 
+  // GH-3356
+  TestRun(test).test("void /[/]/;");
+  TestRun(test).test("void /[{]/u;", {esversion: 6});
+  TestRun(test).test("void /[(?=)*]/u;", {esversion: 6});
+  TestRun(test).test("void /[(?!)+]/u;", {esversion: 6});
+
   test.done();
 };
 
@@ -6673,6 +6679,87 @@ exports["class and method naming"] = function (test) {
 
   run.test(code, {esnext: true});
 
+  TestRun(test, "hazardous method names (see gh-3358)")
+    .addError(3, 17, "'hasOwnProperty' is a really bad name.")
+    .test([
+      "void class {",
+      "  constructor() {}",
+      "  hasOwnProperty() {}",
+      "  toString() {}",
+      "  toLocaleString() {}",
+      "  valueOf() {}",
+      "  isPrototypeOf() {}",
+      "  propertyIsEnumerable() {}",
+      "};"
+   ], {esversion: 6});
+
+  TestRun(test, "hazardous method names -- true duplicate (see gh-3358)")
+    .addError(4, 11, "Duplicate class method 'toString'.")
+    .test([
+      "void class {",
+      "  toString() {}",
+      "  x() {}",
+      "  toString() {}",
+      "};"
+   ], {esversion: 6});
+
+  TestRun(test, "hazardous static method names (see gh-3358)")
+    .addError(3, 24, "'hasOwnProperty' is a really bad name.")
+    .test([
+      "void class {",
+      "  static constructor() {}",
+      "  static hasOwnProperty() {}",
+      "  static toString() {}",
+      "  static toLocaleString() {}",
+      "  static valueOf() {}",
+      "  static isPrototypeOf() {}",
+      "  static propertyIsEnumerable() {}",
+      "};"
+   ], {esversion: 6});
+
+  TestRun(test, "hazardous static method names -- true duplicate (see gh-3358)")
+    .addError(4, 18, "Duplicate static class method 'toString'.")
+    .test([
+      "void class {",
+      "  static toString() {}",
+      "  static x() {}",
+      "  static toString() {}",
+      "};"
+   ], {esversion: 6});
+
+  TestRun(test, "hazardous accessor method names (see gh-3358)")
+    .addError(2, 21, "'hasOwnProperty' is a really bad name.")
+    .addError(3, 21, "'hasOwnProperty' is a really bad name.")
+    .test([
+      "void class {",
+      "  get hasOwnProperty() {}",
+      "  set hasOwnProperty(_) {}",
+      "  get toString() {}",
+      "  set toString(_) {}",
+      "  get toLocaleString() {}",
+      "  set toLocaleString(_) {}",
+      "  get valueOf() {}",
+      "  set valueOf(_) {}",
+      "  get isPrototypeOf() {}",
+      "  set isPrototypeOf(_) {}",
+      "  get propertyIsEnumerable() {}",
+      "  set propertyIsEnumerable(_) {}",
+      "};"
+   ], {esversion: 6});
+
+  TestRun(test, "hazardous accessor method names -- true duplicate (see gh-3358)")
+    .addError(5, 15, "Duplicate getter method 'toString'.")
+    .addError(6, 15, "Duplicate setter method 'toString'.")
+    .test([
+      "void class {",
+      "  get toString() {}",
+      "  set toString(_) {}",
+      "  static x() {}",
+      "  get toString() {}",
+      "  set toString(_) {}",
+      "};"
+   ], {esversion: 6});
+
   test.done();
 };