-
Notifications
You must be signed in to change notification settings - Fork 475
Make unsafe prefer-core-logging rewrites report-only #46588
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| import { AST_NODE_TYPES, ESLintUtils, TSESTree } from "@typescript-eslint/utils"; | ||
| import { AST_NODE_TYPES, ESLintUtils, TSESLint, TSESTree } from "@typescript-eslint/utils"; | ||
|
|
||
| const createRule = ESLintUtils.RuleCreator(name => `https://github.com/github/gh-aw/tree/main/eslint-factory#${name}`); | ||
|
|
||
|
|
@@ -30,6 +30,58 @@ function getConsoleMethod(node: TSESTree.CallExpression): string | null { | |
| return prop.name in CONSOLE_TO_CORE ? prop.name : null; | ||
| } | ||
|
|
||
| function getStaticStringValue(node: TSESTree.CallExpressionArgument): string | null { | ||
| if (node.type === AST_NODE_TYPES.Literal) { | ||
| return typeof node.value === "string" ? node.value : null; | ||
| } | ||
|
|
||
| if (node.type !== AST_NODE_TYPES.TemplateLiteral) { | ||
| return null; | ||
| } | ||
|
|
||
| if (node.expressions.length > 0) { | ||
| return null; | ||
| } | ||
|
|
||
| const parts: string[] = []; | ||
| for (const quasi of node.quasis) { | ||
| if (quasi.value.cooked === null) { | ||
| return null; | ||
| } | ||
| parts.push(quasi.value.cooked); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/diagnosing-bugs] The format specifier set 💡 Suggested fixif (value[i] === "%" && value[i - 1] !== "%" && "sdifojOc".includes(value[i + 1] ?? "")) {Add @copilot please address this. |
||
|
|
||
| return parts.join(""); | ||
| } | ||
|
|
||
| function hasConsoleFormatSpecifier(node: TSESTree.CallExpressionArgument | undefined): boolean { | ||
| const value = node ? getStaticStringValue(node) : null; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [/diagnosing-bugs] 💡 Suggested fixfunction canSuggestCoreReplacement(node: TSESTree.CallExpression): boolean {
const arg = node.arguments[0];
if (node.arguments.length !== 1 || !arg) return false;
if (arg.type === AST_NODE_TYPES.SpreadElement) return false; // single spread still multi-arg at runtime
return !isInterpolatedTemplateLiteral(arg) && !hasConsoleFormatSpecifier(arg);
}This is related to the existing Copilot comment on this PR but is the minimal fix that closes the gap. @copilot please address this. |
||
| if (value === null) { | ||
| return false; | ||
| } | ||
|
|
||
| for (let i = 0; i < value.length - 1; i++) { | ||
| if (value[i] === "%" && value[i - 1] !== "%" && "sdifojO".includes(value[i + 1] ?? "")) { | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| function isInterpolatedTemplateLiteral(node: TSESTree.CallExpressionArgument): boolean { | ||
| return node.type === AST_NODE_TYPES.TemplateLiteral && node.expressions.length > 0; | ||
| } | ||
|
|
||
| function canSuggestCoreReplacement(node: TSESTree.CallExpression): boolean { | ||
| const arg = node.arguments[0]; | ||
| if (node.arguments.length !== 1 || !arg) { | ||
| return false; | ||
| } | ||
|
Comment on lines
+78
to
+80
|
||
|
|
||
| return !isInterpolatedTemplateLiteral(arg) && !hasConsoleFormatSpecifier(arg); | ||
| } | ||
|
|
||
| export const preferCoreLoggingRule = createRule({ | ||
| name: "prefer-core-logging", | ||
| meta: { | ||
|
|
@@ -57,23 +109,23 @@ export const preferCoreLoggingRule = createRule({ | |
| if (!method) return; | ||
|
|
||
| const replacement = CONSOLE_TO_CORE[method]!; | ||
|
|
||
| // Build replacement argument text from original call | ||
| const argsText = node.arguments.map(arg => sourceCode.getText(arg)).join(", "); | ||
| const suggest = canSuggestCoreReplacement(node) | ||
| ? [ | ||
| { | ||
| messageId: "replaceWithCoreMethod" as const, | ||
| data: { replacement, args: sourceCode.getText(node.arguments[0]) }, | ||
| fix(fixer: TSESLint.RuleFixer) { | ||
| return fixer.replaceText(node, `${replacement}(${sourceCode.getText(node.arguments[0])})`); | ||
| }, | ||
| }, | ||
| ] | ||
| : undefined; | ||
|
|
||
| context.report({ | ||
| node, | ||
| messageId: "preferCoreLogging", | ||
| data: { method, replacement }, | ||
| suggest: [ | ||
| { | ||
| messageId: "replaceWithCoreMethod", | ||
| data: { replacement, args: argsText }, | ||
| fix(fixer) { | ||
| return fixer.replaceText(node, `${replacement}(${argsText})`); | ||
| }, | ||
| }, | ||
| ], | ||
| suggest, | ||
| }); | ||
| }, | ||
| }; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[/tdd] Missing test for zero-argument
console.log()— this is a valid call thatcanSuggestCoreReplacementshould classify as report-only (sincenode.arguments.length !== 1). Without a test, a future refactor could accidentally start suggestingcore.info()for it.💡 Suggested test
Add this to the "report-only" invalid test suite.
@copilot please address this.