Skip to content

Commit

Permalink
feat: add sharable eslint config
Browse files Browse the repository at this point in the history
Copy maily from the @wpquark/eslint-config upstream and create one
for typescript as well.

See #434
  • Loading branch information
swashata committed Apr 15, 2019
1 parent ab499e3 commit 9eb1e2c
Show file tree
Hide file tree
Showing 23 changed files with 472 additions and 40 deletions.
3 changes: 0 additions & 3 deletions .eslintrc

This file was deleted.

34 changes: 34 additions & 0 deletions .eslintrc.base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
module.exports = {
extends: [
'@wpquark',
'plugin:@typescript-eslint/recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
'prettier/@typescript-eslint',
],
parser: '@typescript-eslint/parser',
parserOptions: {
project: './tsconfig.json',
tsconfigRootDir: __dirname,
},
rules: {
'import/prefer-default-export': 'off',
'@typescript-eslint/explicit-function-return-type': 'off',
'@typescript-eslint/explicit-member-accessibility': 'off',
'class-methods-use-this': 'off',
'@typescript-eslint/no-explicit-any': 'off',
'import/no-cycle': 'off',
'import/no-dynamic-require': 'off',
'import/named': 'off',
},
settings: {
'import/parsers': {
'@typescript-eslint/parser': ['.ts', '.tsx'],
},
'import/resolver': {
typescript: {
directory: __dirname,
},
},
},
};
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,11 @@
"@babel/preset-typescript": "7.3.3",
"@types/jest": "24.0.11",
"@types/node": "11.13.4",
"@typescript-eslint/eslint-plugin": "1.6.0",
"@typescript-eslint/parser": "1.6.0",
"@wpquark/eslint-config": "3.2.0",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "24.7.1",
"cross-env": "5.2.0",
"eslint": "5.16.0",
"eslint-import-resolver-typescript": "1.1.1",
"jest": "24.7.1",
"lerna": "3.13.2",
"plop": "2.3.0",
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-config/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('./index');
34 changes: 34 additions & 0 deletions packages/eslint-config/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Logs
logs
*.log

# Runtime data
pids
*.pid
*.seed

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# node-waf configuration
.lock-wscript

# Compiled binary addons (http://nodejs.org/api/addons.html)
build/Release

# Dependency directory
# https://www.npmjs.org/doc/misc/npm-faq.html#should-i-check-my-node_modules-folder-into-git
node_modules
*.un~
yarn.lock
src
flow-typed
coverage
decls
examples
111 changes: 111 additions & 0 deletions packages/eslint-config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# `@wpackio/eslint-config`

Shared ESLint configuration for all `@wpackio` packages and more. It has shared
config for both JavaScript and TypeScript projects.

Note that this doesn't come installed with `@wpackio/scripts`. If you wish to
take advantage of this config, then install and use on your own.

## Installation

If using `yarn`

```bash
yarn add --dev @wpackio/eslint-config eslint prettier
```

or with `npm`

```bash
npm i -D @wpackio/eslint-config eslint prettier
```

## Usage with JavaScript

Using it with JavaScript project is very simple. Create a `.eslintrc.js` file in the root of your project and put the code.

```js
module.exports = {
extends: '@wpackio',
};
```

## Usage with TypeScript

Using with typescript requires a little more effort. In the same `.eslintrc.js`
file, put

```js
module.exports = {
extends: ['@wpackio/eslint-config/ts'],
parserOptions: {
project: './tsconfig.json',
tsconfigRootDir: __dirname,
},
settings: {
'import/resolver': {
typescript: {
directory: __dirname,
},
},
},
};
```

Putting `__dirname` in `parserOptions.tsconfigRootDir` and `['import/resolver'].typescript`
is necessary because of [this issue](https://github.com/typescript-eslint/typescript-eslint/issues/251).

For both the cases you can also extend upon the rules.

## Prettier config

Create a `prettier.config.js` file in the root of your project and put the code.

```js
module.exports = require('@wpackio/eslint-config/prettier.config');
```

Now you are ready to go.

## VSCode Integration

Install the [eslint](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint)
plugin for VSCode. Enable autoFormat for `javascript` and `javascriptreact` files.

- Go to Code > Preference [File > Preference for Windows & Linux].
- Edit the WorkSpace Settings (Recommended).

```json
{
"eslint.autoFixOnSave": true,
"[javascript]": {
"editor.formatOnSave": false
},
"[javascriptreact]": {
"editor.formatOnSave": false
}
}
```

If you are using for typescript files, the following additional settings are needed.

```json
{
"eslint.validate": [
"javascript",
"javascriptreact",
{ "language": "typescript", "autoFix": true },
{ "language": "typescriptreact", "autoFix": true }
]
}
```

## Development

This package has the same `npm scripts` as this monorepo. These should be run
using `lerna run <script>`. More information can be found under [CONTRIBUTION.md](../../CONTRIBUTION.md).

- `build`: Use babel to build for nodejs 8.6+. Files inside `src` are compiled and put under `lib`. All type definitions are stripped and individual type declaration files are created.
- `prepare`: Run `build` after `yarn` and before `publish`.
- `lint`: Lint all files using tslint.
- `test`: Run tests on files using jest.
1 change: 1 addition & 0 deletions packages/eslint-config/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = require('../../babel.config.js');
8 changes: 8 additions & 0 deletions packages/eslint-config/config/env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// Default set of env rules for ESLint

module.exports = {
browser: true,
commonjs: true,
es6: true,
node: true,
};
10 changes: 10 additions & 0 deletions packages/eslint-config/config/extends.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Default extends (from where we inherit our config)

module.exports = [
'airbnb',
'plugin:jest/recommended',
'plugin:prettier/recommended',
'prettier',
'prettier/react',
'prettier/babel',
];
69 changes: 69 additions & 0 deletions packages/eslint-config/config/rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Default sets of rules for ESLint

module.exports = {
// ===================================
// Some rules from eslint-plugin-babel
// ===================================
// First turn them off
'new-cap': 'off',
camelcase: 'off',
'no-invalid-this': 'off',
'object-curly-spacing': 'off',
semi: 'off',
'no-unused-expressions': 'off',
'valid-typeof': 'off',
// require a capital letter for constructors
'babel/new-cap': [
'error',
{
newIsCap: true,
newIsCapExceptions: [],
capIsNew: false,
capIsNewExceptions: [
'Immutable.Map',
'Immutable.Set',
'Immutable.List',
],
},
],
// require camel case names
// This one is enhanced from airbnb and accounts for destructuring
// and react UNSAFE_component* methods.
'babel/camelcase': [
'error',
{
properties: 'never',
ignoreDestructuring: true,
},
],
// We would force invalid this rules
// But would only warn about it
'babel/no-invalid-this': 'warn',
// We don't configure curly spacing because of prettier
'babel/object-curly-spacing': 'off',
// We don't configure babel/semi because of prettier
'babel/semi': 'off',
// disallow usage of expressions in statement position
'babel/no-unused-expressions': [
'error',
{
allowShortCircuit: false,
allowTernary: false,
allowTaggedTemplates: false,
},
],
// ensure that the results of typeof are compared against a valid string
// https://eslint.org/docs/rules/valid-typeof
'babel/valid-typeof': ['error', { requireStringLiterals: true }],
'react/no-unused-prop-types': 1,
'react/no-unused-state': 1,
'no-unused-vars': 1,
'prettier/prettier': ['error'],
'no-console': 0,
'no-plusplus': [
2,
{
allowForLoopAfterthoughts: true,
},
],
};
27 changes: 27 additions & 0 deletions packages/eslint-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) 2018 Swashata Ghosh <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

// Use this configuration for standard JavaScript Projects.

const rules = require('./config/rules');
const env = require('./config/env');
const ex = require('./config/extends');

module.exports = {
env,
extends: ex,
parser: 'babel-eslint',
parserOptions: {
ecmaVersion: 2018,
ecmaFeatures: {
jsx: true,
},
sourceType: 'module',
},
plugins: ['babel'],
rules: {
...rules,
},
};
46 changes: 46 additions & 0 deletions packages/eslint-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@wpackio/eslint-config",
"version": "0.0.1",
"description": "Shared ESLint config for all @wpackio packages.",
"keywords": [
"eslint",
"typescript",
"eslint-config",
"eslint-typescript-config"
],
"main": "index.js",
"repository": "https://github.com/swashata/wp-webpack-script",
"homepage": "https://wpack.io",
"author": "Swashata Ghosh &lt;[email protected]&gt; (https://swas.io)",
"license": "MIT",
"private": false,
"peerDependencies": {
"eslint": ">=5.3.0",
"prettier": ">=1.12.1"
},
"dependencies": {
"babel-eslint": "^10.0.1",
"eslint-config-airbnb": "^17.1.0",
"eslint-config-prettier": "^4.1.0",
"eslint-plugin-babel": "^5.3.0",
"eslint-plugin-import": "^2.17.1",
"eslint-plugin-jest": "^22.4.1",
"eslint-plugin-jsx-a11y": "^6.2.1",
"eslint-plugin-prettier": "^3.0.1",
"eslint-plugin-react": "^7.12.4",
"@typescript-eslint/eslint-plugin": "1.6.0",
"@typescript-eslint/parser": "1.6.0",
"eslint-import-resolver-typescript": "1.1.1"
},
"engines": {
"node": ">=8.9.0"
},
"scripts": {},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"eslint": "^5.16.0",
"prettier": "^1.17.0"
}
}
15 changes: 15 additions & 0 deletions packages/eslint-config/prettier.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (c) 2018 Swashata Ghosh <[email protected]>
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

module.exports = {
useTabs: true,
tabWidth: 4,
semi: true,
singleQuote: true,
trailingComma: 'es5',
bracketSpacing: true,
jsxBracketSameLine: false,
arrowParens: 'avoid',
};
Loading

0 comments on commit 9eb1e2c

Please sign in to comment.