Skip to content

eslint-factory: prefer-core-logging offers an unsafe autofix for non-string console.* arguments #46994

Description

@github-actions

Rule

prefer-core-logging (eslint-factory/src/rules/prefer-core-logging.ts)

The rule correctly reports console.log/info/debug and suggests core.info/core.debug. The report is fine; the autofix suggestion is not behavior-preserving for non-string arguments.

console.log(user);        // suggests: core.info(user)
console.log({ id: 1 });   // suggests: core.info({ id: 1 })
console.log(count);       // suggests: core.info(count)

console.log(obj) runs the value through util.inspect, printing a readable representation. core.info has signature info(message: string) and effectively does process.stdout.write(message + EOL), coercing a non-string to "[object Object]", "undefined", etc. Applying the suggestion silently degrades diagnostic output for a very common logging pattern (objects, arrays, numbers, identifiers of unknown type).

Root cause: canSuggestCoreReplacement (rule lines ~76-83) authorizes the suggestion for any single argument that is not an interpolated template literal and not a console format string. It does not require the argument to be statically string-typed, so identifiers/objects/numbers get a fix offered.

Existing tests

Suggestion tests only cover string literals / no-expression templates / multi-arg (suppressed) cases. No test asserts that a non-string single argument is reported without an unsafe fix.

Proposed refinement

Keep reporting preferCoreLogging for all matched console.* calls, but only attach the replaceWithCoreMethod suggestion when the single argument is statically known to be a string:

  • string Literal, or
  • TemplateLiteral with no expressions (already the safe subset).

For identifiers / objects / member expressions / numeric literals, report without a suggestion (the author decides how to stringify). This mirrors the existing conservative handling of interpolated templates and format specifiers.

Acceptance criteria

  • console.log("ready") → reported with the core.info("ready") suggestion (unchanged).
  • console.log(user) → reported without any suggestion.
  • console.log({ id: 1 }) and console.log(count) → reported without any suggestion.
  • Existing valid/invalid tests remain unchanged.
  • npm test passes.

Grounding

No live console.* sites in non-test actions/setup/js/*.cjs today, so this is regression-guard hardening. Confidence: medium-high — the semantic difference between console.log(obj) and core.info(obj) is concrete and the fix is a small tightening of canSuggestCoreReplacement.

Generated by 🤖 ESLint Refiner · 455.7 AIC · ⌖ 10.8 AIC · ⊞ 4.6K ·

  • expires on Jul 27, 2026, 10:19 PM UTC-08:00

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions