@codemachiner/eslint-config v3.0.0
JavaScript ESLint rules
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
- Use
- ESLint plugins
- Using with SublimeText
- Formatting your code using ESLint rules
- Example of
.eslintrc.js - Reading
- Changelog
- 3.0.0 - 8 October 2018
Install
npm i eslint @codemachiner/eslint-config --save-devRun 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-statementsWhen using with flow, Install eslint-plugin-flowtype and eslint-plugin-flowtype-errors.
npm install --save-dev eslint-plugin-flowtype eslint-plugin-flowtype-errorsBesides .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.
- loaded in
react&nodebundles - rules in
@codemachiner/eslint-config/rules/import
json
Lint JSON files.
promise
Enforce best practices for JavaScript promises.
- loaded in
react&nodebundles - rules in
@codemachiner/eslint-config/rules/promise
unicorn
Various awesome ESLint rules.
- loaded in
react&nodebundles - rules in
@codemachiner/eslint-config/rules/unicorn
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.
html
Allows linting and fixing inline scripts contained in HTML files.
- not in peerDependencies:
npm install --save-dev eslint-plugin-html - loaded in
reactbundle - rules in
@codemachiner/eslint-config/rules/html
react
React specific linting rules.
- not in peerDependencies:
npm install --save-dev eslint-plugin-react - loaded in
reactbundle - rules in
@codemachiner/eslint-config/rules/react
jsx-control-statements
ESLint rules for JSX-Control-Statements babel plugin (Neater If and For for React JSX).
- not in peerDependencies:
npm install --save-dev eslint-plugin-jsx-control-statements - loaded in
reactbundle - rules in
@codemachiner/eslint-config/rules/react
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"
],- not in peerDependencies:
npm install --save-dev eslint-plugin-compat - loaded in
reactbundle - rules in
@codemachiner/eslint-config/rules/compat
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.
SublimeLinter-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
FuzzyFilePath
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
- Detect Problems in JavaScript Automatically with ESLint
- Elements of JavaScript Style - while not related to ESLint, it set's you on the path of structuring and organizing yourself better.
- We have a problem with promises - Common mistakes using promises.
- Stack Overflow: What's the difference between dependencies, devDependencies and peerDependencies in npm package.json file?
- An Open Letter to JavaScript Leaders Regarding Semicolons
- Configuring ESLint
- ES6 Destructuring assignment
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
- unicorn:
unicorn/no-process-exit,unicorn/number-literal-case,unicorn/no-fn-reference-in-iterator,unicorn/import-index,unicorn/prefer-spread,unicorn/prefer-add-event-listener,unicorn/prefer-exponentiation-operator - react:
react/void-dom-elements-no-children,react/prefer-es6-class,react/no-unused-prop-types,react/no-unused-state,react/no-unsafe,react/no-will-update-set-state,react/forbid-dom-props,react/forbid-prop-types,react/jsx-filename-extension,react/jsx-max-depth - flowtype-errors:
flowtype-errors/type-import-style,flowtype-errors/require-types-at-top,flowtype-errors/no-unused-expressions,flowtype-errors/array-style-complex-type,flowtype-errors/array-style-simple-type
Changed
jsx-control-statements/jsx-use-if-tagfrom "error" -> "warn"react/sort-compaddedgetDerivedStateFromPropsandcomponentDidCatchflowtype/require-return-typefrom "off" -> "error"
7 years ago
7 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago