Skip to content

Commit

Permalink
fix: rewview suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
aladdin-add committed Nov 7, 2024
1 parent cd750b8 commit 4ece9d8
Show file tree
Hide file tree
Showing 14 changed files with 129 additions and 22 deletions.
29 changes: 23 additions & 6 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand All @@ -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";
Expand Down Expand Up @@ -145,19 +146,35 @@ 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";

const config = purpose === "syntax" ? " {files: [\"**/*.md\"], language: \"markdown/commonmark\"},\n" : " ...markdown.configs.recommended,\n";

exportContent += config;

if (this.answers.mdType === "gfm") {

// the default is commonmark
Expand Down
19 changes: 9 additions & 10 deletions lib/questions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
File renamed without changes.
File renamed without changes.
17 changes: 17 additions & 0 deletions tests/__snapshots__/esm-json-syntax
Original file line number Diff line number Diff line change
@@ -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",
],
}
File renamed without changes.
17 changes: 17 additions & 0 deletions tests/__snapshots__/esm-json5-syntax
Original file line number Diff line number Diff line change
@@ -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",
],
}
File renamed without changes.
17 changes: 17 additions & 0 deletions tests/__snapshots__/esm-jsonc-syntax
Original file line number Diff line number Diff line change
@@ -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",
],
}
File renamed without changes.
17 changes: 17 additions & 0 deletions tests/__snapshots__/esm-markdown-commonmark-syntax
Original file line number Diff line number Diff line change
@@ -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",
],
}
File renamed without changes.
18 changes: 18 additions & 0 deletions tests/__snapshots__/esm-markdown-gfm-syntax
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"configContent": "import markdown from "@eslint/markdown";


/** @type {import('eslint').Linter.Config[]} */
export default [
{files: ["**/*.md"], language: "markdown/commonmark"},
{files: ["**/*.md"], language: "markdown/gfm"},
];",
"configFilename": "eslint.config.js",
"devDependencies": [
"eslint",
"@eslint/markdown",
],
"installFlags": [
"-D",
],
}
17 changes: 11 additions & 6 deletions tests/config-snapshots.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4ece9d8

Please sign in to comment.