Skip to content

Commit 427ac0a

Browse files
authored
fix: use format strings in debug calls (#21247)
* fix: use format strings in debug calls * tweaks * remove debug output for `ConfigLoader` class name * no suggestion if the template literal contains a format specifier * clarify comments * move the check for multiple arguments to the fixer function * remove sperfluous parameter
1 parent b3d876b commit 427ac0a

12 files changed

Lines changed: 516 additions & 45 deletions

File tree

‎docs/src/use/configure/debug.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ ESLint creates a configuration for each file that is linted based on your config
2525
This outputs all of ESLint's debugging information onto the console. You should copy this output to a file and then search for `eslint.config.js` to see which file is loaded. Here's some example output:
2626

2727
```text
28-
eslint:eslint Using file patterns: bin/eslint.js +0ms
28+
eslint:eslint Using file patterns: [ 'bin/eslint.js' ] +0ms
2929
eslint:eslint Searching for eslint.config.js +0ms
3030
eslint:eslint Loading config from C:\Users\nzakas\projects\eslint\eslint\eslint.config.js +5ms
3131
eslint:eslint Config file URL is file:///C:/Users/nzakas/projects/eslint/eslint/eslint.config.js +0ms

‎eslint.config.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ module.exports = defineConfig([
111111
},
112112
rules: {
113113
"internal-rules/multiline-comment-style": "error",
114+
"internal-rules/no-debug-template-literals": "error",
114115
},
115116
},
116117
{

‎lib/cli-engine/lint-result-cache.js‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ class LintResultCache {
8383
invalidCacheStrategyErrorMessage,
8484
);
8585

86-
debug(`Caching results to ${cacheFileLocation}`);
86+
debug("Caching results to %s", cacheFileLocation);
8787

8888
const useChecksum = cacheStrategy === "content";
8989

90-
debug(`Using "${cacheStrategy}" strategy to detect changes`);
90+
debug('Using "%s" strategy to detect changes', cacheStrategy);
9191

9292
this.fileEntryCache = fileEntryCache.create(
9393
cacheFileLocation,
@@ -124,7 +124,8 @@ class LintResultCache {
124124
// If source is present but null, need to reread the file from the filesystem.
125125
if (results.source === null) {
126126
debug(
127-
`Rereading cached result source from filesystem: ${filePath}`,
127+
"Rereading cached result source from filesystem: %s",
128+
filePath,
128129
);
129130
results.source = fs.readFileSync(filePath, "utf-8");
130131
}
@@ -152,7 +153,7 @@ class LintResultCache {
152153
const fileDescriptor = this.fileEntryCache.getFileDescriptor(filePath);
153154

154155
if (fileDescriptor.notFound) {
155-
debug(`File not found on the file system: ${filePath}`);
156+
debug("File not found on the file system: %s", filePath);
156157
return null;
157158
}
158159

@@ -162,7 +163,7 @@ class LintResultCache {
162163
fileDescriptor.meta.hashOfConfig !== hashOfConfig;
163164

164165
if (changed) {
165-
debug(`Cache entry not found or no longer valid: ${filePath}`);
166+
debug("Cache entry not found or no longer valid: %s", filePath);
166167
return null;
167168
}
168169

@@ -188,7 +189,7 @@ class LintResultCache {
188189
const fileDescriptor = this.fileEntryCache.getFileDescriptor(filePath);
189190

190191
if (fileDescriptor && !fileDescriptor.notFound) {
191-
debug(`Updating cached result: ${filePath}`);
192+
debug("Updating cached result: %s", filePath);
192193

193194
// Serialize the result, except that we want to remove the file source if present.
194195
const resultToSerialize = Object.assign({}, result);
@@ -212,7 +213,7 @@ class LintResultCache {
212213
* @returns {void}
213214
*/
214215
reconcile() {
215-
debug(`Persisting cached results: ${this.cacheFileLocation}`);
216+
debug("Persisting cached results: %s", this.cacheFileLocation);
216217
this.fileEntryCache.reconcile();
217218
}
218219
}

‎lib/cli.js‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ const cli = {
284284
return 0;
285285
}
286286

287-
debug(`Running on ${useStdin ? "text" : "files"}`);
287+
debug("Running on %s", useStdin ? "text" : "files");
288288

289289
if (options.fix && options.fixDryRun) {
290290
log.error(
@@ -463,7 +463,7 @@ const cli = {
463463
* Neither option was provided, `options.color` is omitted, so `undefined`.
464464
*/
465465
if (options.color !== void 0) {
466-
debug(`Color setting for output: ${options.color}`);
466+
debug("Color setting for output: %s", options.color);
467467
resultsMeta.color = options.color;
468468
}
469469

@@ -502,7 +502,7 @@ const cli = {
502502
log.error(
503503
"There are suppressions left that do not occur anymore. To resolve this, re-run the command with `--prune-suppressions` to remove unused suppressions. To ignore unused suppressions, use `--pass-on-unpruned-suppressions`.",
504504
);
505-
debug(JSON.stringify(unusedSuppressions, null, 2));
505+
debug("%O", unusedSuppressions);
506506

507507
return 2;
508508
}

‎lib/config/config-loader.js‎

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ async function dynamicImportConfig(filePath, fileURL, mtime) {
196196
* @returns {Promise<any>} The config loaded from the config file.
197197
*/
198198
async function loadConfigFile(filePath, hasUnstableNativeNodeJsTSConfigFlag) {
199-
debug(`Loading config from ${filePath}`);
199+
debug("Loading config from %s", filePath);
200200

201201
const fileURL = pathToFileURL(filePath);
202202

203-
debug(`Config file URL is ${fileURL}`);
203+
debug("Config file URL is %s", fileURL);
204204

205205
const mtime = (await fs.stat(filePath)).mtime.getTime();
206206

@@ -406,7 +406,7 @@ class ConfigLoader {
406406
async loadConfigArrayForFile(filePath) {
407407
assertValidFilePath(filePath);
408408

409-
debug(`Calculating config for file ${filePath}`);
409+
debug("Calculating config for file %s", filePath);
410410

411411
const configFilePath = await this.findConfigFileForPath(filePath);
412412

@@ -425,7 +425,7 @@ class ConfigLoader {
425425
async loadConfigArrayForDirectory(dirPath) {
426426
assertValidFilePath(dirPath);
427427

428-
debug(`Calculating config for directory ${dirPath}`);
428+
debug("Calculating config for directory %s", dirPath);
429429

430430
const absoluteDirPath = path.resolve(
431431
this.#options.cwd,
@@ -434,7 +434,11 @@ class ConfigLoader {
434434
const { configFilePath, basePath } =
435435
await this.#locateConfigFileToUse(absoluteDirPath);
436436

437-
debug(`Using config file ${configFilePath} and base path ${basePath}`);
437+
debug(
438+
"Using config file %s and base path %s",
439+
configFilePath,
440+
basePath,
441+
);
438442
return this.#calculateConfigArray(configFilePath, basePath);
439443
}
440444

@@ -452,7 +456,7 @@ class ConfigLoader {
452456
getCachedConfigArrayForFile(filePath) {
453457
assertValidFilePath(filePath);
454458

455-
debug(`Looking up cached config for ${filePath}`);
459+
debug("Looking up cached config for %s", filePath);
456460

457461
return this.getCachedConfigArrayForPath(path.dirname(filePath));
458462
}
@@ -471,7 +475,7 @@ class ConfigLoader {
471475
getCachedConfigArrayForPath(fileOrDirPath) {
472476
assertValidFilePath(fileOrDirPath);
473477

474-
debug(`Looking up cached config for ${fileOrDirPath}`);
478+
debug("Looking up cached config for %s", fileOrDirPath);
475479

476480
const absoluteDirPath = path.resolve(this.#options.cwd, fileOrDirPath);
477481

@@ -533,7 +537,7 @@ class ConfigLoader {
533537
let basePath = cwd;
534538

535539
if (typeof useConfigFile === "string") {
536-
debug(`Override config file path is ${useConfigFile}`);
540+
debug("Override config file path is %s", useConfigFile);
537541
configFilePath = path.resolve(cwd, useConfigFile);
538542
basePath = cwd;
539543
} else if (useConfigFile !== false) {
@@ -574,7 +578,9 @@ class ConfigLoader {
574578
} = options;
575579

576580
debug(
577-
`Calculating config array from config file ${configFilePath} and base path ${basePath}`,
581+
"Calculating config array from config file %s and base path %s",
582+
configFilePath,
583+
basePath,
578584
);
579585

580586
const configs = new FlatConfigArray(baseConfig || [], {
@@ -584,7 +590,7 @@ class ConfigLoader {
584590

585591
// load config file
586592
if (configFilePath) {
587-
debug(`Loading config file ${configFilePath}`);
593+
debug("Loading config file %s", configFilePath);
588594
const fileConfig = await loadConfigFile(
589595
configFilePath,
590596
hasUnstableNativeNodeJsTSConfigFlag,
@@ -602,14 +608,17 @@ class ConfigLoader {
602608
let emptyConfig = typeof fileConfig === "undefined";
603609

604610
debug(
605-
`Config file ${configFilePath} is ${emptyConfig ? "empty" : "not empty"}`,
611+
"Config file %s is %s",
612+
configFilePath,
613+
emptyConfig ? "empty" : "not empty",
606614
);
607615

608616
if (!emptyConfig) {
609617
if (Array.isArray(fileConfig)) {
610618
if (fileConfig.length === 0) {
611619
debug(
612-
`Config file ${configFilePath} is an empty array`,
620+
"Config file %s is an empty array",
621+
configFilePath,
613622
);
614623
emptyConfig = true;
615624
} else {
@@ -622,7 +631,8 @@ class ConfigLoader {
622631
Object.keys(fileConfig).length === 0
623632
) {
624633
debug(
625-
`Config file ${configFilePath} is an empty object`,
634+
"Config file %s is an empty object",
635+
configFilePath,
626636
);
627637
emptyConfig = true;
628638
} else {

‎lib/eslint/eslint-helpers.js‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,9 +1261,12 @@ async function lintFile(
12611261
cachedResult.messages && cachedResult.messages.length > 0;
12621262

12631263
if (hadMessages && fix) {
1264-
debug(`Reprocessing cached file to allow autofix: ${filePath}`);
1264+
debug(
1265+
"Reprocessing cached file to allow autofix: %s",
1266+
filePath,
1267+
);
12651268
} else {
1266-
debug(`Skipping file since it hasn't changed: ${filePath}`);
1269+
debug("Skipping file since it hasn't changed: %s", filePath);
12671270
return cachedResult;
12681271
}
12691272
}

‎lib/eslint/eslint.js‎

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -759,8 +759,6 @@ class ESLint {
759759
warningService,
760760
);
761761

762-
debug(`Using config loader ${this.#configLoader.constructor.name}`);
763-
764762
privateMembers.set(this, {
765763
options: processedOptions,
766764
linter,
@@ -1003,14 +1001,14 @@ class ESLint {
10031001
}
10041002
}
10051003

1006-
debug(`Using file patterns: ${normalizedPatterns}`);
1004+
debug("Using file patterns: %s", normalizedPatterns);
10071005

10081006
const { concurrency, cwd, globInputPaths, errorOnUnmatchedPattern } =
10091007
eslintOptions;
10101008

10111009
// Delete cache file; should this be done here?
10121010
if (!lintResultCache && cacheFilePath) {
1013-
debug(`Deleting cache file at ${cacheFilePath}`);
1011+
debug("Deleting cache file at %s", cacheFilePath);
10141012

10151013
try {
10161014
if (existsSync(cacheFilePath)) {
@@ -1046,7 +1044,7 @@ class ESLint {
10461044
filePaths,
10471045
);
10481046
if (workerCount) {
1049-
debug(`Linting using ${workerCount} worker thread(s).`);
1047+
debug("Linting using %d worker thread(s).", workerCount);
10501048
let poorConcurrencyNotice;
10511049
if (workerCount <= 2) {
10521050
poorConcurrencyNotice = "disable concurrency";
@@ -1069,7 +1067,7 @@ class ESLint {
10691067
),
10701068
);
10711069
} else {
1072-
debug(`Linting in single-thread mode.`);
1070+
debug("Linting in single-thread mode.");
10731071
results = await lintFilesWithoutMultithreading(this, filePaths);
10741072
}
10751073

‎lib/linter/code-path-analysis/code-path-analyzer.js‎

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
* @author Toru Nagashima
44
*/
55

6+
/* eslint internal-rules/no-debug-template-literals: ["error", { methods: ["dump"] }] -- Check calls to `debug.dump`. */
7+
68
"use strict";
79

810
//------------------------------------------------------------------------------
@@ -194,7 +196,7 @@ function forwardCurrentToHead(analyzer, node) {
194196
? "onCodePathSegmentEnd"
195197
: "onUnreachableCodePathSegmentEnd";
196198

197-
debug.dump(`${eventName} ${currentSegment.id}`);
199+
debug.dump("%s %s", eventName, currentSegment.id);
198200

199201
analyzer.emit(eventName, [currentSegment, node]);
200202
}
@@ -213,7 +215,7 @@ function forwardCurrentToHead(analyzer, node) {
213215
? "onCodePathSegmentStart"
214216
: "onUnreachableCodePathSegmentStart";
215217

216-
debug.dump(`${eventName} ${headSegment.id}`);
218+
debug.dump("%s %s", eventName, headSegment.id);
217219
CodePathSegment.markUsed(headSegment);
218220
analyzer.emit(eventName, [headSegment, node]);
219221
}
@@ -237,7 +239,7 @@ function leaveFromCurrentSegment(analyzer, node) {
237239
? "onCodePathSegmentEnd"
238240
: "onUnreachableCodePathSegmentEnd";
239241

240-
debug.dump(`${eventName} ${currentSegment.id}`);
242+
debug.dump("%s %s", eventName, currentSegment.id);
241243

242244
analyzer.emit(eventName, [currentSegment, node]);
243245
}
@@ -415,7 +417,7 @@ function processCodePathToEnter(analyzer, node) {
415417
state = CodePath.getState(codePath);
416418

417419
// Emits onCodePathStart events.
418-
debug.dump(`onCodePathStart ${codePath.id}`);
420+
debug.dump("onCodePathStart %s", codePath.id);
419421
analyzer.emit("onCodePathStart", [codePath, node]);
420422
}
421423

@@ -683,7 +685,7 @@ function postprocess(analyzer, node) {
683685
leaveFromCurrentSegment(analyzer, node);
684686

685687
// Emits onCodePathEnd event of this code path.
686-
debug.dump(`onCodePathEnd ${codePath.id}`);
688+
debug.dump("onCodePathEnd %s", codePath.id);
687689
analyzer.emit("onCodePathEnd", [codePath, node]);
688690
debug.dumpDot(codePath);
689691

@@ -817,7 +819,9 @@ class CodePathAnalyzer {
817819
onLooped(fromSegment, toSegment) {
818820
if (fromSegment.reachable && toSegment.reachable) {
819821
debug.dump(
820-
`onCodePathSegmentLoop ${fromSegment.id} -> ${toSegment.id}`,
822+
"onCodePathSegmentLoop %s -> %s",
823+
fromSegment.id,
824+
toSegment.id,
821825
);
822826
this.emit("onCodePathSegmentLoop", [
823827
fromSegment,

‎lib/linter/code-path-analysis/debug-helpers.js‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ module.exports = {
106106
}
107107

108108
debug(
109-
[
110-
`${state.currentSegments.map(getId).join(",")})`,
111-
`${node.type}${leaving ? ":exit" : ""}`,
112-
].join(" "),
109+
"%s) %s%s",
110+
state.currentSegments.map(getId).join(","),
111+
node.type,
112+
leaving ? ":exit" : "",
113113
);
114114
},
115115

‎lib/linter/linter.js‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,12 +1529,16 @@ class Linter {
15291529
}
15301530

15311531
debug(
1532-
`Linting code for ${debugTextDescription} (pass ${passNumber})`,
1532+
"Linting code for %s (pass %d)",
1533+
debugTextDescription,
1534+
passNumber,
15331535
);
15341536
messages = this.verify(currentText, config, options);
15351537

15361538
debug(
1537-
`Generating fixed text for ${debugTextDescription} (pass ${passNumber})`,
1539+
"Generating fixed text for %s (pass %d)",
1540+
debugTextDescription,
1541+
passNumber,
15381542
);
15391543
let t;
15401544

@@ -1589,7 +1593,8 @@ class Linter {
15891593
currentText === secondPreviousText
15901594
) {
15911595
debug(
1592-
`Circular fixes detected after pass ${passNumber}. Exiting fix loop.`,
1596+
"Circular fixes detected after pass %d. Exiting fix loop.",
1597+
passNumber,
15931598
);
15941599
slots.warningService.emitCircularFixesWarning(
15951600
options.filename ?? "text",

0 commit comments

Comments
 (0)