Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e3e4c41

Browse files
authored
Fix: fix false positives of no-new-func (#13333)
* Fix: fix false positives of no-new-func * add more tests * fix false positive * fix
1 parent 611c676 commit e3e4c41

File tree

2 files changed

+57
-20
lines changed

2 files changed

+57
-20
lines changed

lib/rules/no-new-func.js

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,26 +29,29 @@ module.exports = {
2929

3030
create(context) {
3131

32-
//--------------------------------------------------------------------------
33-
// Helpers
34-
//--------------------------------------------------------------------------
35-
36-
/**
37-
* Reports a node.
38-
* @param {ASTNode} node The node to report
39-
* @returns {void}
40-
* @private
41-
*/
42-
function report(node) {
43-
context.report({
44-
node,
45-
messageId: "noFunctionConstructor"
46-
});
47-
}
48-
4932
return {
50-
"NewExpression[callee.name = 'Function']": report,
51-
"CallExpression[callee.name = 'Function']": report
33+
"Program:exit"() {
34+
const globalScope = context.getScope();
35+
const variable = globalScope.set.get("Function");
36+
37+
if (variable && variable.defs.length === 0) {
38+
variable.references.forEach(ref => {
39+
const node = ref.identifier;
40+
const { parent } = node;
41+
42+
if (
43+
parent &&
44+
(parent.type === "NewExpression" || parent.type === "CallExpression") &&
45+
node === parent.callee
46+
) {
47+
context.report({
48+
node: parent,
49+
messageId: "noFunctionConstructor"
50+
});
51+
}
52+
});
53+
}
54+
}
5255
};
5356

5457
}

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,24 @@ const ruleTester = new RuleTester();
2121
ruleTester.run("no-new-func", rule, {
2222
valid: [
2323
"var a = new _function(\"b\", \"c\", \"return b+c\");",
24-
"var a = _function(\"b\", \"c\", \"return b+c\");"
24+
"var a = _function(\"b\", \"c\", \"return b+c\");",
25+
{
26+
code: "class Function {}; new Function()",
27+
parserOptions: {
28+
ecmaVersion: 2015
29+
}
30+
},
31+
{
32+
code: "const fn = () => { class Function {}; new Function() }",
33+
parserOptions: {
34+
ecmaVersion: 2015
35+
}
36+
},
37+
"function Function() {}; Function()",
38+
"var fn = function () { function Function() {}; Function() }",
39+
"var x = function Function() { Function(); }",
40+
"call(Function)",
41+
"new Class(Function)"
2542
],
2643
invalid: [
2744
{
@@ -37,6 +54,23 @@ ruleTester.run("no-new-func", rule, {
3754
messageId: "noFunctionConstructor",
3855
type: "CallExpression"
3956
}]
57+
},
58+
{
59+
code: "const fn = () => { class Function {} }; new Function('', '')",
60+
parserOptions: {
61+
ecmaVersion: 2015
62+
},
63+
errors: [{
64+
messageId: "noFunctionConstructor",
65+
type: "NewExpression"
66+
}]
67+
},
68+
{
69+
code: "var fn = function () { function Function() {} }; Function('', '')",
70+
errors: [{
71+
messageId: "noFunctionConstructor",
72+
type: "CallExpression"
73+
}]
4074
}
4175
]
4276
});

0 commit comments

Comments
 (0)