@cldmv/fix-headers 1.3.12 → 2.1.0

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.
Files changed (65) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +354 -25
  3. package/bin/fix-headers.mjs +64 -0
  4. package/dist/index.cjs +28 -0
  5. package/dist/index.mjs +15 -0
  6. package/package.json +39 -19
  7. package/types/src/cli.d.mts +20 -2
  8. package/types/src/config/load.d.mts +36 -0
  9. package/types/src/constants.d.mts +15 -23
  10. package/types/src/core/file-discovery.d.mts +17 -5
  11. package/types/src/core/fix-headers.d.mts +61 -4
  12. package/types/src/core/ignore-rules.d.mts +38 -0
  13. package/types/src/detect/project.d.mts +64 -15
  14. package/types/src/detectors/css.d.mts +0 -8
  15. package/types/src/detectors/go.d.mts +0 -8
  16. package/types/src/detectors/html.d.mts +0 -8
  17. package/types/src/detectors/index.d.mts +31 -8
  18. package/types/src/detectors/json.d.mts +0 -8
  19. package/types/src/detectors/node.d.mts +0 -8
  20. package/types/src/detectors/php.d.mts +0 -8
  21. package/types/src/detectors/python.d.mts +0 -8
  22. package/types/src/detectors/rust.d.mts +0 -8
  23. package/types/src/detectors/yaml.d.mts +0 -8
  24. package/types/src/drivers/go.d.mts +2 -0
  25. package/types/src/drivers/index.d.mts +154 -0
  26. package/types/src/drivers/node.d.mts +2 -0
  27. package/types/src/drivers/php.d.mts +2 -0
  28. package/types/src/drivers/python.d.mts +2 -0
  29. package/types/src/drivers/rust.d.mts +2 -0
  30. package/types/src/drivers/shared.d.mts +78 -0
  31. package/types/src/header/dates.d.mts +108 -0
  32. package/types/src/header/fields.d.mts +48 -0
  33. package/types/src/header/parser.d.mts +6 -2
  34. package/types/src/header/syntax.d.mts +8 -3
  35. package/types/src/header/template.d.mts +7 -4
  36. package/types/src/utils/diff.d.mts +53 -0
  37. package/types/src/utils/fs.d.mts +10 -3
  38. package/types/src/utils/git.d.mts +27 -0
  39. package/types/src/utils/time.d.mts +55 -0
  40. package/index.cjs +0 -39
  41. package/index.mjs +0 -20
  42. package/src/cli.mjs +0 -323
  43. package/src/constants.mjs +0 -50
  44. package/src/core/file-discovery.mjs +0 -213
  45. package/src/core/fix-headers.mjs +0 -413
  46. package/src/detect/project.mjs +0 -190
  47. package/src/detectors/css.mjs +0 -67
  48. package/src/detectors/go.mjs +0 -65
  49. package/src/detectors/html.mjs +0 -67
  50. package/src/detectors/index.mjs +0 -187
  51. package/src/detectors/json.mjs +0 -76
  52. package/src/detectors/node.mjs +0 -90
  53. package/src/detectors/php.mjs +0 -66
  54. package/src/detectors/python.mjs +0 -75
  55. package/src/detectors/rust.mjs +0 -62
  56. package/src/detectors/shared.mjs +0 -45
  57. package/src/detectors/yaml.mjs +0 -79
  58. package/src/fix-header.mjs +0 -30
  59. package/src/header/parser.mjs +0 -145
  60. package/src/header/syntax.mjs +0 -69
  61. package/src/header/template.mjs +0 -65
  62. package/src/utils/fs.mjs +0 -138
  63. package/src/utils/git.mjs +0 -135
  64. package/src/utils/time.mjs +0 -44
  65. package/types/src/detectors/shared.d.mts +0 -14
@@ -1,90 +0,0 @@
1
- /**
2
- * @Project: @cldmv/fix-headers
3
- * @Filename: /src/detectors/node.mjs
4
- * @Date: 2026-03-01T17:59:32-08:00 (1772416772)
5
- * @Author: Nate Corcoran <CLDMV>
6
- * @Email: <Shinrai@users.noreply.github.com>
7
- * -----
8
- * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
9
- * @Last modified time: 2026-03-01T17:59:32-08:00 (1772416772)
10
- * -----
11
- * @Copyright: Copyright (c) 2026-2026 Catalyzed Motivation Inc. All rights reserved.
12
- */
13
-
14
- import { extname } from "node:path";
15
- import { findNearestMarker } from "./shared.mjs";
16
-
17
- /**
18
- * @fileoverview Node.js detector implementation.
19
- * @module fix-headers/detectors/node
20
- */
21
-
22
- const markers = ["package.json"];
23
- const extensions = [".js", ".mjs", ".cjs", ".ts", ".tsx", ".jsx"];
24
-
25
- /**
26
- * Parses Node project name from package marker content.
27
- * @param {string} markerContent - Marker file content.
28
- * @param {string} rootDirName - Fallback root directory name.
29
- * @returns {string} Project name.
30
- */
31
- function parseNodeProjectName(markerContent, rootDirName) {
32
- try {
33
- const parsed = JSON.parse(markerContent);
34
- if (typeof parsed.name === "string" && parsed.name.trim().length > 0) {
35
- return parsed.name.trim();
36
- }
37
- return rootDirName;
38
- } catch {
39
- return rootDirName;
40
- }
41
- }
42
-
43
- /**
44
- * Resolves comment syntax for Node-handled file extensions.
45
- * @param {string} filePath - File path.
46
- * @returns {{kind: "block", blockStart: string, blockLinePrefix: string, blockEnd: string} | null} Syntax descriptor.
47
- */
48
- function resolveNodeCommentSyntax(filePath) {
49
- const extension = extname(filePath).toLowerCase();
50
- if (extensions.includes(extension)) {
51
- return {
52
- kind: "block",
53
- blockStart: "/**",
54
- blockLinePrefix: " *\t",
55
- blockEnd: " */"
56
- };
57
- }
58
- return null;
59
- }
60
-
61
- /**
62
- * Resolves preserved leading prefix for Node files (for example: shebang line).
63
- * @param {string} _filePath - File path.
64
- * @param {string} content - File content.
65
- * @returns {string} Preserved prefix.
66
- */
67
- function resolveNodePreservedPrefix(_filePath, content) {
68
- const shebangMatch = content.match(/^#!.*\b(node|bun|deno|tsx|ts-node)\b.*(?:\r?\n|$)/);
69
- return shebangMatch ? shebangMatch[0] : "";
70
- }
71
-
72
- export const detector = {
73
- id: "node",
74
- priority: 100,
75
- markers,
76
- extensions,
77
- enabledByDefault: true,
78
- findNearestConfig(startPath) {
79
- return findNearestMarker(startPath, markers);
80
- },
81
- parseProjectName(_marker, markerContent, rootDirName) {
82
- return parseNodeProjectName(markerContent, rootDirName);
83
- },
84
- resolvePreservedPrefix(filePath, content) {
85
- return resolveNodePreservedPrefix(filePath, content);
86
- },
87
- resolveCommentSyntax(filePath) {
88
- return resolveNodeCommentSyntax(filePath);
89
- }
90
- };
@@ -1,66 +0,0 @@
1
- /**
2
- * @Project: @cldmv/fix-headers
3
- * @Filename: /src/detectors/php.mjs
4
- * @Date: 2026-03-01T17:59:32-08:00 (1772416772)
5
- * @Author: Nate Corcoran <CLDMV>
6
- * @Email: <Shinrai@users.noreply.github.com>
7
- * -----
8
- * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
9
- * @Last modified time: 2026-03-01T17:59:32-08:00 (1772416772)
10
- * -----
11
- * @Copyright: Copyright (c) 2026-2026 Catalyzed Motivation Inc. All rights reserved.
12
- */
13
-
14
- import { extname } from "node:path";
15
- import { findNearestMarker } from "./shared.mjs";
16
-
17
- /**
18
- * @fileoverview PHP detector implementation.
19
- * @module fix-headers/detectors/php
20
- */
21
-
22
- const markers = ["composer.json"];
23
- const extensions = [".php"];
24
-
25
- /**
26
- * Resolves PHP comment syntax.
27
- * @param {string} filePath - File path.
28
- * @returns {{kind: "block", blockStart: string, blockLinePrefix: string, blockEnd: string} | null} Syntax descriptor.
29
- */
30
- function resolvePhpCommentSyntax(filePath) {
31
- const extension = extname(filePath).toLowerCase();
32
- if (extensions.includes(extension)) {
33
- return {
34
- kind: "block",
35
- blockStart: "/**",
36
- blockLinePrefix: " *\t",
37
- blockEnd: " */"
38
- };
39
- }
40
- return null;
41
- }
42
-
43
- export const detector = {
44
- id: "php",
45
- priority: 80,
46
- markers,
47
- extensions,
48
- enabledByDefault: true,
49
- findNearestConfig(startPath) {
50
- return findNearestMarker(startPath, markers);
51
- },
52
- parseProjectName(_marker, markerContent, rootDirName) {
53
- try {
54
- const parsed = JSON.parse(markerContent);
55
- if (typeof parsed.name === "string" && parsed.name.trim().length > 0) {
56
- return parsed.name.trim();
57
- }
58
- return rootDirName;
59
- } catch {
60
- return rootDirName;
61
- }
62
- },
63
- resolveCommentSyntax(filePath) {
64
- return resolvePhpCommentSyntax(filePath);
65
- }
66
- };
@@ -1,75 +0,0 @@
1
- /**
2
- * @Project: @cldmv/fix-headers
3
- * @Filename: /src/detectors/python.mjs
4
- * @Date: 2026-03-01T17:59:32-08:00 (1772416772)
5
- * @Author: Nate Corcoran <CLDMV>
6
- * @Email: <Shinrai@users.noreply.github.com>
7
- * -----
8
- * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
9
- * @Last modified time: 2026-03-01T17:59:32-08:00 (1772416772)
10
- * -----
11
- * @Copyright: Copyright (c) 2026-2026 Catalyzed Motivation Inc. All rights reserved.
12
- */
13
-
14
- import { extname } from "node:path";
15
- import { findNearestMarker } from "./shared.mjs";
16
-
17
- /**
18
- * @fileoverview Python detector implementation.
19
- * @module fix-headers/detectors/python
20
- */
21
-
22
- const markers = ["pyproject.toml", "setup.py", "requirements.txt"];
23
- const extensions = [".py"];
24
-
25
- /**
26
- * Parses Python project name from pyproject marker content when possible.
27
- * @param {string} markerContent - Marker content.
28
- * @param {string} rootDirName - Fallback root directory name.
29
- * @returns {string} Project name.
30
- */
31
- function parsePythonProjectName(markerContent, rootDirName) {
32
- const pyprojectMatch = markerContent.match(/^name\s*=\s*["']([^"']+)["']/m);
33
- if (pyprojectMatch?.[1]) {
34
- return pyprojectMatch[1];
35
- }
36
- return rootDirName;
37
- }
38
-
39
- /**
40
- * Resolves preserved leading prefix for Python files (for example: shebang line).
41
- * @param {string} _filePath - File path.
42
- * @param {string} content - File content.
43
- * @returns {string} Preserved prefix.
44
- */
45
- function resolvePythonPreservedPrefix(_filePath, content) {
46
- const shebangMatch = content.match(/^#!.*\bpython(?:\d+(?:\.\d+)*)?\b.*(?:\r?\n|$)/);
47
- return shebangMatch ? shebangMatch[0] : "";
48
- }
49
-
50
- export const detector = {
51
- id: "python",
52
- priority: 80,
53
- markers,
54
- extensions,
55
- enabledByDefault: true,
56
- findNearestConfig(startPath) {
57
- return findNearestMarker(startPath, markers);
58
- },
59
- parseProjectName(_marker, markerContent, rootDirName) {
60
- return parsePythonProjectName(markerContent, rootDirName);
61
- },
62
- resolvePreservedPrefix(filePath, content) {
63
- return resolvePythonPreservedPrefix(filePath, content);
64
- },
65
- resolveCommentSyntax(filePath) {
66
- const extension = extname(filePath).toLowerCase();
67
- if (extensions.includes(extension)) {
68
- return {
69
- kind: "line",
70
- linePrefix: "#"
71
- };
72
- }
73
- return null;
74
- }
75
- };
@@ -1,62 +0,0 @@
1
- /**
2
- * @Project: @cldmv/fix-headers
3
- * @Filename: /src/detectors/rust.mjs
4
- * @Date: 2026-03-01T17:59:32-08:00 (1772416772)
5
- * @Author: Nate Corcoran <CLDMV>
6
- * @Email: <Shinrai@users.noreply.github.com>
7
- * -----
8
- * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
9
- * @Last modified time: 2026-03-01T17:59:32-08:00 (1772416772)
10
- * -----
11
- * @Copyright: Copyright (c) 2026-2026 Catalyzed Motivation Inc. All rights reserved.
12
- */
13
-
14
- import { extname } from "node:path";
15
- import { findNearestMarker } from "./shared.mjs";
16
-
17
- /**
18
- * @fileoverview Rust detector implementation.
19
- * @module fix-headers/detectors/rust
20
- */
21
-
22
- const markers = ["Cargo.toml"];
23
- const extensions = [".rs"];
24
-
25
- /**
26
- * Resolves Rust comment syntax.
27
- * @param {string} filePath - File path.
28
- * @returns {{kind: "block", blockStart: string, blockLinePrefix: string, blockEnd: string} | null} Syntax descriptor.
29
- */
30
- function resolveRustCommentSyntax(filePath) {
31
- const extension = extname(filePath).toLowerCase();
32
- if (extensions.includes(extension)) {
33
- return {
34
- kind: "block",
35
- blockStart: "/**",
36
- blockLinePrefix: " *\t",
37
- blockEnd: " */"
38
- };
39
- }
40
- return null;
41
- }
42
-
43
- export const detector = {
44
- id: "rust",
45
- priority: 80,
46
- markers,
47
- extensions,
48
- enabledByDefault: true,
49
- findNearestConfig(startPath) {
50
- return findNearestMarker(startPath, markers);
51
- },
52
- parseProjectName(_marker, markerContent, rootDirName) {
53
- const cargoNameMatch = markerContent.match(/^name\s*=\s*["']([^"']+)["']/m);
54
- if (cargoNameMatch?.[1]) {
55
- return cargoNameMatch[1];
56
- }
57
- return rootDirName;
58
- },
59
- resolveCommentSyntax(filePath) {
60
- return resolveRustCommentSyntax(filePath);
61
- }
62
- };
@@ -1,45 +0,0 @@
1
- /**
2
- * @Project: @cldmv/fix-headers
3
- * @Filename: /src/detectors/shared.mjs
4
- * @Date: 2026-03-01T17:59:32-08:00 (1772416772)
5
- * @Author: Nate Corcoran <CLDMV>
6
- * @Email: <Shinrai@users.noreply.github.com>
7
- * -----
8
- * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
9
- * @Last modified time: 2026-03-01T17:59:32-08:00 (1772416772)
10
- * -----
11
- * @Copyright: Copyright (c) 2026-2026 Catalyzed Motivation Inc. All rights reserved.
12
- */
13
-
14
- import { dirname, join, resolve } from "node:path";
15
- import { pathExists } from "../utils/fs.mjs";
16
-
17
- /**
18
- * @fileoverview Shared detector helpers for nearest marker search.
19
- * @module fix-headers/detectors/shared
20
- */
21
-
22
- /**
23
- * Finds the closest marker file by walking up parent directories.
24
- * @param {string} startPath - Starting directory or file path.
25
- * @param {string[]} markers - Marker filenames to search.
26
- * @returns {Promise<{root: string, marker: string} | null>} Closest located marker.
27
- */
28
- export async function findNearestMarker(startPath, markers) {
29
- let currentDir = resolve(startPath);
30
-
31
- while (true) {
32
- for (const marker of markers) {
33
- const markerPath = join(currentDir, marker);
34
- if (await pathExists(markerPath)) {
35
- return { root: currentDir, marker };
36
- }
37
- }
38
-
39
- const parent = dirname(currentDir);
40
- if (parent === currentDir) {
41
- return null;
42
- }
43
- currentDir = parent;
44
- }
45
- }
@@ -1,79 +0,0 @@
1
- /**
2
- * @Project: @cldmv/fix-headers
3
- * @Filename: /src/detectors/yaml.mjs
4
- * @Date: 2026-03-01 18:28:31 -08:00 (1772418511)
5
- * @Author: Nate Corcoran <CLDMV>
6
- * @Email: <Shinrai@users.noreply.github.com>
7
- * -----
8
- * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
9
- * @Last modified time: 2026-03-01 19:32:27 -08:00 (1772422347)
10
- * -----
11
- * @Copyright: Copyright (c) 2026-2026 Catalyzed Motivation Inc. All rights reserved.
12
- */
13
-
14
- import { extname } from "node:path";
15
- import { findNearestMarker } from "./shared.mjs";
16
-
17
- /**
18
- * @fileoverview YAML detector implementation.
19
- * @module fix-headers/detectors/yaml
20
- */
21
-
22
- const markers = ["package.json", ".git"];
23
- const extensions = [".yaml", ".yml"];
24
-
25
- /**
26
- * Parses YAML project name from nearest marker content when available.
27
- * @param {string} marker - Marker filename.
28
- * @param {string} markerContent - Marker content.
29
- * @param {string} rootDirName - Fallback root directory name.
30
- * @returns {string} Project name.
31
- */
32
- function parseYamlProjectName(marker, markerContent, rootDirName) {
33
- if (marker !== "package.json") {
34
- return rootDirName;
35
- }
36
-
37
- try {
38
- const parsed = JSON.parse(markerContent);
39
- if (typeof parsed.name === "string" && parsed.name.trim().length > 0) {
40
- return parsed.name.trim();
41
- }
42
- return rootDirName;
43
- } catch {
44
- return rootDirName;
45
- }
46
- }
47
-
48
- /**
49
- * Resolves comment syntax for YAML file extensions.
50
- * @param {string} filePath - File path.
51
- * @returns {{kind: "line", linePrefix: string} | null} Syntax descriptor.
52
- */
53
- function resolveYamlCommentSyntax(filePath) {
54
- const extension = extname(filePath).toLowerCase();
55
- if (extensions.includes(extension)) {
56
- return {
57
- kind: "line",
58
- linePrefix: "#"
59
- };
60
- }
61
- return null;
62
- }
63
-
64
- export const detector = {
65
- id: "yaml",
66
- priority: 60,
67
- markers,
68
- extensions,
69
- enabledByDefault: true,
70
- findNearestConfig(startPath) {
71
- return findNearestMarker(startPath, markers);
72
- },
73
- parseProjectName(marker, markerContent, rootDirName) {
74
- return parseYamlProjectName(marker, markerContent, rootDirName);
75
- },
76
- resolveCommentSyntax(filePath) {
77
- return resolveYamlCommentSyntax(filePath);
78
- }
79
- };
@@ -1,30 +0,0 @@
1
- /**
2
- * @Project: @cldmv/fix-headers
3
- * @Filename: /src/fix-header.mjs
4
- * @Date: 2026-03-01 13:34:00 -08:00 (1772400840)
5
- * @Author: Nate Corcoran <CLDMV>
6
- * @Email: <Shinrai@users.noreply.github.com>
7
- * -----
8
- * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
9
- * @Last modified time: 2026-03-01T17:59:32-08:00 (1772416772)
10
- * -----
11
- * @Copyright: Copyright (c) 2026-2026 Catalyzed Motivation Inc. All rights reserved.
12
- */
13
-
14
- import { fixHeaders as fixHeadersCore } from "./core/fix-headers.mjs";
15
-
16
- /**
17
- * @fileoverview Public module API exposing a single function for header updates.
18
- * @module fix-headers
19
- */
20
-
21
- /**
22
- * Runs header fix-up using automatic metadata detection and optional overrides.
23
- * @param {Parameters<typeof fixHeadersCore>[0]} [options] - Runtime options.
24
- * @returns {ReturnType<typeof fixHeadersCore>} Update report.
25
- */
26
- export function fixHeaders(options) {
27
- return fixHeadersCore(options);
28
- }
29
-
30
- export default fixHeaders;
@@ -1,145 +0,0 @@
1
- /**
2
- * @Project: @cldmv/fix-headers
3
- * @Filename: /src/header/parser.mjs
4
- * @Date: 2026-03-01T17:59:32-08:00 (1772416772)
5
- * @Author: Nate Corcoran <CLDMV>
6
- * @Email: <Shinrai@users.noreply.github.com>
7
- * -----
8
- * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
9
- * @Last modified time: 2026-03-01T17:59:32-08:00 (1772416772)
10
- * -----
11
- * @Copyright: Copyright (c) 2026-2026 Catalyzed Motivation Inc. All rights reserved.
12
- */
13
-
14
- import { DEFAULT_MAX_HEADER_SCAN_LINES } from "../constants.mjs";
15
- import { getPreservedPrefixForFile } from "../detectors/index.mjs";
16
- import { getHeaderSyntaxForFile } from "./syntax.mjs";
17
-
18
- /**
19
- * @fileoverview Header parser utilities for extracting and replacing top-of-file headers.
20
- * @module fix-headers/header/parser
21
- */
22
-
23
- /**
24
- * Escapes special regex characters in literal text.
25
- * @param {string} text - Literal text.
26
- * @returns {string} Regex-safe text.
27
- */
28
- function escapeRegex(text) {
29
- return text.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
30
- }
31
-
32
- /**
33
- * Splits detector-defined preserved prefix from file content when present.
34
- * @param {string} filePath - File path used for detector selection.
35
- * @param {string} content - File content.
36
- * @param {{ language?: string, enabledDetectors?: string[], disabledDetectors?: string[], detectorSyntaxOverrides?: Record<string, { linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string }> }} [syntaxOptions={}] - Syntax/detector options.
37
- * @returns {{prefix: string, body: string}} Shebang prefix and remaining body.
38
- */
39
- function splitPreservedPrefix(filePath, content, syntaxOptions = {}) {
40
- const prefix = getPreservedPrefixForFile(filePath, content, syntaxOptions);
41
- if (prefix.length === 0) {
42
- return { prefix: "", body: content };
43
- }
44
-
45
- return {
46
- prefix,
47
- body: content.slice(prefix.length)
48
- };
49
- }
50
-
51
- /**
52
- * Matches a top-of-content header block for a given syntax.
53
- * @param {string} content - Content slice to match from start.
54
- * @param {{kind: "block" | "line" | "html", linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string}} syntax - Header syntax descriptor.
55
- * @returns {RegExpMatchArray | null} Matched header segment.
56
- */
57
- function matchHeaderSegment(content, syntax) {
58
- if (syntax.kind === "html") {
59
- const blockStart = escapeRegex(syntax.blockStart || "<!--");
60
- const blockEnd = escapeRegex(syntax.blockEnd || "-->");
61
- return content.match(new RegExp(`^${blockStart}[\\s\\S]*?${blockEnd}\\n*`));
62
- }
63
-
64
- if (syntax.kind === "line") {
65
- const linePrefix = escapeRegex(syntax.linePrefix || "#");
66
- return content.match(new RegExp(`^(?:${linePrefix}.*\\n)+\\n*`));
67
- }
68
-
69
- // Block comments (/* … */ family). A block comment opens with `/*` and is
70
- // terminated by the FIRST `*/` after the opener (block comments do not nest in
71
- // JS/JSONC/CSS/PHP). Match that real grammar rather than the detector's *styled*
72
- // tokens: `blockStart` may be `/*` or `/**` (so anchor on `/*`, which prefixes
73
- // both), and `blockEnd` carries a leading space (" */") that exists for GENERATION
74
- // only. Using the styled " */" to bound an EXISTING header is the data-loss bug —
75
- // any real closer that isn't byte-for-byte " */" (e.g. `**/`, a tab-indented `*/`,
76
- // or `*/` at column 0) is skipped, so the lazy match runs on to the next `*/`
77
- // downstream (inside a `//` line comment, a string literal, or another block) and
78
- // everything in between is deleted on replace. Trim the styled space off so we stop
79
- // at the header's own terminator.
80
- const blockEnd = escapeRegex((syntax.blockEnd || " */").trim());
81
- return content.match(new RegExp(`^/\\*[\\s\\S]*?${blockEnd}\\n*`));
82
- }
83
-
84
- /**
85
- * Finds the first top-level project header block in a file.
86
- * @param {string} content - File content.
87
- * @param {string} [filePath=""] - File path used for syntax selection.
88
- * @param {{ language?: string, enabledDetectors?: string[], disabledDetectors?: string[], detectorSyntaxOverrides?: Record<string, { linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string }> }} [syntaxOptions={}] - Syntax resolution options.
89
- * @returns {{start: number, end: number} | null} Header location.
90
- */
91
- export function findProjectHeader(content, filePath = "", syntaxOptions = {}) {
92
- const { prefix, body } = splitPreservedPrefix(filePath, content, syntaxOptions);
93
- const scanLines = body.split("\n").slice(0, DEFAULT_MAX_HEADER_SCAN_LINES).join("\n");
94
- const syntax = getHeaderSyntaxForFile(filePath, syntaxOptions);
95
- const match = matchHeaderSegment(scanLines, syntax);
96
-
97
- if (!match) {
98
- return null;
99
- }
100
-
101
- let offset = 0;
102
- let end = 0;
103
- while (true) {
104
- const remaining = scanLines.slice(offset);
105
- const segment = matchHeaderSegment(remaining, syntax);
106
-
107
- if (!segment || !segment[0].includes("@Project:")) {
108
- break;
109
- }
110
-
111
- offset += segment[0].length;
112
- end = offset;
113
- }
114
-
115
- if (end === 0) {
116
- return null;
117
- }
118
-
119
- return {
120
- start: prefix.length,
121
- end: prefix.length + end
122
- };
123
- }
124
-
125
- /**
126
- * Replaces or inserts a project header block.
127
- * @param {string} content - Original file content.
128
- * @param {string} newHeader - Generated header text.
129
- * @param {string} [filePath=""] - File path used for syntax selection.
130
- * @param {{ language?: string, enabledDetectors?: string[], disabledDetectors?: string[], detectorSyntaxOverrides?: Record<string, { linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string }> }} [syntaxOptions={}] - Syntax resolution options.
131
- * @returns {{nextContent: string, changed: boolean}} Updated content result.
132
- */
133
- export function replaceOrInsertHeader(content, newHeader, filePath = "", syntaxOptions = {}) {
134
- const existing = findProjectHeader(content, filePath, syntaxOptions);
135
- if (!existing) {
136
- const { prefix, body } = splitPreservedPrefix(filePath, content, syntaxOptions);
137
- const nextContent = `${prefix}${newHeader}\n\n${body.replace(/^\n+/, "")}`;
138
- return { nextContent, changed: nextContent !== content };
139
- }
140
-
141
- const before = content.slice(0, existing.start);
142
- const after = content.slice(existing.end).replace(/^\n+/, "");
143
- const nextContent = `${before}${newHeader}\n\n${after}`;
144
- return { nextContent, changed: nextContent !== content };
145
- }
@@ -1,69 +0,0 @@
1
- /**
2
- * @Project: @cldmv/fix-headers
3
- * @Filename: /src/header/syntax.mjs
4
- * @Date: 2026-03-01T17:59:32-08:00 (1772416772)
5
- * @Author: Nate Corcoran <CLDMV>
6
- * @Email: <Shinrai@users.noreply.github.com>
7
- * -----
8
- * @Last modified by: Nate Corcoran <CLDMV> (Shinrai@users.noreply.github.com)
9
- * @Last modified time: 2026-03-01T17:59:32-08:00 (1772416772)
10
- * -----
11
- * @Copyright: Copyright (c) 2026-2026 Catalyzed Motivation Inc. All rights reserved.
12
- */
13
-
14
- import { extname } from "node:path";
15
- import { getCommentSyntaxForFile } from "../detectors/index.mjs";
16
-
17
- /**
18
- * @fileoverview Header comment syntax helpers for mapping file extensions to comment styles.
19
- * @module fix-headers/header/syntax
20
- */
21
-
22
- /**
23
- * @typedef {{
24
- * kind: "block" | "line" | "html",
25
- * linePrefix?: string,
26
- * lineSeparator?: string,
27
- * blockStart?: string,
28
- * blockLinePrefix?: string,
29
- * blockEnd?: string
30
- * }} HeaderSyntax
31
- */
32
-
33
- /**
34
- * Resolves comment syntax for a file path based on extension.
35
- * @param {string} filePath - Absolute or relative file path.
36
- * @param {{ language?: string, enabledDetectors?: string[], disabledDetectors?: string[], detectorSyntaxOverrides?: Record<string, { linePrefix?: string, lineSeparator?: string, blockStart?: string, blockLinePrefix?: string, blockEnd?: string }> }} [options={}] - Syntax resolution options.
37
- * @returns {HeaderSyntax} Header syntax descriptor.
38
- */
39
- export function getHeaderSyntaxForFile(filePath, options = {}) {
40
- const extension = extname(filePath).toLowerCase();
41
- if (extension.length === 0) {
42
- return { kind: "block", blockStart: "/**", blockLinePrefix: " *\t", blockEnd: " */" };
43
- }
44
-
45
- return getCommentSyntaxForFile(filePath, options);
46
- }
47
-
48
- /**
49
- * Renders header body lines using a chosen syntax.
50
- * @param {HeaderSyntax} syntax - Header syntax descriptor.
51
- * @param {string[]} lines - Header lines without comment wrappers.
52
- * @returns {string} Formatted header block.
53
- */
54
- export function renderHeaderLines(syntax, lines) {
55
- if (syntax.kind === "line") {
56
- const lineSeparator = typeof syntax.lineSeparator === "string" ? syntax.lineSeparator : "\t";
57
- return lines.map((line) => `${syntax.linePrefix || "#"}${lineSeparator}${line}`).join("\n");
58
- }
59
-
60
- const blockStart = syntax.blockStart || (syntax.kind === "html" ? "<!--" : "/**");
61
- const blockLinePrefix = syntax.blockLinePrefix || (syntax.kind === "html" ? "\t" : " *\t");
62
- const blockEnd = syntax.blockEnd || (syntax.kind === "html" ? "-->" : " */");
63
-
64
- if (syntax.kind === "html") {
65
- return `${blockStart}\n${lines.map((line) => `${blockLinePrefix}${line}`).join("\n")}\n${blockEnd}`;
66
- }
67
-
68
- return `${blockStart}\n${lines.map((line) => `${blockLinePrefix}${line}`).join("\n")}\n${blockEnd}`;
69
- }