diff --git a/lib/rules/no-console.js b/lib/rules/no-console.js index af8b4e7c139b..78193f5b0792 100644 --- a/lib/rules/no-console.js +++ b/lib/rules/no-console.js @@ -98,11 +98,22 @@ module.exports = { } /** - * Checks if it is safe to provide suggestions for the specific node. + * Checks if the MemberExpression node's parent.parent.parent is a + * Program, BlockStatement, StaticBlock, or SwitchCase node. This check + * is necessary to avoid providing a suggestion that might cause a syntax error. + * + * eg. if (a) console.log(b), removing console.log() here will lead to a + * syntax error. + * if (a) { console.log(b) }, removing console.log() here is acceptable. + * + * Additionally, it checks if the callee of the CallExpression node is + * the node itself. + * + * eg. foo(console.log), cannot provide a suggestion here. * @param {ASTNode} node The MemberExpression node to check. - * @returns {boolean} `true` if the node is safe to be fixed via a suggestion + * @returns {boolean} `true` if a suggestion can be provided for a node. */ - function isSafeToProvideSuggestions(node) { + function canProvideSuggestions(node) { const isInStatementListParents = astUtils.STATEMENT_LIST_PARENTS.has(node.parent.parent.parent.type); return ( @@ -127,7 +138,7 @@ module.exports = { node, loc: node.loc, messageId: "unexpected", - suggest: isSafeToProvideSuggestions(node) + suggest: canProvideSuggestions(node) ? [{ messageId: "removeConsole", data: { propertyName },