Skip to content

Instantly share code, notes, and snippets.

@okovalov
Last active January 22, 2020 02:49
Show Gist options
  • Save okovalov/43f0530e00bb899fd11d9266eb534da3 to your computer and use it in GitHub Desktop.
Save okovalov/43f0530e00bb899fd11d9266eb534da3 to your computer and use it in GitHub Desktop.
temporary fork from eslint-config-sarpik
#!/usr/bin/env node
// .eslintrc.js
/**
* The ESLint + Prettier config from Kipras <[email protected]> (https://kipras.org)
*
* Supports TypeScript!
* (
* https://javascriptplayground.com/typescript-eslint/
* & https://www.robertcooper.me/using-eslint-and-prettier-in-a-typescript-project
* )
*
*/
module.exports = {
extends: [
"airbnb",
"eslint:recommended",
"plugin:flowtype/recommended",
"plugin:import/typescript" /** https://github.com/benmosher/eslint-plugin-import#typescript */,
"plugin:@typescript-eslint/eslint-recommended",
"plugin:@typescript-eslint/recommended",
// "plugin:@typescript-eslint/recommended-requiring-type-checking", /** broken atm */
"plugin:monorepo/recommended",
"prettier",
"prettier/react",
"prettier/@typescript-eslint",
],
parser: "@typescript-eslint/parser" /** leggo typescript! */,
parserOptions: {
ecmaVersion: 2020,
// Can I remove these now?
ecmaFeatures: {
impliedStrict: true,
classes: true,
},
},
env: {
browser: true,
node: true,
jquery: true,
jest: true,
},
rules: {
/** typescript-specific */
"@typescript-eslint/no-inferrable-types": 0,
/** end typescript-specific */
"no-debugger": 0,
"no-alert": 0,
"no-await-in-loop": 0,
"no-return-assign": ["error", "except-parens"],
"no-restricted-syntax": [2, "ForInStatement", "LabeledStatement", "WithStatement"],
"no-unused-vars": [
1,
{
ignoreSiblings: true,
argsIgnorePattern: "res|next|^err",
},
],
"prefer-const": [
"error",
{
destructuring: "all",
},
],
"arrow-body-style": [2, "as-needed"],
"no-unused-expressions": [
2,
{
allowTaggedTemplates: true,
},
],
"no-param-reassign": [
2,
{
props: false,
},
],
"no-console": 2,
"import/prefer-default-export": 0,
import: 0,
"func-names": 0,
"space-before-function-paren": 0,
"comma-dangle": 0,
"max-len": 0,
"import/extensions": 0,
"no-underscore-dangle": 0,
"consistent-return": 0,
"react/display-name": 1,
"react/no-array-index-key": 0,
"react/react-in-jsx-scope": 0,
"react/prefer-stateless-function": 0,
"react/forbid-prop-types": 0,
"react/no-unescaped-entities": 0,
"jsx-a11y/accessible-emoji": 0,
"react/require-default-props": 0,
"react/jsx-filename-extension": [
1,
{
extensions: [".js", ".jsx", ".ts", ".tsx"],
},
],
radix: 0,
"no-shadow": [
2,
{
hoist: "all",
allow: ["resolve", "reject", "done", "next", "err", "error"],
},
],
quotes: [
2,
"single",
{
avoidEscape: true,
allowTemplateLiterals: true,
},
],
semi: [2, "always"],
"prettier/prettier": [
"error",
{
"trailingComma": "es5",
"singleQuote": true,
"printWidth": 80,
"semi": true,
"arrowParens": "avoid"
},
],
"jsx-a11y/href-no-hash": "off",
"jsx-a11y/anchor-is-valid": [
"warn",
{
aspects: ["invalidHref"],
},
],
"react-hooks/rules-of-hooks": "error",
"react-hooks/exhaustive-deps": "warn",
},
plugins: [
"html",
"prettier",
"react-hooks",
"flowtype" /** CRA has this & I wanted it to work by default via global install */,
// "@typescript-eslint"
"@typescript-eslint/eslint-plugin",
],
};
haters/
/node_modules
/.pnp
.pnp.js
/coverage
.env
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*
node_modules/
.cache
dist/
build/
*.code-workspace

No-Sweat™ ESLint, Prettier and TypeScript setup

These are my settings for ESLint and Prettier

You might like them - or you might not. Don't worry you can always change them.

What it does

  • Lints JavaScript 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
  • You can see all the rules here. You are very welcome to overwrite any of these settings, or just fork the entire thing to create your own.

Installing

You can use eslint globally and/or locally per project.

It's usually best to install this locally once per project, that way you can have project specific settings as well as sync those settings with others working on your project via git.

I also install globally so that any project or rogue JS file I write will have linting and formatting applied without having to go through the setup. You might disagree and that is okay, just don't do it then 😃.

Local / Per project install

yarn init -y                                            # Create 'package.json' if you haven't already
npx install-peerdeps --dev eslint-config-sarpik --yarn  # Install everything needed by the config
                                                        # You can see in your package.json there's now a big list of devDependencies
touch .eslintrc.js                                      # Create the config file @ the project's root

Your .eslintrc.js file should look like this:

module.exports = {
  "extends": [
    "sarpik"
  ]
}

Tip: You can alternatively put this object in your package.json under the property "eslintConfig": { ... }. This makes one less file in your project.

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

"scripts": {
  "lint": "eslint .",
  "lint:fix": "eslint . --fix"
},

Now you can manually lint your code by running yarn lint (npm run lint) and fix all fixable issues with yarn lint:fix (npm run lint:fix). You probably want your editor to do this though.

Global Install

  1. First install everything needed:
npx install-peerdeps --global eslint-config-sarpik --yarn

(note: npx is not a spelling mistake of npm. npx comes with when node and npm are installed and makes script running easier 😃 (and it works automatically with yarn, too!))

  1. Then you need to make a global .eslintrc.js / .eslintrc file:

ESLint will look for one in your home directory

  • ~/.eslintrc[.js] for UNIX
  • C:\Users\<username>\.eslintrc[.js] for Windows

Your .eslintrc.js file should look like this:

module.exports = {
  "extends": [
    "sarpik"
  ]
}
  1. To use from the CLI, you can now run eslint . or configure your editor as we show next.

Settings

If you'd like to overwrite eslint or prettier settings, you can add the rules in your .eslintrc[.js] file. The ESLint rules go directly under "rules" while prettier options go under "prettier/prettier". Note that prettier rules overwrite anything in my config (trailing comma, and single quote), so you'll need to include those as well.

module.exports = {
  "extends": [
    "sarpik"
  ],
  "rules": {
    "no-console": 2,
    "prettier/prettier": [
      "error",
      {
        "trailingComma": "es5",
        "singleQuote": false,
        "printWidth": 120,
        "tabWidth": 4,
      }
    ]
  }
}

With VS Code

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/FilePreferencesSettings. It's easier to enter these settings while editing the settings.json file, so click the {} icon in the top right corner:

{
  "editor.formatOnSave": true,

  /** disable tslint - no need! */
  "tslint.enable": false,

  "eslint.enable": true,
  "editor.codeActionsOnSave": {
    "source.fixAll": true,
    // "source.fixAll.eslint": true /** included by previous setting */
  },

  // "eslint.run": "onSave",
  "eslint.run": "onType",
  /** work as a formatter, too! */
  "eslint.format.enable": true,

  /**
   * ~~turn formatting off for JS, JSX, TS & TSX - we do this via eslint~~
   * ESLint IS the formatter now!
   * (Prettier is still in the picture, as we have configured it via eslint)
  */
  "[javascript]": {
    "editor.defaultFormatter": "dbaeumer.vscode-eslint"
  },
  "[javascriptreact]": {
    "editor.defaultFormatter": "dbaeumer.vscode-eslint"
  },
  "[typescript]": {
    "editor.defaultFormatter": "dbaeumer.vscode-eslint"
  },
  "[typescriptreact]": {
    "editor.defaultFormatter": "dbaeumer.vscode-eslint"
  },

  /**
   * Optional but IMPORTANT:
   * If you have the prettier extension enabled for other languages like CSS and HTML,
   * turn it off for JS since we are doing it through eslint already
  */
  "prettier.disableLanguages": ["javascript", "javascriptreact", "typescript", "typescriptreact"],

  /** ~~make sure both js and ts are validated~~ no longer needed as of vscode-eslint 2.0 */
//   "eslint.validate": [
//     "javascript",
//     "javascriptreact",
//     "typescript",
//     "typescriptreact",
//   ]
}

See also the README of vscode-eslint.

With Create React App

  1. (meh) You gotta eject first - yarn eject (npm run eject)
  2. Run npx install-peerdeps --dev eslint-config-sarpik --yarn
  3. Crack open your package.json and replace "extends": "react-app" with "extends": "sarpik"

🤬🤬🤬🤬 IT'S NOT WORKING

Start fresh. Sometimes global modules can goof you up. This will remove them all:

yarn global remove eslint-config-sarpik @typescript-eslint/parser @typescript-eslint/eslint-plugin typescript eslint eslint-config-prettier eslint-config-airbnb eslint-plugin-html eslint-plugin-prettier eslint-plugin-import eslint-plugin-jsx-a11y eslint-plugin-react prettier eslint-plugin-react-hooks

To do the above for local, omit the --global flag.

Then if you are using a local install, remove your package-lock.json / yarn.lock file and delete the node_modules/ directory.

rm package-lock.json
rm yarn.lock
rm -rf node_modules

Then follow the above instructions again.

License

MIT © 2019 Kipras Melnikovas

#!/usr/bin/env node
// index.js
// eslint-disable-next-line @typescript-eslint/no-var-requires
const eslintrc = require("./.eslintrc");
console.log('hello here');
module.exports = eslintrc;
MIT License
Copyright (c) 2019-present Kipras Melnikovas
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
{
"name": "eslint-config-okovalov",
"version": "0.0.1",
"description": "ESLint, Prettier and TypeScript setup from Okovalov (forked from Kipras Melnikovas <[email protected]> (https://kipras.org))",
"main": "index.js",
"bin": "./index.js",
"author": "Oleksandr Kovalov <[email protected]>",
"repository": "https://gist.github.com/okovalov/43f0530e00bb899fd11d9266eb534da3",
"license": "MIT",
"private": false,
"scripts": {
"lint": "eslint . --ext .js,.jsx,.ts,.tsx --ignore-pattern '!.eslintrc'",
"lint:fix": "eslint . --ext .js,.jsx,.ts,.tsx --fix --ignore-pattern '!.eslintrc'",
"prepublishOnly": "yarn lint:fix",
"postpublish": "npx install-peerdeps --global --yarn https://gist.github.com/okovalov/43f0530e00bb899fd11d9266eb534da3"
},
"peerDependencies": {
"@typescript-eslint/eslint-plugin": "^2.11.0",
"@typescript-eslint/parser": "^2.11.0",
"babel-eslint": "^10.0.3",
"eslint": "^6.6.0",
"eslint-config-airbnb": "^18.0.0",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-flowtype": "^4.5.2",
"eslint-plugin-html": "^6.0.0",
"eslint-plugin-import": "^2.18.0",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-monorepo": "^0.2.1",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-react": "^7.14.2",
"eslint-plugin-react-hooks": "^2.3.0",
"prettier": "^1.16.4",
"typescript": "^3.7.3"
},
"dependencies": {
"@typescript-eslint/eslint-plugin": "^2.11.0",
"@typescript-eslint/parser": "^2.11.0",
"babel-eslint": "^10.0.3",
"eslint": "^6.6.0",
"eslint-config-airbnb": "^18.0.0",
"eslint-config-prettier": "^6.7.0",
"eslint-plugin-flowtype": "^4.5.2",
"eslint-plugin-html": "^6.0.0",
"eslint-plugin-import": "^2.18.0",
"eslint-plugin-jsx-a11y": "^6.2.3",
"eslint-plugin-monorepo": "^0.2.1",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-react": "^7.14.2",
"eslint-plugin-react-hooks": "^2.3.0",
"prettier": "^1.16.4",
"typescript": "^3.7.3"
},
"keywords": [
"javascript",
"ecmascript",
"eslint",
"lint",
"config",
"prettier",
"sarpik"
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment