-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use @eslint/compat to fix eslint v9 + react plugin (#113)
* fix: use eslint v9 when choosing react fixes #106 * fix: use `--force` * chore: add a note * fix: ask installing eslint v8/v9 * chore: add more tests * fix: rm an unused assignment
- Loading branch information
1 parent
12886ee
commit 61a385e
Showing
45 changed files
with
232 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,9 +28,10 @@ export class ConfigGenerator { | |
this.packageJsonPath = options.packageJsonPath || findPackageJson(this.cwd); | ||
this.answers = options.answers || {}; | ||
this.result = { | ||
devDependencies: [], | ||
devDependencies: ["[email protected]"], | ||
configFilename: "eslint.config.js", | ||
configContent: "" | ||
configContent: "", | ||
installFlags: ["-D"] | ||
}; | ||
} | ||
|
||
|
@@ -72,6 +73,19 @@ export class ConfigGenerator { | |
{ message: "None of these", name: "none" } | ||
] | ||
}, | ||
{ | ||
type: "select", | ||
name: "eslintVersion", | ||
message: "The React plugin doesn't officially support ESLint v9 yet. What would you like to do?", | ||
initial: 0, | ||
choices: [ | ||
{ message: "Install ESLint v9.x with compatibility utilities", name: "9.x" }, | ||
{ message: "Install ESLint v8.x", name: "8.x" } | ||
], | ||
skip() { | ||
return this.state.answers.framework !== "react"; | ||
} | ||
}, | ||
{ | ||
type: "select", | ||
name: "language", | ||
|
@@ -166,9 +180,18 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug | |
} | ||
|
||
if (this.answers.framework === "react") { | ||
this.result.devDependencies.push("eslint-plugin-react"); | ||
importContent += "import pluginReactConfig from \"eslint-plugin-react/configs/recommended.js\";\n"; | ||
exportContent += " pluginReactConfig,\n"; | ||
if (this.answers.eslintVersion === "9.x") { | ||
this.result.devDependencies.push("eslint-plugin-react", "@eslint/compat"); | ||
this.result.installFlags.push("--force"); | ||
importContent += "import pluginReactConfig from \"eslint-plugin-react/configs/recommended.js\";\n"; | ||
importContent += "import { fixupConfigRules } from \"@eslint/compat\";\n"; | ||
exportContent += " ...fixupConfigRules(pluginReactConfig),\n"; | ||
} else if (this.answers.eslintVersion === "8.x") { | ||
this.result.devDependencies[0] = "[email protected]"; | ||
this.result.devDependencies.push("eslint-plugin-react"); | ||
importContent += "import pluginReactConfig from \"eslint-plugin-react/configs/recommended.js\";\n"; | ||
exportContent += " pluginReactConfig,\n"; | ||
} | ||
} | ||
if (this.answers.config) { | ||
const config = this.answers.config; | ||
|
@@ -179,7 +202,19 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug | |
const peers = fetchPeerDependencies(config.packageName); | ||
|
||
if (peers !== null) { | ||
this.result.devDependencies.push(...peers); | ||
const eslintIndex = peers.findIndex(dep => (dep.startsWith("eslint@"))); | ||
|
||
if (eslintIndex === -1) { | ||
// eslint is not in the peer dependencies | ||
|
||
this.result.devDependencies.push(...peers); | ||
} else { | ||
|
||
// eslint is in the peer dependencies => overwrite eslint version | ||
this.result.devDependencies[0] = peers[eslintIndex]; | ||
peers.splice(eslintIndex, 1); | ||
this.result.devDependencies.push(...peers); | ||
} | ||
} | ||
|
||
if (config.type === "flat" || config.type === void 0) { | ||
|
@@ -198,11 +233,6 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug | |
this.result.devDependencies.push("@eslint/eslintrc", "@eslint/js"); | ||
} | ||
|
||
const hasEslint = this.result.devDependencies.some(dep => (/^eslint(@|$)/u.test(dep))); | ||
|
||
if (!hasEslint) { | ||
this.result.devDependencies.push("eslint"); | ||
} | ||
this.result.configContent = `${importContent} | ||
${needCompatHelper ? helperContent : ""} | ||
export default [\n${exportContent}];`; | ||
|
@@ -238,7 +268,7 @@ export default [\n${exportContent}];`; | |
})).packageManager; | ||
|
||
log.info("☕️Installing..."); | ||
installSyncSaveDev(this.result.devDependencies, packageManager); | ||
installSyncSaveDev(this.result.devDependencies, packageManager, this.result.installFlags); | ||
await writeFile(configPath, this.result.configContent); | ||
|
||
// import("eslint") won't work in some cases. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,8 +10,11 @@ export default [ | |
];", | ||
"configFilename": "eslint.config.js", | ||
"devDependencies": [ | ||
"[email protected]", | ||
"globals", | ||
"@eslint/js", | ||
"eslint", | ||
], | ||
"installFlags": [ | ||
"-D", | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,12 @@ export default [ | |
];", | ||
"configFilename": "eslint.config.js", | ||
"devDependencies": [ | ||
"[email protected]", | ||
"globals", | ||
"@eslint/js", | ||
"typescript-eslint", | ||
"eslint", | ||
], | ||
"installFlags": [ | ||
"-D", | ||
], | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,9 +12,12 @@ export default [ | |
];", | ||
"configFilename": "eslint.config.js", | ||
"devDependencies": [ | ||
"[email protected]", | ||
"globals", | ||
"@eslint/js", | ||
"eslint-plugin-vue", | ||
"eslint", | ||
], | ||
"installFlags": [ | ||
"-D", | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,10 +14,13 @@ export default [ | |
];", | ||
"configFilename": "eslint.config.js", | ||
"devDependencies": [ | ||
"[email protected]", | ||
"globals", | ||
"@eslint/js", | ||
"typescript-eslint", | ||
"eslint-plugin-vue", | ||
"eslint", | ||
], | ||
"installFlags": [ | ||
"-D", | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,8 +9,11 @@ export default [ | |
];", | ||
"configFilename": "eslint.config.js", | ||
"devDependencies": [ | ||
"[email protected]", | ||
"globals", | ||
"@eslint/js", | ||
"eslint", | ||
], | ||
"installFlags": [ | ||
"-D", | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,9 +11,12 @@ export default [ | |
];", | ||
"configFilename": "eslint.config.js", | ||
"devDependencies": [ | ||
"[email protected]", | ||
"globals", | ||
"@eslint/js", | ||
"typescript-eslint", | ||
"eslint", | ||
], | ||
"installFlags": [ | ||
"-D", | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,15 +5,18 @@ import pluginReactConfig from "eslint-plugin-react/configs/recommended.js"; | |
|
||
|
||
export default [ | ||
{languageOptions: { globals: {...globals.browser, ...globals.node} }}, | ||
{languageOptions: { globals: globals.browser }}, | ||
pluginJs.configs.recommended, | ||
pluginReactConfig, | ||
];", | ||
"configFilename": "eslint.config.js", | ||
"devDependencies": [ | ||
"[email protected]", | ||
"globals", | ||
"@eslint/js", | ||
"eslint-plugin-react", | ||
"eslint", | ||
], | ||
"installFlags": [ | ||
"-D", | ||
], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,17 +6,20 @@ import pluginReactConfig from "eslint-plugin-react/configs/recommended.js"; | |
|
||
|
||
export default [ | ||
{languageOptions: { globals: {...globals.browser, ...globals.node} }}, | ||
{languageOptions: { globals: globals.browser }}, | ||
pluginJs.configs.recommended, | ||
...tseslint.configs.recommended, | ||
pluginReactConfig, | ||
];", | ||
"configFilename": "eslint.config.js", | ||
"devDependencies": [ | ||
"[email protected]", | ||
"globals", | ||
"@eslint/js", | ||
"typescript-eslint", | ||
"eslint-plugin-react", | ||
"eslint", | ||
], | ||
"installFlags": [ | ||
"-D", | ||
], | ||
} |
Oops, something went wrong.