Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
e253963
Implement rule
Mar 19, 2026
2bbe811
Update messages and reorder
Mar 19, 2026
09f1ff8
Clean up
Mar 19, 2026
226a7aa
Report assignment pattern
Mar 19, 2026
245154a
Add a rule for private identifiers
Mar 19, 2026
d92702f
Add missing tests
Mar 19, 2026
096069e
Reorder tests
Mar 19, 2026
bd32605
lint
Mar 19, 2026
2f38104
Merge branch 'main' into feat/lint-rule-for-unsupported-js
aleksanderkatan Mar 19, 2026
c40da2b
Potential fix for pull request finding
aleksanderkatan Mar 19, 2026
c55d427
Potential fix for pull request finding
aleksanderkatan Mar 19, 2026
e0c6bfb
Merge remote-tracking branch 'origin/main' into feat/lint-rule-for-un…
aleksanderkatan Apr 1, 2026
471e7c2
Merge fixes
aleksanderkatan Apr 1, 2026
eba340b
Report eqeq
aleksanderkatan Apr 1, 2026
b2a1d91
Move tests
aleksanderkatan Apr 1, 2026
ebe784e
Update directiveTracking
aleksanderkatan Apr 1, 2026
46867ea
Report functions
aleksanderkatan Apr 1, 2026
d595ca3
Omit reporting object methods
aleksanderkatan Apr 2, 2026
97fbfb4
Update readme
aleksanderkatan Apr 2, 2026
4247655
Report chain expression
aleksanderkatan Apr 2, 2026
d268b4b
Report ??
aleksanderkatan Apr 2, 2026
51f780f
Report unsupported unary operators
aleksanderkatan Apr 2, 2026
47cb18c
Update binary and unary operators
aleksanderkatan Apr 2, 2026
d264738
Better types
aleksanderkatan Apr 2, 2026
d78b9dd
Review fixes
aleksanderkatan Apr 2, 2026
a319001
Merge branch 'main' into feat/lint-rule-for-unsupported-js
aleksanderkatan Apr 2, 2026
e2a777b
Change from warn to error
aleksanderkatan Apr 2, 2026
415579a
Merge remote-tracking branch 'origin/main' into feat/lint-rule-for-un…
aleksanderkatan Apr 14, 2026
31b2905
Less getDirectiveStack calls
aleksanderkatan Apr 14, 2026
bda87f2
Review fixes
aleksanderkatan Apr 14, 2026
0e39bd3
Report unsupported assignment expressions
aleksanderkatan Apr 14, 2026
483b984
Merge branch 'main' into feat/lint-rule-for-unsupported-js
aleksanderkatan Apr 14, 2026
8760085
Review fixes
aleksanderkatan Apr 14, 2026
3c5f586
Merge branch 'main' into feat/lint-rule-for-unsupported-js
aleksanderkatan Apr 15, 2026
788c52c
Merge branch 'main' into feat/lint-rule-for-unsupported-js
aleksanderkatan Apr 20, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/eslint-plugin/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export default defineConfig([
| [no-invalid-assignment](docs/rules/no-invalid-assignment.md) | Disallow assignments that will generate invalid WGSL | ⭐ | |
| [no-math](docs/rules/no-math.md) | Disallow usage of JavaScript 'Math' methods inside 'use gpu' functions | | ⭐ |
| [no-uninitialized-variables](docs/rules/no-uninitialized-variables.md) | Disallow variable declarations without initializers inside 'use gpu' functions | ⭐ | |
| [no-unsupported-syntax](docs/rules/no-unsupported-syntax.md) | Disallow JS syntax that will not be parsed into valid WGSL. | ⭐ | |
| [no-unwrapped-objects](docs/rules/no-unwrapped-objects.md) | Disallow unwrapped Plain Old JavaScript Objects inside 'use gpu' functions (except returns) | ⭐ | |

<!-- end auto-generated rules list -->
65 changes: 65 additions & 0 deletions packages/eslint-plugin/docs/rules/no-unsupported-syntax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# typegpu/no-unsupported-syntax

📝 Disallow JS syntax that will not be parsed into valid WGSL.

🚨 This rule is enabled in the ⭐ `recommended` config.

<!-- end auto-generated rule header -->

## Rule details

Examples of **incorrect** code for this rule:

```ts
const fn = () => {
'use gpu';
const helper = (n) => 2 * n; // ArrowFunctionExpression
return helper(1);
}
```
```ts
const fn = () => {
'use gpu';
const myStruct = Struct({prop: 1});
const otherStruct = Struct({...myStruct}); // SpreadElement
return otherStruct;
}
```
```ts
const fn = () => {
'use gpu';
throw new Error(); // ThrowStatement
}
```

Examples of **correct** code for this rule:

```ts
const fn = (a) => {
'use gpu';
let counter = 0;
let i = a;
while (i) {
if (i % 2 === 1) {
counter += 1;
}
i >>= 1;
}
return otherFn(counter);
}
```
```ts
const fn = () => {
'use gpu';
let a = 0;
const arr = [1, 2, 3];
for (const item of arr) {
a += item;
}
return a;
}
```

> [!NOTE]
> Note that it is possible that TypeGPU starts/stops supporting JS syntax from version to version.
> Make sure that the minor versions of `typegpu` and `eslint-plugin-typegpu` match.
4 changes: 4 additions & 0 deletions packages/eslint-plugin/src/configs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { noUnwrappedObjects } from './rules/noUnwrappedObjects.ts';
import { noMath } from './rules/noMath.ts';
import { noUninitializedVariables } from './rules/noUninitializedVariables.ts';
import { noInvalidAssignment } from './rules/noInvalidAssignment.ts';
import { noUnsupportedSyntax } from './rules/noUnsupportedSyntax.ts';

export const rules = {
'no-integer-division': noIntegerDivision,
'no-unwrapped-objects': noUnwrappedObjects,
'no-uninitialized-variables': noUninitializedVariables,
'no-math': noMath,
'no-invalid-assignment': noInvalidAssignment,
'no-unsupported-syntax': noUnsupportedSyntax,
} as const;

type Rules = Record<`typegpu/${keyof typeof rules}`, TSESLint.FlatConfig.RuleEntry>;
Expand All @@ -21,6 +23,7 @@ export const recommendedRules = {
'typegpu/no-uninitialized-variables': 'error',
'typegpu/no-math': 'warn',
'typegpu/no-invalid-assignment': 'error',
'typegpu/no-unsupported-syntax': 'error',
} as const satisfies Rules;

export const allRules = {
Expand All @@ -29,4 +32,5 @@ export const allRules = {
'typegpu/no-uninitialized-variables': 'error',
'typegpu/no-math': 'error',
'typegpu/no-invalid-assignment': 'error',
'typegpu/no-unsupported-syntax': 'error',
} as const satisfies Rules;
6 changes: 5 additions & 1 deletion packages/eslint-plugin/src/enhancers/directiveTracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type FunctionNode =

export type DirectiveData = {
getEnclosingTypegpuFunction: () => FunctionNode | undefined;
getDirectiveStack: () => readonly { node: FunctionNode; directives: string[] }[];
};

/**
Expand Down Expand Up @@ -48,13 +49,16 @@ export const directiveTracking: RuleEnhancer<DirectiveData> = () => {
return {
visitors,
state: {
getEnclosingTypegpuFunction: () => {
getEnclosingTypegpuFunction() {
const current = stack.at(-1);
if (current && current.directives.includes('use gpu')) {
return current.node;
}
return undefined;
},
getDirectiveStack() {
return stack;
},
Comment thread
aleksanderkatan marked this conversation as resolved.
},
};
};
Expand Down
252 changes: 252 additions & 0 deletions packages/eslint-plugin/src/rules/noUnsupportedSyntax.ts
Comment thread
aleksanderkatan marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,252 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { enhanceRule } from '../enhanceRule.ts';
import { directiveTracking } from '../enhancers/directiveTracking.ts';
import { createRule } from '../ruleCreator.ts';

export const noUnsupportedSyntax = createRule({
name: 'no-unsupported-syntax',
meta: {
type: 'problem',
docs: {
description: `Disallow JS syntax that will not be parsed into valid WGSL.`,
},
messages: {
unexpected:
"'{{snippet}}' will not parse into valid WGSL because it uses unsupported syntax: {{syntax}}.",
},
schema: [],
},
defaultOptions: [],

create: enhanceRule({ directives: directiveTracking }, (context, state) => {
const { directives } = state;

function report(node: TSESTree.Node, syntax: string) {
context.report({
node,
messageId: 'unexpected',
data: { snippet: context.sourceCode.getText(node), syntax },
});
}

return {
ArrowFunctionExpression(node) {
if (directives.getDirectiveStack().at(-2)?.directives.includes('use gpu')) {
report(node, 'arrow function');
}
},

AssignmentExpression(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
if (unsupportedAssignmentOps.includes(node.operator)) {
report(node, `assignment expression '${node.operator}'`);
}
},

AssignmentPattern(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, 'assignment pattern (default parameter)');
},

AwaitExpression(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, 'await expression');
},

BinaryExpression(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
if (unsupportedBinaryOps.includes(node.operator)) {
report(node, `binary operator '${node.operator}'`);
}
},

ChainExpression(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, 'chain expression');
},

ClassDeclaration(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, 'class declaration');
},

ClassExpression(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, 'class expression');
},

DoWhileStatement(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, 'do-while loop');
},

ForInStatement(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, 'for-in loop');
},

FunctionDeclaration(node) {
if (directives.getDirectiveStack().at(-2)?.directives.includes('use gpu')) {
report(node, 'function declaration');
}
},

FunctionExpression(node) {
if (directives.getDirectiveStack().at(-2)?.directives.includes('use gpu')) {
report(node, 'function expression');
}
},

Literal(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
if ('regex' in node && node.regex) {
report(node, 'regular expression literal');
}
},

LogicalExpression(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
if (node.operator === '??') {
report(node, 'nullish coalescing');
}
},

NewExpression(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, `'new' expression`);
},

PrivateIdentifier(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, 'private identifier');
},

Property(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
if (node.computed) {
report(node, 'computed property key');
}
},

SequenceExpression(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, 'sequence expression (comma operator)');
},

SpreadElement(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, 'spread element');
},

SwitchStatement(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, 'switch statement');
},

TemplateLiteral(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, 'template literal');
},

ThrowStatement(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, 'throw statement');
},

TryStatement(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, 'try-catch statement');
},

UnaryExpression(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
if (unsupportedUnaryOps.includes(node.operator)) {
report(node, `unary operator '${node.operator}'`);
}
},

UpdateExpression(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
if (node.prefix) {
report(node, 'prefix update expression');
}
},

VariableDeclaration(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
if (node.kind === 'var') {
report(node, `'var' declaration`);
}
if (node.declarations.length > 1) {
report(node, 'multiple variable declarations in one statement');
}
},

VariableDeclarator(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
if (node.id.type !== 'Identifier') {
report(node, 'variable declaration using destructuring');
}
},

YieldExpression(node) {
if (!directives.getEnclosingTypegpuFunction()) {
return;
}
report(node, 'yield expression');
},
};
}),
});

const unsupportedAssignmentOps = ['&&=', '**=', '||=', '>>>=', '??='];
const unsupportedBinaryOps = ['==', '!=', '>>>', 'in', 'instanceof', '|>'];
const unsupportedUnaryOps = ['+', 'typeof', 'void', 'delete'];
Loading
Loading