Skip to content

Commit f2f9d75

Browse files
authored
fix: crash in jsonc/auto (#332)
1 parent f3ba02f commit f2f9d75

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

.changeset/purple-beers-warn.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-jsonc": patch
3+
---
4+
5+
fix: crash in `jsonc/auto`

lib/utils/get-auto-jsonc-rules-config/should-use-flat-config.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
import path from "path";
44
import fs from "fs";
55

6-
const FLAT_CONFIG_FILENAME = "eslint.config.js";
6+
const FLAT_CONFIG_FILENAMES = [
7+
"eslint.config.js",
8+
"eslint.config.mjs",
9+
"eslint.config.cjs",
10+
];
711
/**
812
* Returns whether flat config should be used.
913
* @returns {Promise<boolean>} Whether flat config should be used.
@@ -29,25 +33,26 @@ export function shouldUseFlatConfig(cwd: string): boolean {
2933
* @returns {string|undefined} The filename if found or `undefined` if not.
3034
*/
3135
function findFlatConfigFile(cwd: string) {
32-
return findUp(FLAT_CONFIG_FILENAME, { cwd });
36+
return findUp(FLAT_CONFIG_FILENAMES, { cwd });
3337
}
3438

3539
/** We used https://github.com/sindresorhus/find-up/blob/b733bb70d3aa21b22fa011be8089110d467c317f/index.js#L94 as a reference */
36-
function findUp(name: string, options: { cwd: string }) {
40+
function findUp(names: string[], options: { cwd: string }) {
3741
let directory = path.resolve(options.cwd);
3842
const { root } = path.parse(directory);
3943
const stopAt = path.resolve(directory, root);
40-
4144
// eslint-disable-next-line no-constant-condition -- ignore
4245
while (true) {
43-
const target = path.resolve(directory, name);
44-
const stat = fs.existsSync(target)
45-
? fs.statSync(target, {
46-
throwIfNoEntry: false,
47-
})
48-
: null;
49-
if (stat?.isFile()) {
50-
return target;
46+
for (const name of names) {
47+
const target = path.resolve(directory, name);
48+
const stat = fs.existsSync(target)
49+
? fs.statSync(target, {
50+
throwIfNoEntry: false,
51+
})
52+
: null;
53+
if (stat?.isFile()) {
54+
return target;
55+
}
5156
}
5257

5358
if (directory === stopAt) {

0 commit comments

Comments
 (0)