Skip to content

Commit 082071a

Browse files
committed
Comment out lines wrapped in inline control-flow expressions
A line like '{{ if eq .Values.favorite.drink "coffee" }}mug: "true"{{ end }}' previously took the inline-filler path, producing trailing filler after a closed quote — a parse error. The wrapped content is conditional and cannot be validated as always-present, so the whole line is commented out, same as standalone control-flow lines. Pass 1 now marks control-flow spans with a distinct sentinel so pass 2 can classify them.
1 parent 5211458 commit 082071a

2 files changed

Lines changed: 57 additions & 7 deletions

File tree

‎src/languageservice/parser/templateMasking.ts‎

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,10 @@
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. */
3741
const 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. */
4046
const 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*(if|else|end|range|with|define|block|template)\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');

‎test/templateMasking.test.ts‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,35 @@ describe('Template masking', () => {
8282
assert.strictEqual(masked, 'name: "xxxxxxxxxxxxxxxxxx"');
8383
});
8484

85+
it('should comment out a line wrapped in inline control flow', () => {
86+
const line = ' {{ if eq .Values.favorite.drink "coffee" }}mug: "true"{{ end }}';
87+
const masked = maskTemplates(`data:\n${line}\n food: pie`);
88+
const middle = masked.split('\n')[1];
89+
assert.strictEqual(middle, ' #' + ' '.repeat(line.length - 3));
90+
assert.strictEqual(middle.length, line.length);
91+
});
92+
93+
it('should keep an inline non-control expression before content as filler', () => {
94+
const masked = maskTemplates('{{ .Values.prefix }}name: bar');
95+
assert.strictEqual(masked, 'xxxxxxxxxxxxxxxxxxxxname: bar');
96+
});
97+
98+
it('should produce no parse errors on a ConfigMap with inline control flow', () => {
99+
const chart = [
100+
'apiVersion: v1',
101+
'kind: ConfigMap',
102+
'metadata:',
103+
' name: {{ .Release.Name }}-configmap',
104+
'data:',
105+
' myvalue: "Hello World"',
106+
' drink: {{ .Values.favorite.drink | default "tea" | quote }}',
107+
' food: {{ .Values.favorite.food | upper | quote }}',
108+
' {{ if eq .Values.favorite.drink "coffee" }}mug: "true"{{ end }}',
109+
'',
110+
].join('\n');
111+
assert.strictEqual(errorCount(chart, 'helm'), 0);
112+
});
113+
85114
it('should produce parse errors on a Helm chart when disabled', () => {
86115
assert.ok(errorCount(HELM_CHART, 'none') > 0);
87116
});

0 commit comments

Comments
 (0)