Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/purple-beers-warn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-jsonc": patch
---

fix: crash in `jsonc/auto`
29 changes: 17 additions & 12 deletions lib/utils/get-auto-jsonc-rules-config/should-use-flat-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
import path from "path";
import fs from "fs";

const FLAT_CONFIG_FILENAME = "eslint.config.js";
const FLAT_CONFIG_FILENAMES = [
"eslint.config.js",
"eslint.config.mjs",
"eslint.config.cjs",
];
/**
* Returns whether flat config should be used.
* @returns {Promise<boolean>} Whether flat config should be used.
Expand All @@ -29,25 +33,26 @@ export function shouldUseFlatConfig(cwd: string): boolean {
* @returns {string|undefined} The filename if found or `undefined` if not.
*/
function findFlatConfigFile(cwd: string) {
return findUp(FLAT_CONFIG_FILENAME, { cwd });
return findUp(FLAT_CONFIG_FILENAMES, { cwd });
}

/** We used https://github.com/sindresorhus/find-up/blob/b733bb70d3aa21b22fa011be8089110d467c317f/index.js#L94 as a reference */
function findUp(name: string, options: { cwd: string }) {
function findUp(names: string[], options: { cwd: string }) {
let directory = path.resolve(options.cwd);
const { root } = path.parse(directory);
const stopAt = path.resolve(directory, root);

// eslint-disable-next-line no-constant-condition -- ignore
while (true) {
const target = path.resolve(directory, name);
const stat = fs.existsSync(target)
? fs.statSync(target, {
throwIfNoEntry: false,
})
: null;
if (stat?.isFile()) {
return target;
for (const name of names) {
const target = path.resolve(directory, name);
const stat = fs.existsSync(target)
? fs.statSync(target, {
throwIfNoEntry: false,
})
: null;
if (stat?.isFile()) {
return target;
}
}

if (directory === stopAt) {
Expand Down