-
-
Notifications
You must be signed in to change notification settings - Fork 51
feat: Lint rule for invalid assignment #2235
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
Draft
aleksanderkatan
wants to merge
13
commits into
feat/lint-unwrapped-pojos
Choose a base branch
from
feat/lint-rule-for-invalid-assignment
base: feat/lint-unwrapped-pojos
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.
+318
−9
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
6262d7a
Add a base implementation of parameter assign
77f3524
Add rule to config
49a69b6
Add comments, fix types
3027d64
Allow assignments with dollar
c53f113
Add array access tests
bddd512
Handle update expression
23a53b1
Update directiveTracking to hold function node
397b9cb
Handle JS assignment
4085f20
Reduce code duplication
c674e35
Handle parameters better
ee168af
Cleanup & handle global assignments
e100b60
Cleanup tests
eaec416
Adjust tests in typegpu repo
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
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
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,99 @@ | ||
| import { ASTUtils, type TSESTree } from '@typescript-eslint/utils'; | ||
| import { createRule } from '../ruleCreator.ts'; | ||
| import { enhanceRule } from '../enhanceRule.ts'; | ||
| import { directiveTracking } from '../enhancers/directiveTracking.ts'; | ||
| import type { RuleContext } from '@typescript-eslint/utils/ts-eslint'; | ||
|
|
||
| export const invalidAssignment = createRule({ | ||
| name: 'invalid-assignment', | ||
| meta: { | ||
| type: 'problem', | ||
| docs: { | ||
| description: `Avoid assignments that will not generate valid WGSL.`, | ||
| }, | ||
| messages: { | ||
| parameterAssignment: | ||
| "Cannot assign to '{{snippet}}' since WGSL parameters are immutable. If you're using d.ref, please either use '.$' or disable this rule.", | ||
| jsAssignment: | ||
| "Cannot assign to '{{snippet}}' since it is a JS variable defined outside of the current TypeGPU function's scope. Use buffers, workgroup variables or local variables instead.", | ||
| }, | ||
| schema: [], | ||
| }, | ||
| defaultOptions: [], | ||
|
|
||
| create: enhanceRule({ directives: directiveTracking }, (context, state) => { | ||
| const { directives } = state; | ||
|
|
||
| return { | ||
| UpdateExpression(node) { | ||
| const enclosingFn = directives.getEnclosingTypegpuFunction(); | ||
| validateAssignment(context, node, enclosingFn, node.argument); | ||
| }, | ||
|
|
||
| AssignmentExpression(node) { | ||
| const enclosingFn = directives.getEnclosingTypegpuFunction(); | ||
| validateAssignment(context, node, enclosingFn, node.left); | ||
| }, | ||
| }; | ||
| }), | ||
| }); | ||
|
|
||
| function validateAssignment( | ||
| context: Readonly<RuleContext<'parameterAssignment' | 'jsAssignment', []>>, | ||
| node: TSESTree.Node, | ||
| enclosingFn: TSESTree.Node | undefined, | ||
| leftNode: TSESTree.Node, | ||
| ) { | ||
| if (!enclosingFn) { | ||
| return; | ||
| } | ||
|
|
||
| // follow the member expression chain | ||
| let assignee = leftNode; | ||
| while (assignee.type === 'MemberExpression') { | ||
| if ( | ||
| assignee.property.type === 'Identifier' && | ||
| assignee.property.name === '$' | ||
| ) { | ||
| // a dollar was used so we assume this assignment is fine | ||
| return; | ||
| } | ||
| assignee = assignee.object; | ||
| } | ||
| if (assignee.type !== 'Identifier') { | ||
| return; | ||
| } | ||
|
|
||
| // look for a scope that defines the variable | ||
| const variable = ASTUtils.findVariable( | ||
| context.sourceCode.getScope(assignee), | ||
| assignee, | ||
| ); | ||
| // defs is an array because there may be multiple definitions with `var` | ||
| const def = variable?.defs[0]; | ||
|
|
||
| // check if variable is global or was defined outside of current function by checking ranges | ||
| // NOTE: if the variable is an outer function parameter, then the enclosingFn range will be encompassed by node range | ||
| if ( | ||
| !def || | ||
| def && ( | ||
| def.node.range[0] < enclosingFn.range[0] || | ||
| enclosingFn.range[1] < def.node.range[1] | ||
| ) | ||
| ) { | ||
| context.report({ | ||
| messageId: 'jsAssignment', | ||
| node, | ||
| data: { snippet: context.sourceCode.getText(leftNode) }, | ||
| }); | ||
| return; | ||
| } | ||
|
|
||
| if (def.type === 'Parameter') { | ||
| context.report({ | ||
| messageId: 'parameterAssignment', | ||
| node, | ||
| data: { snippet: context.sourceCode.getText(leftNode) }, | ||
| }); | ||
| } | ||
| } | ||
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
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,190 @@ | ||
| import { describe } from 'vitest'; | ||
| import { ruleTester } from './ruleTester.ts'; | ||
| import { invalidAssignment } from '../src/rules/invalidAssignment.ts'; | ||
|
|
||
| describe('invalidAssignment', () => { | ||
| ruleTester.run('parameterAssignment', invalidAssignment, { | ||
| valid: [ | ||
| // not inside 'use gpu' function | ||
| 'const fn = (a) => { a = {}; }', | ||
| 'const fn = (a) => { a.prop = 1; }', | ||
| "const fn = (a) => { a['prop'] = 1; }", | ||
| 'const fn = (a) => { a[0] = 1; }', | ||
|
|
||
| // not using parameter | ||
| "const fn = (a) => { 'use gpu'; let b = 0; b = 1; }", | ||
| "const fn = (a) => { 'use gpu'; { let a = 1; a = 2; } }", | ||
|
|
||
| // correctly accessed | ||
| "const fn = (a) => { 'use gpu'; a.$ = 1 }", | ||
| "const fn = (a) => { 'use gpu'; a.$++; }", | ||
| "const fn = (a) => { 'use gpu'; a.$ += 1; }", | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "const fn = (a) => { 'use gpu'; a = 1; }", | ||
| errors: [{ messageId: 'parameterAssignment', data: { snippet: 'a' } }], | ||
| }, | ||
| { | ||
| code: "let a; const fn = (a) => { 'use gpu'; a = 1; }", | ||
| errors: [{ messageId: 'parameterAssignment', data: { snippet: 'a' } }], | ||
| }, | ||
| { | ||
| code: "const fn = (a) => { 'use gpu'; a.prop = 1; }", | ||
| errors: [{ | ||
| messageId: 'parameterAssignment', | ||
| data: { snippet: 'a.prop' }, | ||
| }], | ||
| }, | ||
| { | ||
| code: "const fn = (a) => { 'use gpu'; a['prop'] = 1; }", | ||
| errors: [{ | ||
| messageId: 'parameterAssignment', | ||
| data: { snippet: "a['prop']" }, | ||
| }], | ||
| }, | ||
| { | ||
| code: "const fn = (a) => { 'use gpu'; a[0] = 1; }", | ||
| errors: [{ | ||
| messageId: 'parameterAssignment', | ||
| data: { snippet: 'a[0]' }, | ||
| }], | ||
| }, | ||
| { | ||
| code: "const fn = (a) => { 'use gpu'; a++; }", | ||
| errors: [{ messageId: 'parameterAssignment', data: { snippet: 'a' } }], | ||
| }, | ||
| { | ||
| code: "const fn = (a) => { 'use gpu'; a += 1; }", | ||
| errors: [{ messageId: 'parameterAssignment', data: { snippet: 'a' } }], | ||
| }, | ||
| { | ||
| code: "const fn = (a) => { 'use gpu'; a.prop1.prop2 = 1; }", | ||
| errors: [{ | ||
| messageId: 'parameterAssignment', | ||
| data: { snippet: 'a.prop1.prop2' }, | ||
| }], | ||
| }, | ||
| { | ||
| code: "const fn = (a) => { 'use gpu'; if (true) { a = 1; } }", | ||
| errors: [{ messageId: 'parameterAssignment', data: { snippet: 'a' } }], | ||
| }, | ||
| { | ||
| code: "const fn = (a) => { 'use gpu'; a = 1; { let a; } }", | ||
| errors: [{ messageId: 'parameterAssignment', data: { snippet: 'a' } }], | ||
| }, | ||
| { | ||
| code: "const fn = (a, b) => { 'use gpu'; a = 1; b = 2; }", | ||
| errors: [ | ||
| { messageId: 'parameterAssignment', data: { snippet: 'a' } }, | ||
| { messageId: 'parameterAssignment', data: { snippet: 'b' } }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "const fn = (a) => { 'use gpu'; a.$prop = 1; }", | ||
| errors: [{ | ||
| messageId: 'parameterAssignment', | ||
| data: { snippet: 'a.$prop' }, | ||
| }], | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| ruleTester.run('jsAssignment', invalidAssignment, { | ||
| valid: [ | ||
| // not inside 'use gpu' function | ||
| 'let a; const fn = () => { a = 1 }', | ||
| 'const outer = (a) => { const fn = () => { a = 1 } }', | ||
| 'const vars = []; const fn = () => { vars[0] = 1 }', | ||
|
|
||
| // correctly accessed | ||
| "const buffer = {}; const fn = () => { 'use gpu'; buffer.$ = 1 }", | ||
| "const outer = (buffer) => { const fn = () => { 'use gpu'; buffer.$ = 1 } }", | ||
| "const buffers = []; const fn = () => { 'use gpu'; buffers[0].$ = 1 }", | ||
| ], | ||
| invalid: [ | ||
| { | ||
| code: "let a; const fn = () => { 'use gpu'; a = 1 }", | ||
| errors: [{ messageId: 'jsAssignment', data: { snippet: 'a' } }], | ||
| }, | ||
| { | ||
| code: "var a; const fn = () => { 'use gpu'; a = 1 }", | ||
| errors: [{ messageId: 'jsAssignment', data: { snippet: 'a' } }], | ||
| }, | ||
| { | ||
| code: "const outer = (a) => { const fn = () => { 'use gpu'; a = 1 } }", | ||
| errors: [{ messageId: 'jsAssignment', data: { snippet: 'a' } }], | ||
| }, | ||
| { | ||
| code: "const a = {}; const fn = () => { 'use gpu'; a.prop = 1; }", | ||
| errors: [{ | ||
| messageId: 'jsAssignment', | ||
| data: { snippet: 'a.prop' }, | ||
| }], | ||
| }, | ||
| { | ||
| code: "const a = {}; const fn = () => { 'use gpu'; a['prop'] = 1; }", | ||
| errors: [{ | ||
| messageId: 'jsAssignment', | ||
| data: { snippet: "a['prop']" }, | ||
| }], | ||
| }, | ||
| { | ||
| code: "const a = []; const fn = () => { 'use gpu'; a[0] = 1; }", | ||
| errors: [{ | ||
| messageId: 'jsAssignment', | ||
| data: { snippet: 'a[0]' }, | ||
| }], | ||
| }, | ||
| { | ||
| code: "const vars = []; const fn = () => { 'use gpu'; vars[0] = 1 }", | ||
| errors: [{ messageId: 'jsAssignment', data: { snippet: 'vars[0]' } }], | ||
| }, | ||
| { | ||
| code: "const fn = () => { 'use gpu'; a += 1; }; let a;", | ||
| errors: [{ messageId: 'jsAssignment', data: { snippet: 'a' } }], | ||
| }, | ||
| { | ||
| code: "let a; const fn = () => { 'use gpu'; a++; }", | ||
| errors: [{ messageId: 'jsAssignment', data: { snippet: 'a' } }], | ||
| }, | ||
| { | ||
| code: "let a; const fn = () => { 'use gpu'; a += 1; }", | ||
| errors: [{ messageId: 'jsAssignment', data: { snippet: 'a' } }], | ||
| }, | ||
| { | ||
| code: | ||
| "const a = {}; const fn = () => { 'use gpu'; a.prop1.prop2 = 1; }", | ||
| errors: [{ | ||
| messageId: 'jsAssignment', | ||
| data: { snippet: 'a.prop1.prop2' }, | ||
| }], | ||
| }, | ||
| { | ||
| code: "let a; const fn = () => { 'use gpu'; if (true) { a = 1; } }", | ||
| errors: [{ messageId: 'jsAssignment', data: { snippet: 'a' } }], | ||
| }, | ||
| { | ||
| code: "let a, b; const fn = () => { 'use gpu'; a = 1; b = 2; }", | ||
| errors: [ | ||
| { messageId: 'jsAssignment', data: { snippet: 'a' } }, | ||
| { messageId: 'jsAssignment', data: { snippet: 'b' } }, | ||
| ], | ||
| }, | ||
| { | ||
| code: "const a = {}; const fn = () => { 'use gpu'; a.$prop = 1; }", | ||
| errors: [{ | ||
| messageId: 'jsAssignment', | ||
| data: { snippet: 'a.$prop' }, | ||
| }], | ||
| }, | ||
| { | ||
| code: "const fn = () => { 'use gpu'; globalThis.prop = 1 }", | ||
| errors: [{ | ||
| messageId: 'jsAssignment', | ||
| data: { snippet: 'globalThis.prop' }, | ||
| }], | ||
| }, | ||
| ], | ||
| }); | ||
| }); |
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.
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.