简体   繁体   English

使用Prettier的VS Code for TypeScript中的快速修复操作

[英]Quick Fix actions in VS Code for TypeScript using Prettier

I have a setup compiling TypeScript trough Babel. 我有一个编译TypeScript槽Babel的安装程序。 In addition I've set up ESLint and Prettier for linting/formatting. 此外,我还设置了ESLint和Prettier进行掉毛/格式化。 ESLint is configured to use the parser from @typescript-eslint/parser - no TSLint involved. ESLint配置为使用@typescript-eslint/parser -不涉及TSLint。

ESLint is correctly applying Prettier rules and showing me the squiggly lines in VS Code for both regular JavaScript and TypeScript. ESLint正确地应用了漂亮的规则,并向我展示了VS Code中针对常规JavaScript和TypeScript的波浪线。 However, only regular JavaScript has any actions available under the "Quick Fix..." option in the VS Code tooltip. 但是,只有常规JavaScript才能在VS Code工具提示的“快速修复...”选项下进行任何操作。 For TypeScript files it always says "No code actions available" for Prettier issues. 对于TypeScript文件,对于更漂亮的问题,它始终显示“没有可用的代码操作”。 Is there any way to make Prettier's quick fixes work with TypeScript files as well? 有什么方法可以使Prettier的快速修复程序与TypeScript文件一起使用?

Here are my configuration files: 这是我的配置文件:

.eslintrc .eslintrc

{
  "extends": [
    "plugin:react/recommended",
    "plugin:@typescript-eslint/recommended",
    "plugin:prettier/recommended",
    "prettier/@typescript-eslint",
    "prettier/react"
  ],
  "parser": "@typescript-eslint/parser",
  "parserOptions": {
    "sourceType": "module",
    "ecmaVersion": 2018,
    "ecmaFeatures": {
      "jsx": true
    }
  },
  "plugins": ["@typescript-eslint"],
  "env": {
    "browser": true,
    "jest": true
  }
}

.prettierrc.js .prettierrc.js

module.exports = {
  printWidth: 120,
  semi: true,
  singleQuote: true,
  tabWidth: 2,
  trailingComma: 'all',
};

babel.config.js babel.config.js

module.exports = api => {
  api.cache.invalidate(() => process.env.NODE_ENV === 'production');

  const presets = ['@babel/env', '@babel/typescript', '@babel/react'];

  const plugins = ['@babel/proposal-class-properties', '@babel/proposal-object-rest-spread'];

  if (process.env.NODE_ENV === 'development') {
    plugins.push('react-hot-loader/babel');
  }

  return {
    presets,
    plugins,
  };
};

This worked for me, when added to my settings.json: 当添加到我的settings.json中时,这对我有用:

"eslint.validate": [
    "javascript",
    "javascriptreact",
    {
        "language": "typescript",
        "autoFix": true
    },
    {
        "language": "typescriptreact",
        "autoFix": true
    }
],

I'm not using Prettier in this project, so YMMV. 我在这个项目中没有使用Prettier,所以是YMMV。

Update: If that doesn't do the trick, you may also need this: 更新:如果不能解决问题,您可能还需要:

"eslint.options": {
  "extensions": [".js", ".jsx", ".ts", ".tsx"]
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM