Repository navigation
release: v2.2.4 - find and keep # headers in CRLF and byte-order-mark… - #167
Merged
Merged
Conversation
…es (#162) The line-comment header match used `.`, which does not match the \r of a CRLF line, so an existing # header in a CRLF file was not found and a second header was inserted above it. A leading UTF-8 byte order mark broke the ^ anchor for every comment style the same way. The byte order mark is now part of the preserved prefix (it keeps byte 0, ahead of a shebang), the header matchers accept CRLF line breaks, and the header, the gap after it and the moved block are written in the file's own line ending, so a CRLF file stays CRLF and a \n header an earlier run wrote into a CRLF file is repaired.
Any '# vi:', '# vim:' or '# ex:' line at the top of a # header counted as a modeline and stayed above the header, so prose such as '# vi: is the editor we use' was not treated as header text. A modeline now has to follow Vim's grammar: 'set'/'se' options closed by ':', or options to the end of the line, every option in opt / opt=val form and at least one of them opt=val or a common boolean option (et, noexpandtab, ...). Versioned 'vim600:' and 'Vim: set ...:' forms are recognised.
#162) A comment after the header was only taken for a stacked copy of a header when its first non-empty line was @project:, so an old header that opened with description text stayed below with stale fields. Such a comment is now also merged when it holds a run of at least four managed fields starting with @project: with only separators and empty lines between them. Its text is kept like the header's own and its fields are replaced. A moved block that would read as a stacked header under either rule gets the 'Moved out of the file header by fix-headers:' lead line, and a comment opening with that line is never merged, so a moved block is never taken back into the header and a second run is a no-op.
…al lines kept above it (#162) A comment is a file header when it holds a @project: or a @filename: field line, so a header with only @filename is now found and rewritten in place. Every file header stacked directly after the file's header is a copy of it and is merged, whatever its first line or field values: its field lines are dropped and its other text is kept like the header's own. A repeated field tag inside the header is a duplicate and is dropped. Blank lines that only spaced a dropped field are dropped with it. A @copyright line after the header's own is compared by holder (without Copyright, (c)/©, years and All rights reserved.) with the regenerated one: the same holder is dropped, another holder is kept as an additional @copyright field under the header's own and reported in the result's copyrightWarnings, the file's extraCopyrights and a CLI warning line (exit code unchanged). The 'Moved out of the file header by fix-headers:' lead line is no longer written: a moved block never holds a @project:/@filename: line. A 2.2.3 lead-line block that still holds one is merged and the lead line dropped; one without is left alone. Only lines already at the very top of the file whose position matters stay above the header, decided per detector: a shebang (any interpreter, JS/TS/Python; Rust except #![...]), a PEP 263 coding line on line 1 or 2, #cloud-config on line 1, @charset "..."; on line 1, Go build constraints with the blank line after them, the <?php line, Markdown front matter, and vi/Vim/ex modelines in any comment syntax. Nothing is hoisted from further down, and a yaml-language-server comment is header text. A .php file that does not open with <?php is skipped, since a comment above it would be output as text.
#162) Earlier versions wrote the header above lines that must come first (<?php, @charset, a cargo-script shebang, Go build constraints, modelines, coding lines) and above a byte order mark. A file that starts with file headers (a @project: or @filename: field line) followed, after blank lines only, by such lines or a byte order mark is now repaired: those lines go back to the top and the header is rewritten below them exactly as for a file that had them first, with stacked copies merged, text moved and duplicates dropped. Any other comment or code before them still means nothing is moved. A .php file whose only content before <?php is such a header is repaired instead of skipped; one with real content before <?php is still skipped. The repair is not a content edit.
…ept above it (#162) Earlier versions kept a shebang or front matter above the header but wrote the header above the next position-critical line, so a PEP 263 coding line after a shebang, or a modeline after a shebang or front matter, ended up below the header. The top of the file is now read as one run: lines already kept at the top, then file headers with blank lines, then lines that would be kept at the top if they directly followed the first ones. Those lines move up to join the first ones in order, judged on the joined run, and the header is rewritten below them. Any other comment or code stops the run.
CodeQL js/polynomial-redos alerts #6, #7 and #8: copyrightHolder's trailing 'all rights reserved' and punctuation patterns and the new header's @copyright match let adjacent quantifiers compete for the same white space. copyrightHolder now uses anchored patterns and string operations, and the @copyright value is read with indexOf and trim. The modeline matcher had the same shape ([ \t]*(.*?)[ \t]* before an optional closer) and a 'set' prefix whose white space overlapped the options; it now captures the rest of the line and trims the closer with string operations, and takes one white space character after 'set'.
… for the fix-headers-dev condition (#157, #158) #157: the CJS entry moves from src/cjs/index.cjs to src/index.cjs and the ESM entry from src/fix-header.mjs to src/index.mjs, so require("./index.mjs") resolves to the sibling from source as well as in dist/. tsup copies the CJS entry with onSuccess into the build's own outDir instead of publicDir. The wrapper's code is unchanged. #158: devcheck.mjs (from @cldmv/uuid) nags and exits when a source checkout runs without --conditions=fix-headers-dev. The ESM entry imports it from an async block (no top-level await, which require(esm) rejects); both bundles keep the import external and the package never ships it. The dev condition gains require: ./src/index.cjs. vitest, the node --test CJS run and the cli / fix:headers scripts pass the condition.
The entry kept no handle on the devcheck it started, so a caller (the CLI, or any ESM or CJS consumer) could start reading and writing files before devcheck exited the process. Keep the promise and await it inside fixHeaders(), the pattern @cldmv/slothlet's entry uses. Still no top-level await, so require() through index.cjs keeps working.
The awaited-promise version only gated fixHeaders(). Loading devcheck.mjs through createRequire (require(esm)) at the top of the entry finishes before any export can be called, from ESM or CJS, sync or async, and still has no top-level await. MODULE_NOT_FOUND (the published package) is ignored; any other error is rethrown.
…and browser safe (#158) A static `import { createRequire } from "node:module"` broke browser bundles of a package using the block (esbuild and webpack fail the build, Vite's bundle throws at load). process.getBuiltinModule (Node >=20.16 / 22.3) keeps the synchronous gate without a static import of a Node built-in, and the check is skipped where there is no process (a browser) or no getBuiltinModule (a process polyfill).
The entry loads devcheck.mjs synchronously through require(esm). Node 22.12 is the only release line that prints an ExperimentalWarning for that (20.19 and 22.13+ don't), so every source-checkout run on 22.12 warned and the CLI's clean-stderr test failed in CI. engines.node and the CI and publish matrix floors move to 22.13.0.
Contributor
Author
🔒 Dependency Review
|
Contributor
Author
|
| File | Raw | Δ Raw | Gzipped | Δ Gzipped |
|---|---|---|---|---|
| bin/fix-headers.mjs | 60.4 kB | +4.8 kB (+8.6%) |
20.8 kB | +1.8 kB |
| dist/index.cjs | 1.7 kB | +143 B (+8.8%) |
995 B | +55 B |
| dist/index.mjs | 50.4 kB | +4.5 kB (+9.7%) |
17.7 kB | +1.7 kB |
| Total | 112.6 kB | +9.4 kB | 39.5 kB | +3.6 kB |
📊 Generated by bundle-size. Brotli sizes also measured but omitted from the table for brevity.
Shinrai
approved these changes
Nov 1, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@cldmv/fix-headersv2.2.4 ChangelogRelease Date: October 2026
Release Type: Patch
Branch:
release/2.2.4Overview
Version 2.2.4 makes a file carry exactly one header. Stacked copies of a header are merged into one whatever their first line is, repeated fields are dropped as duplicates, and a stale
@Copyrightfor the same holder goes away while one naming another holder is kept and reported as a warning. Only lines that must stay at the very top of a file (a shebang, a Python encoding line,#cloud-config,@charset, Go build constraints,<?php, Markdown front matter, a Vim modeline) stay above the header, and files where an earlier version wrote the header above such a line are repaired.#headers are now found in files with CRLF line endings or a byte-order mark. These are the follow-ups to the v2.2.3 header-text fix, tracked in #162.This changes what a run writes to files that have a stacked or duplicated header, a stale
@Copyrightline, or a header an earlier version placed above a shebang or directive: the first run after upgrading cleans them up. Files with a single, clean header are written exactly as before.The rest of the release is packaging and hardening: the entry files are now
src/index.mjsandsrc/index.cjs, a source checkout run without thefix-headers-devcondition stops with a clear message, the Node.js floor is 22.13.0, and two CodeQL ReDoS alerts are fixed.🐛 Bug Fixes
One header per file (#166, fixes #162)
@Project:or@Filename:line. Every file header stacked directly after the first one (blank lines only in between) is merged into it, whatever its first line is and whatever project or file it names. v2.2.3 only merged a stacked comment whose first line was@Project:, so one starting with description text was left as a second header.@Project,@Dateor@Authorline is dropped along with a-----line next to it. In v2.2.3 these were kept as text.@Copyrightlines are compared by holder. A second@Copyrightnaming the same holder as the regenerated one (a stale2013-2025line next to the new2013-2026one) is dropped. One naming another holder (Copyright (c) 2010 Mozilla Foundation) is kept as a field directly under the header's own and reported: in the result's newcopyrightWarningsarray ({ file, copyright }), in each change'sextraCopyrights, and on the CLI as awarning: <file> keeps an additional @Copyright: <value>line withwarnings=<n>in the summary. The warning never changes the exit code.Moved out of the file header by fix-headers:and a@Project:line is merged as a copy of the header, and the lead line is dropped with its fields. Newly moved blocks no longer get a lead line, because they never hold a@Project:or@Filename:line.Only position-critical lines stay above the header (#166)
The header is now the first thing in the file, except for lines already at the very top that would break the file or its tooling if moved: a
#!shebang (JavaScript/TypeScript, Python, Rust scripts), a PEP 263 encoding line,#cloud-config,@charset,//go:build/// +buildconstraints, the<?phpline, Markdown front matter, and a vi/Vim/ex modeline in any comment style. Nothing further down is moved up.#!/usr/bin/env python3, header,# -*- coding: utf-8 -*-), the line moves back up to join the others and the header is rewritten below them. A byte-order mark left below a header goes back to byte 0.vi:/vim:/ex:modelines stay above a#header; prose such as# vi: is the editor we useis header text.<?phpis skipped and reported, because a comment at the top would be output as text. A file where only a fix-headers header comes before<?phpis repaired instead.#headers in CRLF and byte-order-mark files (#166)A
#header in a file with CRLF line endings or a UTF-8 byte-order mark was not found, so a second header was added above it. It is now found and updated in place. The header is written in the file's own line ending, a header an earlier version wrote with\ninto a CRLF file is rewritten with CRLF, and the byte-order mark stays at the start of the file, ahead of the header and any shebang.Entry files and the development check (#165, fixes #157 and #158)
src/index.mjs(wassrc/fix-header.mjs) and the CommonJS wrapper is its siblingsrc/index.cjs(wassrc/cjs/index.cjs), sorequire("./index.mjs")resolves the same way from source and fromdist/. The publisheddist/index.mjs,dist/index.cjsandbin/fix-headers.mjspaths and the package's exports are unchanged. Thefix-headers-devcondition now coversrequireas well asimport.--conditions=fix-headers-devwould silently use a staledist/build. The entry now loads a rootdevcheck.mjsfirst, synchronously, which stops the process with a message saying how to run it. The file is never published, so installed copies skip it. It is loaded throughprocess.getBuiltinModule, with no staticnode:moduleimport and no top-level await, so the entry stays safe for bundlers and keeps working throughrequire().Linear-time text checks (CodeQL #1, #2, #165, #166)
The check for an existing
<…>tag in the author name and the trailing-slash trim on folder paths used regular expressions that backtrack polynomially on long runs of<or/. Both are now index scans with the same results. The copyright-holder and modeline matching added in this release are linear-time too.🔧 CI & tooling
engines.nodeis>=22.13.0(was>=22.12.0), and the CI matrix floor matches. Node 22.12.x prints anExperimentalWarningwhenrequire()loads an ES module, which the CommonJS entry does.cli,fix:headersand CommonJS test scripts and the Vitest config pass--conditions=fix-headers-dev(#165).📚 Documentation
@Copyrightlines" section,copyrightWarningsin the API result, line endings and byte-order marks under the header layout, PHP templates in the skipped files, and the 22.13.0 floor (#165, #166).🔧 Dependencies
Development dependencies only; nothing in the published package changes.
minimatch10.2.6 → 11.0.3 through an npm override, for GHSA-g3hv-75wg-m249 (glob pattern amplification, Dependabot #14). ESLint still declares^10.2.4.katex0.16.47 → 0.19.1 through an npm override, for GHSA-238p-pmpm-9mq7 (prototype pollution gadget, Dependabot #7). It comes in through@eslint/markdown, whose math extension still declares^0.16.0.Upgrade notes
fixHeadersAPI or the CLI. The result gains acopyrightWarningsarray, which is always present.@Copyrightline, a block v2.2.3 moved below the header with its lead line, a header above a shebang or directive, or a CRLF or byte-order-mark#file that got a second header. Run with--dry-run --difffirst to review it.node --conditions=fix-headers-dev; the npm scripts already do.👥 Contributors
Avg: 99.6% ·
3b2cd33· Node lts/*Co-authored-by: Shinrai [email protected]