From f5be5b6f5fffa21bfee6eaef0711824abe37cdd5 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] 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} | 2 +- .../esm-markdown-commonmark-syntax | 17 ++++++++ ...markdown-gfm => esm-markdown-gfm-problems} | 2 +- tests/__snapshots__/esm-markdown-gfm-syntax | 17 ++++++++ tests/config-snapshots.spec.js | 17 +++++--- 14 files changed, 137 insertions(+), 27 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} (88%) create mode 100644 tests/__snapshots__/esm-markdown-commonmark-syntax rename tests/__snapshots__/{esm-markdown-gfm => esm-markdown-gfm-problems} (90%) create mode 100644 tests/__snapshots__/esm-markdown-gfm-syntax diff --git a/lib/config-generator.js b/lib/config-generator.js index 9785442..8b12a3e 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 88% rename from tests/__snapshots__/esm-markdown-commonmark rename to tests/__snapshots__/esm-markdown-commonmark-problems index f11a518..6c6fbf0 100644 --- a/tests/__snapshots__/esm-markdown-commonmark +++ b/tests/__snapshots__/esm-markdown-commonmark-problems @@ -4,7 +4,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ - ...markdown.configs.recommended, + ...markdown.configs.recommended, ];", "configFilename": "eslint.config.js", "devDependencies": [ 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 90% rename from tests/__snapshots__/esm-markdown-gfm rename to tests/__snapshots__/esm-markdown-gfm-problems index 492393a..656f418 100644 --- a/tests/__snapshots__/esm-markdown-gfm +++ b/tests/__snapshots__/esm-markdown-gfm-problems @@ -4,7 +4,7 @@ /** @type {import('eslint').Linter.Config[]} */ export default [ - ...markdown.configs.recommended, + ...markdown.configs.recommended, {files: ["**/*.md"], language: "markdown/gfm"}, ];", "configFilename": "eslint.config.js", 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