@book000/eslint-config 1.17.13 → 1.17.15

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/AGENTS.md CHANGED
@@ -27,7 +27,7 @@ pnpm install
27
27
  pnpm test
28
28
  ```
29
29
 
30
- `pnpm test` runs `node --experimental-vm-modules test.mjs`.
30
+ `pnpm test` runs `node --experimental-vm-modules test.mjs`, then checks malformed-fixture failure behavior in an isolated temporary directory.
31
31
  It creates `src/`, `tsconfig.json` and `eslint.config.mjs` in the repository root, lints one temp file per test case in a single ESLint process, and removes the temp files, `tsconfig.json` and `eslint.config.mjs` afterwards (an empty `src/` is left behind).
32
32
  The script exits with a non-zero code when any case fails, and the summary prints the `Failed:` count (failed cases are marked with `❌`).
33
33
  There is no separate lint or build step.
@@ -0,0 +1,30 @@
1
+ /**
2
+ * Classifies the ESLint messages of one fixture file and decides whether the case passes.
3
+ * A fatal message (parse error) or a rule-less error never counts as a rule result,
4
+ * so a malformed fixture cannot pass a `shouldError: false` case or satisfy `shouldError: true`.
5
+ * @param {Array<{line: number, column: number, severity: number, message: string, ruleId?: string | null, fatal?: boolean}>} messages ESLint messages for the file.
6
+ * @param {string[]} rules Rule IDs the case targets.
7
+ * @param {boolean} shouldError Whether the case expects an error from the target rules.
8
+ * @returns {{passed: boolean, errorCount: number, relevantErrors: string[], ignoredErrors: string[], fatalErrors: string[]}} The classified messages and the verdict.
9
+ */
10
+ export function evaluateMessages(messages, rules, shouldError) {
11
+ let errorCount = 0;
12
+ const relevantErrors = [];
13
+ const ignoredErrors = [];
14
+ const fatalErrors = [];
15
+ for (const message of messages) {
16
+ const severityLabel = message.severity === 2 ? "error" : "warning";
17
+ const line = `[${severityLabel}] ${message.line}:${message.column} ${message.message} ${message.ruleId ?? ""}`;
18
+ if (message.fatal || (!message.ruleId && message.severity === 2)) {
19
+ fatalErrors.push(line);
20
+ } else if (message.ruleId && rules.includes(message.ruleId)) {
21
+ errorCount = 1;
22
+ relevantErrors.push(line);
23
+ } else {
24
+ ignoredErrors.push(line);
25
+ }
26
+ }
27
+ const passed =
28
+ fatalErrors.length === 0 && (shouldError ? errorCount > 0 : errorCount === 0);
29
+ return { passed, errorCount, relevantErrors, ignoredErrors, fatalErrors };
30
+ }
@@ -0,0 +1,27 @@
1
+ // Unit tests for evaluateMessages: a fatal diagnostic must fail a case whatever shouldError is.
2
+ import assert from "node:assert/strict";
3
+ import { evaluateMessages } from "./evaluate-messages.mjs";
4
+
5
+ const rules = ["unicorn/some-rule"];
6
+ const parseError = { line: 1, column: 8, severity: 2, message: "Parsing error", ruleId: null, fatal: true };
7
+ const ruleError = { line: 1, column: 1, severity: 2, message: "bad", ruleId: "unicorn/some-rule" };
8
+ const otherError = { line: 1, column: 1, severity: 2, message: "other", ruleId: "no-var" };
9
+
10
+ const cases = [
11
+ ["clean file, shouldError false", [], false, true],
12
+ ["clean file, shouldError true", [], true, false],
13
+ ["target rule error, shouldError true", [ruleError], true, true],
14
+ ["target rule error, shouldError false", [ruleError], false, false],
15
+ ["other rule error only, shouldError false", [otherError], false, true],
16
+ ["fatal only, shouldError false", [parseError], false, false],
17
+ ["fatal only, shouldError true", [parseError], true, false],
18
+ ["fatal plus target rule error, shouldError true", [parseError, ruleError], true, false],
19
+ ["rule-less error without fatal flag, shouldError false", [{ ...parseError, fatal: false }], false, false],
20
+ ["rule-less warning is ignored, shouldError false", [{ ...parseError, fatal: false, severity: 1 }], false, true],
21
+ ];
22
+ for (const [name, messages, shouldError, expected] of cases) {
23
+ const result = evaluateMessages(messages, rules, shouldError);
24
+ assert.equal(result.passed, expected, name);
25
+ }
26
+ assert.equal(evaluateMessages([parseError], rules, false).fatalErrors.length, 1);
27
+ console.log(`✅ evaluateMessages: ${cases.length} cases`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@book000/eslint-config",
3
- "version": "1.17.13",
3
+ "version": "1.17.15",
4
4
  "description": "ESLint config",
5
5
  "keywords": [
6
6
  "eslint-config"
@@ -31,6 +31,6 @@
31
31
  "eslint": "^10.4.0"
32
32
  },
33
33
  "scripts": {
34
- "test": "node --experimental-vm-modules test.mjs"
34
+ "test": "node evaluate-messages.test.mjs && node --experimental-vm-modules test.mjs"
35
35
  }
36
36
  }
package/test.mjs CHANGED
@@ -5,6 +5,7 @@ import unicorn from "eslint-plugin-unicorn";
5
5
  import fs from "fs";
6
6
  import path from "path";
7
7
  import { promisify } from "util";
8
+ import { evaluateMessages } from "./evaluate-messages.mjs";
8
9
 
9
10
  const execFileAsync = promisify(execFile);
10
11
 
@@ -97,7 +98,7 @@ async function checkUnicornDrift() {
97
98
  };
98
99
  }
99
100
 
100
- async function main() {
101
+ async function main(cleanups) {
101
102
  const testCases = [
102
103
  {
103
104
  name: "prefer-top-level-await: top-level await is allowed",
@@ -432,6 +433,8 @@ async function main() {
432
433
  const tmpDir = path.join(srcDir, "__tmp__cli");
433
434
  if (!fs.existsSync(srcDir)) fs.mkdirSync(srcDir);
434
435
  if (!fs.existsSync(tmpDir)) fs.mkdirSync(tmpDir);
436
+ // Registered up front so a thrown error (failed ESLint run, missing result) still cleans up.
437
+ cleanups.push(() => fs.rmSync(tmpDir, { recursive: true, force: true }));
435
438
 
436
439
  // Create each directory named by a test case's dir (under src) so the
437
440
  // unicorn/filename-case directory name check can be verified.
@@ -442,16 +445,16 @@ async function main() {
442
445
  );
443
446
  // Only directories created by this test are removed during cleanup,
444
447
  // so an existing src/__tests__ etc. is never deleted by mistake.
445
- const createdExtraDirs = new Set();
446
448
  for (const dir of extraDirs) {
447
449
  const dirPath = path.join(srcDir, dir);
448
450
  if (!fs.existsSync(dirPath)) {
449
451
  fs.mkdirSync(dirPath, { recursive: true });
450
- createdExtraDirs.add(dir);
452
+ cleanups.push(() => fs.rmSync(dirPath, { recursive: true, force: true }));
451
453
  }
452
454
  }
453
455
 
454
456
  const tsconfigPath = path.join(process.cwd(), "tsconfig.json");
457
+ cleanups.push(() => fs.rmSync(tsconfigPath, { force: true }));
455
458
  fs.writeFileSync(
456
459
  tsconfigPath,
457
460
  JSON.stringify(
@@ -473,6 +476,7 @@ async function main() {
473
476
 
474
477
  // Temporarily copy index.mjs to eslint.config.mjs so ESLint picks it up as the flat config
475
478
  const flatConfigPath = path.join(process.cwd(), "eslint.config.mjs");
479
+ cleanups.push(() => fs.rmSync(flatConfigPath, { force: true }));
476
480
  fs.copyFileSync(path.join(process.cwd(), "index.mjs"), flatConfigPath);
477
481
 
478
482
  // Write all temp files first; ESLint is started only once afterwards
@@ -512,10 +516,8 @@ async function main() {
512
516
  );
513
517
  jsonOutput = stdout;
514
518
  } catch (error) {
515
- // ESLint exits non-zero when a test case has the expected errors, which is normal here,
516
- // so treat it as success as long as JSON was written to stdout.
517
- // If stdout has no JSON (e.g. a config error), the later JSON.parse throws.
518
- if (error.stdout) {
519
+ // Exit 1 represents lint diagnostics; exit 2 represents a failed ESLint run.
520
+ if (error.code === 1 && error.stdout) {
519
521
  jsonOutput = error.stdout;
520
522
  } else {
521
523
  throw error;
@@ -545,43 +547,34 @@ async function main() {
545
547
  );
546
548
  }
547
549
 
548
- let errorCount = 0;
549
- const relevantErrors = [];
550
- const ignoredErrors = [];
551
- for (const message of fileResult.messages) {
552
- const severityLabel = message.severity === 2 ? "error" : "warning";
553
- const line = `[${severityLabel}] ${message.line}:${message.column} ${message.message} ${message.ruleId ?? ""}`;
554
- if (message.ruleId && rules.includes(message.ruleId)) {
555
- errorCount = 1;
556
- relevantErrors.push(line);
557
- } else {
558
- ignoredErrors.push(line);
559
- }
560
- }
561
-
562
- fs.unlinkSync(tmpFilePath);
563
- return { name, shouldError, errorCount, relevantErrors, ignoredErrors };
550
+ const evaluation = evaluateMessages(fileResult.messages, rules, shouldError);
551
+ return { name, shouldError, ...evaluation };
564
552
  });
565
553
  let pass = 0,
566
554
  fail = 0;
567
555
  for (const {
568
556
  name,
569
557
  shouldError,
570
- errorCount,
558
+ passed,
571
559
  relevantErrors,
572
560
  ignoredErrors,
561
+ fatalErrors,
573
562
  } of results) {
574
- if (shouldError ? errorCount > 0 : errorCount === 0) {
563
+ if (passed) {
575
564
  console.log(`✅ ${name}`);
576
565
  pass++;
577
566
  } else {
578
567
  console.error(
579
568
  `❌ ${name} (expected: ${shouldError ? "error" : "OK"}, actual: ${
580
- errorCount > 0 ? "error" : "OK"
569
+ fatalErrors.length > 0 ? "fatal error" : relevantErrors.length > 0 ? "error" : "OK"
581
570
  })`
582
571
  );
583
572
  fail++;
584
573
  }
574
+ if (fatalErrors.length > 0) {
575
+ console.error(" Fatal or configuration errors:");
576
+ fatalErrors.forEach((e) => console.error(` ${e}`));
577
+ }
585
578
  if (relevantErrors && relevantErrors.length > 0) {
586
579
  console.error(` Errors from the target rules:`);
587
580
  relevantErrors.forEach((e) => console.error(` ${e}`));
@@ -592,12 +585,6 @@ async function main() {
592
585
  }
593
586
  }
594
587
 
595
- fs.unlinkSync(flatConfigPath);
596
- fs.rmSync(tmpDir, { recursive: true });
597
- for (const dir of createdExtraDirs) {
598
- fs.rmSync(path.join(srcDir, dir), { recursive: true });
599
- }
600
- fs.unlinkSync(tsconfigPath);
601
588
 
602
589
  const drift = await checkUnicornDrift();
603
590
  const driftName =
@@ -624,7 +611,12 @@ async function main() {
624
611
  }
625
612
  }
626
613
 
627
- main().catch((e) => {
628
- console.error(e);
629
- process.exit(1);
630
- });
614
+ const cleanups = [];
615
+ main(cleanups)
616
+ .catch((e) => {
617
+ console.error(e);
618
+ process.exitCode = 1;
619
+ })
620
+ .finally(() => {
621
+ for (const cleanup of cleanups.reverse()) cleanup();
622
+ });