Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: fix false positives of no-new-func #13333

Merged
merged 4 commits into from May 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
41 changes: 22 additions & 19 deletions lib/rules/no-new-func.js
Expand Up @@ -29,26 +29,29 @@ module.exports = {

create(context) {

//--------------------------------------------------------------------------
// Helpers
//--------------------------------------------------------------------------

/**
* Reports a node.
* @param {ASTNode} node The node to report
* @returns {void}
* @private
*/
function report(node) {
context.report({
node,
messageId: "noFunctionConstructor"
});
}

return {
"NewExpression[callee.name = 'Function']": report,
"CallExpression[callee.name = 'Function']": report
"Program:exit"() {
const globalScope = context.getScope();
const variable = globalScope.set.get("Function");

if (variable && variable.defs.length === 0) {
variable.references.forEach(ref => {
const node = ref.identifier;
const { parent } = node;

if (
parent &&
(parent.type === "NewExpression" || parent.type === "CallExpression") &&
node === parent.callee
) {
context.report({
node: parent,
messageId: "noFunctionConstructor"
});
}
});
}
}
};

}
Expand Down
36 changes: 35 additions & 1 deletion tests/lib/rules/no-new-func.js
Expand Up @@ -21,7 +21,24 @@ const ruleTester = new RuleTester();
ruleTester.run("no-new-func", rule, {
valid: [
"var a = new _function(\"b\", \"c\", \"return b+c\");",
"var a = _function(\"b\", \"c\", \"return b+c\");"
"var a = _function(\"b\", \"c\", \"return b+c\");",
{
code: "class Function {}; new Function()",
parserOptions: {
ecmaVersion: 2015
}
},
mdjermanovic marked this conversation as resolved.
Show resolved Hide resolved
{
code: "const fn = () => { class Function {}; new Function() }",
parserOptions: {
ecmaVersion: 2015
}
},
"function Function() {}; Function()",
"var fn = function () { function Function() {}; Function() }",
"var x = function Function() { Function(); }",
"call(Function)",
"new Class(Function)"
],
invalid: [
{
Expand All @@ -37,6 +54,23 @@ ruleTester.run("no-new-func", rule, {
messageId: "noFunctionConstructor",
type: "CallExpression"
}]
},
{
code: "const fn = () => { class Function {} }; new Function('', '')",
parserOptions: {
ecmaVersion: 2015
},
errors: [{
messageId: "noFunctionConstructor",
type: "NewExpression"
}]
},
{
code: "var fn = function () { function Function() {} }; Function('', '')",
errors: [{
messageId: "noFunctionConstructor",
type: "CallExpression"
}]
}
]
});