VS Code + ESLint + Prettier + Google 风格 + Typescript

Ale*_*ler 5 typescript eslint visual-studio-code prettier

我正在努力实现这一目标:

使用 VS Code 作为 JavaScript 和 TypeScript 编辑器,eslint-config-google在保存 JavaScript/TypeScript 文档时自动应用格式规则。

我的devDependencies是这些:

  "devDependencies": {
    "@types/node": "^10.12.18",
    "@typescript-eslint/eslint-plugin": "^1.9.0",
    "@typescript-eslint/parser": "^1.9.0",
    "eslint": "5.16.0",
    "eslint-config-google": "0.13.0",
    "eslint-config-prettier": "^6.0.0",
    "eslint-plugin-prettier": "^3.1.0",
    "prettier": "^1.18.2",
    "typescript": "^3.4.3"
  }
Run Code Online (Sandbox Code Playgroud)

我的.eslintrc

{
  "parser": "@typescript-eslint/parser",
  "plugins": ["@typescript-eslint", "prettier"],
  "extends": ["eslint:recommended", "google", "prettier"],
  "parserOptions": {
    "ecmaVersion": 2016,
    "sourceType": "module"
  },
  "env": {
    "es6": true,
    "node": true,
    "mocha": true
  },
  "rules": {
    "prettier/prettier": ["error"]
  }
}
Run Code Online (Sandbox Code Playgroud)

我的.prettierc

{
  "printWidth": 100,
  "singleQuote": true
}
Run Code Online (Sandbox Code Playgroud)

我已经在 VS Code 中安装了扩展并在“保存”上启用了格式prettiereslint

考虑这段代码:

'use strict';

describe('some test', () => {
  it('should return a string value', (done) => {
    return done();
  });
});
Run Code Online (Sandbox Code Playgroud)

ESLint 和 Prettier 都没有抱怨,但在保存文档时,Prettier 删除了周围的括号,doneeslint-config-google根据需要定义了它们。

另外,当删除 周围的括号时done,不会显示错误,而且它们也是必需的。

看起来eslint-config-google和 Prettier 不同步,这可能是我的错。

这里出了什么问题?

小智 2

我正在努力解决类似的问题,但使用 jetbrains 的 webStorm 和一些不同的技术堆栈,但也许以下配置可能适合您,请看看我的设置:

devDependencies

"eslint": "7.12.0",
"eslint-config-google": "0.14.0",
"eslint-config-react-app": "6.0.0",
"eslint-plugin-prettier": "^3.1.4",
"eslint-plugin-react": "7.21.5",
"eslint-plugin-react-hooks": "4.2.0",
Run Code Online (Sandbox Code Playgroud)

.prettierrc

{
  "extends": [
    "plugin:prettier/recommended"
  ],
  "bracketSpacing": false,
  "tabWidth": 2,
  "useTabs": false,
  "trailingComma": "all",
  "singleQuote": true
}
Run Code Online (Sandbox Code Playgroud)

.eslintrc.js

module.exports = {
  env: {
    browser: true,
    es6: true,
  },
  extends: ['plugin:react/recommended', 'google'],
  globals: {
    Atomics: 'readonly',
    SharedArrayBuffer: 'readonly',
  },
  parserOptions: {
    ecmaFeatures: {
      jsx: true,
    },
    ecmaVersion: 2018,
    sourceType: 'module',
  },
  plugins: ['react', 'prettier'],
  rules: {
    'prettier/prettier': 'error',
  },
  settings: {
    react: {
      version: 'detect',
    },
  },
};
Run Code Online (Sandbox Code Playgroud)