Skip to content

Commit

Permalink
Fix: report CallExpression node instead of MemberExpression node
Browse files Browse the repository at this point in the history
  • Loading branch information
archmoj committed Aug 27, 2021
1 parent ef39ca7 commit 083ad40
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
10 changes: 5 additions & 5 deletions lib/rules/no-new-func.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,26 +38,26 @@ module.exports = {
variable.references.forEach(ref => {
const node = ref.identifier;
const { parent } = node;
let isEval = false;
let evalNode;

if (parent) {
if (node === parent.callee && (
parent.type === "NewExpression" ||
parent.type === "CallExpression"
)) {
isEval = true;
evalNode = parent;
} else if (parent.type === "MemberExpression" && node === parent.object) {
const maybeCallee = parent.parent.type === "ChainExpression" ? parent.parent : parent;

if (maybeCallee.parent.type === "CallExpression" && maybeCallee.parent.callee === maybeCallee) {
isEval = true;
evalNode = parent.parent;
}
}
}

if (isEval) {
if (evalNode) {
context.report({
node: parent,
node: evalNode,
messageId: "noFunctionConstructor"
});
}
Expand Down
8 changes: 4 additions & 4 deletions tests/lib/rules/no-new-func.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,28 @@ ruleTester.run("no-new-func", rule, {
code: "var a = Function.call(null, \"b\", \"c\", \"return b+c\");",
errors: [{
messageId: "noFunctionConstructor",
type: "MemberExpression"
type: "CallExpression"
}]
},
{
code: "var a = Function.apply(null, [\"b\", \"c\", \"return b+c\"]);",
errors: [{
messageId: "noFunctionConstructor",
type: "MemberExpression"
type: "CallExpression"
}]
},
{
code: "var a = Function.bind(null, \"b\", \"c\", \"return b+c\")();",
errors: [{
messageId: "noFunctionConstructor",
type: "MemberExpression"
type: "CallExpression"
}]
},
{
code: "var a = Function.bind(null, \"b\", \"c\", \"return b+c\");",
errors: [{
messageId: "noFunctionConstructor",
type: "MemberExpression"
type: "CallExpression"
}]
},
{
Expand Down

0 comments on commit 083ad40

Please sign in to comment.