Giter Club home page Giter Club logo

frontendfixer / -frontendfixer-eslint-config-react Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 212 KB

ESLint, Prettier and Typescript Config for React application

Home Page: https://github.com/frontendfixer/-frontendfixer-eslint-config-react#readme

License: MIT License

JavaScript 68.19% CSS 14.92% TypeScript 7.79% SCSS 9.10%
eslint eslint-airbnb eslint-config eslint-prettier eslint-react eslint-react-config eslint-typescript prettier react typescript

-frontendfixer-eslint-config-react's Introduction

Eslint Prettier TypeScript Setup from @frontendfixer

Eslint Prettier TypeScript Setup for React from @frontendfixer

Table of contents


Features

  • Lints JavaScript and TypeScript based on the latest standards
  • Fixes issues and formatting errors with Prettier
  • Lints + Fixes inside of html script tags
  • Lints + Fixes React via eslint-config-airbnb
  • Lints + Fixes Typescript via airbnb/typescript
  • Lints + Fixes imports according to standard rules

You can see all the base rules and typescript rule

  • You are very welcome to overwrite any of these settings, or just fork the entire thing to create your own.

Installation

  • without typescript
npm install -D eslint prettier@^2.8.8 @frontendfixer/eslint-config-react
  • with typescript
npm install -D eslint prettier@^2.8.8 typescript @frontendfixer/eslint-config-react

Configuration

We need to put our eslint settings in a file in the root of our project. We should create a new .eslintrc or .eslintrc.js file that lives where package.json does:

React config

You can add it in package.json, anywhere top level. Like right under your "scripts" object.

"eslintConfig": {
  "extends": ["@frontendfixer/react"]
}

Or put this in a .eslintrc file

{
  "extends": ["@frontendfixer/react"]
}

Typescript config

{
  "extends": ["@frontendfixer/react"],
  "overrides": [
    {
      "files": ["**/*.{ts,tsx}"],
      "extends": ["@frontendfixer/react/typescript"]
    }
  ]
}

TypeScript users will also need a tsconfig.json file in their project. An empty object ({}) will do!

Add script

You can add two scripts to your package.json to lint and/or fix:

"scripts": {
  "lint": "eslint . --ext .js,.jsx,.ts,.tsx",
  "lint:fix": "eslint --fix . --ext .js,.jsx,.ts,.tsx"
},
  • Now you can manually lint your code by running npm run lint and fix all fixable issues with npm run lint:fix. You probably want your editor to do this though.

Settings

EsLint Rules

If you'd like to overwrite eslint or prettier settings, you can add the rules in your .eslintrc file. The ESLint rules go directly under "rules".

{
  "extends": [
    "@frontendfixer/react"
  ],
  "rules": {
    "no-console": "error",
  }
}
// You can also use {0, 1, 2} for {"off", "warn", "error"}

Prettier Rules

By default these rules are enable for linting

{
  semi: true,
  singleQuote: true,
}

If you want custom prettier options, it's recommended to create a .prettierrc file in your root directory like so:

{
  "trailingComma": "es5",
  "printWidth": 80,
  "quoteProps": "as-needed",
  "semi": true,
  "singleQuote": true,
  "tabWidth": 2,
  "bracketSameLine": true,
  "jsxBracketSameLine": true,
  "jsxSingleQuote": false
}
  • this .prettierrc file is present in config if you want different rules then create a new config

Note if you are switching to double quotes, you'll also need to add this eslint rule, or they will fight to the death!

quotes: ['error', 'double'];

Auto fix lint error with VSCode

You should read this entire thing. Serious!

Once you have done one, or both, of the above installs. You probably want your editor to lint and fix for you. Here are the instructions for VS Code:

  1. Install the ESLint package
  2. Now we need to setup some VS Code settings via Code/File โ†’ Preferences โ†’ Settings. It's easier to enter these settings while editing the settings.json file, so click the Open (Open Settings) icon in the top right corner:
// These are all my auto-save configs
"editor.formatOnSave": true,
// turn it off for JS and JSX, we will do this via eslint
"[javascript][javascriptreact][typescript][typescriptreact]": {
  "editor.formatOnSave": false
},
// tell the ESLint plugin to run on save
"editor.codeActionsOnSave": {
  "source.fixAll.eslint": true
},

After attempting to lint your file for the first time, you may need to click on 'ESLint' in the bottom right and select 'Allow Everywhere' in the alert window.

Finally you'll usually need to restart VS code. They say you don't need to, but it's never worked for me until I restart.


With Create React App

  1. Run npx install-peerdeps --dev @frontendfixer/eslint-config-react
  2. Crack open your package.json and replace "extends": "react-app" with "extends": "@frontendfixer/react"

With Gatsby

  1. Run npx install-peerdeps --dev @frontendfixer/eslint-config-react
  2. follow the Local / Per Project Install steps above

Inspirations

  1. eslint-config-standard
  2. eslint-config-wesbos

-frontendfixer-eslint-config-react's People

Contributors

frontendfixer avatar github-actions[bot] avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.