็ฎไฝไธญๆ
็ป่ฟๅคๅนดๅฎ่ทต๏ผๆไปฌๅ็ฐ๏ผๆๅฝฑๅ็ฐไปฃ JavaScript ๅทฅ็จ็ไปฃ็ ่ดจ้ๅๅผๅไฝ้ช็ไธป่ฆๆ 3 ไธชๆน้ข๏ผ
- ็ฑปๅๅฎๅ จ๏ผ็จไบๆๅๅ็ฐ็ฑปๅใๆผๅ้่ฏฏ๏ผไพๅฆๅฏน่ฑกๆนๆณๆฏๅฆๆญฃ็กฎ่ฐ็จใๅฝๆฐๅๆฐไผ ้็็ฑปๅๆฏๅฆ็ฌฆๅๅฝๆฐไฝ็ๆๆ็ญใ
- Formatting๏ผ็จไบ็ปไธๆ ผๅผ๏ผๆๅไปฃ็ ๅฏ่ฏปๆง๏ผๅๅฐไปฃ็ ๅฒ็ชใไธป่ฆๅ ณๆณจไพๅฆ็ผฉ่ฟใๆข่กใๅ/ๅๅผๅทใๅธฆ/ไธๅธฆๅๅท็ญ้ฎ้ขใ
- Linting๏ผ็จไบๆๅๅ็ฐ้ป่พๆผๆดๅ็ณ็ฒ็จๆณ๏ผๅๅฐ Bug๏ผ้ไฝ็ปดๆคๆๆฌใๅ
ถๅ
ณๆณจ็นๅฏไปฅๆฏ้คไบ
Formattingไนๅค็ไปปไฝๅฐๆน๏ผไพๅฆ้ๅคๅฎไนๅ้ใswitch ไธๅธฆ breakใๅๅคๆๅบฆ็ญใ
่ฟ 3 ไธชๆน้ขไนๆฏๆดๅ ่ฟ็่ฟ่กๆถ Deno ๆๅ ็ฝฎ็ๅ่ฝ๏ผNode ๅนถๆฒกๆๅ ็ฝฎๆฏๆ๏ผๅ่ไปฃไน็ๆฏ็คพๅบ้็พ่ฑ้ฝๆพ็ๅทฅๅ ท๏ผTypeScriptใFlowใBiomeใESLintใoxc-lintใPrettierใdprintใ่ฟไบๅทฅๅ ท็จๅจ Node ้กน็ฎไธญๅญๅจ 3 ไธช้ๅธธๅฝฑๅๅผๅไฝ้ช็้ฎ้ข๏ผ
- ๅทฅๅ ท้ๅ้ฎ้ข๏ผๆๅบ่ฏฅ้ๆฉๅชไบๅทฅๅ ท้ๅๆฅไผๅไธ่ฟฐ 3 ไธช้ฎ้ข๏ผ้ๆฉๅ๏ผไธไธไธช Node ้กน็ฎๅ้ๆฉไธๅๅทฅๅ ท้ๆไนๅ๏ผ
- ๅทฅๅ ทไน้ดๅฒ็ช็ฃจๅ้ฎ้ข๏ผ็กฎๅฎไฝฟ็จ็ๅทฅๅ ทๅ๏ผ่ฟไบๅทฅๅ ทไน้ดๆฏๅฆๆๅฒ็ช๏ผไปฃ็ ๆไบคๆถๆฏๅ format ่ฟๆฏๅ lint๏ผๅทฅๅ ทไน้ด้ ๅ็ๆไฝณๅฎ่ทตๆฏไปไน๏ผ
- ๅทฅๅ
ทไปฌ็ๅคๆ้
็ฝฎ้ฎ้ข๏ผๆฏไธชๅทฅๅ
ท้ฝๆๅพๅคๆ้พๆ็้
็ฝฎ๏ผๅจ้กน็ฎๆ น็ฎๅฝ๏ผๆ
package.json้๏ผๅฐๅคๆๅฑใไธๆฅไธ็พ่ง็ฎๆด๏ผไบๆฅๅขๅ ็่งฃๆๆฌใๆฏไธช Node ้กน็ฎๅฏ่ฝๅทฅๅ ท็ปไธไฝ้ ็ฝฎไธ็ปไธ๏ผ่ฟไธๆญฅๅฏผ่ดๅผๅไฝ้ชไธไธ่ดใ
ไธบไบ่งฃๅณไธ่ฟฐ้ฎ้ข๏ผ็ฐๅจๆ้ๅธธๅคๆ็จๆ็ซ ่ฎฒ่งฃ TypeScript + Prettier + ESLint ็้ ็ฝฎๅๅฎ่ทต๏ผ่ฟไบๆ็ซ ๆ็จ่ฝ็ผ่งฃไธ้จๅ้ฎ้ข๏ผไฝไป็ถๅฐๆไนฑ็ๅทฅๅ ท้พๅ็น็็้ ็ฝฎๆด้ฒ็ป็จๆทใ่ฟไธๆฏๆไปฌ็็ฎๆ ๏ผๆไปฌ็็ฎๆ ๆฏๆไพไธไธช็ปไธ็ๅทฅๅ ทๅฑ่ฝ่ฟไบๅคๆ็ๅฎ่ทต็ป่๏ผ็ป็จๆทๅธฆๆฅ็ฎๅไธ่ดใๅผ็ฎฑๅณ็จ็่ฏๅฅฝๅผๅไฝ้ชใ
ไธบไบ้่ฟฐไธ่
ไน้ด็ๅ
ณ็ณป๏ผ่ฟ้ไปฅไธ่
ๆๅ
ทไปฃ่กจๆง็่งฃๅณๆนๆก TypeScriptใPrettier ๅ ESLint ไฝไธบไพๅญใ
| - | ็ฑปๅๅฎๅ จ | Formatting | Linting |
|---|---|---|---|
| ไปฃ่กจ | TypeScript | Prettier | ESLint |
| ๅ ณๆณจ้ป่พ | โ | โ | โ |
| Auto Fixing | โ | โ | โ |
็ป่ฟๅคๅนดๆผ่ฟ๏ผไธ่ ๅ ณๆณจ็นๅญๅจไธๅฎ็ไบค้๏ผ
็ฑปๅๅฎๅ จๅLintingๅ ณๆณจ็น็ไบค้๏ผไพๅฆ๏ผโๅฝๆฐๅ ฅๅๆฐ้ๅฏนไธไธโ๏ผๆขๅฏ่ฝ่ขซ TypeScript ๆฃๆตๅฐ๏ผไนๅฏ่ฝ่ขซ ESLintๆฃๆตๅบๆฅใFormattingๅLintingๅ ณๆณจ็น็ไบค้๏ผไพๅฆ๏ผโๆฏๅฆไฝฟ็จๅๅท็ปๅฐพโใโไฝฟ็จๅๅผๅท่ฟๆฏๅๅผๅทโ็ญ๏ผๆขๅฏไปฅ่ขซ Prettier ๆฃๆตๅบๆฅๅนถๆง่กๆ ผๅผๅ๏ผไนๅฏไปฅ่ขซ ESLint ๆฃๆตๅบๆฅๅนถๆง่กไฟฎๅคใ
่ฝ็ถๅฝไธๆ ๅตๆฏไธ่ ๅญๅจไธๅฎ็ไบค้๏ผไฝ่ฟไธๆฏๆ็ๆณ็ๆ ๅต๏ผๆ็ๆณ็ๆ ๅตๆฏ๏ผ็ฑปๅๅฎๅ จใFormatting ๅ Linting ๅ ณๆณจไธๅ็้ขๅ๏ผไธๅญๅจไบค้ใ
่ฝ็ถ็ฑปๅๅฎๅ
จไนๅฏ่ฝๅ Linting ็ๅ
ณๆณจ็น้ๅ๏ผไฝๆฏ็คพๅบไธปๆตๅๆณไนไธไผๅฐ TypeScript ๅ ESLint ๆททไธบไธ่ฐ๏ผๆไปฅ่ฟ้ไธ่ฟๅค่ต่ฟฐใ็ถ่๏ผ็คพๅบๅ
ไธๅฐไบบๅฐ Formatting ๅ Linting ๅๅนถ่ตทๆฅไธๅนถๅค็๏ผไพๅฆ @antfu/eslint-configใๆไปฌๅผบ็ไธๅปบ่ฎฎ่ฟๆ ทๅ๏ผไธป่ฆๆไปฅไธๅๅ ๏ผ
- ้ฆๅ ๆฏๅ ไธบๅฎไปฌ็ฎ็ไธไธๆ ท๏ผไธไธ็ไบๆ ๅบ่ฏฅไบค็ปไธไธ็ๅทฅๅ ทใ
- Formatting ๅ Linting ๅฎไปฌ้ ๆ็ๅฟๆบ่ดๆ ไธๅ๏ผReview ไปฃ็ ๆถ๏ผๆไปฌๅพๅพไธ้่ฆๅ ณๆณจ Formatting ็ๆนๅจ๏ผไฝๆฏๆไปฌๅฟ ้กป่ฆไป็ปๆฃๆฅ็กฎ่ฎค Linting ็ๆนๅจ๏ผๅ ไธบ Formatting ็ๆนๅจไธ่ฌๆฏๅฎๅ จ็๏ผไฝๆฏ Linting ็ๆนๅจๅฏ่ฝๅญๅจ้่ฏฏ็ไฟฎๅคใ
- ๅ ไธบ Linting ็ๆนๅจๅฏ่ฝๅญๅจ้่ฏฏ็ไฟฎๅค๏ผ้ ๅ Git Hooks ๆถ๏ผๅฆๆ Linting ็ไฟฎๅคๅ Formatting ็ไฟฎๅคๆททๅๅจไธ่ตท๏ผไปฃ็ ๆไบคๆถๅฎนๆ่ฎฉ้่ฏฏ็ไฟฎๅค็ดๆฅ่ฟๅ ฅ Git Commit๏ผๅฏผ่ด bug ๆด้พ่ขซๅ็ฐใ
- ็คพๅบไธปๆต่ถๅฟๆฏๅฐ Formatter ๅ Linter ๅๅผใไพๅฆ๏ผๆฉๆ ESLint ไน่ขซ็จไบๆ ผๅผๅ๏ผไฝๆฏไป v8.53.0 ๅผๅง๏ผ ESLint ๅบๅผ Formatting RulesใDeno ๅ Biome ไนๅๆ
LinterๅFormatterๅๅผใ
ๆป่่จไน๏ผ็ฑปๅๅฎๅ
จใFormatting ๅ Linting ๆฏๆชๆฅ JavaScript ๅ TypeScript ไปฃ็ ็ๆ็ปไธ่ฟๅป็ไธไธชๆน้ขใ่ฟไธๆฟๆงๅจ Node ๅฎๆนๆไพ่งฃๅณๆนๆกไนๅ๏ผ้ฝๆฏๅฒ่ฃใ้พ็จใๅฝฑๅไปฃ็ ่ดจ้็ๆน้ขใๆไปฌ็ญไธๆฅ Node ๅฎๆนๆไพ็ธๅ
ณๆนๆก๏ผๆไปฅๅๅปบไบ Fenge๏ผๅฐฝๅฏ่ฝๅฑ่ฝๅคๆ๏ผๆด้ฒ็ฎๅ๏ผ่ฎฉๅผๅ่
ไธๆณจไบไธๅกไปฃ็ ใ
English
After years of practice, we have found that the three main aspects affecting modern JavaScript project code quality and development experience are:
- Type Safety: Used to detect type and spelling errors in advance, such as whether object methods are called correctly, whether function parameters match the expected types within the function body, etc.
- Formatting: Used to unify code style, improve readability, and reduce conflicts. It mainly focuses on issues like indentation, line breaks, single/double quotes, with/without semicolons, etc.
- Linting: Used to identify logical flaws and poor practices early, reducing bugs and maintenance costs. Its focus can be anywhere except for
Formatting, such as redefining variables, switch statements without break, cyclomatic complexity, etc.
These three aspects are also built-in features in more advanced runtimes like Deno, which Node does not natively support. Instead, the community offers a variety of tools: TypeScript, Flow, Biome, ESLint, oxc-lint, Prettier, dprint. Using these tools in Node projects brings three major issues impacting development experience:
- Tool Selection Problem: Which set of tools should I choose to optimize the above three problems? If I choose different toolsets for the next Node project, how do I handle it?
- Tool Conflict and Integration Problem: After determining the tools to use, do these tools conflict with each other? Should formatting or linting come first when committing code? What is the best practice for integrating these tools?
- Complex Configuration Problem: Each tool has complex and difficult-to-understand configurations scattered across the project root directory (or
package.json). This not only looks messy but also increases the learning curve. Even if the tools are consistent across Node projects, the configurations may vary, leading to inconsistent development experiences.
To address these issues, there are now many tutorials explaining the configuration and practices of TypeScript + Prettier + ESLint, which can alleviate some problems but still expose users to a cluttered toolchain and cumbersome configurations. Our goal is not this; our goal is to provide a unified tool that abstracts away these complex details, offering a simple, consistent, and ready-to-use development experience.
To illustrate the relationship among these three, let's take the most representative solutions TypeScript, Prettier, and ESLint as examples.
| - | Type Safety | Formatting | Linting |
|---|---|---|---|
| Representative | TypeScript | Prettier | ESLint |
| Focus on Logic | โ | โ | โ |
| Auto Fixing | โ | โ | โ |
Over time, these three areas have developed certain intersections:
- Intersection between
Type SafetyandLinting: For example, "mismatched function parameter count" can be detected by both TypeScript and ESLint. - Intersection between
FormattingandLinting: For example, "whether to end with a semicolon" or "use single or double quotes" can be detected and fixed by both Prettier and ESLint.
Although there are overlaps among them today, the ideal situation is that Type Safety, Formatting, and Linting focus on different domains without overlap.
While Type Safety might also overlap with Linting, the community generally does not confuse TypeScript with ESLint, so this aspect is not elaborated here. However, many people in the community combine Formatting and Linting together, such as @antfu/eslint-config. We strongly do not recommend doing this for several reasons:
- They serve different purposes; specialized tasks should be handled by specialized tools.
- Formatting and Linting impose different cognitive loads. When reviewing code, we often don't need to pay attention to Formatting changes, but we must carefully check Linting changes because Formatting changes are generally safe, while Linting changes may contain incorrect fixes.
- Since Linting changes may contain incorrect fixes, when used with Git Hooks, mixing Linting and Formatting fixes can lead to erroneous fixes entering Git Commits directly, making bugs harder to detect.
- The community trend is to separate Formatters and Linters. For example, early versions of ESLint were also used for formatting, but starting from v8.53.0, ESLint deprecated Formatting Rules. Deno and Biome also separate
LinterandFormatter.
In summary, Type Safety, Formatting, and Linting are three indispensable aspects of the future JavaScript and TypeScript code ecosystem. Before Node provides official solutions, these three areas have been fragmented, difficult to use, and negatively impacted code quality. We couldn't wait for Node to provide related solutions, so we created Fenge to shield developers from complexity, simplify their workflow, and allow them to focus on business logic.
Based on the philosophy outlined above, this tool offers the following features:
- ๐ช Enhanced Type Safety: This tool provides the strictest
tsconfigsettings to bolster the type safety of TypeScript projects. It is also compatible with pure JavaScript projects. - ๐ Formatting: This tool ensures code consistency across your codebase and minimizes merge conflicts by automatically formatting code. It additionally supports the sorting of imports and
package.jsonfiles. - ๐ Linting: This tool comes equipped with a comprehensive set of rules for static code analysis, which helps catch errors and prevent poor coding practices in JavaScript.
- ๐ช Git Hooks: After installation, committing code via Git triggers automatic formatting and linting checks. No additional package installations are required.
We place a high value on Development Experience (DX).
- ๐ฆ All-In-One: You don't need to install
prettier,eslint,lint-stagedorhusky. - โ๏ธ Zero Configs: Comes with sensible default configurations for type safety, formatting, and linting, so you don't need to set up any configurations.
- ๐ Highly Customizable: Every thing is optional. Every thing can be customized.
To quick start, run command below to check formatting and linting style in your project.
npx fengeWe recommend installing it as one of devDependencies in your project.
npm i -D fengeEach of the following usages is optional. You can choose the ones that best fit your needs.
Config tsconfig.json file in your project root.
{
"extends": "fenge/tsconfig"
}Config tsconfig.build.json file in your project root.
For Node.js app:
{
"extends": ["./tsconfig.json", "fenge/tsconfig/node"],
"include": ["src"],
"exclude": ["**/*.test.ts"]
}For Web app:
{
"extends": ["./tsconfig.json", "fenge/tsconfig/web"],
"include": ["src"],
"exclude": ["**/*.test.ts"]
}Build your project by executing:
tsc -p ./tsconfig.build.jsonType-check your project by executing:
tsc -p ./tsconfig.build.json --noEmitFor more beat practices, please refer to @fenge/tsconfig.
Here are some main commands to format or lint code.
# Check project's formatting problems only
$ fenge format
# Check project's formatting problems and apply updates
$ fenge format -u
# Check project's linting problems only
$ fenge lint
# Check project's linting problems and apply updates
$ fenge lint -u
# Check both formatting and linting problems
$ fenge
# Check both formatting and linting problems and apply updates
$ fenge -uThis tool does not require a configuration file. However, you can add a fenge.config.js file to customize formatting and linting rules. This file should export an object with two properties:
format: Accept a function that returns a Prettier Config.lint: Accept a function that returns an ESLint Flat Config.
Tips: These two functions can be async or sync. So you can add
asyncor not in font of the function.
export default {
format: async () => ({
semi: false,
singleQuote: true,
}),
lint: async () => [
{
files: ["**/*.{js,cjs,mjs,jsx}", "**/*.{ts,cts,mts,tsx}"],
rules: {
"no-unused-vars": "error",
},
},
],
};Usually, we recommend reusing the built-in configurations rather than writing them from scratch. The built-in configurations re-export from @fenge/prettier-config and @fenge/eslint-config.
// @ts-check
export default {
format: async () => {
// See https://www.npmjs.com/package/@fenge/prettier-config for prettier-config detail usage
const prettierConfig = (await import("fenge/prettier-config")).default;
return {
...prettierConfig,
// add config below to override the default behavior
semi: false,
};
},
lint: async () => {
// See https://www.npmjs.com/package/@fenge/eslint-config for eslint-config detail usage
const { Builder } = await import("fenge/eslint-config");
return (
new Builder()
.enablePackageJson({
pick: ["packagejson/top-types"], // only these rules will work for package.json files
})
.enableJavaScript({
omit: ["no-var"], // these rules will not work for js files
})
.enableTypeScript()
// apply additional rules or override the built-in rules for ts files
.append({
name: "strictest",
files: ["**/*.{ts,cts,mts,tsx}"],
rules: {
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/consistent-type-assertions": [
"error",
{ assertionStyle: "never" },
],
"@typescript-eslint/no-non-null-assertion": "error",
},
})
.toConfig()
);
},
};Tips: You can even install and use other third-party eslint-config, like @sxzz/eslint-config.
Executing fenge install in the project root will write a pre-commit file to the ${PROJECT_ROOT}/.git/hooks folder. After editing package.json -> scripts -> prepare script and executing it once, each commit (via Git) will trigger a code style check for the committed files.
{
"scripts": {
"prepare": "fenge install"
}
}npm run prepare- Clone this repository.
- Enable Corepack using
corepack enableor globally install @rnm/pm. - Install dependencies using
pnpm install. - Run
pnpm style:updateto develop. - Start coding and submit your PR.
Give a โญ๏ธ if this project helped you!
MIT