|
| 1 | +import type { TSESTree } from '@typescript-eslint/utils'; |
| 2 | +import { enhanceRule } from '../enhanceRule.ts'; |
| 3 | +import { directiveTracking } from '../enhancers/directiveTracking.ts'; |
| 4 | +import { createRule } from '../ruleCreator.ts'; |
| 5 | + |
| 6 | +export const noUnsupportedSyntax = createRule({ |
| 7 | + name: 'no-unsupported-syntax', |
| 8 | + meta: { |
| 9 | + type: 'problem', |
| 10 | + docs: { |
| 11 | + description: `Disallow JS syntax that will not be parsed into valid WGSL.`, |
| 12 | + }, |
| 13 | + messages: { |
| 14 | + unexpected: |
| 15 | + "'{{snippet}}' will not parse into valid WGSL because it uses unsupported syntax: {{syntax}}.", |
| 16 | + }, |
| 17 | + schema: [], |
| 18 | + }, |
| 19 | + defaultOptions: [], |
| 20 | + |
| 21 | + create: enhanceRule({ directives: directiveTracking }, (context, state) => { |
| 22 | + const { directives } = state; |
| 23 | + |
| 24 | + function report(node: TSESTree.Node, syntax: string) { |
| 25 | + context.report({ |
| 26 | + node, |
| 27 | + messageId: 'unexpected', |
| 28 | + data: { snippet: context.sourceCode.getText(node), syntax }, |
| 29 | + }); |
| 30 | + } |
| 31 | + |
| 32 | + return { |
| 33 | + ArrowFunctionExpression(node) { |
| 34 | + if (directives.getDirectiveStack().at(-2)?.directives.includes('use gpu')) { |
| 35 | + report(node, 'arrow function'); |
| 36 | + } |
| 37 | + }, |
| 38 | + |
| 39 | + AssignmentExpression(node) { |
| 40 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 41 | + return; |
| 42 | + } |
| 43 | + if (unsupportedAssignmentOps.includes(node.operator)) { |
| 44 | + report(node, `assignment expression '${node.operator}'`); |
| 45 | + } |
| 46 | + }, |
| 47 | + |
| 48 | + AssignmentPattern(node) { |
| 49 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 50 | + return; |
| 51 | + } |
| 52 | + report(node, 'assignment pattern (default parameter)'); |
| 53 | + }, |
| 54 | + |
| 55 | + AwaitExpression(node) { |
| 56 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 57 | + return; |
| 58 | + } |
| 59 | + report(node, 'await expression'); |
| 60 | + }, |
| 61 | + |
| 62 | + BinaryExpression(node) { |
| 63 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 64 | + return; |
| 65 | + } |
| 66 | + if (unsupportedBinaryOps.includes(node.operator)) { |
| 67 | + report(node, `binary operator '${node.operator}'`); |
| 68 | + } |
| 69 | + }, |
| 70 | + |
| 71 | + ChainExpression(node) { |
| 72 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 73 | + return; |
| 74 | + } |
| 75 | + report(node, 'chain expression'); |
| 76 | + }, |
| 77 | + |
| 78 | + ClassDeclaration(node) { |
| 79 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 80 | + return; |
| 81 | + } |
| 82 | + report(node, 'class declaration'); |
| 83 | + }, |
| 84 | + |
| 85 | + ClassExpression(node) { |
| 86 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 87 | + return; |
| 88 | + } |
| 89 | + report(node, 'class expression'); |
| 90 | + }, |
| 91 | + |
| 92 | + DoWhileStatement(node) { |
| 93 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 94 | + return; |
| 95 | + } |
| 96 | + report(node, 'do-while loop'); |
| 97 | + }, |
| 98 | + |
| 99 | + ForInStatement(node) { |
| 100 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 101 | + return; |
| 102 | + } |
| 103 | + report(node, 'for-in loop'); |
| 104 | + }, |
| 105 | + |
| 106 | + FunctionDeclaration(node) { |
| 107 | + if (directives.getDirectiveStack().at(-2)?.directives.includes('use gpu')) { |
| 108 | + report(node, 'function declaration'); |
| 109 | + } |
| 110 | + }, |
| 111 | + |
| 112 | + FunctionExpression(node) { |
| 113 | + if (directives.getDirectiveStack().at(-2)?.directives.includes('use gpu')) { |
| 114 | + report(node, 'function expression'); |
| 115 | + } |
| 116 | + }, |
| 117 | + |
| 118 | + Literal(node) { |
| 119 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 120 | + return; |
| 121 | + } |
| 122 | + if ('regex' in node && node.regex) { |
| 123 | + report(node, 'regular expression literal'); |
| 124 | + } |
| 125 | + }, |
| 126 | + |
| 127 | + LogicalExpression(node) { |
| 128 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 129 | + return; |
| 130 | + } |
| 131 | + if (node.operator === '??') { |
| 132 | + report(node, 'nullish coalescing'); |
| 133 | + } |
| 134 | + }, |
| 135 | + |
| 136 | + NewExpression(node) { |
| 137 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 138 | + return; |
| 139 | + } |
| 140 | + report(node, `'new' expression`); |
| 141 | + }, |
| 142 | + |
| 143 | + PrivateIdentifier(node) { |
| 144 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 145 | + return; |
| 146 | + } |
| 147 | + report(node, 'private identifier'); |
| 148 | + }, |
| 149 | + |
| 150 | + Property(node) { |
| 151 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 152 | + return; |
| 153 | + } |
| 154 | + if (node.computed) { |
| 155 | + report(node, 'computed property key'); |
| 156 | + } |
| 157 | + }, |
| 158 | + |
| 159 | + SequenceExpression(node) { |
| 160 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 161 | + return; |
| 162 | + } |
| 163 | + report(node, 'sequence expression (comma operator)'); |
| 164 | + }, |
| 165 | + |
| 166 | + SpreadElement(node) { |
| 167 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 168 | + return; |
| 169 | + } |
| 170 | + report(node, 'spread element'); |
| 171 | + }, |
| 172 | + |
| 173 | + SwitchStatement(node) { |
| 174 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 175 | + return; |
| 176 | + } |
| 177 | + report(node, 'switch statement'); |
| 178 | + }, |
| 179 | + |
| 180 | + TemplateLiteral(node) { |
| 181 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 182 | + return; |
| 183 | + } |
| 184 | + report(node, 'template literal'); |
| 185 | + }, |
| 186 | + |
| 187 | + ThrowStatement(node) { |
| 188 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 189 | + return; |
| 190 | + } |
| 191 | + report(node, 'throw statement'); |
| 192 | + }, |
| 193 | + |
| 194 | + TryStatement(node) { |
| 195 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 196 | + return; |
| 197 | + } |
| 198 | + report(node, 'try-catch statement'); |
| 199 | + }, |
| 200 | + |
| 201 | + UnaryExpression(node) { |
| 202 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 203 | + return; |
| 204 | + } |
| 205 | + if (unsupportedUnaryOps.includes(node.operator)) { |
| 206 | + report(node, `unary operator '${node.operator}'`); |
| 207 | + } |
| 208 | + }, |
| 209 | + |
| 210 | + UpdateExpression(node) { |
| 211 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 212 | + return; |
| 213 | + } |
| 214 | + if (node.prefix) { |
| 215 | + report(node, 'prefix update expression'); |
| 216 | + } |
| 217 | + }, |
| 218 | + |
| 219 | + VariableDeclaration(node) { |
| 220 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 221 | + return; |
| 222 | + } |
| 223 | + if (node.kind === 'var') { |
| 224 | + report(node, `'var' declaration`); |
| 225 | + } |
| 226 | + if (node.declarations.length > 1) { |
| 227 | + report(node, 'multiple variable declarations in one statement'); |
| 228 | + } |
| 229 | + }, |
| 230 | + |
| 231 | + VariableDeclarator(node) { |
| 232 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 233 | + return; |
| 234 | + } |
| 235 | + if (node.id.type !== 'Identifier') { |
| 236 | + report(node, 'variable declaration using destructuring'); |
| 237 | + } |
| 238 | + }, |
| 239 | + |
| 240 | + YieldExpression(node) { |
| 241 | + if (!directives.getEnclosingTypegpuFunction()) { |
| 242 | + return; |
| 243 | + } |
| 244 | + report(node, 'yield expression'); |
| 245 | + }, |
| 246 | + }; |
| 247 | + }), |
| 248 | +}); |
| 249 | + |
| 250 | +const unsupportedAssignmentOps = ['&&=', '**=', '||=', '>>>=', '??=']; |
| 251 | +const unsupportedBinaryOps = ['==', '!=', '>>>', 'in', 'instanceof', '|>']; |
| 252 | +const unsupportedUnaryOps = ['+', 'typeof', 'void', 'delete']; |
0 commit comments