Skip to content

Commit

Permalink
Initialize repo (#2)
Browse files Browse the repository at this point in the history
* Copy files from commons

* Move config files into src/

* Add standard repo files

* Add package.json and pnpm-lock.yaml

* Add .eslintrc.js

* Add index.js for exporting configs

* Update README

* Change 2 plugin instances of /all to /recommended

* Update packages

* Add Renovate config

* Add missing package.json fields for packaging
  • Loading branch information
dcroote authored Jun 19, 2024
1 parent 72e6b29 commit 858925a
Show file tree
Hide file tree
Showing 16 changed files with 3,665 additions and 1 deletion.
12 changes: 12 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.build
.env
.idea
.log
.tsbuildinfo
.vscode
build
dist
node_modules
coverage
.DS_Store
.eslintcache
12 changes: 12 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module.exports = {
env: {
node: true,
browser: true,
es6: true,
},
plugins: ['lodash'],
rules: {
// Because some of the modules might be used in browser, prefer import-scope method.
'lodash/import-scope': ['error', 'method'],
},
};
33 changes: 33 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
name: Continuous Build

on:
push:
branches:
- main
pull_request:
branches:
- main

jobs:
lint:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18, 20]
name: Build, lint and test - Node ${{ matrix.node-version }}
steps:
- name: Clone repo
uses: actions/checkout@v4
- name: Install pnpm
uses: pnpm/action-setup@v2
with:
version: 8.8.0
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: 'pnpm'
- name: Install Dependencies
run: pnpm install
- name: Lint
run: pnpm run prettier:check && pnpm run eslint:check
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.build
.env
.idea
.log
.tsbuildinfo
.vscode
build
dist
node_modules
coverage
.DS_Store
.eslintcache
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"

pnpm run prettier:check && pnpm run eslint:check
17 changes: 17 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"bracketSpacing": true,
"printWidth": 120,
"singleQuote": true,
"tabWidth": 2,
"trailingComma": "es5",
"useTabs": false,
"overrides": [
{
"files": "*.md",
"options": {
"parser": "markdown",
"proseWrap": "always"
}
}
]
}
73 changes: 72 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,73 @@
# eslint-plugin-commons
The standard ESLint rule set for API3 projects

> ESLint configurations used across API3 projects.
The modules consists of multiple ESLint configurations supporting wide variety of targets:

- `universal` - Linting rules for universal (both FE and BE) JS/TS code (with the emphasis on TS).
- `react` - Linting rules for React code.
- `next-js` - Linting rules for Next.js code.
- `jest` - Linting rules for Jest tests. Note, that these rules are only applied for JS/TS files with `*.test.*`
extensions.

## Getting started

1. Create an `.eslintrc.js` configuration file in the repo root.
2. Extend the desired configuration(s).
3. Specify the `parserOptions.project` option with the path to the `tsconfig.json` file(s).
4. Install `eslint` (which is a peer dependency of this module) as dev dependencies.

For example:

```js
module.exports = {
extends: ['plugin:@api3/eslint-plugin-commons/universal', 'plugin:@api3/eslint-plugin-commons/jest'],
parserOptions: {
// We focus primarily on TS and for that we need to specify the TS configs which is project specific. The following
// is a common monorepo setup (root config and a config for each package).
project: ['./tsconfig.json', './packages/*/tsconfig.json'],
},
};
```

### Linting commands

We recommend using the following linting commands inside `package.json` scripts:

```json
{
"eslint:check": "eslint --report-unused-disable-directives --cache --ext js,ts,tsx,jsx . --max-warnings 0",
"eslint:fix": "pnpm run eslint:check --fix"
}
```

The `--cache` parameter makes ESLint create a `.eslintcache` file in the root of the project. This file should be put to
`.gitignore`.

## Rules

The configurations are a collection of various rulesets and the config is quite strict. In general there are rules that:

- Have a fixer (import ordering)
- Simplify code (combine two nested ifs)
- Make code more consistent (make `return void` pattern be split on two lines)
- Fix outdated stuff (avoid `!` ts operator when not necessary)
- Avoid vulnerabilities and errors (Number.parseInt without radix)

Tip: Some rules do have fixer with multiple variants of the fixes. You need to use the IDE to prompt the fixes and
choose the one you want.

### Overriding rules

To override a rule, you can use the `rules` section key in your `.eslintrc.js` file. For example:

```js
{
rules: {
'check-file/folder-naming-convention': 'off', // Turns of the kebab-case convention for folder names.
'unicorn/filename-case': 'off' // Turns of the kebab-case convention for filenames.
'import/no-default-export': 'off', // Turns off the rule that disallows default exports.
'import/prefer-default-export': 'error' // Turns on the rule that prefers default exports.
}
}
```
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
module.exports = {
configs: {
universal: require('./src/universal'),
react: require('./src/react'),
nextJs: require('./src/next-js'),
jest: require('./src/jest'),
},
};
57 changes: 57 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "@api3/eslint-plugin-commons",
"version": "0.0.1",
"keywords": [
"eslint",
"eslint-plugin"
],
"license": "MIT",
"engines": {
"node": ">=18.14.0"
},
"sideEffects": false,
"repository": {
"type": "git",
"url": "https://github.com/api3dao/eslint-plugin-commons"
},
"main": "index.js",
"files": [
"index.js",
"src/"
],
"scripts": {
"eslint:check": "eslint --report-unused-disable-directives --cache --ext js,ts,tsx,jsx . --max-warnings 0",
"eslint:fix": "pnpm run eslint:check --fix",
"prepare": "husky",
"prettier:check": "prettier --check \"./**/*.{js,ts,md,json}\"",
"prettier:fix": "prettier --write \"./**/*.{js,ts,md,json}\""
},
"dependencies": {
"@typescript-eslint/eslint-plugin": "^7.13.1",
"@typescript-eslint/parser": "^7.13.1",
"eslint-config-next": "^14.2.3",
"eslint-plugin-check-file": "^2.6.2",
"eslint-plugin-cypress": "^3.2.0",
"eslint-plugin-deprecation": "^3.0.0",
"eslint-plugin-functional": "^6.0.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-jest-formatting": "^3.1.0",
"eslint-plugin-lodash": "^8.0.0",
"eslint-plugin-no-only-tests": "^3.1.0",
"eslint-plugin-promise": "^6.1.1",
"eslint-plugin-react": "^7.32.1",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-unicorn": "^54.0.0",
"lodash": "^4.17.21"
},
"devDependencies": {
"@types/lodash": "^4.17.5",
"eslint": "^8.57.0",
"husky": "^9.0.11",
"prettier": "^3.3.1"
},
"peerDependencies": {
"eslint": "^8.57.0"
}
}
Loading

0 comments on commit 858925a

Please sign in to comment.