Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: crash in jsonc/auto #332

Merged
merged 2 commits into from
Apr 12, 2024
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.
Jump to
Jump to file
Failed to load files.
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