|
| 1 | +/** |
| 2 | + * Check that all new/modified functions in the current git diff use async/await. |
| 3 | + * Fails with exit code 1 if any additions introduce callback-style functions or .then() chains. |
| 4 | + * |
| 5 | + * Usage: node scripts/check-diff-async.mjs |
| 6 | + * In CI: runs against the current PR diff (files changed vs base branch) |
| 7 | + */ |
| 8 | +import { execSync } from 'node:child_process'; |
| 9 | +import { Project, SyntaxKind } from 'ts-morph'; |
| 10 | + |
| 11 | +const CALLBACK_PARAM_PATTERN = /^(cb|callback|next|done|err)$/i; |
| 12 | + |
| 13 | +function getChangedJsFiles() { |
| 14 | + const base = process.env.GITHUB_BASE_REF |
| 15 | + ? `origin/${process.env.GITHUB_BASE_REF}` |
| 16 | + : 'HEAD'; |
| 17 | + const output = execSync(`git diff --name-only --diff-filter=ACMR ${base} -- '*.js'`, { |
| 18 | + encoding: 'utf8', |
| 19 | + }).trim(); |
| 20 | + |
| 21 | + return output ? output.split('\n').filter(f => f.endsWith('.js')) : []; |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * Get added line numbers for a file in the current diff. |
| 26 | + */ |
| 27 | +function getAddedLineNumbers(filePath) { |
| 28 | + const base = process.env.GITHUB_BASE_REF |
| 29 | + ? `origin/${process.env.GITHUB_BASE_REF}` |
| 30 | + : 'HEAD'; |
| 31 | + const diff = execSync(`git diff ${base} -- ${filePath}`, { encoding: 'utf8' }); |
| 32 | + const addedLines = new Set(); |
| 33 | + let currentLine = 0; |
| 34 | + |
| 35 | + for (const line of diff.split('\n')) { |
| 36 | + const hunkMatch = line.match(/^@@ -\d+(?:,\d+)? \+(\d+)(?:,\d+)? @@/); |
| 37 | + |
| 38 | + if (hunkMatch) { |
| 39 | + currentLine = parseInt(hunkMatch[1], 10) - 1; |
| 40 | + continue; |
| 41 | + } |
| 42 | + |
| 43 | + if (line.startsWith('+') && !line.startsWith('+++')) { |
| 44 | + currentLine++; |
| 45 | + addedLines.add(currentLine); |
| 46 | + } else if (!line.startsWith('-')) { |
| 47 | + currentLine++; |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + return addedLines; |
| 52 | +} |
| 53 | + |
| 54 | +const changedFiles = getChangedJsFiles(); |
| 55 | +if (changedFiles.length === 0) { |
| 56 | + console.log('No changed JS files to check.'); |
| 57 | + process.exit(0); |
| 58 | +} |
| 59 | + |
| 60 | +console.log(`Checking ${changedFiles.length} changed JS file(s) for async/await compliance...\n`); |
| 61 | + |
| 62 | +const project = new Project({ |
| 63 | + compilerOptions: { allowJs: true, noEmit: true }, |
| 64 | + skipAddingFilesFromTsConfig: true, |
| 65 | +}); |
| 66 | + |
| 67 | +const filesToCheck = changedFiles.filter(f => |
| 68 | + !f.startsWith('tests/') && |
| 69 | + !f.startsWith('node_modules/') && |
| 70 | + ( |
| 71 | + f.startsWith('lib/') || |
| 72 | + f.startsWith('bin/') || |
| 73 | + !f.includes('/') |
| 74 | + ) |
| 75 | +); |
| 76 | +if (filesToCheck.length === 0) { |
| 77 | + console.log('No source JS files in diff (tests and node_modules excluded).'); |
| 78 | + process.exit(0); |
| 79 | +} |
| 80 | + |
| 81 | +project.addSourceFilesAtPaths(filesToCheck); |
| 82 | + |
| 83 | +const violations = []; |
| 84 | + |
| 85 | +for (const sourceFile of project.getSourceFiles()) { |
| 86 | + const filePath = sourceFile.getFilePath().replace(process.cwd() + '/', ''); |
| 87 | + const addedLines = getAddedLineNumbers(filePath); |
| 88 | + |
| 89 | + if (addedLines.size === 0) continue; |
| 90 | + |
| 91 | + const functions = [ |
| 92 | + ...sourceFile.getDescendantsOfKind(SyntaxKind.FunctionDeclaration), |
| 93 | + ...sourceFile.getDescendantsOfKind(SyntaxKind.FunctionExpression), |
| 94 | + ...sourceFile.getDescendantsOfKind(SyntaxKind.ArrowFunction), |
| 95 | + ...sourceFile.getDescendantsOfKind(SyntaxKind.MethodDeclaration), |
| 96 | + ]; |
| 97 | + |
| 98 | + for (const fn of functions) { |
| 99 | + if (fn.isAsync()) continue; |
| 100 | + |
| 101 | + const startLine = fn.getStartLineNumber(); |
| 102 | + if (!addedLines.has(startLine)) continue; |
| 103 | + |
| 104 | + const params = fn.getParameters(); |
| 105 | + const lastParam = params[params.length - 1]; |
| 106 | + if (lastParam && CALLBACK_PARAM_PATTERN.test(lastParam.getName())) { |
| 107 | + violations.push({ |
| 108 | + file: filePath, |
| 109 | + line: startLine, |
| 110 | + type: 'callback', |
| 111 | + detail: `function has callback parameter '${lastParam.getName()}'`, |
| 112 | + }); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + const propertyAccesses = sourceFile.getDescendantsOfKind(SyntaxKind.PropertyAccessExpression); |
| 117 | + for (const access of propertyAccesses) { |
| 118 | + if (access.getName() !== 'then') continue; |
| 119 | + const line = access.getStartLineNumber(); |
| 120 | + if (addedLines.has(line)) { |
| 121 | + violations.push({ |
| 122 | + file: filePath, |
| 123 | + line, |
| 124 | + type: 'then-chain', |
| 125 | + detail: 'use await instead of .then()', |
| 126 | + }); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +if (violations.length === 0) { |
| 132 | + console.log('✓ All new code in the diff uses async/await.'); |
| 133 | + process.exit(0); |
| 134 | +} |
| 135 | + |
| 136 | +console.error(`✗ Found ${violations.length} async/await violation(s) in the diff:\n`); |
| 137 | +for (const v of violations) { |
| 138 | + console.error(` ${v.file}:${v.line} [${v.type}] ${v.detail}`); |
| 139 | +} |
| 140 | +console.error('\nNew code must use async/await instead of callbacks or .then() chains.'); |
| 141 | +console.error('See the async/await migration guide in CONTRIBUTING.md for help.'); |
| 142 | +process.exit(1); |
0 commit comments