From c5da34e1c42100409699bf76257bb165e6ee324f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Wed, 16 Oct 2024 16:10:07 +0800 Subject: [PATCH 01/10] feat: add question langauges --- lib/config-generator.js | 91 +++++------------------------ lib/questions.js | 101 +++++++++++++++++++++++++++++++++ tests/config-snapshots.spec.js | 10 ++-- 3 files changed, 119 insertions(+), 83 deletions(-) create mode 100644 lib/questions.js diff --git a/lib/config-generator.js b/lib/config-generator.js index 2bcd491..619270d 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -10,6 +10,7 @@ import enquirer from "enquirer"; import { isPackageTypeModule, installSyncSaveDev, fetchPeerDependencies, findPackageJson } from "./utils/npm-utils.js"; import { getShorthandName } from "./utils/naming.js"; import * as log from "./utils/logging.js"; +import { langQuestions, jsQuestions, mdQuestions, installationQuestions } from "./questions.js"; /** * Class representing a ConfigGenerator. @@ -40,65 +41,15 @@ export class ConfigGenerator { * @returns {void} */ async prompt() { - const questions = [ - { - type: "select", - name: "purpose", - message: "How would you like to use ESLint?", - initial: 1, - choices: [ - { message: "To check syntax only", name: "syntax" }, - { message: "To check syntax and find problems", name: "problems" } - ] - }, - { - type: "select", - name: "moduleType", - message: "What type of modules does your project use?", - initial: 0, - choices: [ - { message: "JavaScript modules (import/export)", name: "esm" }, - { message: "CommonJS (require/exports)", name: "commonjs" }, - { message: "None of these", name: "script" } - ] - }, - { - type: "select", - name: "framework", - message: "Which framework does your project use?", - initial: 0, - choices: [ - { message: "React", name: "react" }, - { message: "Vue.js", name: "vue" }, - { message: "None of these", name: "none" } - ] - }, - { - type: "select", - name: "language", - message: "Does your project use TypeScript?", - choices: [ - { message: "No", name: "javascript" }, - { message: "Yes", name: "typescript" } - ], - initial: 0 - }, - { - type: "multiselect", - name: "env", - message: "Where does your code run?", - hint: "(Press to select, to toggle all, to invert selection)", - initial: 0, - choices: [ - { message: "Browser", name: "browser" }, - { message: "Node", name: "node" } - ] - } - ]; + Object.assign(this.answers, await enquirer.prompt(langQuestions)); - const answers = await enquirer.prompt(questions); + if (this.answers.languages.includes("javascript")) { + Object.assign(this.answers, await enquirer.prompt(jsQuestions)); + } - Object.assign(this.answers, answers); + if (this.answers.languages.includes("md")) { + Object.assign(this.answers, await enquirer.prompt(mdQuestions)); + } } /** @@ -107,6 +58,7 @@ export class ConfigGenerator { */ calc() { const isESMModule = isPackageTypeModule(this.packageJsonPath); + const useTs = this.answers.useTs; this.result.configFilename = isESMModule ? "eslint.config.js" : "eslint.config.mjs"; this.answers.config = typeof this.answers.config === "string" @@ -153,7 +105,7 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug exportContent += " pluginJs.configs.recommended,\n"; } - if (this.answers.language === "typescript") { + if (useTs) { extensions.push("ts"); this.result.devDependencies.push("typescript-eslint"); importContent += "import tseslint from \"typescript-eslint\";\n"; @@ -167,7 +119,7 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug exportContent += " ...pluginVue.configs[\"flat/essential\"],\n"; // https://eslint.vuejs.org/user-guide/#how-to-use-a-custom-parser - if (this.answers.language === "typescript") { + if (useTs) { exportContent += " {files: [\"**/*.vue\"], languageOptions: {parserOptions: {parser: tseslint.parser}}},\n"; } } @@ -175,7 +127,7 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug if (this.answers.framework === "react") { extensions.push("jsx"); - if (this.answers.language === "typescript") { + if (useTs) { extensions.push("tsx"); } @@ -243,24 +195,7 @@ export default [\n${exportContent}];`; log.info("The config that you've selected requires the following dependencies:\n"); log.info(this.result.devDependencies.join(", ")); - const questions = [{ - type: "toggle", - name: "executeInstallation", - message: "Would you like to install them now?", - enabled: "Yes", - disabled: "No", - initial: 1 - }, { - type: "select", - name: "packageManager", - message: "Which package manager do you want to use?", - initial: 0, - choices: ["npm", "yarn", "pnpm", "bun"], - skip() { - return this.state.answers.executeInstallation === false; - } - }]; - const { executeInstallation, packageManager } = (await enquirer.prompt(questions)); + const { executeInstallation, packageManager } = (await enquirer.prompt(installationQuestions)); const configPath = path.join(this.cwd, this.result.configFilename); if (executeInstallation === true) { diff --git a/lib/questions.js b/lib/questions.js new file mode 100644 index 0000000..644e455 --- /dev/null +++ b/lib/questions.js @@ -0,0 +1,101 @@ +/** + * @fileoverview all the questions for the quiz + * @author 唯然 + */ + +export const langQuestions = [{ + type: "multiselect", + name: "languages", + message: "What do you want to lint?", + choices: [ + { message: "JavaScript", name: "javascript" }, + { message: "JSON", name: "json" }, + { message: "JSON with comments", name: "jsonc" }, + { message: "JSON5", name: "json5" }, + { message: "Markdown", name: "md" } + ], + initial: 0 +}]; + +export const jsQuestions = [ + { + type: "select", + name: "purpose", + message: "How would you like to use ESLint?", + initial: 1, + choices: [ + { message: "To check syntax only", name: "syntax" }, + { message: "To check syntax and find problems", name: "problems" } + ] + }, + { + type: "select", + name: "moduleType", + message: "What type of modules does your project use?", + initial: 0, + choices: [ + { message: "JavaScript modules (import/export)", name: "esm" }, + { message: "CommonJS (require/exports)", name: "commonjs" }, + { message: "None of these", name: "script" } + ] + }, + { + type: "select", + name: "framework", + message: "Which framework does your project use?", + initial: 0, + choices: [ + { message: "React", name: "react" }, + { message: "Vue.js", name: "vue" }, + { message: "None of these", name: "none" } + ] + }, + { + type: "toggle", + name: "useTs", + message: "Does your project use TypeScript?", + initial: 0 + }, + { + type: "multiselect", + name: "env", + message: "Where does your code run?", + hint: "(Press to select, to toggle all, to invert selection)", + initial: 0, + choices: [ + { message: "Browser", name: "browser" }, + { message: "Node", name: "node" } + ] + } +]; + +export const mdQuestions = [{ + type: "select", + name: "mdType", + message: "What flavor of Markdown to you want to lint?", + initial: 0, + choices: [ + { message: "CommonMark", name: "commonmark" }, + { message: "GitHub Flavored Markdown", name: "gfm" } + ] +}]; + +export const installationQuestions = [ + { + type: "toggle", + name: "executeInstallation", + message: "Would you like to install them now?", + enabled: "Yes", + disabled: "No", + initial: 1 + }, { + type: "select", + name: "packageManager", + message: "Which package manager do you want to use?", + initial: 0, + choices: ["npm", "yarn", "pnpm", "bun"], + skip() { + return this.state.answers.executeInstallation === false; + } + } +]; diff --git a/tests/config-snapshots.spec.js b/tests/config-snapshots.spec.js index c277632..df887b8 100644 --- a/tests/config-snapshots.spec.js +++ b/tests/config-snapshots.spec.js @@ -19,7 +19,7 @@ describe("generate config for esm projects", () => { purpose: ["syntax", "problems"], moduleType: ["esm", "commonjs", "script"], framework: ["react", "vue", "none"], - language: ["javascript", "typescript"], + useTs: [false, true], env: ["browser", "node"] }; @@ -29,14 +29,14 @@ describe("generate config for esm projects", () => { for (let i = 0; i < choices.purpose.length; i++) { for (let j = 0; j < choices.moduleType.length; j++) { for (let k = 0; k < choices.framework.length; k++) { - for (let m = 0; m < choices.language.length; m++) { + for (let m = 0; m < choices.useTs.length; m++) { inputs.push({ - name: `${choices.purpose[i]}-${choices.moduleType[j]}-${choices.framework[k]}-${choices.language[m]}`, + name: `${choices.purpose[i]}-${choices.moduleType[j]}-${choices.framework[k]}-${choices.useTs[m] ? "typescript" : "javascript"}`, answers: { purpose: choices.purpose[i], moduleType: choices.moduleType[j], framework: choices.framework[k], - language: choices.language[m], + useTs: choices.useTs[m], env: ["browser", "node"] } }); @@ -59,7 +59,7 @@ describe("generate config for esm projects", () => { test("sub dir", () => { const sub = join(__filename, "../fixtures/esm-project/sub"); - const generator = new ConfigGenerator({ cwd: sub, answers: { purpose: "problems", moduleType: "esm", framework: "none", language: "javascript", env: ["node"] } }); + const generator = new ConfigGenerator({ cwd: sub, answers: { purpose: "problems", moduleType: "esm", framework: "none", useTs: false, env: ["node"] } }); generator.calc(); From bbb24ef6d46e12c4e313a4e3bf712c6b571efa96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Wed, 23 Oct 2024 16:22:26 +0800 Subject: [PATCH 02/10] chore: languages => ["javascript"] --- lib/config-generator.js | 110 +++++++++++++++++---------------- tests/config-snapshots.spec.js | 3 +- 2 files changed, 58 insertions(+), 55 deletions(-) diff --git a/lib/config-generator.js b/lib/config-generator.js index 619270d..3dd216e 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -12,6 +12,17 @@ import { getShorthandName } from "./utils/naming.js"; import * as log from "./utils/logging.js"; import { langQuestions, jsQuestions, mdQuestions, installationQuestions } from "./questions.js"; +const helperContent = `import path from "path"; +import { fileURLToPath } from "url"; +import { FlatCompat } from "@eslint/eslintrc"; +import pluginJs from "@eslint/js"; + +// mimic CommonJS variables -- not needed if using CommonJS +const __filename = fileURLToPath(import.meta.url); +const __dirname = path.dirname(__filename); +const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: pluginJs.configs.recommended}); +`; + /** * Class representing a ConfigGenerator. */ @@ -67,75 +78,66 @@ export class ConfigGenerator { const extensions = []; // file extensions to lint, the default is ["js", "mjs", "cjs"] let importContent = ""; - const helperContent = `import path from "path"; -import { fileURLToPath } from "url"; -import { FlatCompat } from "@eslint/eslintrc"; -import pluginJs from "@eslint/js"; - -// mimic CommonJS variables -- not needed if using CommonJS -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); -const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: pluginJs.configs.recommended}); -`; let exportContent = ""; let needCompatHelper = false; - if (this.answers.moduleType === "commonjs" || this.answers.moduleType === "script") { - exportContent += ` {files: ["**/*.js"], languageOptions: {sourceType: "${this.answers.moduleType}"}},\n`; - } - - if (this.answers.env?.length > 0) { - this.result.devDependencies.push("globals"); - importContent += "import globals from \"globals\";\n"; - const envContent = { - browser: "globals: globals.browser", - node: "globals: globals.node", - "browser,node": "globals: {...globals.browser, ...globals.node}" - }; - - exportContent += ` {languageOptions: { ${envContent[this.answers.env.join(",")]} }},\n`; - } + if (this.answers.languages?.includes("javascript")) { + if (this.answers.moduleType === "commonjs" || this.answers.moduleType === "script") { + exportContent += ` {files: ["**/*.js"], languageOptions: {sourceType: "${this.answers.moduleType}"}},\n`; + } - if (this.answers.purpose === "syntax") { + if (this.answers.env?.length > 0) { + this.result.devDependencies.push("globals"); + importContent += "import globals from \"globals\";\n"; + const envContent = { + browser: "globals: globals.browser", + node: "globals: globals.node", + "browser,node": "globals: {...globals.browser, ...globals.node}" + }; - // no need to install any plugin - } else if (this.answers.purpose === "problems") { - this.result.devDependencies.push("@eslint/js"); - importContent += "import pluginJs from \"@eslint/js\";\n"; - exportContent += " pluginJs.configs.recommended,\n"; - } + exportContent += ` {languageOptions: { ${envContent[this.answers.env.join(",")]} }},\n`; + } - if (useTs) { - extensions.push("ts"); - this.result.devDependencies.push("typescript-eslint"); - importContent += "import tseslint from \"typescript-eslint\";\n"; - exportContent += " ...tseslint.configs.recommended,\n"; - } + if (this.answers.purpose === "syntax") { - if (this.answers.framework === "vue") { - extensions.push("vue"); - this.result.devDependencies.push("eslint-plugin-vue"); - importContent += "import pluginVue from \"eslint-plugin-vue\";\n"; - exportContent += " ...pluginVue.configs[\"flat/essential\"],\n"; + // no need to install any plugin + } else if (this.answers.purpose === "problems") { + this.result.devDependencies.push("@eslint/js"); + importContent += "import pluginJs from \"@eslint/js\";\n"; + exportContent += " pluginJs.configs.recommended,\n"; + } - // https://eslint.vuejs.org/user-guide/#how-to-use-a-custom-parser if (useTs) { - exportContent += " {files: [\"**/*.vue\"], languageOptions: {parserOptions: {parser: tseslint.parser}}},\n"; + extensions.push("ts"); + this.result.devDependencies.push("typescript-eslint"); + importContent += "import tseslint from \"typescript-eslint\";\n"; + exportContent += " ...tseslint.configs.recommended,\n"; } - } - if (this.answers.framework === "react") { - extensions.push("jsx"); + if (this.answers.framework === "vue") { + extensions.push("vue"); + this.result.devDependencies.push("eslint-plugin-vue"); + importContent += "import pluginVue from \"eslint-plugin-vue\";\n"; + exportContent += " ...pluginVue.configs[\"flat/essential\"],\n"; - if (useTs) { - extensions.push("tsx"); + // https://eslint.vuejs.org/user-guide/#how-to-use-a-custom-parser + if (useTs) { + exportContent += " {files: [\"**/*.vue\"], languageOptions: {parserOptions: {parser: tseslint.parser}}},\n"; + } } - this.result.devDependencies.push("eslint-plugin-react"); - importContent += "import pluginReact from \"eslint-plugin-react\";\n"; - exportContent += " pluginReact.configs.flat.recommended,\n"; - } + if (this.answers.framework === "react") { + extensions.push("jsx"); + if (useTs) { + extensions.push("tsx"); + } + + this.result.devDependencies.push("eslint-plugin-react"); + importContent += "import pluginReact from \"eslint-plugin-react\";\n"; + exportContent += " pluginReact.configs.flat.recommended,\n"; + } + } if (this.answers.config) { const config = this.answers.config; diff --git a/tests/config-snapshots.spec.js b/tests/config-snapshots.spec.js index df887b8..6e9361b 100644 --- a/tests/config-snapshots.spec.js +++ b/tests/config-snapshots.spec.js @@ -33,6 +33,7 @@ describe("generate config for esm projects", () => { inputs.push({ name: `${choices.purpose[i]}-${choices.moduleType[j]}-${choices.framework[k]}-${choices.useTs[m] ? "typescript" : "javascript"}`, answers: { + languages: ["javascript"], purpose: choices.purpose[i], moduleType: choices.moduleType[j], framework: choices.framework[k], @@ -59,7 +60,7 @@ describe("generate config for esm projects", () => { test("sub dir", () => { const sub = join(__filename, "../fixtures/esm-project/sub"); - const generator = new ConfigGenerator({ cwd: sub, answers: { purpose: "problems", moduleType: "esm", framework: "none", useTs: false, env: ["node"] } }); + const generator = new ConfigGenerator({ cwd: sub, answers: { languages: ["javascript"], purpose: "problems", moduleType: "esm", framework: "none", useTs: false, env: ["node"] } }); generator.calc(); From 77337f85a58690bc2f97d337e2f1fb5812265b93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Fri, 25 Oct 2024 14:58:40 +0800 Subject: [PATCH 03/10] feat: support json/md fixes https://github.com/eslint/create-config/issues/148 --- lib/config-generator.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/config-generator.js b/lib/config-generator.js index 3dd216e..9785442 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -80,8 +80,9 @@ export class ConfigGenerator { let importContent = ""; let exportContent = ""; let needCompatHelper = false; + const languages = this.answers.languages; - if (this.answers.languages?.includes("javascript")) { + if (languages?.includes("javascript")) { if (this.answers.moduleType === "commonjs" || this.answers.moduleType === "script") { exportContent += ` {files: ["**/*.js"], languageOptions: {sourceType: "${this.answers.moduleType}"}},\n`; } @@ -138,6 +139,32 @@ export class ConfigGenerator { exportContent += " pluginReact.configs.flat.recommended,\n"; } } + + if (languages?.some(item => item.startsWith("json"))) { + this.result.devDependencies.push("@eslint/json"); + importContent += "import json from \"@eslint/json\";\n"; + } + if (languages?.includes("json")) { + exportContent += " {files: [\"**/*.json\"], language: \"json/json\", ...json.configs.recommended},\n"; + } + if (languages?.includes("jsonc")) { + exportContent += " {files: [\"**/*.jsonc\"], language: \"json/jsonc\", ...json.configs.recommended},\n"; + } + if (languages?.includes("json5")) { + exportContent += " {files: [\"**/*.json5\"], language: \"json/json5\", ...json.configs.recommended},\n"; + } + + if (languages?.includes("md")) { + this.result.devDependencies.push("@eslint/markdown"); + importContent += "import markdown from \"@eslint/markdown\";\n"; + exportContent += " ...markdown.configs.recommended,\n"; + if (this.answers.mdType === "gfm") { + + // the default is commonmark + exportContent += " {files: [\"**/*.md\"], language: \"markdown/gfm\"},\n"; + } + } + if (this.answers.config) { const config = this.answers.config; From cd750b896f40c9cf4f221796d01e526d75f5e69e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Mon, 4 Nov 2024 17:34:00 +0800 Subject: [PATCH 04/10] chore: add tests for json/md --- tests/__snapshots__/esm-javascript-json | 23 +++++++++++++++++++++ tests/__snapshots__/esm-json | 17 +++++++++++++++ tests/__snapshots__/esm-json5 | 17 +++++++++++++++ tests/__snapshots__/esm-jsonc | 17 +++++++++++++++ tests/__snapshots__/esm-markdown-commonmark | 17 +++++++++++++++ tests/__snapshots__/esm-markdown-gfm | 18 ++++++++++++++++ tests/config-snapshots.spec.js | 9 +++++++- 7 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 tests/__snapshots__/esm-javascript-json create mode 100644 tests/__snapshots__/esm-json create mode 100644 tests/__snapshots__/esm-json5 create mode 100644 tests/__snapshots__/esm-jsonc create mode 100644 tests/__snapshots__/esm-markdown-commonmark create mode 100644 tests/__snapshots__/esm-markdown-gfm diff --git a/tests/__snapshots__/esm-javascript-json b/tests/__snapshots__/esm-javascript-json new file mode 100644 index 0000000..1a5e88c --- /dev/null +++ b/tests/__snapshots__/esm-javascript-json @@ -0,0 +1,23 @@ +{ + "configContent": "import globals from "globals"; +import pluginJs from "@eslint/js"; +import json from "@eslint/json"; + + +/** @type {import('eslint').Linter.Config[]} */ +export default [ + {languageOptions: { globals: globals.node }}, + pluginJs.configs.recommended, + {files: ["**/*.json"], language: "json/json", ...json.configs.recommended}, +];", + "configFilename": "eslint.config.js", + "devDependencies": [ + "eslint", + "globals", + "@eslint/js", + "@eslint/json", + ], + "installFlags": [ + "-D", + ], +} \ No newline at end of file diff --git a/tests/__snapshots__/esm-json b/tests/__snapshots__/esm-json new file mode 100644 index 0000000..a6db6ad --- /dev/null +++ b/tests/__snapshots__/esm-json @@ -0,0 +1,17 @@ +{ + "configContent": "import json from "@eslint/json"; + + +/** @type {import('eslint').Linter.Config[]} */ +export default [ + {files: ["**/*.json"], language: "json/json", ...json.configs.recommended}, +];", + "configFilename": "eslint.config.js", + "devDependencies": [ + "eslint", + "@eslint/json", + ], + "installFlags": [ + "-D", + ], +} \ No newline at end of file diff --git a/tests/__snapshots__/esm-json5 b/tests/__snapshots__/esm-json5 new file mode 100644 index 0000000..0bcde92 --- /dev/null +++ b/tests/__snapshots__/esm-json5 @@ -0,0 +1,17 @@ +{ + "configContent": "import json from "@eslint/json"; + + +/** @type {import('eslint').Linter.Config[]} */ +export default [ + {files: ["**/*.json5"], language: "json/json5", ...json.configs.recommended}, +];", + "configFilename": "eslint.config.js", + "devDependencies": [ + "eslint", + "@eslint/json", + ], + "installFlags": [ + "-D", + ], +} \ No newline at end of file diff --git a/tests/__snapshots__/esm-jsonc b/tests/__snapshots__/esm-jsonc new file mode 100644 index 0000000..c5d67a7 --- /dev/null +++ b/tests/__snapshots__/esm-jsonc @@ -0,0 +1,17 @@ +{ + "configContent": "import json from "@eslint/json"; + + +/** @type {import('eslint').Linter.Config[]} */ +export default [ + {files: ["**/*.jsonc"], language: "json/jsonc", ...json.configs.recommended}, +];", + "configFilename": "eslint.config.js", + "devDependencies": [ + "eslint", + "@eslint/json", + ], + "installFlags": [ + "-D", + ], +} \ No newline at end of file diff --git a/tests/__snapshots__/esm-markdown-commonmark b/tests/__snapshots__/esm-markdown-commonmark new file mode 100644 index 0000000..f11a518 --- /dev/null +++ b/tests/__snapshots__/esm-markdown-commonmark @@ -0,0 +1,17 @@ +{ + "configContent": "import markdown from "@eslint/markdown"; + + +/** @type {import('eslint').Linter.Config[]} */ +export default [ + ...markdown.configs.recommended, +];", + "configFilename": "eslint.config.js", + "devDependencies": [ + "eslint", + "@eslint/markdown", + ], + "installFlags": [ + "-D", + ], +} \ No newline at end of file diff --git a/tests/__snapshots__/esm-markdown-gfm b/tests/__snapshots__/esm-markdown-gfm new file mode 100644 index 0000000..492393a --- /dev/null +++ b/tests/__snapshots__/esm-markdown-gfm @@ -0,0 +1,18 @@ +{ + "configContent": "import markdown from "@eslint/markdown"; + + +/** @type {import('eslint').Linter.Config[]} */ +export default [ + ...markdown.configs.recommended, + {files: ["**/*.md"], language: "markdown/gfm"}, +];", + "configFilename": "eslint.config.js", + "devDependencies": [ + "eslint", + "@eslint/markdown", + ], + "installFlags": [ + "-D", + ], +} \ No newline at end of file diff --git a/tests/config-snapshots.spec.js b/tests/config-snapshots.spec.js index 6e9361b..a090e41 100644 --- a/tests/config-snapshots.spec.js +++ b/tests/config-snapshots.spec.js @@ -23,7 +23,14 @@ describe("generate config for esm projects", () => { env: ["browser", "node"] }; - const inputs = []; + const inputs = [ + { name: "esm-json", answers: { languages: ["json"] } }, + { name: "esm-json5", answers: { languages: ["json5"] } }, + { name: "esm-jsonc", answers: { languages: ["jsonc"] } }, + { name: "esm-markdown-commonmark", answers: { languages: ["md"], mdType: "commonmark" } }, + { name: "esm-markdown-gfm", answers: { languages: ["md"], mdType: "gfm" } }, + { name: "esm-javascript-json", answers: { languages: ["javascript", "json"], purpose: "problems", moduleType: "esm", framework: "none", useTs: false, env: ["node"] } } + ]; // generate all possible combinations for (let i = 0; i < choices.purpose.length; i++) { From db143816f7e9fa94195792f838d8e2794a5d3542 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Thu, 7 Nov 2024 11:41:20 +0800 Subject: [PATCH 05/10] fix: review suggestions --- lib/config-generator.js | 39 ++++++++++++++----- lib/questions.js | 19 +++++---- ...ript-json => esm-javascript-json-problems} | 0 .../{esm-json => esm-json-problems} | 0 tests/__snapshots__/esm-json-syntax | 17 ++++++++ .../{esm-json5 => esm-json5-problems} | 0 tests/__snapshots__/esm-json5-syntax | 17 ++++++++ .../{esm-jsonc => esm-jsonc-problems} | 0 tests/__snapshots__/esm-jsonc-syntax | 17 ++++++++ ...nmark => esm-markdown-commonmark-problems} | 0 .../esm-markdown-commonmark-syntax | 17 ++++++++ ...markdown-gfm => esm-markdown-gfm-problems} | 0 tests/__snapshots__/esm-markdown-gfm-syntax | 17 ++++++++ tests/config-snapshots.spec.js | 17 +++++--- 14 files changed, 135 insertions(+), 25 deletions(-) rename tests/__snapshots__/{esm-javascript-json => esm-javascript-json-problems} (100%) rename tests/__snapshots__/{esm-json => esm-json-problems} (100%) create mode 100644 tests/__snapshots__/esm-json-syntax rename tests/__snapshots__/{esm-json5 => esm-json5-problems} (100%) create mode 100644 tests/__snapshots__/esm-json5-syntax rename tests/__snapshots__/{esm-jsonc => esm-jsonc-problems} (100%) create mode 100644 tests/__snapshots__/esm-jsonc-syntax rename tests/__snapshots__/{esm-markdown-commonmark => esm-markdown-commonmark-problems} (100%) create mode 100644 tests/__snapshots__/esm-markdown-commonmark-syntax rename tests/__snapshots__/{esm-markdown-gfm => esm-markdown-gfm-problems} (100%) create mode 100644 tests/__snapshots__/esm-markdown-gfm-syntax diff --git a/lib/config-generator.js b/lib/config-generator.js index 9785442..b68e2a2 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -81,6 +81,7 @@ export class ConfigGenerator { let exportContent = ""; let needCompatHelper = false; const languages = this.answers.languages; + const purpose = this.answers.purpose; if (languages?.includes("javascript")) { if (this.answers.moduleType === "commonjs" || this.answers.moduleType === "script") { @@ -99,10 +100,10 @@ export class ConfigGenerator { exportContent += ` {languageOptions: { ${envContent[this.answers.env.join(",")]} }},\n`; } - if (this.answers.purpose === "syntax") { + if (purpose === "syntax") { // no need to install any plugin - } else if (this.answers.purpose === "problems") { + } else if (purpose === "problems") { this.result.devDependencies.push("@eslint/js"); importContent += "import pluginJs from \"@eslint/js\";\n"; exportContent += " pluginJs.configs.recommended,\n"; @@ -145,23 +146,43 @@ export class ConfigGenerator { importContent += "import json from \"@eslint/json\";\n"; } if (languages?.includes("json")) { - exportContent += " {files: [\"**/*.json\"], language: \"json/json\", ...json.configs.recommended},\n"; + const config = purpose === "syntax" + ? " {files: [\"**/*.json\"], language: \"json/json\"},\n" + : " {files: [\"**/*.json\"], language: \"json/json\", ...json.configs.recommended},\n"; + + exportContent += config; } if (languages?.includes("jsonc")) { - exportContent += " {files: [\"**/*.jsonc\"], language: \"json/jsonc\", ...json.configs.recommended},\n"; + const config = purpose === "syntax" + ? " {files: [\"**/*.jsonc\"], language: \"json/jsonc\"},\n" + : " {files: [\"**/*.jsonc\"], language: \"json/jsonc\", ...json.configs.recommended},\n"; + + exportContent += config; } if (languages?.includes("json5")) { - exportContent += " {files: [\"**/*.json5\"], language: \"json/json5\", ...json.configs.recommended},\n"; + const config = purpose === "syntax" + ? " {files: [\"**/*.json5\"], language: \"json/json5\"},\n" + : " {files: [\"**/*.json5\"], language: \"json/json5\", ...json.configs.recommended},\n"; + + exportContent += config; } if (languages?.includes("md")) { this.result.devDependencies.push("@eslint/markdown"); importContent += "import markdown from \"@eslint/markdown\";\n"; - exportContent += " ...markdown.configs.recommended,\n"; - if (this.answers.mdType === "gfm") { - // the default is commonmark - exportContent += " {files: [\"**/*.md\"], language: \"markdown/gfm\"},\n"; + if (purpose === "syntax") { + const config = this.answers.mdType === "commonmark" ? " {files: [\"**/*.md\"], language: \"markdown/commonmark\"},\n" : " {files: [\"**/*.md\"], language: \"markdown/gfm\"},\n"; + + exportContent += config; + } else if (purpose === "problems") { + exportContent += " ...markdown.configs.recommended,\n"; + + if (this.answers.mdType === "gfm") { + + // the default is commonmark + exportContent += " {files: [\"**/*.md\"], language: \"markdown/gfm\"},\n"; + } } } diff --git a/lib/questions.js b/lib/questions.js index 644e455..29c94f5 100644 --- a/lib/questions.js +++ b/lib/questions.js @@ -15,19 +15,18 @@ export const langQuestions = [{ { message: "Markdown", name: "md" } ], initial: 0 +}, { + type: "select", + name: "purpose", + message: "How would you like to use ESLint?", + initial: 1, + choices: [ + { message: "To check syntax only", name: "syntax" }, + { message: "To check syntax and find problems", name: "problems" } + ] }]; export const jsQuestions = [ - { - type: "select", - name: "purpose", - message: "How would you like to use ESLint?", - initial: 1, - choices: [ - { message: "To check syntax only", name: "syntax" }, - { message: "To check syntax and find problems", name: "problems" } - ] - }, { type: "select", name: "moduleType", diff --git a/tests/__snapshots__/esm-javascript-json b/tests/__snapshots__/esm-javascript-json-problems similarity index 100% rename from tests/__snapshots__/esm-javascript-json rename to tests/__snapshots__/esm-javascript-json-problems diff --git a/tests/__snapshots__/esm-json b/tests/__snapshots__/esm-json-problems similarity index 100% rename from tests/__snapshots__/esm-json rename to tests/__snapshots__/esm-json-problems diff --git a/tests/__snapshots__/esm-json-syntax b/tests/__snapshots__/esm-json-syntax new file mode 100644 index 0000000..5ba9404 --- /dev/null +++ b/tests/__snapshots__/esm-json-syntax @@ -0,0 +1,17 @@ +{ + "configContent": "import json from "@eslint/json"; + + +/** @type {import('eslint').Linter.Config[]} */ +export default [ + {files: ["**/*.json"], language: "json/json"}, +];", + "configFilename": "eslint.config.js", + "devDependencies": [ + "eslint", + "@eslint/json", + ], + "installFlags": [ + "-D", + ], +} \ No newline at end of file diff --git a/tests/__snapshots__/esm-json5 b/tests/__snapshots__/esm-json5-problems similarity index 100% rename from tests/__snapshots__/esm-json5 rename to tests/__snapshots__/esm-json5-problems diff --git a/tests/__snapshots__/esm-json5-syntax b/tests/__snapshots__/esm-json5-syntax new file mode 100644 index 0000000..5b3eb35 --- /dev/null +++ b/tests/__snapshots__/esm-json5-syntax @@ -0,0 +1,17 @@ +{ + "configContent": "import json from "@eslint/json"; + + +/** @type {import('eslint').Linter.Config[]} */ +export default [ + {files: ["**/*.json5"], language: "json/json5"}, +];", + "configFilename": "eslint.config.js", + "devDependencies": [ + "eslint", + "@eslint/json", + ], + "installFlags": [ + "-D", + ], +} \ No newline at end of file diff --git a/tests/__snapshots__/esm-jsonc b/tests/__snapshots__/esm-jsonc-problems similarity index 100% rename from tests/__snapshots__/esm-jsonc rename to tests/__snapshots__/esm-jsonc-problems diff --git a/tests/__snapshots__/esm-jsonc-syntax b/tests/__snapshots__/esm-jsonc-syntax new file mode 100644 index 0000000..00e8548 --- /dev/null +++ b/tests/__snapshots__/esm-jsonc-syntax @@ -0,0 +1,17 @@ +{ + "configContent": "import json from "@eslint/json"; + + +/** @type {import('eslint').Linter.Config[]} */ +export default [ + {files: ["**/*.jsonc"], language: "json/jsonc"}, +];", + "configFilename": "eslint.config.js", + "devDependencies": [ + "eslint", + "@eslint/json", + ], + "installFlags": [ + "-D", + ], +} \ No newline at end of file diff --git a/tests/__snapshots__/esm-markdown-commonmark b/tests/__snapshots__/esm-markdown-commonmark-problems similarity index 100% rename from tests/__snapshots__/esm-markdown-commonmark rename to tests/__snapshots__/esm-markdown-commonmark-problems diff --git a/tests/__snapshots__/esm-markdown-commonmark-syntax b/tests/__snapshots__/esm-markdown-commonmark-syntax new file mode 100644 index 0000000..3013f9f --- /dev/null +++ b/tests/__snapshots__/esm-markdown-commonmark-syntax @@ -0,0 +1,17 @@ +{ + "configContent": "import markdown from "@eslint/markdown"; + + +/** @type {import('eslint').Linter.Config[]} */ +export default [ + {files: ["**/*.md"], language: "markdown/commonmark"}, +];", + "configFilename": "eslint.config.js", + "devDependencies": [ + "eslint", + "@eslint/markdown", + ], + "installFlags": [ + "-D", + ], +} \ No newline at end of file diff --git a/tests/__snapshots__/esm-markdown-gfm b/tests/__snapshots__/esm-markdown-gfm-problems similarity index 100% rename from tests/__snapshots__/esm-markdown-gfm rename to tests/__snapshots__/esm-markdown-gfm-problems diff --git a/tests/__snapshots__/esm-markdown-gfm-syntax b/tests/__snapshots__/esm-markdown-gfm-syntax new file mode 100644 index 0000000..c7ad822 --- /dev/null +++ b/tests/__snapshots__/esm-markdown-gfm-syntax @@ -0,0 +1,17 @@ +{ + "configContent": "import markdown from "@eslint/markdown"; + + +/** @type {import('eslint').Linter.Config[]} */ +export default [ + {files: ["**/*.md"], language: "markdown/gfm"}, +];", + "configFilename": "eslint.config.js", + "devDependencies": [ + "eslint", + "@eslint/markdown", + ], + "installFlags": [ + "-D", + ], +} \ No newline at end of file diff --git a/tests/config-snapshots.spec.js b/tests/config-snapshots.spec.js index a090e41..0f090e3 100644 --- a/tests/config-snapshots.spec.js +++ b/tests/config-snapshots.spec.js @@ -24,12 +24,17 @@ describe("generate config for esm projects", () => { }; const inputs = [ - { name: "esm-json", answers: { languages: ["json"] } }, - { name: "esm-json5", answers: { languages: ["json5"] } }, - { name: "esm-jsonc", answers: { languages: ["jsonc"] } }, - { name: "esm-markdown-commonmark", answers: { languages: ["md"], mdType: "commonmark" } }, - { name: "esm-markdown-gfm", answers: { languages: ["md"], mdType: "gfm" } }, - { name: "esm-javascript-json", answers: { languages: ["javascript", "json"], purpose: "problems", moduleType: "esm", framework: "none", useTs: false, env: ["node"] } } + { name: "esm-json-syntax", answers: { languages: ["json"], purpose: "syntax" } }, + { name: "esm-json-problems", answers: { languages: ["json"], purpose: "problems" } }, + { name: "esm-json5-syntax", answers: { languages: ["json5"], purpose: "syntax" } }, + { name: "esm-json5-problems", answers: { languages: ["json5"], purpose: "problems" } }, + { name: "esm-jsonc-syntax", answers: { languages: ["jsonc"], purpose: "syntax" } }, + { name: "esm-jsonc-problems", answers: { languages: ["jsonc"], purpose: "problems" } }, + { name: "esm-markdown-commonmark-syntax", answers: { languages: ["md"], mdType: "commonmark", purpose: "syntax" } }, + { name: "esm-markdown-commonmark-problems", answers: { languages: ["md"], mdType: "commonmark", purpose: "problems" } }, + { name: "esm-markdown-gfm-syntax", answers: { languages: ["md"], mdType: "gfm", purpose: "syntax" } }, + { name: "esm-markdown-gfm-problems", answers: { languages: ["md"], mdType: "gfm", purpose: "problems" } }, + { name: "esm-javascript-json-problems", answers: { languages: ["javascript", "json"], purpose: "problems", moduleType: "esm", framework: "none", useTs: false, env: ["node"] } } ]; // generate all possible combinations From 08bd3439f14e45269e8d39974324c3e4e4dcb5ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Fri, 8 Nov 2024 09:48:21 +0800 Subject: [PATCH 06/10] fix: add global ignores --- bin/create-config.js | 2 +- lib/config-generator.js | 2 ++ tests/__snapshots__/config@eslint-config-airbnb | 1 + tests/__snapshots__/config@eslint-config-airbnb-base | 1 + tests/__snapshots__/config@eslint-config-standard | 1 + tests/__snapshots__/config@eslint-config-standard-flat | 1 + tests/__snapshots__/config@eslint-config-standard-flat2 | 1 + tests/__snapshots__/config@eslint-config-xo | 1 + tests/__snapshots__/esm-json-problems | 1 + tests/__snapshots__/esm-json-syntax | 1 + tests/__snapshots__/esm-json5-problems | 1 + tests/__snapshots__/esm-json5-syntax | 1 + tests/__snapshots__/esm-jsonc-problems | 1 + tests/__snapshots__/esm-jsonc-syntax | 1 + tests/__snapshots__/esm-markdown-commonmark-problems | 1 + tests/__snapshots__/esm-markdown-commonmark-syntax | 1 + tests/__snapshots__/esm-markdown-gfm-problems | 1 + tests/__snapshots__/esm-markdown-gfm-syntax | 1 + 18 files changed, 19 insertions(+), 1 deletion(-) diff --git a/bin/create-config.js b/bin/create-config.js index 8f83892..9b24348 100755 --- a/bin/create-config.js +++ b/bin/create-config.js @@ -36,7 +36,7 @@ if (sharedConfigIndex === -1) { // passed "--config" const packageName = argv[sharedConfigIndex + 1]; const type = argv.includes("--eslintrc") ? "eslintrc" : "flat"; - const answers = { config: { packageName, type } }; + const answers = { config: { packageName, type }, language: ["javascript"] }; const generator = new ConfigGenerator({ cwd, packageJsonPath, answers }); generator.calc(); diff --git a/lib/config-generator.js b/lib/config-generator.js index b68e2a2..36c65e6 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -139,6 +139,8 @@ export class ConfigGenerator { importContent += "import pluginReact from \"eslint-plugin-react\";\n"; exportContent += " pluginReact.configs.flat.recommended,\n"; } + } else { + exportContent += " {ignores: [\"**/*.js\", \"**/*.cjs\", \"**/*.mjs\"]},\n"; } if (languages?.some(item => item.startsWith("json"))) { diff --git a/tests/__snapshots__/config@eslint-config-airbnb b/tests/__snapshots__/config@eslint-config-airbnb index aba542b..27c3dd0 100644 --- a/tests/__snapshots__/config@eslint-config-airbnb +++ b/tests/__snapshots__/config@eslint-config-airbnb @@ -12,6 +12,7 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, ...compat.extends("airbnb"), ];", "configFilename": "eslint.config.mjs", diff --git a/tests/__snapshots__/config@eslint-config-airbnb-base b/tests/__snapshots__/config@eslint-config-airbnb-base index 33e4a58..3e6c899 100644 --- a/tests/__snapshots__/config@eslint-config-airbnb-base +++ b/tests/__snapshots__/config@eslint-config-airbnb-base @@ -12,6 +12,7 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, ...compat.extends("airbnb-base"), ];", "configFilename": "eslint.config.mjs", diff --git a/tests/__snapshots__/config@eslint-config-standard b/tests/__snapshots__/config@eslint-config-standard index 9894473..1b162ac 100644 --- a/tests/__snapshots__/config@eslint-config-standard +++ b/tests/__snapshots__/config@eslint-config-standard @@ -12,6 +12,7 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, ...compat.extends("standard"), ];", "configFilename": "eslint.config.mjs", diff --git a/tests/__snapshots__/config@eslint-config-standard-flat b/tests/__snapshots__/config@eslint-config-standard-flat index d08ab36..7c4d513 100644 --- a/tests/__snapshots__/config@eslint-config-standard-flat +++ b/tests/__snapshots__/config@eslint-config-standard-flat @@ -4,6 +4,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, ...[].concat(config), ];", "configFilename": "eslint.config.mjs", diff --git a/tests/__snapshots__/config@eslint-config-standard-flat2 b/tests/__snapshots__/config@eslint-config-standard-flat2 index d08ab36..7c4d513 100644 --- a/tests/__snapshots__/config@eslint-config-standard-flat2 +++ b/tests/__snapshots__/config@eslint-config-standard-flat2 @@ -4,6 +4,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, ...[].concat(config), ];", "configFilename": "eslint.config.mjs", diff --git a/tests/__snapshots__/config@eslint-config-xo b/tests/__snapshots__/config@eslint-config-xo index bfb52df..cb391fd 100644 --- a/tests/__snapshots__/config@eslint-config-xo +++ b/tests/__snapshots__/config@eslint-config-xo @@ -4,6 +4,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, ...[].concat(config), ];", "configFilename": "eslint.config.mjs", diff --git a/tests/__snapshots__/esm-json-problems b/tests/__snapshots__/esm-json-problems index a6db6ad..48948c9 100644 --- a/tests/__snapshots__/esm-json-problems +++ b/tests/__snapshots__/esm-json-problems @@ -4,6 +4,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, {files: ["**/*.json"], language: "json/json", ...json.configs.recommended}, ];", "configFilename": "eslint.config.js", diff --git a/tests/__snapshots__/esm-json-syntax b/tests/__snapshots__/esm-json-syntax index 5ba9404..41b9f0b 100644 --- a/tests/__snapshots__/esm-json-syntax +++ b/tests/__snapshots__/esm-json-syntax @@ -4,6 +4,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, {files: ["**/*.json"], language: "json/json"}, ];", "configFilename": "eslint.config.js", diff --git a/tests/__snapshots__/esm-json5-problems b/tests/__snapshots__/esm-json5-problems index 0bcde92..dc1da37 100644 --- a/tests/__snapshots__/esm-json5-problems +++ b/tests/__snapshots__/esm-json5-problems @@ -4,6 +4,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, {files: ["**/*.json5"], language: "json/json5", ...json.configs.recommended}, ];", "configFilename": "eslint.config.js", diff --git a/tests/__snapshots__/esm-json5-syntax b/tests/__snapshots__/esm-json5-syntax index 5b3eb35..bfd022e 100644 --- a/tests/__snapshots__/esm-json5-syntax +++ b/tests/__snapshots__/esm-json5-syntax @@ -4,6 +4,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, {files: ["**/*.json5"], language: "json/json5"}, ];", "configFilename": "eslint.config.js", diff --git a/tests/__snapshots__/esm-jsonc-problems b/tests/__snapshots__/esm-jsonc-problems index c5d67a7..da5dc57 100644 --- a/tests/__snapshots__/esm-jsonc-problems +++ b/tests/__snapshots__/esm-jsonc-problems @@ -4,6 +4,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, {files: ["**/*.jsonc"], language: "json/jsonc", ...json.configs.recommended}, ];", "configFilename": "eslint.config.js", diff --git a/tests/__snapshots__/esm-jsonc-syntax b/tests/__snapshots__/esm-jsonc-syntax index 00e8548..4028a57 100644 --- a/tests/__snapshots__/esm-jsonc-syntax +++ b/tests/__snapshots__/esm-jsonc-syntax @@ -4,6 +4,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, {files: ["**/*.jsonc"], language: "json/jsonc"}, ];", "configFilename": "eslint.config.js", diff --git a/tests/__snapshots__/esm-markdown-commonmark-problems b/tests/__snapshots__/esm-markdown-commonmark-problems index f11a518..4f5f4c8 100644 --- a/tests/__snapshots__/esm-markdown-commonmark-problems +++ b/tests/__snapshots__/esm-markdown-commonmark-problems @@ -4,6 +4,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, ...markdown.configs.recommended, ];", "configFilename": "eslint.config.js", diff --git a/tests/__snapshots__/esm-markdown-commonmark-syntax b/tests/__snapshots__/esm-markdown-commonmark-syntax index 3013f9f..1141464 100644 --- a/tests/__snapshots__/esm-markdown-commonmark-syntax +++ b/tests/__snapshots__/esm-markdown-commonmark-syntax @@ -4,6 +4,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, {files: ["**/*.md"], language: "markdown/commonmark"}, ];", "configFilename": "eslint.config.js", diff --git a/tests/__snapshots__/esm-markdown-gfm-problems b/tests/__snapshots__/esm-markdown-gfm-problems index 492393a..a3d9072 100644 --- a/tests/__snapshots__/esm-markdown-gfm-problems +++ b/tests/__snapshots__/esm-markdown-gfm-problems @@ -4,6 +4,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, ...markdown.configs.recommended, {files: ["**/*.md"], language: "markdown/gfm"}, ];", diff --git a/tests/__snapshots__/esm-markdown-gfm-syntax b/tests/__snapshots__/esm-markdown-gfm-syntax index c7ad822..4ba1ac9 100644 --- a/tests/__snapshots__/esm-markdown-gfm-syntax +++ b/tests/__snapshots__/esm-markdown-gfm-syntax @@ -4,6 +4,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ + {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, {files: ["**/*.md"], language: "markdown/gfm"}, ];", "configFilename": "eslint.config.js", From 16e8eab8fceca0c705355130acaddf067f45a560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Fri, 8 Nov 2024 10:11:45 +0800 Subject: [PATCH 07/10] fix: add plugins --- lib/config-generator.js | 10 ++++++---- tests/__snapshots__/esm-json-syntax | 2 +- tests/__snapshots__/esm-json5-syntax | 2 +- tests/__snapshots__/esm-jsonc-syntax | 2 +- tests/__snapshots__/esm-markdown-commonmark-syntax | 2 +- tests/__snapshots__/esm-markdown-gfm-syntax | 2 +- 6 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/config-generator.js b/lib/config-generator.js index 36c65e6..35193bf 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -149,21 +149,21 @@ export class ConfigGenerator { } if (languages?.includes("json")) { const config = purpose === "syntax" - ? " {files: [\"**/*.json\"], language: \"json/json\"},\n" + ? " {files: [\"**/*.json\"], plugins: {json}, language: \"json/json\"},\n" : " {files: [\"**/*.json\"], language: \"json/json\", ...json.configs.recommended},\n"; exportContent += config; } if (languages?.includes("jsonc")) { const config = purpose === "syntax" - ? " {files: [\"**/*.jsonc\"], language: \"json/jsonc\"},\n" + ? " {files: [\"**/*.jsonc\"], plugins: {json}, language: \"json/jsonc\"},\n" : " {files: [\"**/*.jsonc\"], language: \"json/jsonc\", ...json.configs.recommended},\n"; exportContent += config; } if (languages?.includes("json5")) { const config = purpose === "syntax" - ? " {files: [\"**/*.json5\"], language: \"json/json5\"},\n" + ? " {files: [\"**/*.json5\"], plugins: {json}, language: \"json/json5\"},\n" : " {files: [\"**/*.json5\"], language: \"json/json5\", ...json.configs.recommended},\n"; exportContent += config; @@ -174,7 +174,9 @@ export class ConfigGenerator { importContent += "import markdown from \"@eslint/markdown\";\n"; if (purpose === "syntax") { - const config = this.answers.mdType === "commonmark" ? " {files: [\"**/*.md\"], language: \"markdown/commonmark\"},\n" : " {files: [\"**/*.md\"], language: \"markdown/gfm\"},\n"; + const config = this.answers.mdType === "commonmark" + ? " {files: [\"**/*.md\"], plugins: {markdown}, language: \"markdown/commonmark\"},\n" + : " {files: [\"**/*.md\"], plugins: {markdown}, language: \"markdown/gfm\"},\n"; exportContent += config; } else if (purpose === "problems") { diff --git a/tests/__snapshots__/esm-json-syntax b/tests/__snapshots__/esm-json-syntax index 41b9f0b..341d54c 100644 --- a/tests/__snapshots__/esm-json-syntax +++ b/tests/__snapshots__/esm-json-syntax @@ -5,7 +5,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, - {files: ["**/*.json"], language: "json/json"}, + {files: ["**/*.json"], plugins: {json}, language: "json/json"}, ];", "configFilename": "eslint.config.js", "devDependencies": [ diff --git a/tests/__snapshots__/esm-json5-syntax b/tests/__snapshots__/esm-json5-syntax index bfd022e..df28057 100644 --- a/tests/__snapshots__/esm-json5-syntax +++ b/tests/__snapshots__/esm-json5-syntax @@ -5,7 +5,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, - {files: ["**/*.json5"], language: "json/json5"}, + {files: ["**/*.json5"], plugins: {json}, language: "json/json5"}, ];", "configFilename": "eslint.config.js", "devDependencies": [ diff --git a/tests/__snapshots__/esm-jsonc-syntax b/tests/__snapshots__/esm-jsonc-syntax index 4028a57..07a7989 100644 --- a/tests/__snapshots__/esm-jsonc-syntax +++ b/tests/__snapshots__/esm-jsonc-syntax @@ -5,7 +5,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, - {files: ["**/*.jsonc"], language: "json/jsonc"}, + {files: ["**/*.jsonc"], plugins: {json}, language: "json/jsonc"}, ];", "configFilename": "eslint.config.js", "devDependencies": [ diff --git a/tests/__snapshots__/esm-markdown-commonmark-syntax b/tests/__snapshots__/esm-markdown-commonmark-syntax index 1141464..0f827ff 100644 --- a/tests/__snapshots__/esm-markdown-commonmark-syntax +++ b/tests/__snapshots__/esm-markdown-commonmark-syntax @@ -5,7 +5,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, - {files: ["**/*.md"], language: "markdown/commonmark"}, + {files: ["**/*.md"], plugins: {markdown}, language: "markdown/commonmark"}, ];", "configFilename": "eslint.config.js", "devDependencies": [ diff --git a/tests/__snapshots__/esm-markdown-gfm-syntax b/tests/__snapshots__/esm-markdown-gfm-syntax index 4ba1ac9..fa0de92 100644 --- a/tests/__snapshots__/esm-markdown-gfm-syntax +++ b/tests/__snapshots__/esm-markdown-gfm-syntax @@ -5,7 +5,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, - {files: ["**/*.md"], language: "markdown/gfm"}, + {files: ["**/*.md"], plugins: {markdown}, language: "markdown/gfm"}, ];", "configFilename": "eslint.config.js", "devDependencies": [ From afb72a1828aab8031adf53e2ec6d42bd2d272452 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Fri, 8 Nov 2024 16:11:57 +0800 Subject: [PATCH 08/10] Update bin/create-config.js Co-authored-by: Milos Djermanovic --- bin/create-config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/create-config.js b/bin/create-config.js index 9b24348..48a400e 100755 --- a/bin/create-config.js +++ b/bin/create-config.js @@ -36,7 +36,7 @@ if (sharedConfigIndex === -1) { // passed "--config" const packageName = argv[sharedConfigIndex + 1]; const type = argv.includes("--eslintrc") ? "eslintrc" : "flat"; - const answers = { config: { packageName, type }, language: ["javascript"] }; + const answers = { config: { packageName, type }, languages: ["javascript"] }; const generator = new ConfigGenerator({ cwd, packageJsonPath, answers }); generator.calc(); From effc9b1575e1b1254ac402ba4ed1b053bd06f5b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=94=AF=E7=84=B6?= Date: Fri, 8 Nov 2024 20:12:34 +0800 Subject: [PATCH 09/10] Update lib/questions.js Co-authored-by: Milos Djermanovic --- lib/questions.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/questions.js b/lib/questions.js index 29c94f5..377c53c 100644 --- a/lib/questions.js +++ b/lib/questions.js @@ -71,7 +71,7 @@ export const jsQuestions = [ export const mdQuestions = [{ type: "select", name: "mdType", - message: "What flavor of Markdown to you want to lint?", + message: "What flavor of Markdown do you want to lint?", initial: 0, choices: [ { message: "CommonMark", name: "commonmark" }, From cabfdc90cedcef655dc02acefe496acb3732b1d3 Mon Sep 17 00:00:00 2001 From: "WEIRAN\\weira" Date: Sun, 10 Nov 2024 10:59:06 +0800 Subject: [PATCH 10/10] fix: languages defaults to ["javascript"] --- lib/config-generator.js | 14 +++++++------- tests/__snapshots__/config@eslint-config-airbnb | 1 - .../__snapshots__/config@eslint-config-airbnb-base | 1 - tests/__snapshots__/config@eslint-config-standard | 1 - .../config@eslint-config-standard-flat | 1 - .../config@eslint-config-standard-flat2 | 1 - tests/__snapshots__/config@eslint-config-xo | 1 - 7 files changed, 7 insertions(+), 13 deletions(-) diff --git a/lib/config-generator.js b/lib/config-generator.js index 35193bf..d28d817 100644 --- a/lib/config-generator.js +++ b/lib/config-generator.js @@ -80,10 +80,10 @@ export class ConfigGenerator { let importContent = ""; let exportContent = ""; let needCompatHelper = false; - const languages = this.answers.languages; + const languages = this.answers.languages ?? ["javascript"]; const purpose = this.answers.purpose; - if (languages?.includes("javascript")) { + if (languages.includes("javascript")) { if (this.answers.moduleType === "commonjs" || this.answers.moduleType === "script") { exportContent += ` {files: ["**/*.js"], languageOptions: {sourceType: "${this.answers.moduleType}"}},\n`; } @@ -143,25 +143,25 @@ export class ConfigGenerator { exportContent += " {ignores: [\"**/*.js\", \"**/*.cjs\", \"**/*.mjs\"]},\n"; } - if (languages?.some(item => item.startsWith("json"))) { + if (languages.some(item => item.startsWith("json"))) { this.result.devDependencies.push("@eslint/json"); importContent += "import json from \"@eslint/json\";\n"; } - if (languages?.includes("json")) { + if (languages.includes("json")) { const config = purpose === "syntax" ? " {files: [\"**/*.json\"], plugins: {json}, language: \"json/json\"},\n" : " {files: [\"**/*.json\"], language: \"json/json\", ...json.configs.recommended},\n"; exportContent += config; } - if (languages?.includes("jsonc")) { + if (languages.includes("jsonc")) { const config = purpose === "syntax" ? " {files: [\"**/*.jsonc\"], plugins: {json}, language: \"json/jsonc\"},\n" : " {files: [\"**/*.jsonc\"], language: \"json/jsonc\", ...json.configs.recommended},\n"; exportContent += config; } - if (languages?.includes("json5")) { + if (languages.includes("json5")) { const config = purpose === "syntax" ? " {files: [\"**/*.json5\"], plugins: {json}, language: \"json/json5\"},\n" : " {files: [\"**/*.json5\"], language: \"json/json5\", ...json.configs.recommended},\n"; @@ -169,7 +169,7 @@ export class ConfigGenerator { exportContent += config; } - if (languages?.includes("md")) { + if (languages.includes("md")) { this.result.devDependencies.push("@eslint/markdown"); importContent += "import markdown from \"@eslint/markdown\";\n"; diff --git a/tests/__snapshots__/config@eslint-config-airbnb b/tests/__snapshots__/config@eslint-config-airbnb index 27c3dd0..aba542b 100644 --- a/tests/__snapshots__/config@eslint-config-airbnb +++ b/tests/__snapshots__/config@eslint-config-airbnb @@ -12,7 +12,6 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug /** @type {import('eslint').Linter.Config[]} */ export default [ - {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, ...compat.extends("airbnb"), ];", "configFilename": "eslint.config.mjs", diff --git a/tests/__snapshots__/config@eslint-config-airbnb-base b/tests/__snapshots__/config@eslint-config-airbnb-base index 3e6c899..33e4a58 100644 --- a/tests/__snapshots__/config@eslint-config-airbnb-base +++ b/tests/__snapshots__/config@eslint-config-airbnb-base @@ -12,7 +12,6 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug /** @type {import('eslint').Linter.Config[]} */ export default [ - {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, ...compat.extends("airbnb-base"), ];", "configFilename": "eslint.config.mjs", diff --git a/tests/__snapshots__/config@eslint-config-standard b/tests/__snapshots__/config@eslint-config-standard index 1b162ac..9894473 100644 --- a/tests/__snapshots__/config@eslint-config-standard +++ b/tests/__snapshots__/config@eslint-config-standard @@ -12,7 +12,6 @@ const compat = new FlatCompat({baseDirectory: __dirname, recommendedConfig: plug /** @type {import('eslint').Linter.Config[]} */ export default [ - {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, ...compat.extends("standard"), ];", "configFilename": "eslint.config.mjs", diff --git a/tests/__snapshots__/config@eslint-config-standard-flat b/tests/__snapshots__/config@eslint-config-standard-flat index 7c4d513..d08ab36 100644 --- a/tests/__snapshots__/config@eslint-config-standard-flat +++ b/tests/__snapshots__/config@eslint-config-standard-flat @@ -4,7 +4,6 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ - {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, ...[].concat(config), ];", "configFilename": "eslint.config.mjs", diff --git a/tests/__snapshots__/config@eslint-config-standard-flat2 b/tests/__snapshots__/config@eslint-config-standard-flat2 index 7c4d513..d08ab36 100644 --- a/tests/__snapshots__/config@eslint-config-standard-flat2 +++ b/tests/__snapshots__/config@eslint-config-standard-flat2 @@ -4,7 +4,6 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ - {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, ...[].concat(config), ];", "configFilename": "eslint.config.mjs", diff --git a/tests/__snapshots__/config@eslint-config-xo b/tests/__snapshots__/config@eslint-config-xo index cb391fd..bfb52df 100644 --- a/tests/__snapshots__/config@eslint-config-xo +++ b/tests/__snapshots__/config@eslint-config-xo @@ -4,7 +4,6 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ - {ignores: ["**/*.js", "**/*.cjs", "**/*.mjs"]}, ...[].concat(config), ];", "configFilename": "eslint.config.mjs",