@shepherdjerred/helm-types 0.0.0-dev.706

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.
@@ -0,0 +1,103 @@
1
+ import { isYAMLKey } from "./yaml-comments.ts";
2
+
3
+ /**
4
+ * Helper: Check if a line is part of a commented YAML block
5
+ */
6
+ function isPartOfYAMLBlock(line: string, trimmed: string): boolean {
7
+ return (
8
+ line.startsWith(" ") ||
9
+ line.startsWith("\t") ||
10
+ trimmed.startsWith("-") ||
11
+ trimmed.startsWith("#")
12
+ );
13
+ }
14
+
15
+ /**
16
+ * Helper: Check if this is a section header followed by a YAML key
17
+ */
18
+ function isSectionHeaderForCommentedBlock(
19
+ nextLine: string | undefined,
20
+ ): boolean {
21
+ if (nextLine == null || nextLine === "") {
22
+ return false;
23
+ }
24
+ const nextTrimmed = nextLine.trim();
25
+ return /^[\w.-]+:\s*(?:\||$)/.test(nextTrimmed);
26
+ }
27
+
28
+ /**
29
+ * Helper: Check if line indicates start of real documentation
30
+ */
31
+ function isRealDocumentation(trimmed: string): boolean {
32
+ return trimmed.startsWith("--") || trimmed.startsWith("#");
33
+ }
34
+
35
+ /**
36
+ * Filter out commented-out YAML blocks from the START of a comment string
37
+ * The YAML AST gives us ALL comments, including commented-out config sections
38
+ * We only remove these if they appear BEFORE the real documentation starts
39
+ */
40
+ export function filterCommentedOutYAML(comment: string): string {
41
+ const lines = comment.split("\n");
42
+ let startIndex = 0;
43
+ let inCommentedBlock = false;
44
+ let hasSeenRealDoc = false;
45
+
46
+ // First pass: find where real documentation starts
47
+ for (let i = 0; i < lines.length; i++) {
48
+ const line = lines[i] ?? "";
49
+ const trimmed = line.trim();
50
+
51
+ // Blank line could be end of commented block
52
+ if (trimmed === "") {
53
+ if (inCommentedBlock) {
54
+ inCommentedBlock = false;
55
+ }
56
+ continue;
57
+ }
58
+
59
+ // Check if this looks like a commented-out YAML key
60
+ const looksLikeYAMLKeyLocal = isYAMLKey(trimmed);
61
+
62
+ if (looksLikeYAMLKeyLocal && !hasSeenRealDoc) {
63
+ // This starts a commented-out YAML block
64
+ inCommentedBlock = true;
65
+ continue;
66
+ }
67
+
68
+ // If we're in a commented block, check if this line is part of it
69
+ if (inCommentedBlock) {
70
+ if (isPartOfYAMLBlock(line, trimmed)) {
71
+ continue;
72
+ } else {
73
+ // This line doesn't look like YAML content, we're out of the block
74
+ inCommentedBlock = false;
75
+ hasSeenRealDoc = true;
76
+ startIndex = i;
77
+ }
78
+ } else if (!hasSeenRealDoc) {
79
+ const nextLine = lines[i + 1];
80
+
81
+ // Check if this is a section header for a commented-out block
82
+ if (isSectionHeaderForCommentedBlock(nextLine)) {
83
+ continue;
84
+ }
85
+
86
+ // Check if this line indicates real documentation
87
+ if (isRealDocumentation(trimmed)) {
88
+ hasSeenRealDoc = true;
89
+ startIndex = i;
90
+ } else {
91
+ // First real prose line
92
+ hasSeenRealDoc = true;
93
+ startIndex = i;
94
+ }
95
+ }
96
+ }
97
+
98
+ // Return everything from where real documentation starts
99
+ return lines
100
+ .slice(startIndex)
101
+ .map((l) => l.trim())
102
+ .join("\n");
103
+ }
@@ -0,0 +1,150 @@
1
+ import type { CommentWithMetadata } from "./yaml-comments.ts";
2
+
3
+ /**
4
+ * Regex-based fallback parser for comments that the YAML AST loses
5
+ * This handles cases with commented-out YAML keys and inconsistent indentation
6
+ * Returns comments with metadata for debugging
7
+ */
8
+ export function parseCommentsWithRegex(
9
+ yamlContent: string,
10
+ ): Map<string, CommentWithMetadata> {
11
+ const comments = new Map<string, CommentWithMetadata>();
12
+ const lines = yamlContent.split("\n");
13
+ let pendingComment: string[] = [];
14
+ let pendingCommentIndent = -1;
15
+ let pendingDebugInfo: string[] = [];
16
+
17
+ lines.forEach((line, lineNum) => {
18
+ const trimmed = line.trim();
19
+
20
+ // Skip empty lines - blank lines reset pending comments
21
+ // This ensures comments for commented-out keys don't leak to next real key
22
+ if (!trimmed) {
23
+ // Only reset if we had a commented-out key (pendingCommentIndent === -1)
24
+ // This allows multi-line comments for real keys to work
25
+ if (pendingComment.length > 0 && pendingCommentIndent === -1) {
26
+ pendingDebugInfo.push(
27
+ `Line ${String(lineNum)}: Blank line after commented-out key, resetting pending`,
28
+ );
29
+ pendingComment = [];
30
+ pendingDebugInfo = [];
31
+ }
32
+ return;
33
+ }
34
+
35
+ // Check if this is a comment line
36
+ if (trimmed.startsWith("#")) {
37
+ // Extract comment text (handle multiple # characters)
38
+ let commentText = trimmed;
39
+ while (commentText.startsWith("#")) {
40
+ commentText = commentText.slice(1);
41
+ }
42
+ commentText = commentText.trim();
43
+
44
+ // Skip commented-out YAML keys (these are not documentation)
45
+ // Match patterns like "key: value" or "key:" but NOT prose-like text
46
+ // This includes quoted values like: key: "value"
47
+ const looksLikeYAMLKeyLocal =
48
+ /^[\w.-]+:\s*(?:\||$|[\w.-]+$|"[^"]*"$|'[^']*'$|[[{])/.test(
49
+ commentText,
50
+ );
51
+
52
+ if (looksLikeYAMLKeyLocal) {
53
+ // This is a commented-out YAML key, which means the pending comments
54
+ // were describing this commented-out section, not a future real key
55
+ // So we should discard them
56
+ pendingDebugInfo.push(
57
+ `Line ${String(lineNum)}: Commented-out YAML key detected: "${commentText}", discarding pending`,
58
+ );
59
+ pendingComment = [];
60
+ pendingCommentIndent = -1;
61
+ pendingDebugInfo = [];
62
+ } else {
63
+ const commentIndent = line.search(/\S/);
64
+
65
+ // If we just discarded comments (indent === -1), this is a fresh start
66
+ if (pendingCommentIndent === -1) {
67
+ pendingComment = [commentText];
68
+ pendingCommentIndent = commentIndent;
69
+ pendingDebugInfo = [
70
+ `Line ${String(lineNum)}: Fresh start (indent=${String(commentIndent)}): "${commentText}"`,
71
+ ];
72
+ } else if (
73
+ commentIndent === pendingCommentIndent ||
74
+ Math.abs(commentIndent - pendingCommentIndent) <= 2
75
+ ) {
76
+ // Same indent level, add to pending
77
+ pendingComment.push(commentText);
78
+ pendingDebugInfo.push(
79
+ `Line ${String(lineNum)}: Continuing (indent=${String(commentIndent)}): "${commentText}"`,
80
+ );
81
+ } else {
82
+ // Different indent, reset
83
+ pendingDebugInfo.push(
84
+ `Line ${String(lineNum)}: Different indent (${String(commentIndent)} vs ${String(pendingCommentIndent)}), resetting`,
85
+ );
86
+ pendingComment = [commentText];
87
+ pendingCommentIndent = commentIndent;
88
+ pendingDebugInfo.push(
89
+ `Line ${String(lineNum)}: New start: "${commentText}"`,
90
+ );
91
+ }
92
+ }
93
+ return;
94
+ }
95
+
96
+ // Check if this is a YAML key line
97
+ const keyMatchRegex = /^([\w.-]+):\s/;
98
+ const keyMatch = keyMatchRegex.exec(trimmed);
99
+ if (keyMatch && pendingComment.length > 0) {
100
+ const key = keyMatch[1];
101
+ if (key != null && key !== "") {
102
+ const keyIndent = line.search(/\S/);
103
+
104
+ // Only associate comment if indentation matches closely
105
+ // Allow 2 space difference (for comment being indented slightly different)
106
+ if (
107
+ pendingCommentIndent === -1 ||
108
+ Math.abs(keyIndent - pendingCommentIndent) <= 2
109
+ ) {
110
+ const commentText = pendingComment.join("\n");
111
+ const debugInfo = [
112
+ ...pendingDebugInfo,
113
+ `Line ${String(lineNum)}: Associating with key "${key}"`,
114
+ ].join("\n");
115
+
116
+ comments.set(key, {
117
+ text: commentText,
118
+ metadata: {
119
+ source: "REGEX",
120
+ rawComment: commentText,
121
+ indent: keyIndent,
122
+ debugInfo,
123
+ },
124
+ });
125
+ } else {
126
+ pendingDebugInfo.push(
127
+ `Line ${String(lineNum)}: Skipping key "${key}" due to indent mismatch (${String(keyIndent)} vs ${String(pendingCommentIndent)})`,
128
+ );
129
+ }
130
+
131
+ // Reset for next key
132
+ pendingComment = [];
133
+ pendingCommentIndent = -1;
134
+ pendingDebugInfo = [];
135
+ }
136
+ } else if (!trimmed.startsWith("#")) {
137
+ // Non-comment, non-key line - reset pending comment
138
+ if (pendingComment.length > 0) {
139
+ pendingDebugInfo.push(
140
+ `Line ${String(lineNum)}: Non-comment/non-key line, resetting pending`,
141
+ );
142
+ }
143
+ pendingComment = [];
144
+ pendingCommentIndent = -1;
145
+ pendingDebugInfo = [];
146
+ }
147
+ });
148
+
149
+ return comments;
150
+ }