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

feat: Add suggestions to no-console #17680

Merged
merged 12 commits into from Nov 17, 2023
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
76 changes: 74 additions & 2 deletions lib/rules/no-console.js
Expand Up @@ -43,8 +43,11 @@ module.exports = {
}
],

hasSuggestions: true,

messages: {
unexpected: "Unexpected console statement."
unexpected: "Unexpected console statement.",
removeConsole: "Remove the console.{{ propertyName }}()."
}
},

Expand Down Expand Up @@ -94,6 +97,64 @@ module.exports = {
);
}

/**
* Checks if removing the ExpressionStatement node will cause ASI to
* break.
* eg.
* foo()
* console.log();
* [1, 2, 3].forEach(a => doSomething(a))
*
* Removing the console.log(); statement should leave two statements, but
* here the two statements will become one because [ causes continuation after
* foo().
* @param {ASTNode} node The ExpressionStatement node to check.
* @returns {boolean} `true` if ASI will break after removing the ExpressionStatement
* node.
*/
function maybeAsiHazard(node) {
const SAFE_TOKENS_BEFORE = /^[:;{]$/u; // One of :;{
const UNSAFE_CHARS_AFTER = /^[-[(/+`]/u; // One of [(/+-`

const tokenBefore = sourceCode.getTokenBefore(node);
const tokenAfter = sourceCode.getTokenAfter(node);

return (
Boolean(tokenAfter) &&
UNSAFE_CHARS_AFTER.test(tokenAfter.value) &&
tokenAfter.value !== "++" &&
tokenAfter.value !== "--" &&
Boolean(tokenBefore) &&
!SAFE_TOKENS_BEFORE.test(tokenBefore.value)
);
}

/**
* 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 a suggestion can be provided for a node.
*/
function canProvideSuggestions(node) {
Rec0iL99 marked this conversation as resolved.
Show resolved Hide resolved
return (
node.parent.type === "CallExpression" &&
node.parent.callee === node &&
node.parent.parent.type === "ExpressionStatement" &&
astUtils.STATEMENT_LIST_PARENTS.has(node.parent.parent.parent.type) &&
!maybeAsiHazard(node.parent.parent)
);
}

/**
* Reports the given reference as a violation.
* @param {eslint-scope.Reference} reference The reference to report.
Expand All @@ -102,10 +163,21 @@ module.exports = {
function report(reference) {
const node = reference.identifier.parent;

const propertyName = astUtils.getStaticPropertyName(node);

context.report({
node,
loc: node.loc,
messageId: "unexpected"
messageId: "unexpected",
suggest: canProvideSuggestions(node)
? [{
messageId: "removeConsole",
data: { propertyName },
fix(fixer) {
return fixer.remove(node.parent.parent);
Rec0iL99 marked this conversation as resolved.
Show resolved Hide resolved
}
}]
: []
});
}

Expand Down