Skip to content

Commit de1d085

Browse files
authored
feat: remove style guides (#108)
* feat: remove style guides from questions * fix: do not include multi eslint * docs: use latest * fix: rename styleguide to config * chore: rm unused Signed-off-by: 唯然 <weiran.zsd@outlook.com> * chore: add a few more tests * chore: add a few more tests --------- Signed-off-by: 唯然 <weiran.zsd@outlook.com>
1 parent b2320d8 commit de1d085

46 files changed

Lines changed: 134 additions & 162 deletions

Some content is hidden

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

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,5 @@ npm init @eslint/config@latest -- --config eslint-config-standard
2828
To use an eslintrc-style (legacy) shared config:
2929

3030
```bash
31-
npm init @eslint/config -- --eslintrc --config eslint-config-standard
31+
npm init @eslint/config@latest -- --eslintrc --config eslint-config-standard
3232
```

bin/create-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ if (sharedConfigIndex === -1) {
3131
// passed "--config"
3232
const packageName = argv[sharedConfigIndex + 1];
3333
const type = argv.includes("--eslintrc") ? "eslintrc" : "flat";
34-
const answers = { purpose: "style", moduleType: "module", styleguide: { packageName, type } };
34+
const answers = { config: { packageName, type } };
3535
const generator = new ConfigGenerator({ cwd, packageJsonPath, answers });
3636

3737
generator.calc();

lib/config-generator.js

Lines changed: 34 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,6 @@ import { isPackageTypeModule, installSyncSaveDev, fetchPeerDependencies, findPac
1111
import { getShorthandName } from "./utils/naming.js";
1212
import * as log from "./utils/logging.js";
1313

14-
// TODO: need to specify the package version - they may export flat configs in the future.
15-
const jsStyleGuides = [
16-
{ message: "Airbnb: https://github.com/airbnb/javascript", name: "airbnb", value: { packageName: "eslint-config-airbnb-base", type: "eslintrc" } },
17-
{ message: "Standard: https://github.com/standard/standard", name: "standard", value: { packageName: "eslint-config-standard", type: "eslintrc" } },
18-
{ message: "XO: https://github.com/xojs/eslint-config-xo", name: "xo", value: { packageName: "eslint-config-xo", type: "eslintrc" } }
19-
];
20-
const tsStyleGuides = [
21-
{ message: "Standard: https://github.com/standard/eslint-config-standard-with-typescript", name: "standard", value: { packageName: "eslint-config-standard-with-typescript", type: "eslintrc" } },
22-
{ message: "XO: https://github.com/xojs/eslint-config-xo-typescript", name: "xo", value: { packageName: "eslint-config-xo-typescript", type: "eslintrc" } }
23-
];
24-
2514
/**
2615
* Class representing a ConfigGenerator.
2716
*/
@@ -39,7 +28,7 @@ export class ConfigGenerator {
3928
this.packageJsonPath = options.packageJsonPath || findPackageJson(this.cwd);
4029
this.answers = options.answers || {};
4130
this.result = {
42-
devDependencies: ["eslint"],
31+
devDependencies: [],
4332
configFilename: "eslint.config.js",
4433
configContent: ""
4534
};
@@ -58,8 +47,7 @@ export class ConfigGenerator {
5847
initial: 1,
5948
choices: [
6049
{ message: "To check syntax only", name: "syntax" },
61-
{ message: "To check syntax and find problems", name: "problems" },
62-
{ message: "To check syntax, find problems, and enforce code style", name: "style" }
50+
{ message: "To check syntax and find problems", name: "problems" }
6351
]
6452
},
6553
{
@@ -110,20 +98,6 @@ export class ConfigGenerator {
11098
const answers = await enquirer.prompt(questions);
11199

112100
Object.assign(this.answers, answers);
113-
114-
if (answers.purpose === "style") {
115-
116-
const choices = this.answers.language === "javascript" ? jsStyleGuides : tsStyleGuides;
117-
const styleguideAnswer = await enquirer.prompt({
118-
type: "select",
119-
name: "styleguide",
120-
message: "Which style guide do you want to follow?",
121-
choices,
122-
result: choice => choices.find(it => it.name === choice).value
123-
});
124-
125-
Object.assign(this.answers, styleguideAnswer);
126-
}
127101
}
128102

129103
/**
@@ -134,22 +108,9 @@ export class ConfigGenerator {
134108
const isESMModule = isPackageTypeModule(this.packageJsonPath);
135109

136110
this.result.configFilename = isESMModule ? "eslint.config.js" : "eslint.config.mjs";
137-
this.answers.styleguide = typeof this.answers.styleguide === "string"
138-
? { packageName: this.answers.styleguide, type: "flat" }
139-
: this.answers.styleguide;
140-
141-
// replaced `eslint-config-airbnb-base` with `eslint-config-airbnb`(for react supports)
142-
if (this.answers.styleguide?.packageName === "eslint-config-airbnb-base" && this.answers.framework === "react") {
143-
this.answers.styleguide.packageName = "eslint-config-airbnb";
144-
this.answers.framework = "none";
145-
}
146-
147-
// tseslint should have been a dependency in the shared configs
148-
// avoid install tseslint - it may install multi tseslint versions
149-
// see https://github.com/eslint/create-config/issues/97
150-
if (this.answers.styleguide && this.answers.language === "typescript") {
151-
this.answers.language = "javascript";
152-
}
111+
this.answers.config = typeof this.answers.config === "string"
112+
? { packageName: this.answers.config, type: "flat" }
113+
: this.answers.config;
153114

154115
let importContent = "";
155116
const helperContent = `import path from "path";
@@ -188,28 +149,6 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug
188149
this.result.devDependencies.push("@eslint/js");
189150
importContent += "import pluginJs from \"@eslint/js\";\n";
190151
exportContent += " pluginJs.configs.recommended,\n";
191-
} else if (this.answers.purpose === "style") {
192-
const styleguide = this.answers.styleguide;
193-
194-
this.result.devDependencies.push(styleguide.packageName);
195-
196-
// install peer dependencies - it's needed for most eslintrc-style shared configs.
197-
const peers = fetchPeerDependencies(styleguide.packageName);
198-
199-
if (peers !== null) {
200-
this.result.devDependencies.push(...peers);
201-
}
202-
203-
if (styleguide.type === "flat" || styleguide.type === void 0) {
204-
importContent += `import styleGuide from "${styleguide.packageName}";\n`;
205-
exportContent += " ...[].concat(styleGuide),\n";
206-
} else if (styleguide.type === "eslintrc") {
207-
needCompatHelper = true;
208-
209-
const shorthandName = getShorthandName(styleguide.packageName, "eslint-config");
210-
211-
exportContent += ` ...compat.extends("${shorthandName}"),\n`;
212-
}
213152
}
214153

215154
if (this.answers.language === "typescript") {
@@ -231,10 +170,39 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug
231170
importContent += "import pluginReactConfig from \"eslint-plugin-react/configs/recommended.js\";\n";
232171
exportContent += " pluginReactConfig,\n";
233172
}
173+
if (this.answers.config) {
174+
const config = this.answers.config;
175+
176+
this.result.devDependencies.push(config.packageName);
177+
178+
// install peer dependencies - it's needed for most eslintrc-style shared configs.
179+
const peers = fetchPeerDependencies(config.packageName);
180+
181+
if (peers !== null) {
182+
this.result.devDependencies.push(...peers);
183+
}
184+
185+
if (config.type === "flat" || config.type === void 0) {
186+
importContent += `import config from "${config.packageName}";\n`;
187+
exportContent += " ...[].concat(config),\n";
188+
} else if (config.type === "eslintrc") {
189+
needCompatHelper = true;
190+
191+
const shorthandName = getShorthandName(config.packageName, "eslint-config");
192+
193+
exportContent += ` ...compat.extends("${shorthandName}"),\n`;
194+
}
195+
}
234196

235197
if (needCompatHelper) {
236198
this.result.devDependencies.push("@eslint/eslintrc", "@eslint/js");
237199
}
200+
201+
const hasEslint = this.result.devDependencies.some(dep => (/^eslint(@|$)/u.test(dep)));
202+
203+
if (!hasEslint) {
204+
this.result.devDependencies.push("eslint");
205+
}
238206
this.result.configContent = `${importContent}
239207
${needCompatHelper ? helperContent : ""}
240208
export default [\n${exportContent}];`;

tests/__snapshots__/style-esm-react-airbnb renamed to tests/__snapshots__/config@eslint-config-airbnb

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
2-
"configContent": "import globals from "globals";
3-
2+
"configContent": "
43
import path from "path";
54
import { fileURLToPath } from "url";
65
import { FlatCompat } from "@eslint/eslintrc";
@@ -12,13 +11,10 @@ const __dirname = path.dirname(__filename);
1211
const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: pluginJs.configs.recommended});
1312

1413
export default [
15-
{languageOptions: { globals: globals.browser }},
1614
...compat.extends("airbnb"),
1715
];",
1816
"configFilename": "eslint.config.mjs",
1917
"devDependencies": [
20-
"eslint",
21-
"globals",
2218
"eslint-config-airbnb",
2319
"eslint@^7.32.0 || ^8.2.0",
2420
"eslint-plugin-import@^2.25.3",

tests/__snapshots__/style-esm-none-airbnb renamed to tests/__snapshots__/config@eslint-config-airbnb-base

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
2-
"configContent": "import globals from "globals";
3-
2+
"configContent": "
43
import path from "path";
54
import { fileURLToPath } from "url";
65
import { FlatCompat } from "@eslint/eslintrc";
@@ -12,13 +11,10 @@ const __dirname = path.dirname(__filename);
1211
const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: pluginJs.configs.recommended});
1312

1413
export default [
15-
{languageOptions: { globals: globals.browser }},
1614
...compat.extends("airbnb-base"),
1715
];",
1816
"configFilename": "eslint.config.mjs",
1917
"devDependencies": [
20-
"eslint",
21-
"globals",
2218
"eslint-config-airbnb-base",
2319
"eslint@^7.32.0 || ^8.2.0",
2420
"eslint-plugin-import@^2.25.2",

tests/__snapshots__/style-esm-none-xo-typescript renamed to tests/__snapshots__/config@eslint-config-standard

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
2-
"configContent": "import globals from "globals";
3-
2+
"configContent": "
43
import path from "path";
54
import { fileURLToPath } from "url";
65
import { FlatCompat } from "@eslint/eslintrc";
@@ -12,18 +11,15 @@ const __dirname = path.dirname(__filename);
1211
const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: pluginJs.configs.recommended});
1312

1413
export default [
15-
{languageOptions: { globals: globals.browser }},
16-
...compat.extends("xo-typescript"),
14+
...compat.extends("standard"),
1715
];",
1816
"configFilename": "eslint.config.mjs",
1917
"devDependencies": [
20-
"eslint",
21-
"globals",
22-
"eslint-config-xo-typescript",
23-
"@typescript-eslint/eslint-plugin@>=7.0.2",
24-
"@typescript-eslint/parser@>=7.0.2",
25-
"eslint@>=8.56.0",
26-
"typescript@>=5.0.0",
18+
"eslint-config-standard",
19+
"eslint@^8.0.1",
20+
"eslint-plugin-import@^2.25.2",
21+
"eslint-plugin-n@^15.0.0 || ^16.0.0 ",
22+
"eslint-plugin-promise@^6.0.0",
2723
"@eslint/eslintrc",
2824
"@eslint/js",
2925
],
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"configContent": "import config from "eslint-config-standard";
3+
4+
5+
export default [
6+
...[].concat(config),
7+
];",
8+
"configFilename": "eslint.config.mjs",
9+
"devDependencies": [
10+
"eslint-config-standard",
11+
"eslint@^8.0.1",
12+
"eslint-plugin-import@^2.25.2",
13+
"eslint-plugin-n@^15.0.0 || ^16.0.0 ",
14+
"eslint-plugin-promise@^6.0.0",
15+
],
16+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"configContent": "import config from "eslint-config-standard";
3+
4+
5+
export default [
6+
...[].concat(config),
7+
];",
8+
"configFilename": "eslint.config.mjs",
9+
"devDependencies": [
10+
"eslint-config-standard",
11+
"eslint@^8.0.1",
12+
"eslint-plugin-import@^2.25.2",
13+
"eslint-plugin-n@^15.0.0 || ^16.0.0 ",
14+
"eslint-plugin-promise@^6.0.0",
15+
],
16+
}

tests/__snapshots__/style-esm-none-xo-javascript renamed to tests/__snapshots__/config@eslint-config-xo

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
2-
"configContent": "import globals from "globals";
3-
2+
"configContent": "
43
import path from "path";
54
import { fileURLToPath } from "url";
65
import { FlatCompat } from "@eslint/eslintrc";
@@ -12,13 +11,10 @@ const __dirname = path.dirname(__filename);
1211
const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: pluginJs.configs.recommended});
1312

1413
export default [
15-
{languageOptions: { globals: globals.node }},
1614
...compat.extends("xo"),
1715
];",
1816
"configFilename": "eslint.config.mjs",
1917
"devDependencies": [
20-
"eslint",
21-
"globals",
2218
"eslint-config-xo",
2319
"eslint@>=8.56.0",
2420
"@eslint/eslintrc",

tests/__snapshots__/problems-commonjs-none-javascript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ export default [
1010
];",
1111
"configFilename": "eslint.config.js",
1212
"devDependencies": [
13-
"eslint",
1413
"globals",
1514
"@eslint/js",
15+
"eslint",
1616
],
1717
}

0 commit comments

Comments
 (0)