3.0.0 • Published 6 years ago

@codemachiner/eslint-config v3.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
6 years ago

JavaScript ESLint rules

npm package version dev-badge peer-badge

JavaScript ESLint bundle with best practices and common use rules for writing more consistent code.

"semi": [ "error", "never" ] :godmode: ... the horror :goberserk:

Other bundles: XO, eslint-config-airbnb, eslint-config-google, more


Install

npm i eslint @codemachiner/eslint-config --save-dev

Run npm info "@codemachiner/eslint-config@latest" peerDependencies to get the packages needed in your own package.json.

It should be something like this:

...
"devDependencies": {
    "eslint": "^5.6.1",
    "eslint-config-prettier": "^3.1.0",
    "eslint-plugin-import": "^2.14.0",
    "eslint-plugin-json": "^1.2.1",
    "eslint-plugin-no-inferred-method-name": "^1.0.2",
    "eslint-plugin-promise": "^4.0.1",
    "eslint-plugin-prettier": "^3.0.0",
    "eslint-plugin-unicorn": "^6.0.1",
    "prettier": "^1.14.3"
}
...

Use

Add the react or node target file in your .eslintrc file:

{
    "extends": [
        // use for Node.js projects
        "@codemachiner/eslint-config/targets/node",

        // use for React projects
        "@codemachiner/eslint-config/targets/react",

        // optional Flow support
        "@codemachiner/eslint-config/rules/flow",
    ]
}

When using react target, besides the peer dependencies, also install eslint-plugin-jsx-control-statements

npm install --save-dev eslint-plugin-jsx-control-statements

When using with flow, Install eslint-plugin-flowtype and eslint-plugin-flowtype-errors.

npm install --save-dev eslint-plugin-flowtype eslint-plugin-flowtype-errors

Besides .eslintrc, prettier also needs configuring. Here's a recommended .prettierrc config:

{
  "semi": false,
  "printWidth": 80,
  "tabWidth": 2,
  "useTabs": false,
  "singleQuote": false,
  "trailingComma": "es5",
  "bracketSpacing": true,
  "jsxBracketSameLine": true,
  "arrowParens": "avoid"
}

ESLint plugins

import

Support for ES2015+ (ES6+) import/export syntax.

json

Lint JSON files.

promise

Enforce best practices for JavaScript promises.

unicorn

Various awesome ESLint rules.

flowtype & flowtype-errors

  • flowtype: Flow specific linting rules.
  • flowtype-errors: Runs your code through Flow and passes the type check errors as linting errors. Any editor that has ESLint support now supports Flow.

  • rules in @codemachiner/eslint-config/rules/flow

html

Allows linting and fixing inline scripts contained in HTML files.

react

React specific linting rules.

jsx-control-statements

ESLint rules for JSX-Control-Statements babel plugin (Neater If and For for React JSX).

compat

Lint the browser compatibility of your code (using caniuse). Uses browserslist definition in your package.json.

"browserslist": [
    "last 2 chrome versions",
    "last 2 firefox versions",
    "last 2 ie versions"
],

no-inferred-method-name

In ES6, compact methods and unnamed function expression assignments within object literals do not create a lexical identification (name) binding that corresponds to the function name identifier for recursion or event binding. The compact method syntax will not be an appropriate option for these types of solutions, and a named function expression should be used instead. This custom ESLint rule will identify instances where a function name is being called and a lexical identifier is unavailable within a compact object literal.

Using with SublimeText

SublimeLinter

Sublime Text 3 plugin that provides a framework for linting code. Whatever language you code in, SublimeLinter can help you write cleaner, better, more bug-free code. SublimeLinter has been designed to provide maximum flexibility and usability for users and maximum simplicity for linter authors.

Sublime​Linter-contrib-eslint

Sublime Text 3 plugin for SublimeLinter that provides an interface to ESLint. It will be used with files that have the "javascript" syntax.

:godmode: TIP 1: Use SublimeLinter-contrib-eslint_d for "as you type lightning fast" linting.

:godmode: TIP 1.1: In order to make eslint-plugin-import and .eslintignore work you need to add chdir to your *.sublime-project file:

{
    "SublimeLinter": {
        "linters": {
            "eslint_d": {
                "chdir": "${project}"
            }
        }
    }
}

Read more here: fix Sublime integration without workaround and Relative stdin-filename breaks my plugin

Fuzzy​File​Path

Fuzzy search and insert filenames inside your current project directory. Highly customizable.

...
"scopes": [
    {
        // JS - ES6 import from/import "*"
        "scope"         : "\\.js\\s",
        "prefix"        : [ "from", "import" ],
        "extensions"    : [ "js", "jsx", "sass", "scss", "less", "css" ],

        "auto"          : true,
        "relative"      : false,
        "base_directory": false,

        "replace_on_insert": [
            // Remove extensions from path
            [ "\\.js$", "" ],
            [ "\\.jsx$", "" ],
            [ "\\.json$", "" ],

            // Remove base path and setup Webpack to resolve
            [ "\\/source/", "" ],
            [ "\\/assets/", "" ]
        ],
    }, {
        // CSS - import ""
        "scope"         : "source\\.(css|sass|less)",
        "prefix"        : [ "import" ],
        "extensions"    : [ "css", "scss", "less" ],

        "auto"             : true,
        "relative"         : true,
        "base_directory"   : false,
        "replace_on_insert": [],
    }, {
        // CSS - *: url()
        "scope"         : "source\\.(css|sass|less)",
        "prefix"        : [ "url" ],
        "extensions"    : [ "png", "gif", "jpeg", "jpg", "woff", "ttf", "svg", "otf" ],

        "auto"             : true,
        "relative"         : true,
        "base_directory"   : false,
        "replace_on_insert": [],
    },
]
...

Formatting your code using ESLint rules

ESLint-Formatter

Plugin to auto-format your javascript code according to the ESLint configuration files you already have.

While using the plugin with eslint you will notice a delay when formatting. This is because of the node startup time on each lint. You can configure it to use eslint_d which starts a server in the background and interfaces eslint, making the formatting almost instant.

eslint-watch

You can autofix you files without any editor plugin by running eslint --fix on files as they change. You will need to install eslint-watch, which interfaces whatever eslint you have in your project.

  • npm install eslint-watch --save-dev
  • add a script to package.json:
    "lint:watch": "esw --format simple-success --fix --watch --changed --ext .js,.jsx SOURCE_DIR"
  • run in terminal: npm run lint:watch

Example of .eslintrc.js

Using babel-eslint and eslint-import-resolver-webpack

/* eslint-env node */

module.exports = {
    root  : true,
    parser: "babel-eslint",

    extends: [ "@codemachiner/eslint-config/targets/react" ],

    settings: {
        // Use webpack to resolve modules in imports
        "import/resolver": {
            webpack: {
                config: "./webpack.config.js",
            },
        },

        // Recommended if you use eslint_d
        "import/cache": {
            lifetime: 5,
        },

        // A list of regex strings that, if matched by a path, will not report
        // the matching module if no exports are found.
        "import/ignore": [ "\.(sass|scss|less|css)$" ],
    },

    // Add your custom rules here
    rules: {
        // Don"t require .jsx extension when importing
        "import/extensions": [
            "error", "always", {
                js : "never",
                jsx: "never",
            },
        ],
    },
}

Reading

Changelog

History of all changes in CHANGELOG.md

3.0.0 - 8 October 2018

Restructuring bundles as "target" files. Supporting React and Node.js, with optional Flow support.

Delegating all stylistic rules to prettier via eslint-config-prettier and eslint-plugin-prettier.

Added

Changed

3.0.0

6 years ago

2.9.7

6 years ago

2.9.6

6 years ago

2.9.5

6 years ago

2.9.4

6 years ago

2.9.3

6 years ago

2.9.2

6 years ago

2.9.1

7 years ago

2.9.0

7 years ago

2.8.3

7 years ago

2.8.2

7 years ago

2.8.1

7 years ago

2.8.0

7 years ago

2.7.2

7 years ago

2.7.0

7 years ago

2.6.0

7 years ago

2.5.1

7 years ago

2.5.0

7 years ago

2.4.0

7 years ago

2.3.2

7 years ago

2.3.1

7 years ago

2.3.0

7 years ago

2.2.1

7 years ago

2.1.6

7 years ago

2.1.5

7 years ago

2.1.4

7 years ago

2.1.3

7 years ago

2.1.2

7 years ago

2.1.1

7 years ago

2.1.0

7 years ago

2.0.2

7 years ago

2.0.1

7 years ago

1.1.6

7 years ago

1.1.4

7 years ago

1.1.3

7 years ago

1.1.2

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago