Skip to content

Commit

Permalink
feat: add eslint-graph-config package
Browse files Browse the repository at this point in the history
Signed-off-by: Tomás Migone <[email protected]>
  • Loading branch information
tmigone committed Feb 20, 2024
1 parent 80a00bc commit 5a1d3bd
Show file tree
Hide file tree
Showing 8 changed files with 572 additions and 38 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ This repository is a Yarn workspaces monorepo containing the following packages:
| Package | Latest version | Description |
| --- | --- | --- |
| [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. |
| [eslint-graph-config](./packages/eslint-graph-config) | [![npm version]()]() | Shared linting and formatting rules for TypeScript projects. |
| [subgraph-service](./packages/subgraph-service) | [![npm version]()]() | Contracts for the Subgraph data service in Graph Horizon. |
| [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 |


Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"packageManager": "[email protected]",
"workspaces": [
"packages/contracts",
"packages/eslint-graph-config",
"packages/subgraph-service",
"packages/sdk"
],
Expand Down
48 changes: 48 additions & 0 deletions packages/eslint-graph-config/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# eslint-graph-config

This repository contains shared linting and formatting rules for TypeScript projects.

## Installation

```bash
yarn add --dev eslint eslint-graph-config
```

For projects on this monorepo, you can use the following command to install the package:

```bash
yarn add --dev eslint eslint-graph-config@workspace:^x.y.z
```

To enable the rules, you need to create an `eslint.config.js` file in the root of your project with the following content:

```javascript
import eslintGraphConfig from 'eslint-graph-config'
export default eslintGraphConfig
```

## Tooling

This package uses the following tools:
- [ESLint](https://eslint.org/) as the base linting tool
- [typescript-eslint](https://typescript-eslint.io/) for TypeScript support
- [ESLint Stylistic](https://eslint.style/) as the formatting tool

**Why no prettier?**
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).

## VSCode support

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.

The following settings should be added to your `settings.json` file:
```json
{
"editor.defaultFormatter": "dbaeumer.vscode-eslint",
"eslint.format.enable": true,
"eslint.experimental.useFlatConfig": true,
"eslint.workingDirectories": [{ "pattern": "./packages/*/" }]
}
```

Additionally you can configure the `Format document` keyboard shortcut to run `eslint --fix` on demand.
3 changes: 3 additions & 0 deletions packages/eslint-graph-config/eslint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// This file only exists to enable linting on index.js
import config from './index.js'
export default config
53 changes: 53 additions & 0 deletions packages/eslint-graph-config/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// @ts-check

/* eslint-disable @typescript-eslint/no-unsafe-assignment */
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-unsafe-argument */

import eslint from '@eslint/js'
import noOnlyTests from 'eslint-plugin-no-only-tests'
import noSecrets from 'eslint-plugin-no-secrets'
import stylistic from '@stylistic/eslint-plugin'
import tseslint from 'typescript-eslint'

export default tseslint.config(
// Base eslint configuration
// @ts-expect-error tseslint doesn't recognize eslint types for some reason
eslint.configs.recommended,

// Enable linting with type information
// https://typescript-eslint.io/getting-started/typed-linting
...tseslint.configs.recommendedTypeChecked,
{
languageOptions: {
parserOptions: {
project: true,
tsconfigRootDir: import.meta.dirname,
},
},
},

// Formatting and stylistic rules
stylistic.configs['recommended-flat'],

// Custom rules
{
plugins: {
'no-only-tests': noOnlyTests,
'no-secrets': noSecrets,
},
rules: {
'prefer-const': 'warn',
'@typescript-eslint/no-inferrable-types': 'warn',
'@typescript-eslint/no-empty-function': 'warn',
'no-only-tests/no-only-tests': 'error',
'no-secrets/no-secrets': 'error',
'sort-imports': [
'warn', {
memberSyntaxSortOrder: ['none', 'all', 'multiple', 'single'],
ignoreCase: true,
allowSeparatedGroups: true,
}],
},
},
)
17 changes: 17 additions & 0 deletions packages/eslint-graph-config/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "eslint-graph-config",
"version": "0.0.1",
"description": "Linting and formatting rules for The Graph's TypeScript projects",
"main": "index.js",
"type": "module",
"author": "The Graph Team",
"license": "GPL-2.0-or-later",
"devDependencies": {
"@stylistic/eslint-plugin": "^1.6.2",
"eslint": "^8.56.0",
"eslint-plugin-no-only-tests": "^3.1.0",
"eslint-plugin-no-secrets": "^0.8.9",
"typescript": "^5.3.3",
"typescript-eslint": "^7.0.2"
}
}
11 changes: 11 additions & 0 deletions packages/eslint-graph-config/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"resolveJsonModule": true
}
}
Loading

0 comments on commit 5a1d3bd

Please sign in to comment.