-
-
Notifications
You must be signed in to change notification settings - Fork 52
feat: More lint rules #2153
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
50
commits into
main
Choose a base branch
from
feat/simple-rules
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.
+165
−0
Open
feat: More lint rules #2153
Changes from all commits
Commits
Show all changes
50 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
0c879de
Merge branch 'main' into feat/type-aware-lint-plugin-poc
460e199
Report on || instead of &&
99a4b32
Merge remote-tracking branch 'origin/main' into feat/type-aware-lint-…
8312172
Fix lock
7825379
Merge remote-tracking branch 'origin/main' into feat/type-aware-lint-…
3b6ee06
Add a base for the rule
a2cc376
Rule enhancers POC
f2a0ff1
Clean up types
252665f
Update directiveTracking API
ec684e3
Add more tests and implement rule logic
770fd03
Implement directive tracking
7c91882
Self review
2664a72
Ignores returns
8e85376
Handle nested structs
6c6b9e6
Add spreadOperator rule
e08e883
Add uninitializedVariable rule
9ccb7d4
Add math rule
384e05e
Update configs
e74192b
Nits
86808d4
Update packages/eslint-plugin/src/rules/math.ts
aleksanderkatan ae072c2
Merge remote-tracking branch 'origin/main' into feat/simple-rules
cf810de
Remove the spread operator rule (delegated to a different PR)
c19c195
Post-merge fix
89db14c
Fix tet name
d819f80
Add exception for `for...of`
8ab729c
Fix tests
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 |
|---|---|---|
| @@ -1,20 +1,28 @@ | ||
| import type { TSESLint } from '@typescript-eslint/utils'; | ||
| import { integerDivision } from './rules/integerDivision.ts'; | ||
| import { unwrappedPojos } from './rules/unwrappedPojos.ts'; | ||
| import { math } from './rules/math.ts'; | ||
| import { uninitializedVariable } from './rules/uninitializedVariable.ts'; | ||
|
|
||
| export const rules = { | ||
| 'integer-division': integerDivision, | ||
| 'unwrapped-pojo': unwrappedPojos, | ||
| 'uninitialized-variable': uninitializedVariable, | ||
| math: math, | ||
| } as const; | ||
|
|
||
| type Rules = Record<`typegpu/${keyof typeof rules}`, TSESLint.FlatConfig.RuleEntry>; | ||
|
|
||
| export const recommendedRules: Rules = { | ||
| 'typegpu/integer-division': 'warn', | ||
| 'typegpu/unwrapped-pojo': 'warn', | ||
| 'typegpu/uninitialized-variable': 'warn', | ||
| 'typegpu/math': 'warn', | ||
| }; | ||
|
|
||
| export const allRules: Rules = { | ||
| 'typegpu/integer-division': 'error', | ||
| 'typegpu/unwrapped-pojo': 'error', | ||
| 'typegpu/uninitialized-variable': 'error', | ||
| 'typegpu/math': '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,42 @@ | ||
| import { createRule } from '../ruleCreator.ts'; | ||
| import { enhanceRule } from '../enhanceRule.ts'; | ||
| import { directiveTracking } from '../enhancers/directiveTracking.ts'; | ||
|
|
||
| export const math = createRule({ | ||
| name: 'math', | ||
| meta: { | ||
| type: 'suggestion', | ||
| docs: { | ||
| description: `Disallow usage of JavaScript 'Math' methods inside 'use gpu' functions; use 'std' instead.`, | ||
| }, | ||
| messages: { | ||
| math: "Using Math methods, such as '{{snippet}}', is not advised, and may not work as expected. Use 'std' instead.", | ||
| }, | ||
| schema: [], | ||
| }, | ||
| defaultOptions: [], | ||
|
|
||
| create: enhanceRule({ directives: directiveTracking }, (context, state) => { | ||
| const { directives } = state; | ||
|
|
||
| return { | ||
| CallExpression(node) { | ||
| if (!directives.insideUseGpu()) { | ||
| return; | ||
| } | ||
|
|
||
| if ( | ||
| node.callee.type === 'MemberExpression' && | ||
| node.callee.object.type === 'Identifier' && | ||
| node.callee.object.name === 'Math' | ||
| ) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'math', | ||
| data: { snippet: context.sourceCode.getText(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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import { enhanceRule } from '../enhanceRule.ts'; | ||
| import { directiveTracking } from '../enhancers/directiveTracking.ts'; | ||
| import { createRule } from '../ruleCreator.ts'; | ||
|
|
||
| export const uninitializedVariable = createRule({ | ||
| name: 'uninitialized-variable', | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: `Always assign an initial value when declaring a variable inside TypeGPU functions.`, | ||
| }, | ||
| messages: { | ||
| uninitializedVariable: "'{{snippet}}' should have an initial value.", | ||
| }, | ||
| schema: [], | ||
| }, | ||
| defaultOptions: [], | ||
|
|
||
| create: enhanceRule({ directives: directiveTracking }, (context, state) => { | ||
| const { directives } = state; | ||
|
|
||
| return { | ||
| VariableDeclarator(node) { | ||
| if (!directives.insideUseGpu()) { | ||
| return; | ||
| } | ||
| if (node.parent?.parent?.type === 'ForOfStatement') { | ||
| // one exception where we allow uninitialized variable | ||
| return; | ||
| } | ||
| if (node.init === null) { | ||
| context.report({ | ||
| node, | ||
| messageId: 'uninitializedVariable', | ||
| data: { snippet: context.sourceCode.getText(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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import { describe } from 'vitest'; | ||
| import { ruleTester } from './ruleTester.ts'; | ||
| import { math } from '../src/rules/math.ts'; | ||
|
|
||
| describe('math', () => { | ||
| ruleTester.run('math', math, { | ||
| valid: [ | ||
| 'const result = Math.sin(1);', | ||
| 'const t = std.sin(Math.PI)', | ||
| "const fn = () => { 'use gpu'; const vec = std.sin(Math.PI); }", | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "const fn = () => { 'use gpu'; const vec = Math.sin(0); }", | ||
| errors: [ | ||
| { | ||
| messageId: 'math', | ||
| data: { snippet: 'Math.sin(0)' }, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
| }); |
44 changes: 44 additions & 0 deletions
44
packages/eslint-plugin/tests/uninitializedVariable.test.ts
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,44 @@ | ||
| import { describe } from 'vitest'; | ||
aleksanderkatan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| import { ruleTester } from './ruleTester.ts'; | ||
| import { uninitializedVariable } from '../src/rules/uninitializedVariable.ts'; | ||
aleksanderkatan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| describe('uninitializedVariable', () => { | ||
| ruleTester.run('uninitializedVariable', uninitializedVariable, { | ||
| valid: [ | ||
| 'let a;', | ||
| 'let a, b;', | ||
| "const fn = () => { 'use gpu'; const vec = d.vec3f(); }", | ||
| "const fn = () => { 'use gpu'; let vec = d.vec3f(); }", | ||
| `const fn = () => { 'use gpu'; | ||
| let a = 0; | ||
| for (const foo of tgpu.unroll([1, 2, 3])) { | ||
| a += foo; | ||
| } | ||
| }`, | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "const fn = () => { 'use gpu'; let vec; }", | ||
| errors: [ | ||
| { | ||
| messageId: 'uninitializedVariable', | ||
| data: { snippet: 'vec' }, | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "const fn = () => { 'use gpu'; let a = 1, b, c = d.vec3f(), d; }", | ||
| errors: [ | ||
| { | ||
| messageId: 'uninitializedVariable', | ||
| data: { snippet: 'b' }, | ||
| }, | ||
| { | ||
| messageId: 'uninitializedVariable', | ||
| data: { snippet: 'd' }, | ||
| }, | ||
| ], | ||
| }, | ||
| ], | ||
| }); | ||
| }); | ||
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.
Uh oh!
There was an error while loading. Please reload this page.