Skip to content

Commit

Permalink
Individual configs added. Closed #19
Browse files Browse the repository at this point in the history
  • Loading branch information
mlegenhausen committed Nov 11, 2021
1 parent a5b621e commit a3ca03f
Show file tree
Hide file tree
Showing 16 changed files with 2,286 additions and 215 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/lib
6 changes: 6 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"extends": "./lib/index.js",
"parserOptions": {
"project": "./tsconfig.json"
}
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
lib/
node_modules/
4 changes: 4 additions & 0 deletions .husky/pre-push
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

yarn lint
134 changes: 109 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,109 @@
# eslint-config-werk85

**Note:** Currently only Typescript Projects are supported.

## Installation

```sh
yarn add eslint-config-werk85 eslint -D
yarn add eslint-config-werk85 eslint@7 -D
```

## Included plugins
## Usage

Following plugins are included in `werk85/modern`:
Create a `.eslintrc.json` in the root of your project with the following content.

* [typescript-eslint](https://github.com/typescript-eslint/typescript-eslint)
```json
{
"root": true,
"extents": "werk85",
"parserOptions": {
"project": "./tsconfig.json"
}
}
```

Now add the following `lint:es` and `lint:es:fix` scripts to your root `package.json`:

```json
{
...
"scripts": {
"lint:es": "yarn eslint .",
"lint:es:fix": "yarn eslint --fix ."
}
...
}
```

### Monorepo Usage

In a monorepo setup is the configuration a little bit different. You need to extend the configuration as described in the following JSON snippet and add the `scripts` to each `packages/*/package.json` file. Then run the linting in all sub packages with `lerna` or `yarn` workspace tools.

```json
{
"root": true,
"extends": "werk85",
"parserOptions": {
"project": "./tsconfig.json"
},
"overrides": [
// This makes sure you can use commonjs `require` calls in `js` files that are included in your `tsconfig.json` files.
{
"files": ["*.js"],
"rules": {
"@typescript-eslint/no-var-requires": "off"
}
},
// Exclude this
{
"files": ["packages/*/test/**"],
"extends": ["werk85", "werk85/jest"],
"parserOptions": {
"project": "./test/tsconfig.json"
}
}
]
}
```

The configuration expects that you have in each project root, `packages/*` and `packages/*/test` directory a `tsconfig.json` that `includes` or `excludes` all files you want to lint. For linting of `*.js` files make sure you set `allowJs: true` in your root `tsconfig.json` if you extend all other configurations from it or in each individually.

#### Extending the Monorepo Base Configuration

If you need to extend a configuration e.g. for using the `werk85/react` ruleset in your frontend package, you need to create a `.eslintrc.js` file in the folder for which the new configuration should be applied and copy the following code.

```js
const path = require('path')

const project = path.join(__dirname, 'tsconfig.json')

module.exports = {
extends: 'werk85/react',
parserOptions: {
project
},
settings: {
'import/resolver': {
typescript: {
project
}
}
}
}
```

This base configuration can now be adjusted to your needs. It is important to define the `project` properties as absolute paths else it can happen that `eslint` uses the wrong `tsconfig.json`.

If you want to lint the `.eslintrc.js` file in your project you need to explicitly add the file in your `tsconfig.json`s `includes` property. Hidden files are excluded by default from typescript projects.

## Included Configurations

### Base Configuration

Following plugins are included in `werk85`:

* [deprecation](https://github.com/gund/eslint-plugin-deprecation)
* [import](https://github.com/import-js/eslint-plugin-import)
* [jest](https://github.com/jest-community/eslint-plugin-jest)
* [prettier](https://github.com/prettier/eslint-plugin-prettier)
* [typescript-eslint](https://github.com/typescript-eslint/typescript-eslint)

Please consult the documentation of each plugin for further rule explanation.

Expand All @@ -26,35 +114,31 @@ The following plugins are added addtionally to `werk85/react`:
* [react](https://github.com/yannickcr/eslint-plugin-react)
* [react-hooks](https://github.com/facebook/react/tree/main/packages/eslint-plugin-react-hooks)

## Usage
### Jest

Create a `.eslintrc.js` file in your project root and add the following content
The following plugins are added addtionally to `werk85/jest`:

```js
module.exports = {
extends: [
'werk85/modern'
],
parserOptions: {
project: './tsconfig.json'
}
}
```
* [jest](https://github.com/jest-community/eslint-plugin-jest)

For convinience you can add the following npm scripts to your `package.json`
This configuration can be added as follows to yours `.eslintrc.json`:

```json
{
"scripts": {
"lint": "eslint --ext .ts,.tsx src",
"lint:fix": "eslint --ext .ts,.tsx --fix src"
}
"overrides": [
{
"files": "test/**",
"extends": ["werk85", "werk85/jest"],
"parserOptions": {
"project": "./test/tsconfig.json"
}
}
]
}
```

### Error: Unable to resolve path to module

If this error occurs set the `import/resolver` typescript directory in your `.eslintrc.js` file to the path of the `tsconfig.json` file that defines the module `paths` parameter.
If this error occurs set the `import/resolver` typescript directory in your `.eslintrc.json` or `.eslintrc.js` file to the path of the `tsconfig.json` file that defines the module `paths` parameter.

```js
module.exports = {
Expand Down
115 changes: 0 additions & 115 deletions modern.js

This file was deleted.

41 changes: 27 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,28 +1,41 @@
{
"name": "eslint-config-werk85",
"version": "4.4.0",
"version": "5.0.0-beta.4",
"main": "index.js",
"repository": "git@github.com:werk85/eslint-rules.git",
"repository": "git@github.com:werk85/eslint-config-werk85.git",
"author": "Malte Legenhausen <malte@werk85.de>",
"license": "MIT",
"scripts": {
"build": "tsc -p tsconfig.cjs.json && ts-node ./scripts/build.ts",
"lint": "eslint --ext .js,.ts .",
"postinstall": "husky install",
"prepublishOnly": "ts-node ./scripts/pre-publish.ts",
"prerelease": "yarn build",
"release": "ts-node ./scripts/release.ts"
},
"dependencies": {
"@typescript-eslint/eslint-plugin": "^4.19.0",
"@typescript-eslint/parser": "^4.19.0",
"eslint-config-prettier": "^8.1.0",
"eslint-import-resolver-typescript": "^2.4.0",
"eslint-plugin-deprecation": "^1.2.0",
"eslint-plugin-import": "^2.22.1",
"eslint-plugin-jest": "^24.4.0",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-react": "^7.23.1",
"@typescript-eslint/eslint-plugin": "^4.33.0",
"@typescript-eslint/parser": "^4.33.0",
"eslint-config-prettier": "^8.3.0",
"eslint-import-resolver-typescript": "^2.5.0",
"eslint-plugin-deprecation": "^1.2.1",
"eslint-plugin-import": "^2.24.2",
"eslint-plugin-jest": "^25.0.1",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.26.1",
"eslint-plugin-react-hooks": "^4.2.0",
"prettier": "^2.2.1",
"typescript": "^4.2.3"
"prettier": "^2.4.1",
"typescript": "^4.4.3"
},
"peerDependencies": {
"eslint": "^7.22.0"
},
"devDependencies": {
"eslint": "^7.22.0"
"@types/eslint": "^7.28.1",
"@types/node": "^16.10.3",
"eslint": "^7.22.0",
"husky": "^7.0.2",
"npm-check-updates": "^11.8.5",
"ts-node": "^10.2.1"
}
}
12 changes: 12 additions & 0 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import fs from 'fs'
import pkg from '../package.json'

fs.writeFileSync(
'lib/package.json',
JSON.stringify(
Object.fromEntries(Object.entries(pkg).filter(([key]) => !['scripts', 'devDependencies', 'files'].includes(key))),
undefined,
2
),
'utf8'
)
2 changes: 2 additions & 0 deletions scripts/pre-publish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
console.error('"yarn publish" can not be run from root, run "yarn release" instead')
process.exit(1)
4 changes: 4 additions & 0 deletions scripts/release.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import cp from 'child_process'
import pkg from '../package.json'

cp.execFileSync('yarn', ['publish', '--new-version', pkg.version, '--tag', 'beta'], { cwd: 'lib', stdio: 'inherit' })

0 comments on commit a3ca03f

Please sign in to comment.