Skip to content

Commit

Permalink
fix: crash in jsonc/auto (#332)
Browse files Browse the repository at this point in the history
  • Loading branch information
ota-meshi authored Apr 12, 2024
1 parent f3ba02f commit f2f9d75
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
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

0 comments on commit f2f9d75

Please sign in to comment.