1616 * becomes a comment of identical length. Control-flow lines such as
1717 * `{{- if .Values.enabled }}` and `{{- end }}` drop out of the document
1818 * structure entirely.
19+ * - A line that starts with a control-flow expression but carries other
20+ * content (`{{ if ... }}mug: "true"{{ end }}`) is also commented out:
21+ * the content is conditional, so it cannot be validated as
22+ * always-present, and relocating it would break offset preservation.
1923 * - An inline expression after other content (`foo: {{ .Values.name }}`)
2024 * is replaced by an unquoted filler scalar of identical length, so the
2125 * value parses as a plain string.
@@ -36,8 +40,19 @@ const TEMPLATE_SPAN = /\{\{[\s\S]*?\}\}|\{\{[^\n]*$/gm;
3640/** Filler character for inline expressions. Parses as a plain scalar. */
3741const INLINE_FILL = 'x' ;
3842
39- /** Internal sentinel; NUL cannot appear in an LSP document's text. */
43+ /** Internal sentinels; these control characters cannot appear in an LSP
44+ * document's text. The control-flow sentinel marks spans like `{{ if }}`
45+ * so pass 2 can tell them apart from value expressions. */
4046const SENTINEL = '\u0000' ;
47+ const CONTROL_SENTINEL = '\u0001' ;
48+ // eslint-disable-next-line no-control-regex
49+ const ANY_SENTINEL = / [ \u0000 \u0001 ] / ;
50+ // eslint-disable-next-line no-control-regex
51+ const ANY_SENTINEL_GLOBAL = / [ \u0000 \u0001 ] / g;
52+
53+ /** Matches control-flow expressions (`{{ if }}`, `{{- end }}`, ...) that
54+ * affect document structure rather than producing a value. */
55+ const CONTROL_FLOW = / ^ \{ \{ - ? \s * ( i f | e l s e | e n d | r a n g e | w i t h | d e f i n e | b l o c k | t e m p l a t e ) \b / ;
4156
4257/**
4358 * Mask Go-template expressions in `text`, preserving length and line
@@ -51,23 +66,29 @@ export function maskTemplates(text: string): string {
5166
5267 // Pass 1: replace every character of every template span with a
5368 // sentinel, preserving newlines, so pass 2 can classify lines.
54- const masked = text . replace ( TEMPLATE_SPAN , ( m ) => m . replace ( / [ ^ \n ] / g, SENTINEL ) ) ;
69+ // Control-flow spans get a distinct sentinel.
70+ const masked = text . replace ( TEMPLATE_SPAN , ( m ) => m . replace ( / [ ^ \n ] / g, CONTROL_FLOW . test ( m ) ? CONTROL_SENTINEL : SENTINEL ) ) ;
5571
5672 // Pass 2: per line, decide comment-out vs inline filler.
5773 const lines = masked . split ( '\n' ) ;
5874 for ( let i = 0 ; i < lines . length ; i ++ ) {
5975 const line = lines [ i ] ;
60- if ( ! line . includes ( SENTINEL ) ) {
76+ if ( ! ANY_SENTINEL . test ( line ) ) {
6177 continue ;
6278 }
63- if ( line . split ( SENTINEL ) . join ( '' ) . trim ( ) . length === 0 ) {
64- // Template-only line: comment it out at the first non-space column,
65- // preserving both indentation and total length.
79+ const templateOnly = line . replace ( ANY_SENTINEL_GLOBAL , '' ) . trim ( ) . length === 0 ;
80+ // A line that starts with a control-flow expression but carries other
81+ // content ({{ if ... }}mug: "true"{{ end }}) holds conditional content
82+ // that cannot be validated as always-present; drop the whole line.
83+ const wrappedInControlFlow = line . trimStart ( ) . startsWith ( CONTROL_SENTINEL ) ;
84+ if ( templateOnly || wrappedInControlFlow ) {
85+ // Comment out at the first non-space column, preserving both
86+ // indentation and total length.
6687 const indent = line . match ( / ^ [ \t ] * / ) [ 0 ] . length ;
6788 lines [ i ] = line . slice ( 0 , indent ) + '#' + ' ' . repeat ( Math . max ( 0 , line . length - indent - 1 ) ) ;
6889 } else {
6990 // Inline expression: same-length filler scalar.
70- lines [ i ] = line . split ( SENTINEL ) . join ( INLINE_FILL ) ;
91+ lines [ i ] = line . replace ( ANY_SENTINEL_GLOBAL , INLINE_FILL ) ;
7192 }
7293 }
7394 return lines . join ( '\n' ) ;
0 commit comments