-
-
Notifications
You must be signed in to change notification settings - Fork 47
feat: Lint plugin POC #2097
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
aleksanderkatan
wants to merge
24
commits into
main
Choose a base branch
from
feat/type-aware-lint-plugin-poc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
feat: Lint plugin POC #2097
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
91eeaa8
Add package.json
e063b7c
Add index.ts
a558156
Add tsup config
1af79a1
Add a dummy rule for testing
d0b6651
Add testing infrastructure
bb433b8
Add a proper rule
dfbaddd
Remove the dummy rule
4eead5a
Add a readme stub
19bbcd7
Add formatting
0ec6db1
Check for a cast in parent
6712b4b
Review fixes
320563b
Add a describe to please vitest gods
b5d3a21
Add configuration for test:types
c255562
Increase old size limit
ea990e6
Bump attest
cfd112e
Fix failing build
30af466
Add recommended rules
adb6799
Add all rules
340a924
Fix configuration so that the plugin is usable
9042d4e
Update lock
2ea027a
Merge remote-tracking branch 'origin/main' into feat/type-aware-lint-…
a49362e
Review fixes
ff415f6
Remove an unnecessary dependency
0795486
smol
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <div align="center"> | ||
|
|
||
| # eslint-plugin-typegpu | ||
|
|
||
| 🚧 **Under Construction** 🚧 - | ||
| [GitHub](https://github.com/software-mansion/TypeGPU/tree/main/packages/eslint-plugin) | ||
|
|
||
| </div> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "exclude": ["."], | ||
| "fmt": { | ||
| "exclude": ["!."], | ||
| "singleQuote": true | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| { | ||
| "name": "eslint-plugin-typegpu", | ||
| "version": "0.9.0", | ||
| "description": "An eslint plugin containing custom rules for TypeGPU.", | ||
| "type": "module", | ||
| "exports": { | ||
| ".": { | ||
| "types": "./dist/index.d.ts", | ||
| "import": "./dist/index.js", | ||
| "require": "./dist/index.cjs" | ||
| } | ||
| }, | ||
| "scripts": { | ||
| "build": "tsup", | ||
| "test:types": "pnpm tsc --p ./tsconfig.json --noEmit", | ||
| "test": "vitest" | ||
| }, | ||
| "sideEffects": false, | ||
| "license": "MIT", | ||
| "packageManager": "pnpm@10.15.1", | ||
| "dependencies": { | ||
| "@typescript-eslint/utils": "^8.53.0" | ||
| }, | ||
| "peerDependencies": { | ||
| "eslint": "^9.0.0" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/node": "^25.0.10", | ||
| "@typescript-eslint/rule-tester": "^8.53.1", | ||
| "eslint": "^9.39.2", | ||
| "typescript": "^5.9.3", | ||
| "vitest": "^4.0.17" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| import type { TSESLint } from '@typescript-eslint/utils'; | ||
| import { integerDivision } from './rules/integerDivision.ts'; | ||
|
|
||
| export const rules = { | ||
| 'integer-division': integerDivision, | ||
| } as const; | ||
|
|
||
| type Rules = Record< | ||
| `typegpu/${keyof typeof rules}`, | ||
| TSESLint.FlatConfig.RuleEntry | ||
| >; | ||
|
|
||
| export const recommendedRules: Rules = { | ||
| 'typegpu/integer-division': 'warn', | ||
| }; | ||
|
|
||
| export const allRules: Rules = { | ||
| 'typegpu/integer-division': 'error', | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| import pkg from '../package.json'; | ||
| import type { TSESLint } from '@typescript-eslint/utils'; | ||
| import { allRules, recommendedRules, rules } from './configs.ts'; | ||
|
|
||
| const pluginBase: TSESLint.FlatConfig.Plugin = { | ||
| meta: { | ||
| name: pkg.name, | ||
| version: pkg.version, | ||
| }, | ||
| rules, | ||
| }; | ||
|
|
||
| const recommended: TSESLint.FlatConfig.Config = { | ||
| name: 'typegpu/recommended', | ||
| plugins: { typegpu: pluginBase }, | ||
| rules: recommendedRules, | ||
| }; | ||
|
|
||
| const all: TSESLint.FlatConfig.Config = { | ||
| name: 'typegpu/all', | ||
| plugins: { typegpu: pluginBase }, | ||
| rules: allRules, | ||
| }; | ||
|
|
||
| const plugin: TSESLint.FlatConfig.Plugin = { | ||
| ...pluginBase, | ||
| configs: { | ||
| recommended, | ||
| all, | ||
| }, | ||
| }; | ||
|
|
||
| export default plugin; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| import { ESLintUtils } from '@typescript-eslint/utils'; | ||
|
|
||
| export const createRule = ESLintUtils.RuleCreator( | ||
| // TODO: docs for lint rules | ||
| () => `https://docs.swmansion.com/TypeGPU/getting-started/`, | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| import type { TSESTree } from '@typescript-eslint/utils'; | ||
| import { createRule } from '../ruleCreator.ts'; | ||
|
|
||
| // TODO: detect `std.div(d.u32(1), d.u32(2))` | ||
| export const integerDivision = createRule({ | ||
| name: 'integer-division', | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { description: `Avoid dividing numbers wrapped in 'u32' and 'i32'.` }, | ||
| messages: { | ||
| intDiv: | ||
| "'{{snippet}}' might result in floating point values. To perform integer division, wrap the result in 'd.u32' or 'd.i32' instead.", | ||
| }, | ||
| schema: [], | ||
| }, | ||
| defaultOptions: [], | ||
|
|
||
| create(context) { | ||
| return { | ||
| BinaryExpression(node) { | ||
| if (node.operator !== '/') { | ||
| return; | ||
| } | ||
|
|
||
| if (node.parent?.type === 'CallExpression' && isIntCast(node.parent)) { | ||
| return; | ||
| } | ||
|
|
||
| if (isIntCast(node.left) && isIntCast(node.right)) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'intDiv', | ||
| data: { snippet: context.sourceCode.getText(node) }, | ||
| }); | ||
| } | ||
| }, | ||
| }; | ||
| }, | ||
| }); | ||
|
|
||
| /** | ||
| * Checks if a node is a call expression to an integer cast function (i32 or u32). | ||
| * | ||
| * @example | ||
| * // for simplicity, using code snippets instead of ASTs | ||
| * isIntCasts('d.u32()'); // true | ||
| * isIntCasts('i32()'); // true | ||
| * isIntCasts('f32()'); // false | ||
| */ | ||
| function isIntCast(node: TSESTree.Expression): boolean { | ||
| if (node.type !== 'CallExpression') { | ||
| return false; | ||
| } | ||
|
|
||
| let callee: TSESTree.Node = node.callee; | ||
| while (callee.type === 'MemberExpression') { | ||
| callee = callee.property; | ||
| } | ||
|
|
||
| return callee.type === 'Identifier' && ['i32', 'u32'].includes(callee.name); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import { describe } from 'vitest'; | ||
| import { integerDivision } from '../src/rules/integerDivision.ts'; | ||
| import { ruleTester } from './ruleTester.ts'; | ||
|
|
||
| describe('integerDivision', () => { | ||
| ruleTester.run('integerDivision', integerDivision, { | ||
| valid: [ | ||
| '1 / 2', | ||
| 'd.u32(1) / 2', | ||
| '1 / d.u32(2)', | ||
| 'd.u32(d.u32(1) / d.u32(2))', | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: 'd.u32(1) / d.u32(2)', | ||
| errors: [ | ||
| { messageId: 'intDiv', data: { snippet: 'd.u32(1) / d.u32(2)' } }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'd.i32(1) / d.i32(2)', | ||
| errors: [ | ||
| { messageId: 'intDiv', data: { snippet: 'd.i32(1) / d.i32(2)' } }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'd.u32(1) / d.i32(2)', | ||
| errors: [ | ||
| { messageId: 'intDiv', data: { snippet: 'd.u32(1) / d.i32(2)' } }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'u32(1) / u32(2)', | ||
| errors: [ | ||
| { messageId: 'intDiv', data: { snippet: 'u32(1) / u32(2)' } }, | ||
| ], | ||
| }, | ||
| { | ||
| code: 'd.u32(1) / d.u32(2) / d.u32(3)', | ||
| errors: [ | ||
| { messageId: 'intDiv', data: { snippet: 'd.u32(1) / d.u32(2)' } }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| import { RuleTester } from '@typescript-eslint/rule-tester'; | ||
| import { afterAll, describe, it } from 'vitest'; | ||
|
|
||
| // RuleTester relies on global hooks for tests. | ||
| // Vitest doesn't make the hooks available globally, so we need to bind them. | ||
| RuleTester.describe = describe; | ||
| RuleTester.it = it; | ||
| RuleTester.afterAll = afterAll; | ||
|
|
||
| export const ruleTester = new RuleTester(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "types": ["node"] | ||
| }, | ||
| "include": ["src/**/*", "tests/**/*"], | ||
| "exclude": ["node_modules", "dist"] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { defineConfig, type Options } from 'tsup'; | ||
|
|
||
| const config: Options = { | ||
| entry: ['src/index.ts'], | ||
| format: ['cjs', 'esm'], | ||
| dts: true, | ||
| clean: true, | ||
| splitting: true, | ||
| sourcemap: true, | ||
| }; | ||
|
|
||
| export default defineConfig(config); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { defineConfig } from 'vitest/config'; | ||
|
|
||
| export default defineConfig({ | ||
| test: { | ||
| include: ['tests/**/*.test.ts'], | ||
| environment: 'node', | ||
| }, | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think something like
12 / d.u32(5)would be a common pattern (wrapping just the denominator and expecting integer division). I know coding agents tend to do that.