Code Monkey home page Code Monkey logo

eslint-config-algolia's Introduction

eslint-config-algolia

Version Build Status License Downloads

This is Algolia's linting and formatting of JavaScript projects (Vanilla, React, Vue) repository.

It explains to you how to setup your project to use it and never worry again about indentation, curly spaces, let vs const etc...

This code style is only as useful as you activate travis for your project so that it runs linting in your test phase and warns you.

Just focus on coding.

Table of Contents

Setup your project

Base requirements

yarn add \
eslint @babel/eslint-parser prettier \
eslint-config-algolia eslint-config-prettier \
eslint-plugin-import eslint-plugin-prettier \
eslint-plugin-eslint-comments eslint-plugin-jsdoc \
--dev

Vanilla

.eslintrc.js

module.exports = {
  extends: 'algolia'
};

package.json

{
  "scripts": {
    "test": "npm run lint",
    "lint": "eslint .",
    "lint:fix": "npm run lint -- --fix"
  }
}

Jest

We recommend using Jest as a test runner.

terminal

yarn add eslint-plugin-jest --dev

.eslintrc.js

module.exports = {
  extends: ['algolia', 'algolia/jest']
};

package.json

{
  "scripts": {
    "test": "npm run lint",
    "lint": "eslint .",
    "lint:fix": "npm run lint -- --fix"
  }
}

React

terminal

yarn add eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-jsx-a11y --dev

.eslintrc.js

module.exports = {
  extends: ['algolia', 'algolia/react']
};

package.json

{
  "scripts": {
    "test": "npm run lint",
    "lint": "eslint .",
    "lint:fix": "npm run lint -- --fix"
  }
}

Flow

terminal

yarn add eslint-plugin-flowtype --dev

.eslintrc.js

module.exports = {
  extends: ['algolia', 'algolia/flowtype']
};

Flow with React

terminal

yarn add eslint-plugin-flowtype eslint-plugin-react eslint-plugin-react-hooks --dev

.eslintrc.js

module.exports = {
  extends: ['algolia', 'algolia/flowtype', 'algolia/react']
};

package.json

{
  "scripts": {
    "test": "npm run lint",
    "lint": "eslint .",
    "lint:fix": "npm run lint -- --fix"
  }
}

TypeScript

terminal

yarn add @typescript-eslint/parser @typescript-eslint/eslint-plugin typescript --dev

.eslintrc.js

module.exports = {
  extends: ['algolia', 'algolia/typescript']

  parserOptions: {
    project: '<path-to-tsconfig.json>',
  },
};

package.json

{
  "scripts": {
    "test": "npm run lint",
    "lint": "eslint --ext .js,.ts,.tsx .",
    "lint:fix": "npm run lint -- --fix"
  }
}

You also need to make sure that all the files you want to lint are also included in tsconfig.json with the include property.

TypeScript with React

terminal

yarn add @typescript-eslint/parser @typescript-eslint/eslint-plugin typescript eslint-plugin-react eslint-plugin-react-hooks eslint-plugin-jsx-a11y --dev

.eslintrc.js

module.exports = {
  extends: ['algolia', 'algolia/react', 'algolia/typescript']
};

Note: Be sure to put the algolia/typescript configuration last so the parser is properly set for TypeScript files.

package.json

{
  "scripts": {
    "test": "npm run lint",
    "lint": "eslint --ext .js,.ts,.tsx .",
    "lint:fix": "npm run lint -- --fix"
  }
}

Vue

terminal

yarn add eslint-plugin-vue --dev

.eslintrc.js

module.exports = {
  extends: ['algolia', 'algolia/vue']
};

package.json

{
  "scripts": {
    "test": "npm run lint",
    "lint": "eslint --ext .js,.vue .",
    "lint:fix": "npm run lint -- --fix"
  }
}

VSCode

{
  "eslint.validate": [
    {
      "language": "vue",
      "autoFix": true
    }
  ]
}

Node.js

package.json

{
  "scripts": {
    "test": "npm run lint",
    "lint": "eslint .",
    "lint:fix": "npm run lint -- --fix"
  }
}

.eslintrc.js

module.exports = {
  extends: 'algolia',
  rules: {
    'import/no-commonjs': 'off'
  }
};

Existing codebase setup

If you have a lot of files already written and wants to now use our linting tools, you might have a lot of errors. There's hope!

Just reformat all the files automatically and then manually fix remaining errors.

terminal

npm run lint:fix

Setup autofix in IDE

Don't overlook this part, autofixing on save is a huge productivity boost.

Use any ESLint integration and activate "Fix on save" option.

Also activate "Lint HTML files" when dealing with .vue components.

Ignoring files

See "Ignoring Files and Directories" on ESLint website.

Contributing

Test

We have a sample-project.

yarn test

Release

npm run release

eslint-config-algolia's People

Contributors

bobylito avatar bodinsamuel avatar francoischalifour avatar greenkeeperio-bot avatar haroenv avatar iam4x avatar jerska avatar jerskouille avatar millotp avatar pixelastic avatar rarkins avatar rayrutjes avatar redox avatar renovate-bot avatar renovate[bot] avatar samouss avatar sbellone avatar vvo avatar yannickcr avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

eslint-config-algolia's Issues

activate no-magic-numbers?

https://eslint.org/docs/rules/no-magic-numbers

I feel like in most situations you do want to activate this to enforce clear semantics in code.

For example:

statusCodeType = const statusFamily = Math.floor(statusCode / 100);
if (statusCodeType === 3) redirect();

VS

statusCodeType = const statusFamily = Math.floor(statusCode / 100);
if (statusCodeType === STATUSES_REDIRECTS) redirect();

WDYT?

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Error type: undefined. Note: this is a nested preset so please contact the preset author if you are unable to fix it yourself.

no-unused-expression is kind of too strict

I'm used to do conditional invocations like:

tryThat() || doThat();
// or
ensureThat() && doThat();

I find it very readable but it's currently not allowed, what do you think?

Action Required: Fix Renovate Configuration

There is an error with this repository's Renovate configuration that needs to be fixed. As a precaution, Renovate will stop PRs until it is resolved.

Location: package.json
Error type: The renovate configuration file contains some invalid settings
Message: Configuration option 'packageRules[0].node' should be a json object, Invalid configuration option: author, Invalid configuration option: license, Invalid configuration option: main, Invalid configuration option: packageRules[0].yarn, Invalid configuration option: packageRules[1].conventional-changelog-cli, Invalid configuration option: packageRules[1].doctoc, Invalid configuration option: packageRules[1].json, Invalid configuration option: private, Invalid configuration option: renovate, Invalid configuration option: scripts, Invalid configuration option: version, Invalid configuration option: workspaces, The "node" object can only be configured at the top level of a config but was found inside "packageRules[0]"

Remove Jest plugin from base

I'm working on a small plugin for Atom, and I don't use Jest.
Can I disable that plugin when extending the algolia config?
Otherwise could we remove that plugin from base config?

Overly complicated line?

The documentation indicates that the lint line is overly complicated. Does not look like so to me. Is that an outdated documentation?

image

Add angular rules

In the same spirit of Vue support, could we support Angular in our eslint? I have no idea what's necessary, any hint @iam4x?

Use prettier in JS projects

We could go further in term of linting and code formatting by:

  • removing stylistic rules from our eslint config files
  • use prettier auto formatting with pre commit hook on all supported projects (instantsearch.js/react-instantsearch for now)

Configuration for rule "@typescript-eslint/array-type" is invalid

I got the following error when trying to lint my project with 'algolia/typescript':

Error: .eslintrc.js » eslint-config-algolia/typescript » ./rules/typescript.js:
        Configuration for rule "@typescript-eslint/array-type" is invalid:
        Value "array-simple" should be object.

Here's my config:

// .eslintrc.js

module.exports = {
  extends: 'algolia/typescript'
}

And relevant dependency versions:

  • babel-eslint: ^10.0.2
  • eslint: ^6.1.0
  • eslint-config-algolia: ^13.4.0
  • eslint-config-prettier: ^6.0.0
  • eslint-plugin-import: ^2.18.2
  • eslint-plugin-jest: ^22.15.1
  • eslint-plugin-prettier: ^3.1.0
  • prettier: ^1.18.2

I'll try to investigate later, but thought I'd file this in case someone wants to give it a spin.

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Repository problems

These problems occurred while renovating this repository. View logs.

  • WARN: Using npm packages for Renovate presets is now deprecated. Please migrate to repository-based presets instead.

Other Branches

These updates are pending. To force PRs open, click the checkbox below.

  • chore(deps): lock file maintenance

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Ignored or Blocked

These are blocked by an existing closed PR and will not be recreated unless you click a checkbox below.

Detected dependencies

circleci
.circleci/config.yml
  • node 5.0.2
  • cimg/node 16.17.0
npm
package.json
  • conventional-changelog-cli 3.0.0
  • doctoc 2.2.1
  • json 11.0.0
  • renovate-config-algolia 2.1.10
  • node ^16.0.0
  • yarn ^3.0.0
  • yarn 3.6.1
packages/eslint-config-algolia/package.json
  • @babel/core 7.22.8
  • @babel/eslint-parser 7.22.7
  • eslint 8.44.0
  • eslint-config-prettier 8.8.0
  • eslint-plugin-eslint-comments 3.2.0
  • eslint-plugin-import 2.27.5
  • eslint-plugin-jest 27.2.2
  • eslint-plugin-jsdoc 46.4.3
  • eslint-plugin-jsx-a11y 6.7.1
  • eslint-plugin-prettier 5.0.0
  • eslint-plugin-react 7.32.2
  • eslint-plugin-react-hooks 4.6.0
  • prettier 3.0.0
  • eslint ^5.16.0 || ^6.8.0 || ^7.2.0 || ^8.0.0
  • prettier ^2.2.1
nvm
.nvmrc
  • node 18.16.1

  • Check this box to trigger a request for Renovate to run again on this repository

Proposal to disable no-return-await

I have two issues with no-return-await.

When there are multiple awaits

no-return-await tells you to change

async function myFunc() {
  await anotherFunc();
  return await anotherFunc2();
}

into

async function myFunc() {
  await anotherFunc();
  return anotherFunc2();
}

This is OK, until you start doing a try...catch here:

async function myFunc() {
  try {
    await anotherFunc();
    return anotherFunc2();
  } catch (e) {
    // Catches rejections from `anotherFunc`, but not from `anotherFunc2`
  }
}

Here if you add await after the return again, eslint will not complain anymore.
But you've most of the time already removed it, and it doesn't jump to my eyes that anotherFunc2 errors won't be catched.

When there's only one await

For me, things are even worse with only one await.
no-return-await tells you to change

async function myFunc() {
  return await anotherFunc();
}

into

async function myFunc() {
  return anotherFunc();
}

which require-await tells you to change to:

function myFunc() {
  return anotherFunc();
}

Suddenly, you have the same issue than the previous one, plus this function isn't marked async anymore, but you still need to await it if you call it.
Now if you add a try...catch but don't know if anotherFunc is async, it's now even worse, because you have no indication whatsoever that there's async stuff going on there:

function myFunc() {
  try {
    return anotherFunc();
  } catch (e) {
    // Looks 100% legit, not even an `async` keyword to tip us off
  }
}

An alternative solution

There's another pattern where eslint won't complain:

async function myFunc() {
  const res = await anotherFunc();
  return res;
}

Now that I know about it, I'm thinking about using it everywhere I have such returns of non-explicit promises.

Drawback

I assume that:

async function myFunc() {
  return await anotherFunc();
}

will actually create a wrapper promise on top of the other one, which could be concerning performance-wise.
I also believe that this is a case that should be easily optimizable by the compiler though.
Finally, I believe that the better clarity outweighs clearly the creation of a useless Promise instance.

cc @vvo @Haroenv who I pinged on Slack :)

switch to arrow-body-style always

https://eslint.org/docs/rules/arrow-body-style

Today our eslint transforms:

() => {
  return 'a';
}

into:

() => 'a';

This becomes annoying when you want to add, a console.log or another step:

() => {
  const i = '2';
  return 'a' + i;
};

Then the whole git diff gets broken (instead of a one line change, you get N line changed, N being the number of lines of the single return, can be high in React world (JSX) or with object literals.

I propose to always have braces.

WDYT?

.eslintignore

Shouldn't there be a default .eslintignore file that at least excluded node_modules? Or mention of it in the documentation?

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.