|
| 1 | +--- |
| 2 | +title: Lint Plugin |
| 3 | +description: A guide on how to use the optional lint plugin for TypeGPU |
| 4 | +--- |
| 5 | +import { Tabs, TabItem } from '@astrojs/starlight/components'; |
| 6 | + |
| 7 | +[eslint-plugin-typegpu](https://www.npmjs.com/package/eslint-plugin-typegpu) is an optional (but highly recommended) tool for projects using TypeGPU. |
| 8 | +It helps with writing 'use gpu' functions by highlighting common pitfalls and unsupported syntax. |
| 9 | + |
| 10 | +## Installation |
| 11 | + |
| 12 | +<Tabs syncKey='package-manager'> |
| 13 | + <TabItem label='npm' icon='seti:npm'> |
| 14 | + ```sh frame=none |
| 15 | + npm add -D eslint-plugin-typegpu |
| 16 | + ``` |
| 17 | + </TabItem> |
| 18 | + <TabItem label='pnpm' icon='pnpm'> |
| 19 | + ```sh frame=none |
| 20 | + pnpm add -D eslint-plugin-typegpu |
| 21 | + ``` |
| 22 | + </TabItem> |
| 23 | + <TabItem label='yarn' icon='seti:yarn'> |
| 24 | + ```sh frame=none |
| 25 | + yarn add -D eslint-plugin-typegpu |
| 26 | + ``` |
| 27 | + </TabItem> |
| 28 | +</Tabs> |
| 29 | + |
| 30 | +After installing, the plugin still needs to be added to a config. |
| 31 | +The way this is done depends on the linter used. |
| 32 | + |
| 33 | +## Configuring |
| 34 | + |
| 35 | +### ESLint (`eslint.config.js`) |
| 36 | + |
| 37 | +The plugin needs to be imported and included in `defineConfig`. |
| 38 | + |
| 39 | +```ts |
| 40 | +import { defineConfig } from 'eslint/config'; |
| 41 | +import typegpu from 'eslint-plugin-typegpu'; |
| 42 | + |
| 43 | +export default defineConfig([ |
| 44 | + { |
| 45 | + plugins: { typegpu }, |
| 46 | + rules: { |
| 47 | + 'typegpu/no-integer-division': 'warn', |
| 48 | + 'typegpu/no-math': 'warn', |
| 49 | + // other rules you want to enable |
| 50 | + } |
| 51 | + } |
| 52 | +]); |
| 53 | +``` |
| 54 | + |
| 55 | +The plugin also exports `recommended` and `all` configs, which may be used instead of listing the rules one by one. |
| 56 | + |
| 57 | +```ts |
| 58 | +import { defineConfig } from 'eslint/config'; |
| 59 | +import typegpu from 'eslint-plugin-typegpu'; |
| 60 | + |
| 61 | +export default defineConfig([ |
| 62 | + typegpu.configs.recommended, |
| 63 | +]); |
| 64 | +``` |
| 65 | + |
| 66 | +### Oxlint (`oxlint.config.ts`) |
| 67 | + |
| 68 | +The plugin needs to be listed in `jsPlugins`. The rules can either be listed one by one, or a config can be used. |
| 69 | + |
| 70 | +```ts |
| 71 | +import { defineConfig } from 'oxlint'; |
| 72 | +import typegpu from 'eslint-plugin-typegpu'; |
| 73 | + |
| 74 | +export default defineConfig({ |
| 75 | + jsPlugins: ['eslint-plugin-typegpu'], |
| 76 | + rules: { |
| 77 | + ...typegpu.configs.recommended.rules, |
| 78 | + // (optional) changes from the recommended preset |
| 79 | + }, |
| 80 | +}); |
| 81 | +``` |
| 82 | + |
| 83 | +### Oxlint (`.oxlintrc.json`) |
| 84 | + |
| 85 | +The plugin needs to be listed in `jsPlugins`, and the rules have to be listed one by one. |
| 86 | +In this type of config there is no way to enable all rules |
| 87 | + |
| 88 | +```json |
| 89 | +{ |
| 90 | + "$schema": "./node_modules/oxlint/configuration_schema.json", |
| 91 | + "jsPlugins": ["eslint-plugin-typegpu"], |
| 92 | + "rules": { |
| 93 | + "typegpu/no-integer-division": "warn", |
| 94 | + "typegpu/no-math": "warn", |
| 95 | + ... |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +## Rules |
| 101 | + |
| 102 | +For the full list of supported rules, as well as their specifics, see the [README.md](https://www.npmjs.com/package/eslint-plugin-typegpu) of the plugin. |
0 commit comments