Skip to content

Commit 5a1d3bd

Browse files
committed
feat: add eslint-graph-config package
Signed-off-by: Tomás Migone <[email protected]>
1 parent 80a00bc commit 5a1d3bd

File tree

8 files changed

+572
-38
lines changed

8 files changed

+572
-38
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ This repository is a Yarn workspaces monorepo containing the following packages:
3737
| Package | Latest version | Description |
3838
| --- | --- | --- |
3939
| [contracts](./packages/contracts) | [![npm version](https://badge.fury.io/js/@graphprotocol%2Fcontracts.svg)](https://badge.fury.io/js/@graphprotocol%2Fcontracts) | Contracts enabling the open and permissionless decentralized network known as The Graph protocol. |
40+
| [eslint-graph-config](./packages/eslint-graph-config) | [![npm version]()]() | Shared linting and formatting rules for TypeScript projects. |
41+
| [subgraph-service](./packages/subgraph-service) | [![npm version]()]() | Contracts for the Subgraph data service in Graph Horizon. |
4042
| [sdk](./packages/sdk) | [![npm version](https://badge.fury.io/js/@graphprotocol%2Fsdk.svg)](https://badge.fury.io/js/@graphprotocol%2Fsdk) | TypeScript based SDK to interact with the protocol contracts |
4143

4244

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
"packageManager": "[email protected]",
99
"workspaces": [
1010
"packages/contracts",
11+
"packages/eslint-graph-config",
1112
"packages/subgraph-service",
1213
"packages/sdk"
1314
],
+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# eslint-graph-config
2+
3+
This repository contains shared linting and formatting rules for TypeScript projects.
4+
5+
## Installation
6+
7+
```bash
8+
yarn add --dev eslint eslint-graph-config
9+
```
10+
11+
For projects on this monorepo, you can use the following command to install the package:
12+
13+
```bash
14+
yarn add --dev eslint eslint-graph-config@workspace:^x.y.z
15+
```
16+
17+
To enable the rules, you need to create an `eslint.config.js` file in the root of your project with the following content:
18+
19+
```javascript
20+
import eslintGraphConfig from 'eslint-graph-config'
21+
export default eslintGraphConfig
22+
```
23+
24+
## Tooling
25+
26+
This package uses the following tools:
27+
- [ESLint](https://eslint.org/) as the base linting tool
28+
- [typescript-eslint](https://typescript-eslint.io/) for TypeScript support
29+
- [ESLint Stylistic](https://eslint.style/) as the formatting tool
30+
31+
**Why no prettier?**
32+
Instead of prettier we use ESLint Stylistic which is a set of ESLint rules focused on formatting and styling code. As opposed to prettier, ESLint Stylistic runs entirely within ESLint and does not require a separate tool to be run (e.g. `prettier`, `eslint-plugin-prettier` and `eslint-config-prettier`). Additionally it's supposed to be [more efficient](https://eslint.style/guide/why#linters-vs-formatters) and [less opinionated](https://antfu.me/posts/why-not-prettier).
33+
34+
## VSCode support
35+
36+
If you are using VSCode you can install the [ESLint extension](https://marketplace.visualstudio.com/items?itemName=dbaeumer.vscode-eslint) to get real-time linting and formatting support.
37+
38+
The following settings should be added to your `settings.json` file:
39+
```json
40+
{
41+
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
42+
"eslint.format.enable": true,
43+
"eslint.experimental.useFlatConfig": true,
44+
"eslint.workingDirectories": [{ "pattern": "./packages/*/" }]
45+
}
46+
```
47+
48+
Additionally you can configure the `Format document` keyboard shortcut to run `eslint --fix` on demand.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// This file only exists to enable linting on index.js
2+
import config from './index.js'
3+
export default config

packages/eslint-graph-config/index.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// @ts-check
2+
3+
/* eslint-disable @typescript-eslint/no-unsafe-assignment */
4+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
5+
/* eslint-disable @typescript-eslint/no-unsafe-argument */
6+
7+
import eslint from '@eslint/js'
8+
import noOnlyTests from 'eslint-plugin-no-only-tests'
9+
import noSecrets from 'eslint-plugin-no-secrets'
10+
import stylistic from '@stylistic/eslint-plugin'
11+
import tseslint from 'typescript-eslint'
12+
13+
export default tseslint.config(
14+
// Base eslint configuration
15+
// @ts-expect-error tseslint doesn't recognize eslint types for some reason
16+
eslint.configs.recommended,
17+
18+
// Enable linting with type information
19+
// https://typescript-eslint.io/getting-started/typed-linting
20+
...tseslint.configs.recommendedTypeChecked,
21+
{
22+
languageOptions: {
23+
parserOptions: {
24+
project: true,
25+
tsconfigRootDir: import.meta.dirname,
26+
},
27+
},
28+
},
29+
30+
// Formatting and stylistic rules
31+
stylistic.configs['recommended-flat'],
32+
33+
// Custom rules
34+
{
35+
plugins: {
36+
'no-only-tests': noOnlyTests,
37+
'no-secrets': noSecrets,
38+
},
39+
rules: {
40+
'prefer-const': 'warn',
41+
'@typescript-eslint/no-inferrable-types': 'warn',
42+
'@typescript-eslint/no-empty-function': 'warn',
43+
'no-only-tests/no-only-tests': 'error',
44+
'no-secrets/no-secrets': 'error',
45+
'sort-imports': [
46+
'warn', {
47+
memberSyntaxSortOrder: ['none', 'all', 'multiple', 'single'],
48+
ignoreCase: true,
49+
allowSeparatedGroups: true,
50+
}],
51+
},
52+
},
53+
)
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "eslint-graph-config",
3+
"version": "0.0.1",
4+
"description": "Linting and formatting rules for The Graph's TypeScript projects",
5+
"main": "index.js",
6+
"type": "module",
7+
"author": "The Graph Team",
8+
"license": "GPL-2.0-or-later",
9+
"devDependencies": {
10+
"@stylistic/eslint-plugin": "^1.6.2",
11+
"eslint": "^8.56.0",
12+
"eslint-plugin-no-only-tests": "^3.1.0",
13+
"eslint-plugin-no-secrets": "^0.8.9",
14+
"typescript": "^5.3.3",
15+
"typescript-eslint": "^7.0.2"
16+
}
17+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"compilerOptions": {
3+
"target": "es2020",
4+
"module": "commonjs",
5+
"esModuleInterop": true,
6+
"forceConsistentCasingInFileNames": true,
7+
"strict": true,
8+
"skipLibCheck": true,
9+
"resolveJsonModule": true
10+
}
11+
}

0 commit comments

Comments
 (0)