-
-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: support languages json/markdown #151
Draft
aladdin-add
wants to merge
10
commits into
main
Choose a base branch
from
issue148
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c5da34e
feat: add question langauges
aladdin-add bbb24ef
chore: languages => ["javascript"]
aladdin-add 77337f8
feat: support json/md
aladdin-add cd750b8
chore: add tests for json/md
aladdin-add db14381
fix: review suggestions
aladdin-add 08bd343
fix: add global ignores
aladdin-add 16e8eab
fix: add plugins
aladdin-add afb72a1
Update bin/create-config.js
aladdin-add effc9b1
Update lib/questions.js
aladdin-add cabfdc9
fix: languages defaults to ["javascript"]
aladdin-add File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
|
@@ -10,6 +10,18 @@ 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"; | ||
|
||
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. | ||
|
@@ -40,65 +52,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 <space> to select, <a> to toggle all, <i> 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 +69,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" | ||
|
@@ -115,73 +78,91 @@ 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; | ||
const languages = this.answers.languages; | ||
|
||
if (this.answers.moduleType === "commonjs" || this.answers.moduleType === "script") { | ||
exportContent += ` {files: ["**/*.js"], languageOptions: {sourceType: "${this.answers.moduleType}"}},\n`; | ||
} | ||
if (languages?.includes("javascript")) { | ||
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}" | ||
}; | ||
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`; | ||
} | ||
exportContent += ` {languageOptions: { ${envContent[this.answers.env.join(",")]} }},\n`; | ||
} | ||
|
||
if (this.answers.purpose === "syntax") { | ||
if (this.answers.purpose === "syntax") { | ||
|
||
// 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"; | ||
} | ||
// 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"; | ||
} | ||
|
||
if (this.answers.language === "typescript") { | ||
extensions.push("ts"); | ||
this.result.devDependencies.push("typescript-eslint"); | ||
importContent += "import tseslint from \"typescript-eslint\";\n"; | ||
exportContent += " ...tseslint.configs.recommended,\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.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 (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"; | ||
|
||
// https://eslint.vuejs.org/user-guide/#how-to-use-a-custom-parser | ||
if (this.answers.language === "typescript") { | ||
exportContent += " {files: [\"**/*.vue\"], languageOptions: {parserOptions: {parser: tseslint.parser}}},\n"; | ||
// https://eslint.vuejs.org/user-guide/#how-to-use-a-custom-parser | ||
if (useTs) { | ||
exportContent += " {files: [\"**/*.vue\"], languageOptions: {parserOptions: {parser: tseslint.parser}}},\n"; | ||
} | ||
} | ||
} | ||
|
||
if (this.answers.framework === "react") { | ||
extensions.push("jsx"); | ||
if (this.answers.framework === "react") { | ||
extensions.push("jsx"); | ||
|
||
if (this.answers.language === "typescript") { | ||
extensions.push("tsx"); | ||
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 (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") { | ||
|
||
this.result.devDependencies.push("eslint-plugin-react"); | ||
importContent += "import pluginReact from \"eslint-plugin-react\";\n"; | ||
exportContent += " pluginReact.configs.flat.recommended,\n"; | ||
// the default is commonmark | ||
exportContent += " {files: [\"**/*.md\"], language: \"markdown/gfm\"},\n"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A better UX if it could export a predefined config for gfm? |
||
} | ||
} | ||
|
||
if (this.answers.config) { | ||
|
@@ -243,24 +224,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) { | ||
|
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 |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* @fileoverview all the questions for the quiz | ||
* @author 唯然<[email protected]> | ||
*/ | ||
|
||
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" } | ||
] | ||
}, | ||
aladdin-add marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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 <space> to select, <a> to toggle all, <i> 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?", | ||
aladdin-add marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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; | ||
} | ||
} | ||
]; |
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 |
---|---|---|
@@ -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", | ||
], | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
didn't use
skip
- as there is a known issue when using multi-selection: enquirer/enquirer#298