diff --git a/.eslintrc.js b/.eslintrc.js index 7600d7e6..6074b37d 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -3,5 +3,8 @@ module.exports = { extends: ['scratch', 'scratch/node'], env: { es6: true + }, + rules: { + 'require-jsdoc': 'off' } }; diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index a739c635..f2643d17 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -11,6 +11,7 @@ jobs: - uses: actions/checkout@v4 with: persist-credentials: false + fetch-depth: 0 - name: Install Node.js uses: actions/setup-node@v4 with: @@ -18,5 +19,7 @@ jobs: cache: npm - run: npm ci - run: npm run lint + - run: node scripts/check-conventions.mjs --base=origin/${{ github.base_ref }} + if: github.event_name == 'pull_request' - run: npm run build - run: npm run test diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 00000000..505bac95 --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,24 @@ +# AGENTS.md + +scratch-audio plays MistWarp project sounds: decoding (including ADPCM), the sound players, effects such as pitch, pan and volume, and microphone loudness. It is a fork of TurboWarp's audio engine, written in CommonJS JavaScript, and scratch-gui pins it to a commit. + +## Where code goes + +- `src/AudioEngine.js` is the entry point the VM uses. `SoundPlayer.js` plays one sound, `SoundBank.js` holds a target's sounds, and `effects/` holds the effect chain. `Loudness.js` reads the microphone. + +## Things that look safe to change but are not + +- Sound timing and effects must match Scratch. The scratch-parity project (git.rotur.dev/Mist/scratch-parity) includes a sound timing regression, and its results must not change. +- Browsers suspend an `AudioContext` until the page gets a user gesture. Resume it, don't assume it is running. +- Keep the `AudioEngine`, `SoundPlayer` and effect method names stable. scratch-vm calls them directly. + +## Checks + +`npm run lint`, `npm run tap` (this tap version has no `terse` reporter), `npm run build`, and `npm run check:conventions`, which rejects code comments and em dashes in the lines you add. + +## Working here + +- The default branch is `develop`. This repo is a fork, so open PRs with `gh pr create -R MistWarp/scratch-audio --base develop`. +- No code comments: name things so the code explains itself. No em dashes anywhere. +- Never add `Co-Authored-By` or any AI or tool attribution to commits or PRs. +- READMEs are overviews. Documentation goes in MistWarp/docs (https://mistwarp.org/docs/). diff --git a/package-lock.json b/package-lock.json index 4c9f381b..94da56ad 100644 --- a/package-lock.json +++ b/package-lock.json @@ -21,6 +21,7 @@ "eslint-config-scratch": "9.0.3", "json": "9.0.6", "tap": "^16.3.10", + "typescript": "~6.0.3", "web-audio-test-api": "0.5.2", "webpack": "4.47.0", "webpack-cli": "3.3.12" @@ -10880,6 +10881,20 @@ "is-typedarray": "^1.0.0" } }, + "node_modules/typescript": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-6.0.3.tgz", + "integrity": "sha512-y2TvuxSZPDyQakkFRPZHKFm+KKVqIisdg9/CZwm9ftvKXLP8NRWj38/ODjNbr43SsoXqNuAisEf1GdCxqWcdBw==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, "node_modules/unbox-primitive": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/unbox-primitive/-/unbox-primitive-1.0.2.tgz", diff --git a/package.json b/package.json index 3296230b..768a59f3 100644 --- a/package.json +++ b/package.json @@ -10,7 +10,8 @@ "build": "webpack --bail", "watch": "webpack --watch", "lint": "eslint .", - "tap": "tap test/effects/*.js test/*.js" + "tap": "tap test/effects/*.js test/*.js", + "check:conventions": "node scripts/check-conventions.mjs" }, "repository": { "type": "git", @@ -40,6 +41,7 @@ "eslint-config-scratch": "9.0.3", "json": "9.0.6", "tap": "^16.3.10", + "typescript": "~6.0.3", "web-audio-test-api": "0.5.2", "webpack": "4.47.0", "webpack-cli": "3.3.12" diff --git a/scripts/check-conventions.mjs b/scripts/check-conventions.mjs new file mode 100644 index 00000000..16e59e6f --- /dev/null +++ b/scripts/check-conventions.mjs @@ -0,0 +1,129 @@ +import {execFileSync} from 'node:child_process'; +import fs from 'node:fs'; +import ts from 'typescript'; + +const EM_DASH = '\u2014'; +const SOURCE_FILE = /\.(js|jsx|mjs|cjs|ts|tsx)$/; +const TEXT_FILE = /\.(js|jsx|mjs|cjs|ts|tsx|json|md|css|html|yml|yaml|txt)$/; +const SKIPPED_PATHS = [ + /^dist\//, + /(^|\/)package-lock\.json$/ +]; +const DIRECTIVE = /^(\/\/|\/\*)\s*(eslint[\s-]|global\s|@ts-|istanbul\s|prettier-ignore|\/\s* option.startsWith('--base=')) || '--base=origin/develop').slice('--base='.length); + +const git = (...args) => execFileSync('git', args, {encoding: 'utf8', maxBuffer: 1 << 30}); + +const diffArgs = staged ? + ['diff', '--cached'] : + ['diff', git('merge-base', base, 'HEAD').trim()]; + +const parseDiff = text => { + const files = new Map(); + const removed = new Set(); + let current = null; + let line = 0; + for (const row of text.split('\n')) { + if (row.startsWith('+++ ')) { + const path = row.slice(4).replace(/^b\//, ''); + current = path === '/dev/null' ? null : path; + if (current && !files.has(current)) files.set(current, new Map()); + } else if (row.startsWith('@@')) { + const match = /\+(\d+)/.exec(row); + line = match ? Number(match[1]) : 0; + } else if (row.startsWith('+') && current) { + files.get(current).set(line, row.slice(1)); + line += 1; + } else if (row.startsWith('-') && !row.startsWith('---')) { + removed.add(row.slice(1).trim()); + } + } + return {files, removed}; +}; + +const lineStarts = text => { + const starts = [0]; + for (let index = 0; index < text.length; index++) { + if (text[index] === '\n') starts.push(index + 1); + } + return starts; +}; + +const lineOf = (starts, position) => { + let low = 0; + let high = starts.length - 1; + while (low < high) { + const middle = Math.ceil((low + high) / 2); + if (starts[middle] <= position) low = middle; + else high = middle - 1; + } + return low + 1; +}; + +const commentRanges = (path, text) => { + const kind = /\.tsx$/.test(path) ? ts.ScriptKind.TSX : + /\.ts$/.test(path) ? ts.ScriptKind.TS : + /\.jsx$/.test(path) ? ts.ScriptKind.JSX : ts.ScriptKind.JS; + const source = ts.createSourceFile(path, text, ts.ScriptTarget.Latest, true, kind); + const seen = new Map(); + const collect = ranges => { + for (const range of ranges || []) seen.set(range.pos, range); + }; + const visit = node => { + if (node.kind === ts.SyntaxKind.JsxText || node.kind === ts.SyntaxKind.JsxTextAllWhiteSpaces) return; + if (node.kind >= ts.SyntaxKind.FirstToken && node.kind <= ts.SyntaxKind.LastToken) { + collect(ts.getLeadingCommentRanges(text, node.pos)); + collect(ts.getTrailingCommentRanges(text, node.end)); + return; + } + collect(ts.getLeadingCommentRanges(text, node.pos)); + node.getChildren(source).forEach(visit); + }; + visit(source); + collect(ts.getLeadingCommentRanges(text, source.endOfFileToken.pos)); + return [...seen.values()]; +}; + +const problems = []; +const {files, removed: removedLines} = parseDiff(git(...diffArgs, '--unified=0', '--no-color', '--diff-filter=ACMRD')); + +const removedText = [...removedLines].join('\n'); +const wasRemoved = text => text.length > 0 && removedText.includes(text); + +for (const [path, added] of files) { + if (SKIPPED_PATHS.some(pattern => pattern.test(path)) || !TEXT_FILE.test(path)) continue; + for (const [line, content] of added) { + if (content.includes(EM_DASH) && !removedLines.has(content.trim())) { + problems.push(`${path}:${line} em dash: use a comma, parentheses or "to"`); + } + } + if (!SOURCE_FILE.test(path)) continue; + const text = staged ? git('show', `:${path}`) : fs.readFileSync(path, 'utf8'); + const starts = lineStarts(text); + for (const range of commentRanges(path, text)) { + const comment = text.slice(range.pos, range.end); + if (DIRECTIVE.test(comment)) continue; + const first = lineOf(starts, range.pos); + const last = lineOf(starts, range.end - 1); + for (let line = first; line <= last; line++) { + if (typeof added.get(line) !== 'string') continue; + const lineStart = starts[line - 1]; + const lineEnd = line < starts.length ? starts[line] - 1 : text.length; + const portion = text.slice(Math.max(range.pos, lineStart), Math.min(range.end, lineEnd)).trim(); + if (!/[A-Za-z0-9]/.test(portion) || wasRemoved(portion)) continue; + problems.push(`${path}:${line} code comment: name things so the code explains itself`); + break; + } + } +} + +if (problems.length) { + console.error(problems.join('\n')); + console.error(`\n${problems.length} convention problem${problems.length === 1 ? '' : 's'} in the changed lines.`); + process.exitCode = 1; +} else { + console.log('conventions ok'); +} diff --git a/src/.eslintrc.js b/src/.eslintrc.js index bfdc88da..10ea5895 100644 --- a/src/.eslintrc.js +++ b/src/.eslintrc.js @@ -1,5 +1,8 @@ module.exports = { root: true, extends: ['scratch', 'scratch/es6'], - env: {browser: true} + env: {browser: true}, + rules: { + 'require-jsdoc': 'off' + } };