Skip to content

Commit 4a2a6ce

Browse files
committed
Update file extensions on configs
1 parent 8b904dd commit 4a2a6ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+118
-74
lines changed

lib/config-generator.js

Lines changed: 45 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
* @fileoverview to generate config files.
33
* @author 唯然<[email protected]>
44
*/
5+
6+
//-----------------------------------------------------------------------------
7+
// Imports
8+
//-----------------------------------------------------------------------------
9+
510
import process from "node:process";
611
import path from "node:path";
712
import { spawnSync } from "node:child_process";
@@ -11,6 +16,41 @@ import { isPackageTypeModule, installSyncSaveDev, fetchPeerDependencies, findPac
1116
import { getShorthandName } from "./utils/naming.js";
1217
import * as log from "./utils/logging.js";
1318

19+
//-----------------------------------------------------------------------------
20+
// Helpers
21+
//-----------------------------------------------------------------------------
22+
23+
/**
24+
* Get the file extensions to lint based on the user's answers.
25+
* @param {Object} answers The answers provided by the user.
26+
* @returns {string[]} The file extensions to lint.
27+
*/
28+
function getExtensions(answers) {
29+
const extensions = ["js", "mjs", "cjs"];
30+
31+
if (answers.language === "typescript") {
32+
extensions.push("ts");
33+
}
34+
35+
if (answers.framework === "vue") {
36+
extensions.push("vue");
37+
}
38+
39+
if (answers.framework === "react") {
40+
extensions.push("jsx");
41+
42+
if (answers.language === "typescript") {
43+
extensions.push("tsx");
44+
}
45+
}
46+
47+
return extensions;
48+
}
49+
50+
//-----------------------------------------------------------------------------
51+
// Exports
52+
//-----------------------------------------------------------------------------
53+
1454
/**
1555
* Class representing a ConfigGenerator.
1656
*/
@@ -112,7 +152,7 @@ export class ConfigGenerator {
112152
this.answers.config = typeof this.answers.config === "string"
113153
? { packageName: this.answers.config, type: "flat" }
114154
: this.answers.config;
115-
const extensions = []; // file extensions to lint, the default is ["js", "mjs", "cjs"]
155+
const extensions = `**/*.{${getExtensions(this.answers)}}`;
116156

117157
let importContent = "import { defineConfig } from \"eslint/config\";\n";
118158
const helperContent = `import path from "node:path";
@@ -141,7 +181,7 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: js.c
141181
"browser,node": "globals: {...globals.browser, ...globals.node}"
142182
};
143183

144-
exportContent += ` { files: ["**/*.js"], languageOptions: { ${envContent[this.answers.env.join(",")]} } },\n`;
184+
exportContent += ` { files: ["${extensions}"], languageOptions: { ${envContent[this.answers.env.join(",")]} } },\n`;
145185
}
146186

147187
if (this.answers.purpose === "syntax") {
@@ -150,35 +190,27 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: js.c
150190
} else if (this.answers.purpose === "problems") {
151191
this.result.devDependencies.push("@eslint/js");
152192
importContent += "import js from \"@eslint/js\";\n";
153-
exportContent += " { files: [\"**/*.js\"], plugins: { js }, extends: [\"js/recommended\"] },\n";
193+
exportContent += ` { files: ["${extensions}"], plugins: { js }, extends: ["js/recommended"] },\n`;
154194
}
155195

156196
if (this.answers.language === "typescript") {
157-
extensions.push("ts");
158197
this.result.devDependencies.push("typescript-eslint");
159198
importContent += "import tseslint from \"typescript-eslint\";\n";
160199
exportContent += " tseslint.configs.recommended,\n";
161200
}
162201

163202
if (this.answers.framework === "vue") {
164-
extensions.push("vue");
165203
this.result.devDependencies.push("eslint-plugin-vue");
166204
importContent += "import pluginVue from \"eslint-plugin-vue\";\n";
167205
exportContent += " pluginVue.configs[\"flat/essential\"],\n";
168206

169207
// https://eslint.vuejs.org/user-guide/#how-to-use-a-custom-parser
170208
if (this.answers.language === "typescript") {
171-
exportContent += " {files: [\"**/*.vue\"], languageOptions: { parserOptions: { parser: tseslint.parser } } },\n";
209+
exportContent += " { files: [\"**/*.vue\"], languageOptions: { parserOptions: { parser: tseslint.parser } } },\n";
172210
}
173211
}
174212

175213
if (this.answers.framework === "react") {
176-
extensions.push("jsx");
177-
178-
if (this.answers.language === "typescript") {
179-
extensions.push("tsx");
180-
}
181-
182214
this.result.devDependencies.push("eslint-plugin-react");
183215
importContent += "import pluginReact from \"eslint-plugin-react\";\n";
184216
exportContent += " pluginReact.configs.flat.recommended,\n";
@@ -224,7 +256,7 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: js.c
224256
this.result.devDependencies.push("@eslint/eslintrc", "@eslint/js");
225257
}
226258

227-
const lintFilesConfig = extensions.length > 0 ? ` { files: ["**/*.{${["js", "mjs", "cjs", ...extensions]}}"] },\n` : "";
259+
const lintFilesConfig = ` { files: ["${extensions}"] },\n`;
228260

229261
exportContent = `${lintFilesConfig}${exportContent}`;
230262

tests/__snapshots__/config@eslint-config-airbnb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const __dirname = path.dirname(__filename);
1212
const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: js.configs.recommended});
1313

1414
export default defineConfig([
15+
{ files: ["**/*.{js,mjs,cjs}"] },
1516
compat.extends("airbnb"),
1617
]);",
1718
"configFilename": "eslint.config.mjs",

tests/__snapshots__/config@eslint-config-airbnb-base

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const __dirname = path.dirname(__filename);
1212
const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: js.configs.recommended});
1313

1414
export default defineConfig([
15+
{ files: ["**/*.{js,mjs,cjs}"] },
1516
compat.extends("airbnb-base"),
1617
]);",
1718
"configFilename": "eslint.config.mjs",

tests/__snapshots__/config@eslint-config-standard

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const __dirname = path.dirname(__filename);
1212
const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: js.configs.recommended});
1313

1414
export default defineConfig([
15+
{ files: ["**/*.{js,mjs,cjs}"] },
1516
compat.extends("standard"),
1617
]);",
1718
"configFilename": "eslint.config.mjs",

tests/__snapshots__/config@eslint-config-standard-flat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import config from "eslint-config-standard";
44

55

66
export default defineConfig([
7+
{ files: ["**/*.{js,mjs,cjs}"] },
78
config,
89
]);",
910
"configFilename": "eslint.config.mjs",

tests/__snapshots__/config@eslint-config-standard-flat2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import config from "eslint-config-standard";
44

55

66
export default defineConfig([
7+
{ files: ["**/*.{js,mjs,cjs}"] },
78
config,
89
]);",
910
"configFilename": "eslint.config.mjs",

tests/__snapshots__/config@eslint-config-xo

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import config from "eslint-config-xo";
44

55

66
export default defineConfig([
7+
{ files: ["**/*.{js,mjs,cjs}"] },
78
config,
89
]);",
910
"configFilename": "eslint.config.mjs",

tests/__snapshots__/empty

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44

55
export default defineConfig([
6-
{}
6+
{ files: ["**/*.{js,mjs,cjs}"] },
77
]);",
88
"configFilename": "eslint.config.js",
99
"devDependencies": [

tests/__snapshots__/problems-commonjs-none-javascript

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import js from "@eslint/js";
55

66

77
export default defineConfig([
8+
{ files: ["**/*.{js,mjs,cjs}"] },
89
{ files: ["**/*.js"], languageOptions: { sourceType: "commonjs" } },
9-
{ files: ["**/*.js"], languageOptions: { globals: {...globals.browser, ...globals.node} } },
10-
{ files: ["**/*.js"], plugins: { js }, extends: ["js/recommended"] },
10+
{ files: ["**/*.{js,mjs,cjs}"], languageOptions: { globals: {...globals.browser, ...globals.node} } },
11+
{ files: ["**/*.{js,mjs,cjs}"], plugins: { js }, extends: ["js/recommended"] },
1112
]);",
1213
"configFilename": "eslint.config.js",
1314
"devDependencies": [

tests/__snapshots__/problems-commonjs-none-typescript

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import tseslint from "typescript-eslint";
88
export default defineConfig([
99
{ files: ["**/*.{js,mjs,cjs,ts}"] },
1010
{ files: ["**/*.js"], languageOptions: { sourceType: "commonjs" } },
11-
{ files: ["**/*.js"], languageOptions: { globals: {...globals.browser, ...globals.node} } },
12-
{ files: ["**/*.js"], plugins: { js }, extends: ["js/recommended"] },
11+
{ files: ["**/*.{js,mjs,cjs,ts}"], languageOptions: { globals: {...globals.browser, ...globals.node} } },
12+
{ files: ["**/*.{js,mjs,cjs,ts}"], plugins: { js }, extends: ["js/recommended"] },
1313
tseslint.configs.recommended,
1414
]);",
1515
"configFilename": "eslint.config.js",

0 commit comments

Comments
 (0)