Skip to content

Commit c6cc6c5

Browse files
authored
feat: check Object.prototype property names in new-cap (#21269)
1 parent 1c73469 commit c6cc6c5

2 files changed

Lines changed: 120 additions & 29 deletions

File tree

‎lib/rules/new-cap.js‎

Lines changed: 8 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,30 +29,6 @@ const CAPS_ALLOWED = [
2929
"BigInt",
3030
];
3131

32-
/**
33-
* A reducer function to invert an array to an Object mapping the string form of the key, to `true`.
34-
* @param {Object} map Accumulator object for the reduce.
35-
* @param {string} key Object key to set to `true`.
36-
* @returns {Object} Returns the updated Object for further reduction.
37-
*/
38-
function invert(map, key) {
39-
map[key] = true;
40-
return map;
41-
}
42-
43-
/**
44-
* Creates an object with the cap is new exceptions as its keys and true as their values.
45-
* @param {Object} config Rule configuration
46-
* @returns {Object} Object with cap is new exceptions.
47-
*/
48-
function calculateCapIsNewExceptions(config) {
49-
const capIsNewExceptions = Array.from(
50-
new Set([...config.capIsNewExceptions, ...CAPS_ALLOWED]),
51-
);
52-
53-
return capIsNewExceptions.reduce(invert, {});
54-
}
55-
5632
//------------------------------------------------------------------------------
5733
// Rule Definition
5834
//------------------------------------------------------------------------------
@@ -125,12 +101,15 @@ module.exports = {
125101
const [config] = context.options;
126102
const skipProperties = !config.properties;
127103

128-
const newIsCapExceptions = config.newIsCapExceptions.reduce(invert, {});
104+
const newIsCapExceptions = new Set(config.newIsCapExceptions);
129105
const newIsCapExceptionPattern = config.newIsCapExceptionPattern
130106
? new RegExp(config.newIsCapExceptionPattern, "u")
131107
: null;
132108

133-
const capIsNewExceptions = calculateCapIsNewExceptions(config);
109+
const capIsNewExceptions = new Set([
110+
...config.capIsNewExceptions,
111+
...CAPS_ALLOWED,
112+
]);
134113
const capIsNewExceptionPattern = config.capIsNewExceptionPattern
135114
? new RegExp(config.capIsNewExceptionPattern, "u")
136115
: null;
@@ -178,16 +157,16 @@ module.exports = {
178157

179158
/**
180159
* Check if capitalization is allowed for a CallExpression
181-
* @param {Object} allowedMap Object mapping calleeName to a Boolean
160+
* @param {Set<string>} allowedNames Set of allowed callee names
182161
* @param {ASTNode} node CallExpression node
183162
* @param {string} calleeName Capitalized callee name from a CallExpression
184163
* @param {Object} pattern RegExp object from options pattern
185164
* @returns {boolean} Returns true if the callee may be capitalized
186165
*/
187-
function isCapAllowed(allowedMap, node, calleeName, pattern) {
166+
function isCapAllowed(allowedNames, node, calleeName, pattern) {
188167
const sourceText = sourceCode.getText(node.callee);
189168

190-
if (allowedMap[calleeName] || allowedMap[sourceText]) {
169+
if (allowedNames.has(calleeName) || allowedNames.has(sourceText)) {
191170
return true;
192171
}
193172

‎tests/lib/rules/new-cap.js‎

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,34 @@ ruleTester.run("new-cap", rule, {
140140
code: "(Date?.UTC)();",
141141
languageOptions: { ecmaVersion: 2020 },
142142
},
143+
{
144+
code: "const x = new toString();",
145+
options: [{ newIsCapExceptions: ["toString"] }],
146+
},
147+
{
148+
code: "const x = new constructor();",
149+
options: [{ newIsCapExceptions: ["constructor"] }],
150+
},
151+
{
152+
code: "const x = new hasOwnProperty();",
153+
options: [{ newIsCapExceptions: ["hasOwnProperty"] }],
154+
},
155+
{
156+
code: "const x = new isPrototypeOf();",
157+
options: [{ newIsCapExceptions: ["isPrototypeOf"] }],
158+
},
159+
{
160+
code: "const x = new propertyIsEnumerable();",
161+
options: [{ newIsCapExceptions: ["propertyIsEnumerable"] }],
162+
},
163+
{
164+
code: "const x = new valueOf();",
165+
options: [{ newIsCapExceptions: ["valueOf"] }],
166+
},
167+
{
168+
code: "const x = new toLocaleString();",
169+
options: [{ newIsCapExceptions: ["toLocaleString"] }],
170+
},
143171
],
144172
invalid: [
145173
{
@@ -405,5 +433,89 @@ ruleTester.run("new-cap", rule, {
405433
languageOptions: { ecmaVersion: 2020 },
406434
errors: [{ messageId: "upper", column: 7, endColumn: 10 }],
407435
},
436+
{
437+
code: "const x = new toString();",
438+
errors: [
439+
{
440+
messageId: "lower",
441+
line: 1,
442+
column: 15,
443+
endLine: 1,
444+
endColumn: 23,
445+
},
446+
],
447+
},
448+
{
449+
code: "const x = new constructor();",
450+
errors: [
451+
{
452+
messageId: "lower",
453+
line: 1,
454+
column: 15,
455+
endLine: 1,
456+
endColumn: 26,
457+
},
458+
],
459+
},
460+
{
461+
code: "const x = new hasOwnProperty();",
462+
errors: [
463+
{
464+
messageId: "lower",
465+
line: 1,
466+
column: 15,
467+
endLine: 1,
468+
endColumn: 29,
469+
},
470+
],
471+
},
472+
{
473+
code: "const x = new isPrototypeOf();",
474+
errors: [
475+
{
476+
messageId: "lower",
477+
line: 1,
478+
column: 15,
479+
endLine: 1,
480+
endColumn: 28,
481+
},
482+
],
483+
},
484+
{
485+
code: "const x = new propertyIsEnumerable();",
486+
errors: [
487+
{
488+
messageId: "lower",
489+
line: 1,
490+
column: 15,
491+
endLine: 1,
492+
endColumn: 35,
493+
},
494+
],
495+
},
496+
{
497+
code: "const x = new valueOf();",
498+
errors: [
499+
{
500+
messageId: "lower",
501+
line: 1,
502+
column: 15,
503+
endLine: 1,
504+
endColumn: 22,
505+
},
506+
],
507+
},
508+
{
509+
code: "const x = new toLocaleString();",
510+
errors: [
511+
{
512+
messageId: "lower",
513+
line: 1,
514+
column: 15,
515+
endLine: 1,
516+
endColumn: 29,
517+
},
518+
],
519+
},
408520
],
409521
});

0 commit comments

Comments
 (0)