Skip to content

Commit 727ec5d

Browse files
authored
feat: Use defineConfig() and globalIgnores() helpers (#164)
1 parent 473c962 commit 727ec5d

File tree

24 files changed

+352
-327
lines changed

24 files changed

+352
-327
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,14 @@ if (result.imports.size) {
114114
.filter(([key, imp]) => imp.added && !key.startsWith("node:"))
115115
.map(([key]) => key);
116116

117-
console.log(
118-
"\nYou will need to install the following packages to use the new config:",
119-
);
120-
console.log(`${addedImports.map(imp => `- ${imp}`).join("\n")}\n`);
121-
console.log("You can install them using the following command:\n");
122-
console.log(`npm install ${addedImports.join(" ")} -D\n`);
117+
if (addedImports.length) {
118+
console.log(
119+
"\nYou will need to install the following packages to use the new config:",
120+
);
121+
console.log(`${addedImports.map(imp => `- ${imp}`).join("\n")}\n`);
122+
console.log("You can install them using the following command:\n");
123+
console.log(`npm install ${addedImports.join(" ")} -D\n`);
124+
}
123125
}
124126

125127
if (result.messages.length) {

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

Lines changed: 20 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -645,7 +645,7 @@ function createPlugins(plugins, migration) {
645645
/**
646646
* Creates an object expression for the `ignorePatterns` property.
647647
* @param {LegacyConfig} config The config to create the object expression for.
648-
* @returns {ObjectExpression} The AST for the object expression.
648+
* @returns {CallExpression} The AST for the object expression.
649649
*/
650650
function createGlobalIgnores(config) {
651651
const ignorePatterns = Array.isArray(config.ignorePatterns)
@@ -656,8 +656,9 @@ function createGlobalIgnores(config) {
656656
b.literal(convertIgnorePatternToMinimatch(pattern)),
657657
),
658658
);
659-
return b.objectExpression([
660-
b.property("init", b.identifier("ignores"), ignorePatternsArray),
659+
660+
return b.callExpression(b.identifier("globalIgnores"), [
661+
ignorePatternsArray,
661662
]);
662663
}
663664

@@ -755,40 +756,9 @@ function migrateConfigObject(migration, config) {
755756
);
756757
}
757758

758-
// if there are either files or ignores, map so the resulting object has files and ignores
759-
if (files || ignores) {
760-
extendsCallExpression = b.callExpression(
761-
b.memberExpression(extendsCallExpression, b.identifier("map")),
762-
[
763-
b.arrowFunctionExpression(
764-
[b.identifier("config")],
765-
b.objectExpression([
766-
b.spreadElement(b.identifier("config")),
767-
...(files
768-
? [
769-
b.property(
770-
"init",
771-
b.identifier("files"),
772-
files,
773-
),
774-
]
775-
: []),
776-
...(ignores
777-
? [
778-
b.property(
779-
"init",
780-
b.identifier("ignores"),
781-
ignores,
782-
),
783-
]
784-
: []),
785-
]),
786-
),
787-
],
788-
);
789-
}
790-
791-
configArrayElements.push(b.spreadElement(extendsCallExpression));
759+
properties.push(
760+
b.property("init", b.identifier("extends"), extendsCallExpression),
761+
);
792762
}
793763

794764
/*
@@ -878,6 +848,11 @@ export function migrateConfig(
878848
const migration = new Migration(config);
879849
const body = [];
880850

851+
// always use defineConfig
852+
migration.imports.set("eslint/config", {
853+
bindings: ["defineConfig"],
854+
});
855+
881856
/** @type {Array<CallExpression|ObjectExpression|SpreadElement>} */
882857
const configArrayElements = [
883858
...migrateConfigObject(
@@ -945,6 +920,7 @@ export function migrateConfig(
945920
}
946921

947922
if (config.ignorePatterns) {
923+
migration.imports.get("eslint/config").bindings.push("globalIgnores");
948924
configArrayElements.unshift(createGlobalIgnores(config));
949925
}
950926

@@ -1007,6 +983,11 @@ export function migrateConfig(
1007983
// output any inits
1008984
body.push(...migration.inits);
1009985

986+
// the defineConfig() call
987+
const defineConfigNode = b.callExpression(b.identifier("defineConfig"), [
988+
b.arrayExpression(configArrayElements),
989+
]);
990+
1010991
// output the actual config array to the program
1011992
if (!isModule) {
1012993
body.push(
@@ -1017,14 +998,12 @@ export function migrateConfig(
1017998
b.identifier("module"),
1018999
b.identifier("exports"),
10191000
),
1020-
b.arrayExpression(configArrayElements),
1001+
defineConfigNode,
10211002
),
10221003
),
10231004
);
10241005
} else {
1025-
body.push(
1026-
b.exportDefaultDeclaration(b.arrayExpression(configArrayElements)),
1027-
);
1006+
body.push(b.exportDefaultDeclaration(defineConfigNode));
10281007
}
10291008

10301009
return {

packages/migrate-config/tests/fixtures/basic-eslintrc/expected.cjs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
const {
2+
defineConfig,
3+
globalIgnores,
4+
} = require("eslint/config");
5+
16
const {
27
fixupConfigRules,
38
fixupPluginRules,
@@ -23,9 +28,9 @@ const compat = new FlatCompat({
2328
allConfig: js.configs.all
2429
});
2530

26-
module.exports = [{
27-
ignores: ["*/a.js", "dir/**/*", "**/dir/"],
28-
}, ...fixupConfigRules(compat.extends("eslint:recommended", "plugin:import/errors")), {
31+
module.exports = defineConfig([globalIgnores(["*/a.js", "dir/**/*", "**/dir/"]), {
32+
extends: fixupConfigRules(compat.extends("eslint:recommended", "plugin:import/errors")),
33+
2934
plugins: {
3035
prettier,
3136
import: fixupPluginRules(_import),
@@ -51,13 +56,10 @@ module.exports = [{
5156
quotes: ["error"],
5257
"no-console": ["warn"],
5358
},
54-
}, ...compat.extends("plugin:@typescript-eslint/recommended").map(config => ({
55-
...config,
56-
files: ["**/*.ts"],
57-
ignores: ["**/*.d.ts"],
58-
})), {
59+
}, {
5960
files: ["**/*.ts"],
6061
ignores: ["**/*.d.ts"],
62+
extends: compat.extends("plugin:@typescript-eslint/recommended"),
6163

6264
plugins: {
6365
"@typescript-eslint": typescriptEslint,
@@ -71,4 +73,4 @@ module.exports = [{
7173
"@typescript-eslint/no-explicit-any": ["error"],
7274
"@typescript-eslint/no-unused-vars": ["error"],
7375
},
74-
}];
76+
}]);

packages/migrate-config/tests/fixtures/basic-eslintrc/expected.mjs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { defineConfig, globalIgnores } from "eslint/config";
12
import { fixupConfigRules, fixupPluginRules } from "@eslint/compat";
23
import prettier from "eslint-plugin-prettier";
34
import _import from "eslint-plugin-import";
@@ -20,9 +21,9 @@ const compat = new FlatCompat({
2021
allConfig: js.configs.all
2122
});
2223

23-
export default [{
24-
ignores: ["*/a.js", "dir/**/*", "**/dir/"],
25-
}, ...fixupConfigRules(compat.extends("eslint:recommended", "plugin:import/errors")), {
24+
export default defineConfig([globalIgnores(["*/a.js", "dir/**/*", "**/dir/"]), {
25+
extends: fixupConfigRules(compat.extends("eslint:recommended", "plugin:import/errors")),
26+
2627
plugins: {
2728
prettier,
2829
import: fixupPluginRules(_import),
@@ -48,13 +49,10 @@ export default [{
4849
quotes: ["error"],
4950
"no-console": ["warn"],
5051
},
51-
}, ...compat.extends("plugin:@typescript-eslint/recommended").map(config => ({
52-
...config,
53-
files: ["**/*.ts"],
54-
ignores: ["**/*.d.ts"],
55-
})), {
52+
}, {
5653
files: ["**/*.ts"],
5754
ignores: ["**/*.d.ts"],
55+
extends: compat.extends("plugin:@typescript-eslint/recommended"),
5856

5957
plugins: {
6058
"@typescript-eslint": typescriptEslint,
@@ -68,4 +66,4 @@ export default [{
6866
"@typescript-eslint/no-explicit-any": ["error"],
6967
"@typescript-eslint/no-unused-vars": ["error"],
7068
},
71-
}];
69+
}]);

packages/migrate-config/tests/fixtures/gitignore-complex/expected.cjs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
const {
2+
defineConfig,
3+
globalIgnores,
4+
} = require("eslint/config");
5+
16
const js = require("@eslint/js");
27

38
const {
@@ -16,10 +21,10 @@ const compat = new FlatCompat({
1621
});
1722
const gitignorePath = path.resolve(__dirname, ".gitignore");
1823

19-
module.exports = [{
20-
ignores: ["**/baz"],
21-
}, includeIgnoreFile(gitignorePath), ...compat.extends("eslint:recommended"), {
24+
module.exports = defineConfig([globalIgnores(["**/baz"]), includeIgnoreFile(gitignorePath), {
25+
extends: compat.extends("eslint:recommended"),
26+
2227
rules: {
2328
"no-console": "off",
2429
},
25-
}];
30+
}]);

packages/migrate-config/tests/fixtures/gitignore-complex/expected.mjs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { defineConfig, globalIgnores } from "eslint/config";
12
import path from "node:path";
23
import { fileURLToPath } from "node:url";
34
import js from "@eslint/js";
@@ -13,10 +14,10 @@ const compat = new FlatCompat({
1314
});
1415
const gitignorePath = path.resolve(__dirname, ".gitignore");
1516

16-
export default [{
17-
ignores: ["**/baz"],
18-
}, includeIgnoreFile(gitignorePath), ...compat.extends("eslint:recommended"), {
17+
export default defineConfig([globalIgnores(["**/baz"]), includeIgnoreFile(gitignorePath), {
18+
extends: compat.extends("eslint:recommended"),
19+
1920
rules: {
2021
"no-console": "off",
2122
},
22-
}];
23+
}]);
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
1+
const {
2+
defineConfig,
3+
} = require("eslint/config");
4+
15
const {
26
includeIgnoreFile,
37
} = require("@eslint/compat");
48

59
const path = require("node:path");
610
const gitignorePath = path.resolve(__dirname, ".gitignore");
711

8-
module.exports = [includeIgnoreFile(gitignorePath), {
12+
module.exports = defineConfig([includeIgnoreFile(gitignorePath), {
913
rules: {
1014
"no-console": "off",
1115
},
12-
}];
16+
}]);
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { defineConfig } from "eslint/config";
12
import { includeIgnoreFile } from "@eslint/compat";
23
import path from "node:path";
34
import { fileURLToPath } from "node:url";
@@ -6,8 +7,8 @@ const __filename = fileURLToPath(import.meta.url);
67
const __dirname = path.dirname(__filename);
78
const gitignorePath = path.resolve(__dirname, ".gitignore");
89

9-
export default [includeIgnoreFile(gitignorePath), {
10+
export default defineConfig([includeIgnoreFile(gitignorePath), {
1011
rules: {
1112
"no-console": "off",
1213
},
13-
}];
14+
}]);
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
const {
2+
defineConfig,
3+
} = require("eslint/config");
4+
15
const _import = require("eslint-plugin-import");
26

37
const {
@@ -6,12 +10,12 @@ const {
610

711
const reactHooks = require("eslint-plugin-react-hooks");
812

9-
module.exports = [{
13+
module.exports = defineConfig([{
1014
plugins: {
1115
import: fixupPluginRules(_import),
1216
},
1317
}, {
1418
plugins: {
1519
"react-hooks": fixupPluginRules(reactHooks),
1620
},
17-
}];
21+
}]);
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1+
import { defineConfig } from "eslint/config";
12
import _import from "eslint-plugin-import";
23
import { fixupPluginRules } from "@eslint/compat";
34
import reactHooks from "eslint-plugin-react-hooks";
45

5-
export default [{
6+
export default defineConfig([{
67
plugins: {
78
import: fixupPluginRules(_import),
89
},
910
}, {
1011
plugins: {
1112
"react-hooks": fixupPluginRules(reactHooks),
1213
},
13-
}];
14+
}]);

0 commit comments

Comments
 (0)