@cldmv/fix-headers 2.2.2 → 2.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +72 -6
- package/bin/fix-headers.mjs +34 -15
- package/dist/index.cjs +6 -4
- package/dist/index.mjs +31 -14
- package/package.json +11 -7
- package/types/index.d.mts +2 -2
- package/types/src/core/fix-headers.d.mts +14 -2
- package/types/src/detectors/css.d.mts +1 -0
- package/types/src/detectors/go.d.mts +1 -0
- package/types/src/detectors/index.d.mts +32 -7
- package/types/src/detectors/node.d.mts +2 -1
- package/types/src/detectors/php.d.mts +2 -0
- package/types/src/detectors/python.d.mts +9 -1
- package/types/src/detectors/rust.d.mts +1 -0
- package/types/src/detectors/top-lines.d.mts +30 -0
- package/types/src/detectors/yaml.d.mts +1 -0
- package/types/src/header/parser.d.mts +75 -7
- package/types/src/utils/text.d.mts +33 -0
- /package/types/src/{fix-header.d.mts → index.d.mts} +0 -0
|
@@ -80,9 +80,11 @@ export function getCommentSyntaxForFile(filePath: string, options?: {
|
|
|
80
80
|
blockEnd?: string;
|
|
81
81
|
};
|
|
82
82
|
/**
|
|
83
|
-
* Resolves
|
|
83
|
+
* Resolves the content at the top of a file that stays above the header: the detector's
|
|
84
|
+
* leading block (front matter), then the run of lines at the very top whose position matters
|
|
85
|
+
* (see {@link DetectorProfile}). A line further down is never part of it.
|
|
84
86
|
* @param {string} filePath - File path.
|
|
85
|
-
* @param {string} content -
|
|
87
|
+
* @param {string} content - File content, without a byte order mark.
|
|
86
88
|
* @param {{ language?: string, enabledDetectors?: string[], disabledDetectors?: string[], forcedDetectors?: string[] }} [options={}] - Runtime options.
|
|
87
89
|
* @returns {string} Preserved prefix (possibly empty).
|
|
88
90
|
*/
|
|
@@ -92,6 +94,19 @@ export function getPreservedPrefixForFile(filePath: string, content: string, opt
|
|
|
92
94
|
disabledDetectors?: string[];
|
|
93
95
|
forcedDetectors?: string[];
|
|
94
96
|
}): string;
|
|
97
|
+
/**
|
|
98
|
+
* Says why a file's content cannot carry a header (a PHP file that does not open with `<?php`,
|
|
99
|
+
* where a comment at the top would be output as text), or null when it can.
|
|
100
|
+
* @param {string} filePath - File path.
|
|
101
|
+
* @param {string} content - File content.
|
|
102
|
+
* @param {{ enabledDetectors?: string[], disabledDetectors?: string[], forcedDetectors?: string[] }} [options={}] - Runtime options.
|
|
103
|
+
* @returns {string | null} Skip reason, or null.
|
|
104
|
+
*/
|
|
105
|
+
export function getContentSkipReason(filePath: string, content: string, options?: {
|
|
106
|
+
enabledDetectors?: string[];
|
|
107
|
+
disabledDetectors?: string[];
|
|
108
|
+
forcedDetectors?: string[];
|
|
109
|
+
}): string | null;
|
|
95
110
|
/**
|
|
96
111
|
* @fileoverview Detector registry and shared selector helpers.
|
|
97
112
|
* @module fix-headers/detectors
|
|
@@ -103,10 +118,15 @@ export function getPreservedPrefixForFile(filePath: string, content: string, opt
|
|
|
103
118
|
* enabledByDefault: boolean,
|
|
104
119
|
* requiresForce?: boolean,
|
|
105
120
|
* resolveCommentSyntax: (filePath: string) => ({kind: "block" | "line" | "html", linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string} | null),
|
|
106
|
-
* resolvePreservedPrefix?: (filePath: string, content: string) => string
|
|
121
|
+
* resolvePreservedPrefix?: (filePath: string, content: string) => string,
|
|
122
|
+
* keepsAboveHeader?: (line: string, index: number) => boolean,
|
|
123
|
+
* resolveContentSkipReason?: (content: string) => string | null
|
|
107
124
|
* }} DetectorProfile
|
|
108
|
-
* A file-type detector: which extensions it handles and the comment syntax
|
|
109
|
-
*
|
|
125
|
+
* A file-type detector: which extensions it handles and the comment syntax of those files, and
|
|
126
|
+
* what stays above the header: `resolvePreservedPrefix` takes a leading block (front matter),
|
|
127
|
+
* and `keepsAboveHeader` accepts each line of the run at the very top of the file that keeps its
|
|
128
|
+
* place (a shebang, an encoding declaration; editor modelines are accepted for every detector).
|
|
129
|
+
* `resolveContentSkipReason` says why a file's content cannot carry a header at all. Which project a file belongs to is resolved separately,
|
|
110
130
|
* from the manifest drivers in `src/drivers/`. A detector with `requiresForce: true` is used only
|
|
111
131
|
* when its id is listed in the `forcedDetectors` option (see {@link getEnabledDetectors}).
|
|
112
132
|
*/
|
|
@@ -119,8 +139,11 @@ export function getPreservedPrefixForFile(filePath: string, content: string, opt
|
|
|
119
139
|
*/
|
|
120
140
|
export const DETECTOR_PROFILES: DetectorProfile[];
|
|
121
141
|
/**
|
|
122
|
-
* A file-type detector: which extensions it handles and the comment syntax
|
|
123
|
-
*
|
|
142
|
+
* A file-type detector: which extensions it handles and the comment syntax of those files, and
|
|
143
|
+
* what stays above the header: `resolvePreservedPrefix` takes a leading block (front matter),
|
|
144
|
+
* and `keepsAboveHeader` accepts each line of the run at the very top of the file that keeps its
|
|
145
|
+
* place (a shebang, an encoding declaration; editor modelines are accepted for every detector).
|
|
146
|
+
* `resolveContentSkipReason` says why a file's content cannot carry a header at all. Which project a file belongs to is resolved separately,
|
|
124
147
|
* from the manifest drivers in `src/drivers/`. A detector with `requiresForce: true` is used only
|
|
125
148
|
* when its id is listed in the `forcedDetectors` option (see {@link getEnabledDetectors}).
|
|
126
149
|
*/
|
|
@@ -138,4 +161,6 @@ export type DetectorProfile = {
|
|
|
138
161
|
blockEnd?: string;
|
|
139
162
|
} | null);
|
|
140
163
|
resolvePreservedPrefix?: (filePath: string, content: string) => string;
|
|
164
|
+
keepsAboveHeader?: (line: string, index: number) => boolean;
|
|
165
|
+
resolveContentSkipReason?: (content: string) => string | null;
|
|
141
166
|
};
|
|
@@ -2,7 +2,7 @@ export namespace detector {
|
|
|
2
2
|
export let id: string;
|
|
3
3
|
export { extensions };
|
|
4
4
|
export let enabledByDefault: boolean;
|
|
5
|
-
export
|
|
5
|
+
export { isShebang as keepsAboveHeader };
|
|
6
6
|
export function resolveCommentSyntax(filePath: any): {
|
|
7
7
|
kind: "block";
|
|
8
8
|
blockStart: string;
|
|
@@ -15,4 +15,5 @@ export namespace detector {
|
|
|
15
15
|
* @module fix-headers/detectors/node
|
|
16
16
|
*/
|
|
17
17
|
declare const extensions: string[];
|
|
18
|
+
import { isShebang } from "./top-lines.mjs";
|
|
18
19
|
export {};
|
|
@@ -2,6 +2,8 @@ export namespace detector {
|
|
|
2
2
|
export let id: string;
|
|
3
3
|
export { extensions };
|
|
4
4
|
export let enabledByDefault: boolean;
|
|
5
|
+
export function keepsAboveHeader(line: any, index: any): boolean;
|
|
6
|
+
export function resolveContentSkipReason(content: any): string;
|
|
5
7
|
export function resolveCommentSyntax(filePath: any): {
|
|
6
8
|
kind: "block";
|
|
7
9
|
blockStart: string;
|
|
@@ -2,7 +2,7 @@ export namespace detector {
|
|
|
2
2
|
export let id: string;
|
|
3
3
|
export { extensions };
|
|
4
4
|
export let enabledByDefault: boolean;
|
|
5
|
-
export
|
|
5
|
+
export { keepsAboveHeader };
|
|
6
6
|
export function resolveCommentSyntax(filePath: any): {
|
|
7
7
|
kind: string;
|
|
8
8
|
linePrefix: string;
|
|
@@ -13,4 +13,12 @@ export namespace detector {
|
|
|
13
13
|
* @module fix-headers/detectors/python
|
|
14
14
|
*/
|
|
15
15
|
declare const extensions: string[];
|
|
16
|
+
/**
|
|
17
|
+
* Whether a line at the top of a Python file keeps its place above the header: a shebang on
|
|
18
|
+
* line 1, or a PEP 263 encoding declaration on line 1 or 2 (Python reads it nowhere else).
|
|
19
|
+
* @param {string} line - The line.
|
|
20
|
+
* @param {number} index - Zero-based line number.
|
|
21
|
+
* @returns {boolean} Whether the line stays above the header.
|
|
22
|
+
*/
|
|
23
|
+
declare function keepsAboveHeader(line: string, index: number): boolean;
|
|
16
24
|
export {};
|
|
@@ -2,6 +2,7 @@ export namespace detector {
|
|
|
2
2
|
export let id: string;
|
|
3
3
|
export { extensions };
|
|
4
4
|
export let enabledByDefault: boolean;
|
|
5
|
+
export function keepsAboveHeader(line: any, index: any): boolean;
|
|
5
6
|
export function resolveCommentSyntax(filePath: any): {
|
|
6
7
|
kind: "block";
|
|
7
8
|
blockStart: string;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Whether a comment line is a vi/Vim/ex modeline, in either of Vim's forms:
|
|
3
|
+
* `vim: set ts=4 sw=4:` (`se`/`set`, options, a closing `:`, then any text) or
|
|
4
|
+
* `vim: ts=4 sw=4 et` (options to the end of the line, separated by white space or `:`).
|
|
5
|
+
* Every option must be option syntax, and at least one must be `opt=val` or a common boolean
|
|
6
|
+
* option, so prose such as `vi: is the editor we use` is not a modeline. Vim reads modelines
|
|
7
|
+
* only in the first and last few lines of a file (`modelines`, default 5), so one at the top
|
|
8
|
+
* stops working once a header is put above it.
|
|
9
|
+
* @param {string} line - The comment line, comment opener included.
|
|
10
|
+
* @returns {boolean} Whether the line is a modeline.
|
|
11
|
+
*/
|
|
12
|
+
export function isModeline(line: string): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* A shebang (`#!`), which only works on a file's first line.
|
|
15
|
+
* @param {string} line - The line.
|
|
16
|
+
* @param {number} index - Zero-based line number in the file.
|
|
17
|
+
* @returns {boolean} Whether the line is a shebang on the first line.
|
|
18
|
+
*/
|
|
19
|
+
export function isShebang(line: string, index: number): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Takes the run of lines at the start of `content` that `keep` accepts, line endings included.
|
|
22
|
+
* The run stops at the first line `keep` rejects (or at a last line without a line break, which
|
|
23
|
+
* is taken when accepted).
|
|
24
|
+
* @param {string} content - Text starting at a line start.
|
|
25
|
+
* @param {(line: string, index: number) => boolean} keep - Whether a line (without its line
|
|
26
|
+
* ending) at a zero-based line number of the file stays above the header.
|
|
27
|
+
* @param {number} [firstIndex=0] - Line number of the first line of `content` in the file.
|
|
28
|
+
* @returns {string} The lines taken.
|
|
29
|
+
*/
|
|
30
|
+
export function takeTopLines(content: string, keep: (line: string, index: number) => boolean, firstIndex?: number): string;
|
|
@@ -2,6 +2,7 @@ export namespace detector {
|
|
|
2
2
|
export let id: string;
|
|
3
3
|
export { extensions };
|
|
4
4
|
export let enabledByDefault: boolean;
|
|
5
|
+
export function keepsAboveHeader(line: any, index: any): boolean;
|
|
5
6
|
export function resolveCommentSyntax(filePath: any): {
|
|
6
7
|
kind: "line";
|
|
7
8
|
linePrefix: string;
|
|
@@ -1,3 +1,55 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Splits the text of an existing header block that is not header fields from the fields.
|
|
3
|
+
* `foreign` holds every line that is neither a field, a separator next to a field, nor an
|
|
4
|
+
* empty line around them: description text, unknown `@Tag:` lines, a dash line among text, a
|
|
5
|
+
* line wrapping a field's value. `copyrights` holds the values of the `@Copyright` lines after
|
|
6
|
+
* the header's own. A stacked copy's lead line (see {@link MOVED_BLOCK_LEAD}) is neither.
|
|
7
|
+
* Lines come back as written (comment prefix included), in their original order, with blank
|
|
8
|
+
* lines between them kept (unless a dropped line sat between the two pieces of text) and blank
|
|
9
|
+
* lines around them dropped.
|
|
10
|
+
* @param {string} headerText - The existing header block (every stacked segment).
|
|
11
|
+
* @param {string} [filePath=""] - File path used for syntax selection.
|
|
12
|
+
* @param {Parameters<typeof findProjectHeader>[2]} [syntaxOptions={}] - Syntax resolution options.
|
|
13
|
+
* @returns {{foreign: string[], copyrights: string[]}} The non-field lines, as written, and the additional copyright values.
|
|
14
|
+
*/
|
|
15
|
+
export function splitHeaderText(headerText: string, filePath?: string, syntaxOptions?: Parameters<typeof findProjectHeader>[2]): {
|
|
16
|
+
foreign: string[];
|
|
17
|
+
copyrights: string[];
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Returns the lines of an existing header block that are not header fields (the `foreign`
|
|
21
|
+
* lines of {@link splitHeaderText}).
|
|
22
|
+
* @param {string} headerText - The existing header block (every stacked segment).
|
|
23
|
+
* @param {string} [filePath=""] - File path used for syntax selection.
|
|
24
|
+
* @param {Parameters<typeof findProjectHeader>[2]} [syntaxOptions={}] - Syntax resolution options.
|
|
25
|
+
* @returns {string[]} The non-field lines, as written.
|
|
26
|
+
*/
|
|
27
|
+
export function extractForeignHeaderLines(headerText: string, filePath?: string, syntaxOptions?: Parameters<typeof findProjectHeader>[2]): string[];
|
|
28
|
+
/**
|
|
29
|
+
* The holder named by a `@Copyright` value, for comparing two notices: the value without a
|
|
30
|
+
* leading `Copyright`, `(c)` / `©`, the year or year range, and a trailing
|
|
31
|
+
* `All rights reserved.`, with white space collapsed, trailing punctuation dropped and
|
|
32
|
+
* lower-cased. `Copyright (c) 2013-2025 Example Inc. All rights reserved.` and
|
|
33
|
+
* `Copyright (c) 2013-2026 Example Inc. All rights reserved.` name the same holder.
|
|
34
|
+
* @param {string} value - A `@Copyright` value.
|
|
35
|
+
* @returns {string} The holder (empty when the notice names none).
|
|
36
|
+
*/
|
|
37
|
+
export function copyrightHolder(value: string): string;
|
|
38
|
+
/**
|
|
39
|
+
* Repairs a file an earlier fix-headers version wrote its header above lines that must come
|
|
40
|
+
* first (`<?php`, `@charset`, a shebang, Go build constraints, a modeline, ...; see
|
|
41
|
+
* getPreservedPrefixForFile), or above a byte order mark. The top of the file is read as one
|
|
42
|
+
* run: lines already kept at the top (possibly none), then file headers with blank lines, then
|
|
43
|
+
* lines that would be kept at the top if they directly followed the first ones (or a byte
|
|
44
|
+
* order mark). Those last lines are moved up to join the first ones, in order, and the headers
|
|
45
|
+
* placed after them, where the header is then rewritten as usual. Any other content before
|
|
46
|
+
* those lines leaves the file as it is: nothing is moved up past it.
|
|
47
|
+
* @param {string} content - File content.
|
|
48
|
+
* @param {string} [filePath=""] - File path used for syntax selection.
|
|
49
|
+
* @param {Parameters<typeof findProjectHeader>[2]} [syntaxOptions={}] - Syntax resolution options.
|
|
50
|
+
* @returns {string} The content with the lines back at the top (unchanged when there is nothing to repair).
|
|
51
|
+
*/
|
|
52
|
+
export function restoreTopLines(content: string, filePath?: string, syntaxOptions?: Parameters<typeof findProjectHeader>[2]): string;
|
|
1
53
|
/**
|
|
2
54
|
* Finds the first top-level project header block in a file.
|
|
3
55
|
* @param {string} content - File content.
|
|
@@ -24,14 +76,26 @@ export function findProjectHeader(content: string, filePath?: string, syntaxOpti
|
|
|
24
76
|
end: number;
|
|
25
77
|
} | null;
|
|
26
78
|
/**
|
|
27
|
-
* Replaces or inserts a project header block.
|
|
28
|
-
* @
|
|
79
|
+
* Replaces or inserts a project header block. The existing header is the file's first comment
|
|
80
|
+
* (after what stays above it, see {@link splitPreservedPrefix}) when it is a file header, plus
|
|
81
|
+
* every file header stacked directly after it, which are copies of it: their fields are
|
|
82
|
+
* replaced by the new header's. Text in them that is not a header field (see
|
|
83
|
+
* {@link splitHeaderText}) is never dropped: with `strictHeaderBlock` (the default) it is
|
|
84
|
+
* written as its own comment block directly below the header, separated from it and from the
|
|
85
|
+
* rest of the file by the margin; with `strictHeaderBlock: false` it stays inside the header,
|
|
86
|
+
* after the fields. A `@Copyright` line after the header's own that names the same holder as
|
|
87
|
+
* the new header's (see {@link copyrightHolder}) is a duplicate and dropped; one naming another
|
|
88
|
+
* holder is kept as an additional `@Copyright` field under the new header's, in order, and
|
|
89
|
+
* reported in `extraCopyrights`.
|
|
90
|
+
* @param {string} original - Original file content.
|
|
29
91
|
* @param {string} newHeader - Generated header text.
|
|
30
92
|
* @param {string} [filePath=""] - File path used for syntax selection.
|
|
31
|
-
* @param {{ language?: string, enabledDetectors?: string[], disabledDetectors?: string[], forcedDetectors?: string[], detectorSyntaxOverrides?: Record<string, { linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string }>, spacing?: number, margin?: number }} [syntaxOptions={}] - Syntax resolution options.
|
|
32
|
-
* @returns {{nextContent: string, changed: boolean}} Updated
|
|
93
|
+
* @param {{ language?: string, enabledDetectors?: string[], disabledDetectors?: string[], forcedDetectors?: string[], detectorSyntaxOverrides?: Record<string, { linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string }>, spacing?: number, margin?: number, strictHeaderBlock?: boolean }} [syntaxOptions={}] - Syntax resolution options.
|
|
94
|
+
* @returns {{nextContent: string, changed: boolean, header: string, written: string, extraCopyrights: string[]}} Updated
|
|
95
|
+
* content result: `header` is the header block as written, `written` everything that took the old header's place (the
|
|
96
|
+
* header and the block of text moved below it), and `extraCopyrights` the additional copyright values the header keeps.
|
|
33
97
|
*/
|
|
34
|
-
export function replaceOrInsertHeader(
|
|
98
|
+
export function replaceOrInsertHeader(original: string, newHeader: string, filePath?: string, syntaxOptions?: {
|
|
35
99
|
language?: string;
|
|
36
100
|
enabledDetectors?: string[];
|
|
37
101
|
disabledDetectors?: string[];
|
|
@@ -45,21 +109,25 @@ export function replaceOrInsertHeader(content: string, newHeader: string, filePa
|
|
|
45
109
|
}>;
|
|
46
110
|
spacing?: number;
|
|
47
111
|
margin?: number;
|
|
112
|
+
strictHeaderBlock?: boolean;
|
|
48
113
|
}): {
|
|
49
114
|
nextContent: string;
|
|
50
115
|
changed: boolean;
|
|
116
|
+
header: string;
|
|
117
|
+
written: string;
|
|
118
|
+
extraCopyrights: string[];
|
|
51
119
|
};
|
|
52
120
|
/**
|
|
53
121
|
* Returns a file's content with its project header taken out: the file as it would read
|
|
54
122
|
* with no header at all. Blank lines between the header and the next content, and a body
|
|
55
123
|
* that is only whitespace, are dropped, so two versions of a file that differ only in their
|
|
56
124
|
* header (its fields, framing, spacing or margin, or whether it has one) give the same body.
|
|
57
|
-
* @param {string}
|
|
125
|
+
* @param {string} original - File content.
|
|
58
126
|
* @param {string} [filePath=""] - File path used for syntax selection.
|
|
59
127
|
* @param {{ language?: string, enabledDetectors?: string[], disabledDetectors?: string[], forcedDetectors?: string[], detectorSyntaxOverrides?: Record<string, { linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string }>, spacing?: number, margin?: number }} [syntaxOptions={}] - Syntax resolution options.
|
|
60
128
|
* @returns {string} The content outside the header.
|
|
61
129
|
*/
|
|
62
|
-
export function extractHeaderlessBody(
|
|
130
|
+
export function extractHeaderlessBody(original: string, filePath?: string, syntaxOptions?: {
|
|
63
131
|
language?: string;
|
|
64
132
|
enabledDetectors?: string[];
|
|
65
133
|
disabledDetectors?: string[];
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
*
|
|
3
|
+
* @Project: @cldmv/fix-headers
|
|
4
|
+
* @Filename: /src/utils/text.mjs
|
|
5
|
+
* @Date: 2026-10-31T16:30:00-07:00 (1793489400)
|
|
6
|
+
* @Author: Nate Corcoran <CLDMV>
|
|
7
|
+
* @Email: <Shinrai@users.noreply.github.com>
|
|
8
|
+
* -----
|
|
9
|
+
* @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
|
|
10
|
+
* @Last modified time: 2026-10-31T16:30:00-07:00 (1793489400)
|
|
11
|
+
* -----
|
|
12
|
+
* @Copyright: Copyright (c) 2013-2026 Catalyzed Motivation Inc. All rights reserved.
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
/**
|
|
16
|
+
* @fileoverview String checks written as index scans instead of regular expressions, so they
|
|
17
|
+
* run in linear time on any input (the regex forms backtrack polynomially on long runs of
|
|
18
|
+
* `<` or `/`).
|
|
19
|
+
* @module fix-headers/utils/text
|
|
20
|
+
*/
|
|
21
|
+
/**
|
|
22
|
+
* Tells whether text contains a `<…>` tag with at least one character inside, the same test
|
|
23
|
+
* as `/<[^>]+>/`.
|
|
24
|
+
* @param {string} text - Text to check.
|
|
25
|
+
* @returns {boolean} Whether a non-empty `<…>` tag is present.
|
|
26
|
+
*/
|
|
27
|
+
export function hasAngleTag(text: string): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Removes every trailing `/`, the same as `.replace(/\/+$/, "")`.
|
|
30
|
+
* @param {string} text - Text to trim.
|
|
31
|
+
* @returns {string} The text without trailing slashes.
|
|
32
|
+
*/
|
|
33
|
+
export function trimTrailingSlashes(text: string): string;
|
|
File without changes
|