Difference between revisions of "Eslint"

From Bitpost wiki
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 8: Line 8:
 
  # Then from your project...
 
  # Then from your project...
 
  npm install --save-dev eslint-config-google
 
  npm install --save-dev eslint-config-google
 +
* Configure eslint (see below)
 
* Install the eslint vscode extension.
 
* Install the eslint vscode extension.
* Configure eslint in one of these ways:
+
** Get the eslint extension
** Run the vscode command EsLint: Create EsLint configuration
+
** Restart vscode and open a project with JS files.
** Run the [eslint --init] command from the root of your project
+
** An eslint dialog should pop up, click Allow.  Should be all you need.
** Copy an .eslintrc.js file from another project into the root of your project.
 
* Restart the project, and eslint should prompt you to allow it to be enabled.
 
  
 
==== Configuration ====
 
==== Configuration ====
Line 33: Line 32:
 
   },
 
   },
 
  };
 
  };
Otherwise, this will step you through it.  Use the google format as a baseline:
+
Otherwise, use one of these to step you through it.  Use the google format as a baseline:
cd myproject
+
* Run the vscode command EsLint: Create EsLint configuration
eslint --init
+
* Run the [eslint --init] command from the root of your project
 
 
==== vscode integration ====
 
* Get the eslint extension
 
* Restart vscode and open a project with JS files.
 
* An eslint dialog should pop up, click Allow.  Should be all you need.
 
  
 
==== eslint rules ====
 
==== eslint rules ====
 
eslint rules are generally ridiculously aggressive, truly for the OCD.  But you get used to it, for better or worse.  There are some pretty stupid ones, eg:
 
eslint rules are generally ridiculously aggressive, truly for the OCD.  But you get used to it, for better or worse.  There are some pretty stupid ones, eg:
 
* [https://eslint.org/docs/rules/no-mixed-operators No mixed operators]!  Durrr... learn to program.
 
* [https://eslint.org/docs/rules/no-mixed-operators No mixed operators]!  Durrr... learn to program.
 +
* No trailing spaces, even in comments
 +
* String literals must use single quotes; JSX strings must use double quotes.
 +
I'm not even sure which rule is causing some of those restrictions.  It gets complicated.  So don't go too crazy.

Latest revision as of 20:21, 20 September 2020

Intro

eslint is a Javascript linter and format-enforcer. Love it or die. I'm following Google's rules, with allowance for longer lines. My job uses a very strict set of rules.

Installation

Do NOT USE APT, it is available and seems to work but this is a node module. Don't be stupid, use npm. Note the mind-bogglingly-insane number of dependencies. Node world, you scare me...

  • Install eslint node module
# From anywhere...
npm install -g eslint
# Then from your project...
npm install --save-dev eslint-config-google
  • Configure eslint (see below)
  • Install the eslint vscode extension.
    • Get the eslint extension
    • Restart vscode and open a project with JS files.
    • An eslint dialog should pop up, click Allow. Should be all you need.

Configuration

If you have an existing .eslintrc.js that works well in another project, JUST COPY IT to the root of the new vscode folder-based project. Here's a good example:

module.exports = {
  'env': {
    'browser': true,
    'es2020': true,
  },
  // this is insane: 'extends': ['eslint:recommended', 'google'],
  'extends': ['google'],
  'parserOptions': {
    'ecmaVersion': 11,
    'sourceType': 'module',
  },
  'rules': {
    // MDM they don't call me [Michael 4k] for nothing.  I have to save SOME lint dignity.  Is this too much to ask?
    'max-len': [1, {'code': 120}],
  },
};

Otherwise, use one of these to step you through it. Use the google format as a baseline:

  • Run the vscode command EsLint: Create EsLint configuration
  • Run the [eslint --init] command from the root of your project

eslint rules

eslint rules are generally ridiculously aggressive, truly for the OCD. But you get used to it, for better or worse. There are some pretty stupid ones, eg:

  • No mixed operators! Durrr... learn to program.
  • No trailing spaces, even in comments
  • String literals must use single quotes; JSX strings must use double quotes.

I'm not even sure which rule is causing some of those restrictions. It gets complicated. So don't go too crazy.