Skip to content

Clean up the engine code - #851

Merged
mathiasrw merged 30 commits into
mainfrom
cleanup/engine
Oct 23, 2026
Merged

mathiasrw merged 30 commits into
mainfrom
cleanup/engine

Conversation

@mathiasrw

@mathiasrw mathiasrw commented Oct 23, 2026 •

Copy link
Copy Markdown
Owner

A code cleanup with no change in what RexReplace does. It is made on main after the whole train was merged. yarn test-ci passes with the same 1127 tests, and the speed is the same (20 000 matches: 221 ms on main, 229 ms here).

Smaller

  • dynamicReplacement() is 150 lines, not 320. The 48 values were written out twice, and copied once more in the per-match branch. Now one object of names, and the numbers of Only make the --js numbers the code uses, and only count files for $i and $c to close #814 #830, feed one function, which is made once instead of once per file.
  • getFlags() is merged into getFinalRegex(). The RE2 case was a copy of the runtime case.
  • Dead code is gone: the two "node < 6" checks, the code in comments in engine.ts, the colour stub font, note(), and the displayHelp argument of panic() with config.showHelp.
  • shim.ts is gone. ontoRun.has(target) was the same as inRun.has(target), and it was the only use of Set.intersection.
  • thrownText() uses tryOr() in place of three copies of a try.
  • renamed() is the one place that marks a rename in the status and names it.
  • splitMatch() is the one place that takes the offset, the text and the named groups off the arguments of a replace function.

Made once

  • codeUses() finds once per run what the code of --js names, with a plain text search: the named groups, and whether it runs per match, counts what came before, or needs the size or the times. Before, three regexes asked this, and the pattern was compiled again for each file.
  • The regexes in renameFiles(), the stack trace helpers and thrownText() are in re.
  • The cache of replacement files, the unreadable ones and the start time are reset per run, so a config that is used again does not keep old ones.

One job each

Function Does
checkFlags() Panics on flags that can not go together
unpackConfig() Makes the config the run works with from the flags
processFiles() Runs the worker pool over the files
run() Calls the three above, and picks the route: piped data, renames or files
readReplacement() The replacement as text: from the pipe, a file, or as typed
matchPrinter() Prints each match for --output-match
planRenames() The old and the new path of each file
checkNameInvalid() The names that can not be made, with the reason
checkNameClash() Panics when planned renames clash (was findClashes())

src/output.ts now only prints. src/status.ts is new: it records what happens in a run (warn, error, panic, verbose, checkExpectations, Panic) and prints through output.ts. The detail of an error on its own line (#852) is made there now. config.stopped is now config.plzStop, as it asks the workers to stop.

Names

isFile() is pathKind() and gives 'file', 'glob' or null. updated() is markFileChanged(), info() is printName(), chat() is verbose(), getFinalPattern() is regexPattern(), handlePipedData() is usePipeAsContent(). The parameters of doReplacement() lose the _rr at the end, as the code of the user can not see them anyway. devguide.md has the new names.

Good to know

  • The flag checks now come before the pattern and the replacement are read, so when two things are wrong at once, another panic can be the one that is told.
  • A / in a new name with --rename is now one of the invalid names, so the messages come in the order of the files, and such a name is no longer also checked for a clash.

@mathiasrw
mathiasrw added this pull request to stack #853 October 23, 2026 16:51
Base automatically changed from fix/839 to main October 23, 2026 17:13
@mathiasrw
mathiasrw marked this pull request as draft October 23, 2026 17:13
@mathiasrw
mathiasrw marked this pull request as ready for review October 23, 2026 17:18
@mathiasrw
mathiasrw merged commit 0e6f360 into main Oct 23, 2026
7 checks passed
@mathiasrw
mathiasrw deleted the cleanup/engine branch October 24, 2026 04:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant