|
| 1 | +const ITERATION_METHODS = new Set([ |
| 2 | + 'every', |
| 3 | + 'filter', |
| 4 | + 'find', |
| 5 | + 'findIndex', |
| 6 | + 'flatMap', |
| 7 | + 'forEach', |
| 8 | + 'map', |
| 9 | + 'reduce', |
| 10 | + 'reduceRight', |
| 11 | + 'some' |
| 12 | +]); |
| 13 | +const LOOP_NODES = new Set(['DoWhileStatement', 'ForInStatement', 'ForOfStatement', 'ForStatement', 'WhileStatement']); |
| 14 | + |
| 15 | +/** @type {import('eslint').Rule.RuleModule} */ |
| 16 | +export default { |
| 17 | + meta: { |
| 18 | + type: 'suggestion', |
| 19 | + name: 'no-gpu-upload-in-loop', |
| 20 | + docs: { |
| 21 | + description: 'Flags direct WebGPU queue uploads repeated by loops or collection callbacks.', |
| 22 | + category: 'Performance', |
| 23 | + recommended: false |
| 24 | + }, |
| 25 | + schema: [], |
| 26 | + messages: { |
| 27 | + 'repeated-upload': |
| 28 | + '`{{method}}` runs inside repeated iteration. Batch uploads or merge dirty ranges before crossing the WebGPU boundary.' |
| 29 | + } |
| 30 | + }, |
| 31 | + create(context) { |
| 32 | + return { |
| 33 | + CallExpression(node) { |
| 34 | + const method = getUploadMethod(node.callee); |
| 35 | + if (method && isRepeated(node)) { |
| 36 | + context.report({ node, messageId: 'repeated-upload', data: { method } }); |
| 37 | + } |
| 38 | + } |
| 39 | + }; |
| 40 | + } |
| 41 | +}; |
| 42 | + |
| 43 | +function getUploadMethod(callee) { |
| 44 | + if (callee.type !== 'MemberExpression' || !isGpuQueue(callee.object)) return null; |
| 45 | + const name = callee.computed |
| 46 | + ? callee.property.type === 'Literal' && typeof callee.property.value === 'string' |
| 47 | + ? callee.property.value |
| 48 | + : undefined |
| 49 | + : callee.property.type === 'Identifier' |
| 50 | + ? callee.property.name |
| 51 | + : undefined; |
| 52 | + return name === 'writeBuffer' || name === 'writeTexture' ? name : null; |
| 53 | +} |
| 54 | + |
| 55 | +function isGpuQueue(node) { |
| 56 | + if (node.type === 'Identifier') return /queue$/iu.test(node.name); |
| 57 | + if (node.type !== 'MemberExpression') return false; |
| 58 | + if (!node.computed && node.property.type === 'Identifier') return /queue$/iu.test(node.property.name); |
| 59 | + return node.property.type === 'Literal' && typeof node.property.value === 'string' |
| 60 | + ? /queue$/iu.test(node.property.value) |
| 61 | + : false; |
| 62 | +} |
| 63 | + |
| 64 | +function isRepeated(node) { |
| 65 | + let current = node.parent; |
| 66 | + while (current) { |
| 67 | + if (LOOP_NODES.has(current.type)) return true; |
| 68 | + if (isFunction(current)) return isIterationCallback(current); |
| 69 | + current = current.parent; |
| 70 | + } |
| 71 | + return false; |
| 72 | +} |
| 73 | + |
| 74 | +function isFunction(node) { |
| 75 | + return ( |
| 76 | + node.type === 'ArrowFunctionExpression' || node.type === 'FunctionDeclaration' || node.type === 'FunctionExpression' |
| 77 | + ); |
| 78 | +} |
| 79 | + |
| 80 | +function isIterationCallback(node) { |
| 81 | + const call = node.parent; |
| 82 | + if (call?.type !== 'CallExpression' || !call.arguments.includes(node)) return false; |
| 83 | + const callee = call.callee; |
| 84 | + return ( |
| 85 | + callee.type === 'MemberExpression' && |
| 86 | + !callee.computed && |
| 87 | + callee.property.type === 'Identifier' && |
| 88 | + ITERATION_METHODS.has(callee.property.name) |
| 89 | + ); |
| 90 | +} |
0 commit comments