Skip to content

Commit

Permalink
[[FIX]] Allow built-in method names in classes
Browse files Browse the repository at this point in the history
  • Loading branch information
jugglinmike authored and rwaldron committed Feb 20, 2019
1 parent 668c4a3 commit b0c224b
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/jshint.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 === "{") {
Expand Down
81 changes: 81 additions & 0 deletions tests/unit/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -6679,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();
};

Expand Down

0 comments on commit b0c224b

Please sign in to comment.