|
| 1 | +# eslint-plugin-leafygreen |
| 2 | + |
| 3 | +Lint Rules for LeafyGreen UI |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +You'll first need to install [ESLint](https://eslint.org/): |
| 8 | + |
| 9 | +```sh |
| 10 | +pnpm install -D eslint |
| 11 | +``` |
| 12 | + |
| 13 | +Next, install `@lg-tools/eslint-plugin`: |
| 14 | + |
| 15 | +```sh |
| 16 | +pnpm install -D @lg-tools/eslint-plugin |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +Add `@lg-tools` to the plugins section of your `.eslintrc` configuration file. (You can omit the `/eslint-plugin` suffix): |
| 22 | + |
| 23 | +```json |
| 24 | +{ |
| 25 | + "plugins": ["@lg-tools"], |
| 26 | + "extends": ["plugin:@lg-tools/internal"] |
| 27 | +} |
| 28 | +``` |
| 29 | + |
| 30 | +Optionally configure the rules you want to use under the rules section. |
| 31 | + |
| 32 | +```json |
| 33 | +{ |
| 34 | + "rules": { |
| 35 | + "@lg-tools/some-rule": ["warn"] |
| 36 | + } |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +## Rules |
| 41 | + |
| 42 | +<!-- |
| 43 | + Auto-generate this section with |
| 44 | + `pnpm --filter="@lg-tools/eslint-plugin" run docs` |
| 45 | +--> |
| 46 | +<!-- begin auto-generated rules list --> |
| 47 | + |
| 48 | +💼 Configurations enabled in.\ |
| 49 | +🔧 Automatically fixable by the [`--fix` CLI option](https://eslint.org/docs/user-guide/command-line-interface#--fix). |
| 50 | + |
| 51 | +| Name | Description | 💼 | 🔧 | |
| 52 | +| :------------------------------------------------------- | :------------------------------------------------- | :------------------ | :-- | |
| 53 | +| [no-indirect-imports](docs/rules/no-indirect-imports.md) | Forbid indirect imports from `src/` or `packages/` | ![badge-internal][] | 🔧 | |
| 54 | + |
| 55 | +<!-- end auto-generated rules list --> |
| 56 | + |
| 57 | +## Contributing |
| 58 | + |
| 59 | +To create a new rule: |
| 60 | + |
| 61 | +### 1. Run the `create-rule` script |
| 62 | + |
| 63 | +```sh |
| 64 | +pnpm --filter @lg-tools/eslint-plugin run create-rule <rule-name> |
| 65 | +``` |
| 66 | + |
| 67 | +This will create a new file in `src/rules`, and a test file in `src/tests` |
| 68 | + |
| 69 | +### 2. Add AST Listeners |
| 70 | + |
| 71 | +Inside your new `src/rules/<my-rule>.ts` file, add the appropriate metadata, and write AST listeners. |
| 72 | + |
| 73 | +The return object of a rule's `create` method should return at least one AST listener. |
| 74 | + |
| 75 | +These work similar to CSS selectors, and run a function when the ESLint static analyzer hits a specific AST node. |
| 76 | + |
| 77 | +See [ESLint Docs](https://eslint.org/docs/latest/extend/custom-rules) for more details. |
| 78 | + |
| 79 | +```ts |
| 80 | +export const exampleRule = createRule({ |
| 81 | + // ... |
| 82 | + create: context => { |
| 83 | + return { |
| 84 | + VariableDeclaration: node => { |
| 85 | + // Executes on any variable declaration |
| 86 | + // e.g. const myVar = 5; |
| 87 | + }, |
| 88 | + }; |
| 89 | + }, |
| 90 | +}); |
| 91 | +``` |
| 92 | + |
| 93 | +## 3. Add tests |
| 94 | + |
| 95 | +Inside the new `src/test/<my-rule>.spec.ts` file, add valid and invalid test cases. |
| 96 | + |
| 97 | +See [ESLint RuleTester docs](https://eslint.org/docs/latest/integrate/nodejs-api#ruletester) for more details. |
| 98 | + |
| 99 | +### Useful Resources |
| 100 | + |
| 101 | +- [ESLint Docs](https://eslint.org/docs/latest/extend/custom-rules) |
| 102 | +- [ESLint RuleTester](https://eslint.org/docs/latest/integrate/nodejs-api#ruletester) |
| 103 | +- [ESTree Spec](https://github.com/estree/estree/blob/master/README.md) |
| 104 | +- [JSX Spec](https://github.com/facebook/jsx/blob/main/README.md) |
| 105 | +- [TSESLint Listener Definitions](https://github.com/typescript-eslint/typescript-eslint/blob/main/packages/utils/src/ts-eslint/Rule.ts#L293) |
0 commit comments