This guide is for one job: going through existing code and removing comment clutter, in a pull request that changes comments and nothing else.
It applies to every Source Academy repository. Each repository's AGENTS.md has the short rules for writing new comments, and the facts that differ from repository to repository: which files are generated, and which commands to run. This guide says how to judge the comments that are already there. If a repository has no AGENTS.md yet, see When there is no AGENTS.md.
Not every repository has one yet (this documentation repository does not). Then use these defaults, and say in the pull request which you used:
Generated files are those whose first lines say so, those in .gitignore, and those that .gitattributes marks linguist-generated. When in doubt, ask in the issue.
Checks are the scripts of package.json (format, lint, test, build), or the commands in the README.
Writing the AGENTS.md is a good pull request of its own.
Read the code first, then ask in this order. Stop at the first question that decides it.
Is it true? If it contradicts the code, fix it or delete it. A wrong comment is worse than none.
Would a user of the package see it in their editor or in generated documentation? That is the comment of any exported symbol, and of any member of one: a method, property, enum member or parameter of an exported class, interface, type or enum. Keep it, and only fix the wording. This question comes before the next one, because such a comment often just says what the symbol is or does, and is still wanted.
Does it say why? A reason, a constraint, an invariant, a workaround, a trap, the format of some data. If it only says what the next lines do, or tells the history ("previously", "used to", "fixed per review"), delete it.
Is it as short as it can be? Shorten it to the reason. Drop issue and pull request numbers and the names of reviewers or review tools. Keep one see #123 only if the issue holds context that the comment cannot carry.
Is it in the right place? An explanation of a whole module's model belongs at the top of that module. Anything else longer than about ten lines belongs in a document, with a one-line pointer.
When unsure, keep the comment and say so in the pull request.
Some comments are instructions to a tool, not text for people. Leave them exactly as they are: eslint-disable..., @ts-expect-error, @ts-ignore, istanbul ignore ..., prettier-ignore, @jest-environment ..., #! lines, license headers, /// <reference ...>, and the equivalents in other languages (for example Credo's # credo:disable-for-next-line).
The repository's format, lint and test commands pass (see its AGENTS.md).
The code is unchanged. For TypeScript, print every file without its comments, once on the base commit and once on your branch, and compare. The printer keeps types, interfaces and imports; compiling to JavaScript would drop them, and a changed type could go unnoticed:
bash
# on the base commit (> /tmp/before.txt), then on your branch (> /tmp/after.txt)yarn node -e 'const ts = require("typescript"), fs = require("fs");const printer = ts.createPrinter({ removeComments: true });for (const f of process.argv.slice(1)) { const sf = ts.createSourceFile(f, fs.readFileSync(f, "utf8"), ts.ScriptTarget.Latest, true); console.log("//// " + f + "\n" + printer.printFile(sf));}' $(git ls-files -- '*.ts' '*.tsx' '*.mts' '*.cts') > /tmp/before.txtdiff /tmp/before.txt /tmp/after.txt
Open /tmp/before.txt first: if it is empty, no file was compared and the check proves nothing (the list above takes every tracked TypeScript file of the repository, not only those under src). Leave out the generated files that the repository's AGENTS.md lists, in both runs, with an exclude pattern such as ':!src/parser/python-grammar.ts' after the extensions.
No difference means that no code and no type changed. The printer normalizes whitespace, so a reformatting does not show here: the diff of your pull request does. The check does not cover directives (they are comments too), so leave those alone. A script that does the check for every language is planned.
Body: the number of comment lines before and after; the long comments you kept, one line each on why; the comments you rewrote (before and after); the judgement calls a person should check; and "No code changed", with the output of the check.
Cleaning up comments
This guide is for one job: going through existing code and removing comment clutter, in a pull request that changes comments and nothing else.
It applies to every Source Academy repository. Each repository's
AGENTS.mdhas the short rules for writing new comments, and the facts that differ from repository to repository: which files are generated, and which commands to run. This guide says how to judge the comments that are already there. If a repository has noAGENTS.mdyet, see When there is noAGENTS.md.What a cleanup pull request is
AGENTS.mdlists them.When there is no
AGENTS.mdNot every repository has one yet (this documentation repository does not). Then use these defaults, and say in the pull request which you used:
.gitignore, and those that.gitattributesmarkslinguist-generated. When in doubt, ask in the issue.package.json(format, lint, test, build), or the commands in the README.AGENTS.mdis a good pull request of its own.For each comment
Read the code first, then ask in this order. Stop at the first question that decides it.
see #123only if the issue holds context that the comment cannot carry.When unsure, keep the comment and say so in the pull request.
Examples
These are from
py-slang.src/engines/py2js/runtime.ts(int -> bigint,float -> number,None -> null, and so on)enclosingPreludeFunction(py-slang#397). Unset for both native/bridged ..."(py-slang#397)(issue #341)// String concatenation: str + str only.aboveif (typeof l === "string" || ...Directives are code
Some comments are instructions to a tool, not text for people. Leave them exactly as they are:
eslint-disable...,@ts-expect-error,@ts-ignore,istanbul ignore ...,prettier-ignore,@jest-environment ...,#!lines, license headers,/// <reference ...>, and the equivalents in other languages (for example Credo's# credo:disable-for-next-line).Never
TODOthat is still true. Open the issue and writeTODO(#123): ..., or leave it as it is.Before you open the pull request
The repository's format, lint and test commands pass (see its
AGENTS.md).The code is unchanged. For TypeScript, print every file without its comments, once on the base commit and once on your branch, and compare. The printer keeps types, interfaces and imports; compiling to JavaScript would drop them, and a changed type could go unnoticed:
Open
/tmp/before.txtfirst: if it is empty, no file was compared and the check proves nothing (the list above takes every tracked TypeScript file of the repository, not only those undersrc). Leave out the generated files that the repository'sAGENTS.mdlists, in both runs, with an exclude pattern such as':!src/parser/python-grammar.ts'after the extensions.No difference means that no code and no type changed. The printer normalizes whitespace, so a reformatting does not show here: the diff of your pull request does. The check does not cover directives (they are comments too), so leave those alone. A script that does the check for every language is planned.
The pull request
Comments: <path>.Afterwards
If the same clutter comes back, make the rules in the repository's
AGENTS.mdsharper. Do not add to this guide.