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

feat: Add findConfigFile() method to FlatESLint #17142

Merged
merged 1 commit into from May 3, 2023
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
67 changes: 49 additions & 18 deletions lib/eslint/flat-eslint.js
Expand Up @@ -321,26 +321,15 @@ async function loadFlatConfigFile(filePath) {
}

/**
* Calculates the config array for this run based on inputs.
* @param {FlatESLint} eslint The instance to create the config array for.
* Determines which config file to use. This is determined by seeing if an
* override config file was passed, and if so, using it; otherwise, as long
* as override config file is not explicitly set to `false`, it will search
* upwards from the cwd for a file named `eslint.config.js`.
* @param {import("./eslint").ESLintOptions} options The ESLint instance options.
* @returns {FlatConfigArray} The config array for `eslint``.
* @returns {{configFilePath:string,basePath:string}} Location information for
* the config file.
*/
async function calculateConfigArray(eslint, {
cwd,
baseConfig,
overrideConfig,
configFile,
ignore: shouldIgnore,
ignorePatterns
}) {

// check for cached instance
const slots = privateMembers.get(eslint);

if (slots.configs) {
return slots.configs;
}
async function locateConfigFileToUse({ configFile, cwd }) {

// determine where to load config file from
let configFilePath;
Expand All @@ -360,7 +349,36 @@ async function calculateConfigArray(eslint, {
basePath = path.resolve(path.dirname(configFilePath));
}

return {
configFilePath,
basePath
};

}

/**
* Calculates the config array for this run based on inputs.
* @param {FlatESLint} eslint The instance to create the config array for.
* @param {import("./eslint").ESLintOptions} options The ESLint instance options.
* @returns {FlatConfigArray} The config array for `eslint``.
*/
async function calculateConfigArray(eslint, {
cwd,
baseConfig,
overrideConfig,
configFile,
ignore: shouldIgnore,
ignorePatterns
}) {

// check for cached instance
const slots = privateMembers.get(eslint);

if (slots.configs) {
return slots.configs;
}

const { configFilePath, basePath } = await locateConfigFileToUse({ configFile, cwd });
const configs = new FlatConfigArray(baseConfig || [], { basePath, shouldIgnore });

// load config file
Expand Down Expand Up @@ -1160,6 +1178,19 @@ class FlatESLint {
return configs.getConfig(absolutePath);
}

/**
* Finds the config file being used by this instance based on the options
* passed to the constructor.
* @returns {string|undefined} The path to the config file being used or
* `undefined` if no config file is being used.
*/
async findConfigFile() {
const options = privateMembers.get(this).options;
const { configFilePath } = await locateConfigFileToUse(options);

return configFilePath;
}

/**
* Checks if a given path is ignored by ESLint.
* @param {string} filePath The path of the file to check.
Expand Down
37 changes: 37 additions & 0 deletions tests/lib/eslint/flat-eslint.js
Expand Up @@ -3954,6 +3954,43 @@ describe("FlatESLint", () => {
});
});

describe("findConfigFile()", () => {

it("should return null when overrideConfigFile is true", async () => {
const engine = new FlatESLint({
overrideConfigFile: true
});

assert.strictEqual(await engine.findConfigFile(), void 0);
});

it("should return custom config file path when overrideConfigFile is a nonempty string", async () => {
const engine = new FlatESLint({
overrideConfigFile: "my-config.js"
});
const configFilePath = path.resolve(__dirname, "../../../my-config.js");

assert.strictEqual(await engine.findConfigFile(), configFilePath);
});

it("should return root level eslint.config.js when overrideConfigFile is null", async () => {
const engine = new FlatESLint({
overrideConfigFile: null
});
const configFilePath = path.resolve(__dirname, "../../../eslint.config.js");

assert.strictEqual(await engine.findConfigFile(), configFilePath);
});

it("should return root level eslint.config.js when overrideConfigFile is not specified", async () => {
const engine = new FlatESLint();
const configFilePath = path.resolve(__dirname, "../../../eslint.config.js");

assert.strictEqual(await engine.findConfigFile(), configFilePath);
});

});

describe("getRulesMetaForResults()", () => {

it("should throw an error when this instance did not lint any files", async () => {
Expand Down