@actualsize/eslint-config 中文文档教程

发布于 4年前 浏览 10 更新于 3年前

@actualsize/eslint-config

About

ESLint 配置构建于 Airbnb 的配置Unicorn。 它包括 Prettier 以自动格式化代码。

Usage Instructions

  1. 使用 npm i -D eslint @actualsize/eslint-configyarn add -D eslint @actualsize/eslint-config< 在您的项目中安装 eslint 和此配置< /code>

  2. 在您的 .eslintrc.json 文件中,将以下内容之一添加到 extends 数组:

Vanilla (default):"@actualsize/eslint-config"
TypeScript without React :"@actualsize/eslint-config/configs/ts"
React:"@actualsize/eslint-config/configs/react"
TypeScript with React:"@actualsize/eslint-config/configs/ts-react"

就是这样! 所有扩展配置、Prettier 和一些额外的规则更改应该开箱即用。


Example Usage

这应该根据项目而改变,但这里有一个普通项目的良好基线:

.eslintrc

// .eslintrc.json
{
  "env": {
    "node": true,
    "browser": true,
    "es2020": true,
  },
  "extends": [
    "@actualsize/eslint-config"
  ],
}

Typescript

如果您使用的是 TypeScript,也将其添加到您的 .eslintrc

// inside .eslintrc.json root object
"parserOptions": {
    "project": "./tsconfig.json"
}

作为以及项目根目录下的 tsconfig.json 文件:

Node

// tsconfig.json
{
  "compilerOptions": {
    "lib": ["es2020"],
    "module": "commonjs",
    "target": "es2020",

    "sourceMap": true,
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "exclude": [
    "node_modules"
  ]
}

Browser

// tsconfig.json
{
  "compilerOptions": {
    "lib": ["es2020", "dom"],
    "module": "commonjs",
    "target": "es6",

    "jsx": "react",

    "sourceMap": true,
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "exclude": [
    "node_modules"
  ]
}

Prettier

并且不要忘记您的 Prettier 配置!

.prettierrc

// .prettierrc
{
  "printWidth": 62,
  "tabWidth": 2,
  "useTabs": true,
  "semicolons": true,
  "singleQuote": false,
  "quoteProps": "consistent",
  "jsxSingleQuote": false,
  "trailingComma": "es5",
  "bracketSpacing": true,
  "jsxBracketSameLine": false,
  "arrowParens": "always"
}

@actualsize/eslint-config

About

This ESLint config is built on top of Airbnb's config and Unicorn. It includes Prettier to automatically format code.

Usage Instructions

  1. Install eslint and this config in your project with npm i -D eslint @actualsize/eslint-config or yarn add -D eslint @actualsize/eslint-config

  2. In your .eslintrc.json file, add one of the following to the extends array:

Vanilla (default):"@actualsize/eslint-config"
TypeScript without React :"@actualsize/eslint-config/configs/ts"
React:"@actualsize/eslint-config/configs/react"
TypeScript with React:"@actualsize/eslint-config/configs/ts-react"

That's it! All of the extended configs, Prettier, and some extra rule changes should just work out of the box.


Example Usage

This should change depending on the project, but here's a good baseline for a vanilla project:

.eslintrc

// .eslintrc.json
{
  "env": {
    "node": true,
    "browser": true,
    "es2020": true,
  },
  "extends": [
    "@actualsize/eslint-config"
  ],
}

Typescript

If you're using TypeScript, add this to your .eslintrc too:

// inside .eslintrc.json root object
"parserOptions": {
    "project": "./tsconfig.json"
}

As well as a tsconfig.json file at the root of the project:

Node

// tsconfig.json
{
  "compilerOptions": {
    "lib": ["es2020"],
    "module": "commonjs",
    "target": "es2020",

    "sourceMap": true,
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "exclude": [
    "node_modules"
  ]
}

Browser

// tsconfig.json
{
  "compilerOptions": {
    "lib": ["es2020", "dom"],
    "module": "commonjs",
    "target": "es6",

    "jsx": "react",

    "sourceMap": true,
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  },
  "exclude": [
    "node_modules"
  ]
}

Prettier

And don't forget your Prettier configs!

.prettierrc

// .prettierrc
{
  "printWidth": 62,
  "tabWidth": 2,
  "useTabs": true,
  "semicolons": true,
  "singleQuote": false,
  "quoteProps": "consistent",
  "jsxSingleQuote": false,
  "trailingComma": "es5",
  "bracketSpacing": true,
  "jsxBracketSameLine": false,
  "arrowParens": "always"
}
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文