Skip to content

Commit 63cb367

Browse files
authored
feat: Better conversion of .eslintrc.js files in migrate-config (#172)
1 parent 484b6ca commit 63cb367

File tree

19 files changed

+1343
-416
lines changed

19 files changed

+1343
-416
lines changed

packages/migrate-config/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
"@eslint/compat": "^1.2.8",
5050
"@eslint/eslintrc": "^3.1.0",
5151
"camelcase": "^8.0.0",
52+
"espree": "^10.3.0",
5253
"recast": "^0.23.7"
5354
}
5455
}

packages/migrate-config/src/migrate-config-cli.js

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
import fsp from "node:fs/promises";
2424
import path from "node:path";
25-
import { migrateConfig } from "./migrate-config.js";
25+
import { migrateConfig, migrateJSConfig } from "./migrate-config.js";
2626
import { Legacy } from "@eslint/eslintrc";
2727

2828
//-----------------------------------------------------------------------------
@@ -64,7 +64,6 @@ if (!configFilePath) {
6464
process.exit(1);
6565
}
6666

67-
const config = loadConfigFile(path.resolve(configFilePath));
6867
const ignorePatterns = await loadIgnoreFile(
6968
path.resolve(configFilePath, "../", ".eslintignore"),
7069
);
@@ -74,37 +73,38 @@ const configFileBasename = path.basename(configFilePath, configFileExtname);
7473
const resultFileBasename = configFileBasename.startsWith(".eslintrc")
7574
? "eslint.config"
7675
: configFileBasename;
77-
const resultFilePath = `${path.dirname(configFilePath)}/${resultFileBasename}.${resultExtname}`;
7876

7977
console.log("\nMigrating", configFilePath);
8078

81-
if (configFileExtname.endsWith("js")) {
82-
console.error(
83-
"\nWARNING: This tool does not yet work great for .eslintrc.(js|cjs|mjs) files.",
84-
);
85-
console.error(
86-
"It will convert the evaluated output of our config file, not the source code.",
87-
);
88-
console.error(
89-
"Please review the output carefully to ensure it is correct.\n",
90-
);
91-
}
92-
9379
if (ignorePatterns) {
9480
console.log("Also importing your .eslintignore file");
81+
}
9582

96-
if (!config.ignorePatterns) {
97-
config.ignorePatterns = [];
98-
}
83+
const isJS = configFileExtname.endsWith("js");
84+
const resultFilePath = `${path.dirname(configFilePath)}/${resultFileBasename}${isJS ? configFileExtname : `.${resultExtname}`}`;
9985

100-
// put the .eslintignore patterns last so they can override config ignores
101-
config.ignorePatterns = [...config.ignorePatterns, ...ignorePatterns];
86+
let result;
87+
88+
if (isJS) {
89+
console.error(
90+
"\nIMPORTANT: Migration of JavaScript configuration files results in removal of comments.",
91+
);
92+
console.error("Please review the output carefully.\n");
93+
94+
const code = await fsp.readFile(configFilePath, "utf8");
95+
result = migrateJSConfig(code, {
96+
ignorePatterns,
97+
gitignore,
98+
});
99+
} else {
100+
const config = loadConfigFile(path.resolve(configFilePath));
101+
result = migrateConfig(config, {
102+
sourceType: commonjs ? "commonjs" : "module",
103+
ignorePatterns,
104+
gitignore,
105+
});
102106
}
103107

104-
const result = migrateConfig(config, {
105-
sourceType: commonjs ? "commonjs" : "module",
106-
gitignore,
107-
});
108108
await fsp.writeFile(resultFilePath, result.code);
109109

110110
console.log("\nWrote new config to", resultFilePath);

0 commit comments

Comments
 (0)