8000 fix: do not report on shadowed constructors in `no-new-wrappers` (#17… · eslint/eslint@631648e · GitHub
[go: up one dir, main page]

Skip to content

Commit 631648e

Browse files
authored
fix: do not report on shadowed constructors in no-new-wrappers (#17447)
* fix: do not report on shadowed constructors in `no-n 10000 ew-wrappers` * unit tests for undefined constructors
1 parent 757bfe1 commit 631648e

File tree

2 files changed

+67
-8
lines changed

2 files changed

+67
-8
lines changed

lib/rules/no-new-wrappers.js

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
"use strict";
77

8+
//------------------------------------------------------------------------------
9+
// Requirements
10+
//------------------------------------------------------------------------------
11+
12+
const { getVariableByName } = require("./utils/ast-utils");
13+
814
//------------------------------------------------------------------------------
915
// Rule Definition
1016
//------------------------------------------------------------------------------
@@ -28,18 +34,24 @@ module.exports = {
2834
},
2935

3036
create(context) {
37+
const { sourceCode } = context;
3138

3239
return {
3340

3441
NewExpression(node) {
3542
const wrapperObjects = ["String", "Number", "Boolean"];
36-
37-
if (wrapperObjects.includes(node.callee.name)) {
38-
context.report({
39-
node,
40-
messageId: "noConstructor",
41-
data: { fn: node.callee.name }
42-
});
43+
const { name } = node.callee;
44+
45+
if (wrapperObjects.includes(name)) {
46+
const variable = getVariableByName(sourceCode.getScope(node), name);
47+
48+
if (variable && variable.identifiers.length === 0) {
49+
context.report({
50+
node,
51+
messageId: "noConstructor",
52+
data: { fn: name }
53+
});
54+
}
4355
}
4456
}
4557
};

tests/lib/rules/no-new-wrappers.js

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,36 @@ const ruleTester = new RuleTester();
2121
ruleTester.run("no-new-wrappers", rule, {
2222
valid: [
2323
"var a = new Object();",
24-
"var a = String('test'), b = String.fromCharCode(32);"
24+
"var a = String('test'), b = String.fromCharCode(32);",
25+
`
26+
function test(Number) {
27+
return new Number;
28+
}
29+
`,
30+
{
31+
code: `
32+
import String from "./string";
33+
const str = new String(42);
34+
`,
35+
parserOptions: { ecmaVersion: 6, sourceType: "module" }
36+
},
37+
`
38+
if (foo) {
39+
result = new Boolean(bar);
40+
} else {
41+
var Boolean = CustomBoolean;
42+
}
43+
`,
44+
{
45+
code: "new String()",
46+
globals: {
47+
String: "off"
48+
}
49+
},
50+
`
51+
/* global Boolean:off */
52+
assert(new Boolean);
53+
`
2554
],
2655
invalid: [
2756
{
@@ -53,6 +82,24 @@ ruleTester.run("no-new-wrappers", rule, {
5382
},
5483
type: "NewExpression"
5584
}]
85+
},
86+
{
87+
code: `
88+
const a = new String('bar');
89+
{
90+
const String = CustomString;
91+
const b = new String('foo');
92+
}
93+
`,
94+
parserOptions: { ecmaVersion: 6 },
95+
errors: [{
96+
messageId: "noConstructor",
97+
data: {
98+
fn: "String"
99+
},
100+
type: "NewExpression",
101+
line: 2
102+
}]
56103
}
57104
]
58105
});

0 commit comments

Comments
 (0)
0