Skip to content

Instantly share code, notes, and snippets.

@olitreadwell
Last active June 29, 2023 16:24
Show Gist options
  • Save olitreadwell/257f95f0ab52ea4899cc9517ce84271d to your computer and use it in GitHub Desktop.
Save olitreadwell/257f95f0ab52ea4899cc9517ce84271d to your computer and use it in GitHub Desktop.
eslint
module.exports = {
env: {
browser: true,
es2021: true,
node: true,
},
extends: ['eslint:recommended', 'plugin:react/recommended', 'plugin:@typescript-eslint/recommended'],
overrides: [],
parser: '@typescript-eslint/parser',
parserOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
},
plugins: ['import', '@typescript-eslint', 'jsx-a11y', 'react', 'react-hooks'],
rules: {
indent: ['warn', 2],
'linebreak-style': ['warn', 'unix'],
quotes: ['warn', 'single'],
semi: ['warn', 'always'],
'max-len': ['warn', { code: 160, comments: 160 }],
'no-console': 'warn',
'import/extensions': ['warn', 'never'],
'import/no-extraneous-dependencies': ['warn', { devDependencies: true }],
'react/jsx-filename-extension': ['warn', { extensions: ['.js', '.jsx', '.tsx'] }],
'react-hooks/rules-of-hooks': 'warn',
'react-hooks/exhaustive-deps': 'warn',
'no-unreachable': 'warn',
'no-unused-expressions': 'warn',
'no-unused-labels': 'warn',
'no-unused-vars': 'warn',
'no-empty': 'warn',
'no-empty-character-class': 'warn',
'no-empty-function': 'warn',
'no-empty-pattern': 'warn',
'no-extra-boolean-cast': 'warn',
'no-extra-semi': 'warn',
'no-lone-blocks': 'warn',
'no-useless-call': 'warn',
'no-useless-catch': 'warn',
'no-useless-concat': 'warn',
'no-useless-constructor': 'warn',
'no-useless-escape': 'warn',
'no-useless-return': 'warn',
complexity: ['warn', { max: 15 } ],
'max-depth': 'warn',
'max-lines': ['warn', { max: 50, skipBlankLines: true, skipComments: true } ],
'max-lines-per-function': ['warn', { max: 30, skipBlankLines: true, skipComments: true } ],
'max-nested-callbacks': 'warn',
'max-statements': 'warn',
},
};

Hey there, Junior Software Engineers! 🌟

Let's dive into the world of ESLint, a handy tool that helps improve the quality of your JavaScript code. Don't worry if you're new to this – we'll guide you through the process step by step! πŸ”

Installing ESLint:

To get started, open your project's terminal and run this command:

npm install eslint --save-dev

This command will download and install ESLint as a development dependency in your project.

Initiating ESLint:

Once the installation is complete, initiate ESLint by running this command in your terminal within the project directory:

npx eslint --init

Follow the prompts with the following choices:

  • How would you like to use ESLint?: Choose the option "To check syntax, find problems, and enforce code style."

  • What type of modules does your project use?: Select "JavaScript modules (import/export)".

  • Which framework does your project use?: Choose "None of these".

  • Does your project use TypeScript?: Select "No".

  • Where does your code run?: Choose "Browser".

  • How would you like to define a style for your project?: Select "Use a popular style guide".

  • Which style guide do you want to follow?: Pick "Airbnb: https://github.com/airbnb/javascript".

  • What format do you want your config file to be in?: Choose "JavaScript".

ESLint will install the necessary packages and create a .eslintrc.json file in your project directory.

Avoiding Additional Imports:

To keep things simple, you don't need any additional plugins or dependencies. We've already provided a configuration file that doesn't require any extras.

Setting the Config File:

Open the .eslintrc.json file and replace its content with this configuration code:

{
  "env": {
    "browser": true,
    "es2021": true
  },
  "parserOptions": {
    "ecmaVersion": 2021,
    "sourceType": "module"
  },
  "plugins": ["import"],
  "extends": ["eslint:recommended", "plugin:react/recommended"],
  "rules": {
    // Stylistic rules
    "indent": ["warn", 2, { "SwitchCase": 1 }],

    // Enforces consistent linebreak style (unix)
    "linebreak-style": ["warn", "unix"],

    // Enforces the use of single quotes for strings
    "quotes": ["warn", "single"],

    // Enforces semicolons at the end of statements
    "semi": ["warn", "always"],

    // Limits lines of code and comments to 60 characters per line
    "max-len": ["warn", { "code": 60, "comments": 60 }],

    // Best practices
    // Warns against the use of console statements
    "no-console": "warn",

    // Import rules
    // Warns when file extensions are required in import statements
    "import/extensions": ["warn", "never"],

    // JavaScript language features

    // Warns about unreachable code
    "no-unreachable": "warn",

    // Warns about unused expressions
    "no-unused-expressions": "warn",

    // Warns about unused labels
    "no-unused-labels": "warn",

    // Warns about unused variables
    "no-unused-vars": "warn",

    // Warns about empty blocks/statements
    "no-empty": "warn",

    // Warns about empty character classes in regular expressions
    "no-empty-character-class": "warn",

    // Warns about empty function declarations
    "no-empty-function": "warn",

    // Warns about empty object patterns
    "no-empty-pattern": "warn",

    // Warns about unnecessary boolean casts
    "no-extra-boolean-cast": "warn",

    // Warns about unnecessary semicolons
    "no-extra-semi": "warn",

    // Warns about unnecessary lone blocks
    "no-lone-blocks": "warn",

    // Warns about unnecessary function callings
    "no-useless-call": "warn",

    // Warns about unnecessary try/catch blocks
    "no-useless-catch": "warn",

    // Warns about unnecessary string concatenation
    "no-useless-concat": "warn",

    // Warns about unnecessary constructor declarations
    "no-useless-constructor": "warn",

    // Warns about unnecessary escape characters
    "no-useless-escape": "warn",

    // Warns about unnecessary return statements
    "no-useless-return": "warn",

    // Warns about code complexity exceeding a maximum threshold
    "complexity": ["warn", { "max": 15 }],

    // Warns about excessive nested code blocks
    "max-depth": "warn",

    // Warns about files exceeding a maximum number of lines (excluding blank lines and comments)
    "max-lines": [
      "warn",
      { "max": 50, "skipBlankLines": true, "skipComments": true }
    ],

    // Warns about functions exceeding a maximum number of lines (excluding blank lines and comments)
    "max-lines-per-function": [
      "warn",
      { "max": 30, "skipBlankLines": true, "skipComments": true }
    ],

    // Warns about excessive nested callbacks
    "max-nested-callbacks": "warn",

    // Warns about functions exceeding a maximum number of statements
    "max-statements": "warn"
  }
}

Using this configuration, ESLint will enforce coding standards and provide helpful warnings to make your code cleaner and easier to read.

Optional: ESLint Extension for Visual Studio Code:

If you're using Visual Studio Code as your code editor, you can install the ESLint extension to lint your code directly within the editor. Find the extension in the Visual Studio Code Marketplace by searching for "ESLint".

For a more detailed guide on setting up ESLint in Visual Studio Code, you can check out this tutorial: Linting and Formatting with ESLint in VS Code

That's it! You've successfully installed ESLint, initiated it in your project, and set up the configuration file. πŸŽ‰

Remember, ESLint is your friendly code assistant, ready to help you write high-quality code. Don't be discouraged if you encounter warnings – it's all part of the learning process. Take it step by step, and you'll gradually become more comfortable with ESLint's suggestions.

If you have any questions or need further assistance, feel free to ask. Happy coding! πŸš€

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment