首先集成ESLint与Prettier并消除规则冲突,接着配置.eslintrc.cjs和.prettierrc文件,最后设置VSCode保存时自动修复与格式化,实现JavaScript/TypeScript开发环境的统一与高效。

要为 VSCode 打造最前沿的 JavaScript/TypeScript 开发环境,核心是集成现代 Lint 工具和格式化工具,并确保它们协同工作。关键在于使用 ESLint 做代码检查,Prettier 做格式化,通过 eslint-config-prettier 消除规则冲突,并启用保存时自动修复。
在项目中安装必要的依赖:
npm install --save-dev eslint @typescript-eslint/parser @typescript-eslint/eslint-plugin prettier eslint-config-prettier eslint-plugin-prettier如果你使用 TypeScript,还需要安装:
npm install --save-dev typescript创建或更新 .eslintrc.cjs 配置文件:
立即学习“Java免费学习笔记(深入)”;
module.exports = { root: true, parser: '@typescript-eslint/parser', extends: [ 'eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier', 'plugin:prettier/recommended' ], plugins: ['@typescript-eslint'], env: { browser: true, es2021: true, node: true }, rules: { // 可根据团队习惯自定义规则 } };在项目根目录创建 .prettierrc 文件,定义格式化偏好:
{ "semi": true, "trailingComma": "es5", "singleQuote": true, "printWidth": 80, "tabWidth": 2 }你也可以添加 .prettierignore 排除不需要格式化的文件,比如 dist、node_modules 等。
打开 VSCode 的设置(settings.json),加入以下内容:
{ "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.fixAll.eslint": true }, "eslint.validate": ["javascript", "typescript"] }确保已安装 VSCode 插件:ESLint(由 Microsoft 提供)和 Prettier - Code formatter(由 Prettier 官方提供)。
为了获得更精确的类型级 lint 检查(如检测未使用的变量、不正确的类型赋值),在 .eslintrc.cjs 中启用项目模式:
parserOptions: { project: './tsconfig.json', tsconfigRootDir: __dirname }注意:这会增加 ESLint 的运行开销,建议只在大型项目中开启,并确保有 tsconfig.json 文件存在。
基本上就这些。配置完成后,VSCode 会在你保存文件时自动执行 ESLint 检查并用 Prettier 格式化代码,确保团队风格统一且代码质量可控。
以上就是如何为VSCode配置最前沿的JavaScript/TypeScript开发环境,包括Lint和格式化?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号