Skip to content

Commit

Permalink
fix: --inspect-config only for flat config and respect -c (#18306)
Browse files Browse the repository at this point in the history
* fix: --inspect-config only for flat config and respect -c

* Remove unnecessary flat config check
  • Loading branch information
nzakas committed Apr 16, 2024
1 parent 09675e1 commit e1ac0b5
Show file tree
Hide file tree
Showing 4 changed files with 1,373 additions and 1,295 deletions.
12 changes: 0 additions & 12 deletions bin/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,18 +148,6 @@ ${getErrorMessage(error)}`;
return;
}

// Call the config inspector if `--inspect-config` is present.
if (process.argv.includes("--inspect-config")) {

console.warn("You can also run this command directly using 'npx @eslint/config-inspector' in the same directory as your configuration file.");

const spawn = require("cross-spawn");

spawn.sync("npx", ["@eslint/config-inspector"], { encoding: "utf8", stdio: "inherit" });

return;
}

// Otherwise, call the CLI.
const cli = require("../lib/cli");
const exitCode = await cli.execute(
Expand Down
41 changes: 40 additions & 1 deletion lib/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const fs = require("fs"),
path = require("path"),
{ promisify } = require("util"),
{ LegacyESLint } = require("./eslint"),
{ ESLint, shouldUseFlatConfig } = require("./eslint/eslint"),
{ ESLint, shouldUseFlatConfig, locateConfigFileToUse } = require("./eslint/eslint"),
createCLIOptions = require("./options"),
log = require("./shared/logging"),
RuntimeInfo = require("./shared/runtime-info"),
Expand Down Expand Up @@ -336,6 +336,27 @@ async function printResults(engine, results, format, outputFile, resultsMeta) {
*/
const cli = {

/**
* Calculates the command string for the --inspect-config operation.
* @param {string} configFile The path to the config file to inspect.
* @returns {Promise<string>} The command string to execute.
*/
async calculateInspectConfigFlags(configFile) {

// find the config file
const {
configFilePath,
basePath,
error
} = await locateConfigFileToUse({ cwd: process.cwd(), configFile });

if (error) {
throw error;
}

return ["--config", configFilePath, "--basePath", basePath];
},

/**
* Executes the CLI based on an array of arguments that is passed in.
* @param {string|Array|Object} args The arguments to process.
Expand Down Expand Up @@ -425,6 +446,24 @@ const cli = {
return 0;
}

if (options.inspectConfig) {

log.info("You can also run this command directly using 'npx @eslint/config-inspector' in the same directory as your configuration file.");

try {
const flatOptions = await translateOptions(options, "flat");
const spawn = require("cross-spawn");
const flags = await cli.calculateInspectConfigFlags(flatOptions.overrideConfigFile);

spawn.sync("npx", ["@eslint/config-inspector", ...flags], { encoding: "utf8", stdio: "inherit" });
} catch (error) {
log.error(error);
return 2;
}

return 0;
}

debug(`Running on ${useStdin ? "text" : "files"}`);

if (options.fix && options.fixDryRun) {
Expand Down
3 changes: 2 additions & 1 deletion lib/eslint/eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -1214,5 +1214,6 @@ async function shouldUseFlatConfig() {

module.exports = {
ESLint,
shouldUseFlatConfig
shouldUseFlatConfig,
locateConfigFileToUse
};

0 comments on commit e1ac0b5

Please sign in to comment.