Skip to content

Commit 7669de2

Browse files
committed
Preserve comments during source file shrinking and update translation strings
- should not remove comments - outputs in test cases should not be stripped from comments - [Pasted text] - as we no longer strip comments, limit the text to 'strip function bodies'
1 parent 2f49adc commit 7669de2

17 files changed

Lines changed: 128 additions & 49 deletions

File tree

‎apps/editor/src/context/utils/shrink-file/languages/c-style.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,14 @@ export const shrink_c_style = (content: string): string => {
2121
const next_char = line[i + 1]
2222

2323
if (is_in_block_comment) {
24+
if (skip_body_depth == 0) {
25+
processed_line += char
26+
}
2427
if (char == '*' && next_char == '/') {
2528
is_in_block_comment = false
29+
if (skip_body_depth == 0) {
30+
processed_line += next_char
31+
}
2632
i += 2
2733
} else {
2834
i++
@@ -61,11 +67,17 @@ export const shrink_c_style = (content: string): string => {
6167
}
6268

6369
if (char == '/' && next_char == '/') {
70+
if (skip_body_depth == 0) {
71+
processed_line += line.substring(i)
72+
}
6473
break
6574
}
6675

6776
if (char == '/' && next_char == '*') {
6877
is_in_block_comment = true
78+
if (skip_body_depth == 0) {
79+
processed_line += '/*'
80+
}
6981
i += 2
7082
continue
7183
}

‎apps/editor/src/context/utils/shrink-file/languages/css.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ export const shrink_css = (content: string): string => {
1414
const next_char = line[i + 1]
1515

1616
if (is_in_block_comment) {
17+
processed_line += char
1718
if (char == '*' && next_char == '/') {
1819
is_in_block_comment = false
20+
processed_line += next_char
1921
i += 2
2022
} else {
2123
i++
@@ -46,11 +48,13 @@ export const shrink_css = (content: string): string => {
4648

4749
// Support // for SCSS/LESS, though not standard CSS
4850
if (char == '/' && next_char == '/') {
51+
processed_line += line.substring(i)
4952
break
5053
}
5154

5255
if (char == '/' && next_char == '*') {
5356
is_in_block_comment = true
57+
processed_line += '/*'
5458
i += 2
5559
continue
5660
}

‎apps/editor/src/context/utils/shrink-file/languages/html.ts‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,7 @@ export const shrink_html = (content: string): string => {
2424
}
2525
)
2626

27-
// 3. Strip HTML Comments
28-
processed = processed.replace(/<!--[\s\S]*?-->/g, '')
29-
30-
// 4. Shrink HTML Lines (Trim and remove empty lines)
27+
// 3. Shrink HTML Lines (Trim and remove empty lines)
3128
const lines = processed.split(/\r?\n/)
3229
const shrunk_lines: string[] = []
3330
for (const line of lines) {
@@ -38,16 +35,15 @@ export const shrink_html = (content: string): string => {
3835
}
3936
processed = shrunk_lines.join('\n')
4037

41-
// 5. Restore Scripts
38+
// 4. Restore Scripts
4239
processed = processed.replace(/___SCRIPT_(\d+)___/g, (_, idx) => {
4340
const { open, content } = scripts[parseInt(idx)]
44-
// Treat as C-style but do NOT strip bodies (preserve logic, just strip comments)
45-
// as scripts in HTML are often minimal or structural.
41+
// Treat as C-style but do NOT strip bodies
4642
const shrunk = shrink_c_style(content).trimEnd()
4743
return `${open}\n${shrunk}\n</script>`
4844
})
4945

50-
// 6. Restore Styles
46+
// 5. Restore Styles
5147
processed = processed.replace(/___STYLE_(\d+)___/g, (_, idx) => {
5248
const { open, content } = styles[parseInt(idx)]
5349
// Shrink CSS (strips bodies/properties usually)

‎apps/editor/src/context/utils/shrink-file/languages/jsx.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ export const shrink_jsx = (content: string): string => {
2424
: false
2525

2626
if (is_in_block_comment) {
27+
if (!current_skipped && use_effect_start_depth === -1) {
28+
processed_line += char
29+
}
2730
if (char == '*' && next_char == '/') {
2831
is_in_block_comment = false
32+
if (!current_skipped && use_effect_start_depth === -1) {
33+
processed_line += next_char
34+
}
2935
i += 2
3036
} else {
3137
i++
@@ -76,11 +82,17 @@ export const shrink_jsx = (content: string): string => {
7682
}
7783

7884
if (char == '/' && next_char == '/') {
85+
if (!current_skipped && use_effect_start_depth === -1) {
86+
processed_line += line.substring(i)
87+
}
7988
break
8089
}
8190

8291
if (char == '/' && next_char == '*') {
8392
is_in_block_comment = true
93+
if (!current_skipped && use_effect_start_depth === -1) {
94+
processed_line += '/*'
95+
}
8496
i += 2
8597
continue
8698
}

‎apps/editor/src/context/utils/shrink-file/languages/python.ts‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ export const shrink_python = (content: string): string => {
1616
skip_indent_threshold = -1
1717
}
1818

19-
// 3. Strip Comments (simplified char loop to handle strings)
19+
// 3. Keep comments, but track non-comment code specifically
2020
let processed_line = ''
21+
let code_part = ''
2122
let in_string: string | false = false
2223
let i = 0
2324

@@ -27,6 +28,7 @@ export const shrink_python = (content: string): string => {
2728
if (in_string) {
2829
if (char == '\\') {
2930
processed_line += char + (line[i + 1] || '')
31+
code_part += char + (line[i + 1] || '')
3032
i += 2
3133
continue
3234
}
@@ -37,35 +39,41 @@ export const shrink_python = (content: string): string => {
3739
line.substring(i, i + in_string.length) === in_string)
3840
) {
3941
processed_line += in_string
42+
code_part += in_string
4043
i += in_string.length
4144
in_string = false
4245
continue
4346
}
4447
processed_line += char
48+
code_part += char
4549
i++
4650
} else {
4751
if (char == '"' || char === "'") {
4852
const is_triple = line.substring(i, i + 3) === char.repeat(3)
4953
in_string = is_triple ? char.repeat(3) : char
5054
processed_line += in_string
55+
code_part += in_string
5156
i += in_string.length
5257
continue
5358
}
5459
if (char == '#') {
60+
processed_line += line.substring(i)
5561
break
5662
}
5763
processed_line += char
64+
code_part += char
5865
i++
5966
}
6067
}
6168

6269
const trimmed = processed_line.trim()
70+
const trimmed_code = code_part.trim()
6371
if (!trimmed) continue
6472

6573
// 4. Check for Block Start to Strip
66-
if (trimmed.endsWith(':')) {
74+
if (trimmed_code.endsWith(':')) {
6775
// Keep 'class', strip others (def, if, while, etc.)
68-
const is_keeper = /^class\s/.test(trimmed)
76+
const is_keeper = /^class\s/.test(trimmed_code)
6977
if (!is_keeper) {
7078
result.push(processed_line)
7179
result.push(line.substring(0, current_indent) + ' ' + '# ...')

‎apps/editor/src/context/utils/shrink-file/languages/ruby.ts‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ export const shrink_ruby = (content: string): string => {
1616
skip_indent_threshold = -1
1717
}
1818

19-
// 3. Strip Comments (simplified char loop to handle strings)
19+
// 3. Keep comments, but track non-comment code specifically
2020
let processed_line = ''
21+
let code_part = ''
2122
let in_string: string | false = false
2223
let i = 0
2324

@@ -27,38 +28,45 @@ export const shrink_ruby = (content: string): string => {
2728
if (in_string) {
2829
if (char == '\\') {
2930
processed_line += char + (line[i + 1] || '')
31+
code_part += char + (line[i + 1] || '')
3032
i += 2
3133
continue
3234
}
3335
if (char === in_string) {
3436
processed_line += in_string
37+
code_part += in_string
3538
in_string = false
3639
i++
3740
continue
3841
}
3942
processed_line += char
43+
code_part += char
4044
i++
4145
} else {
4246
if (char == '"' || char === "'") {
4347
in_string = char
4448
processed_line += char
49+
code_part += char
4550
i++
4651
continue
4752
}
4853
if (char == '#') {
54+
processed_line += line.substring(i)
4955
break
5056
}
5157
processed_line += char
58+
code_part += char
5259
i++
5360
}
5461
}
5562

5663
const trimmed = processed_line.trim()
64+
const trimmed_code = code_part.trim()
5765
if (!trimmed) continue
5866

5967
// 4. Check for Block Start to Strip (def)
6068
// We only strip 'def' methods that span multiple lines (don't end with 'end')
61-
if (/^def\b/.test(trimmed) && !/\bend\s*$/.test(trimmed)) {
69+
if (/^def\b/.test(trimmed_code) && !/\bend\s*$/.test(trimmed_code)) {
6270
result.push(processed_line)
6371
result.push(line.substring(0, current_indent) + ' ' + '# ...')
6472
skip_indent_threshold = current_indent

‎apps/editor/src/context/utils/shrink-file/languages/sql.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ export const shrink_sql = (content: string): string => {
1313
const next_char = line[i + 1]
1414

1515
if (is_in_block_comment) {
16+
processed_line += char
1617
if (char == '*' && next_char == '/') {
1718
is_in_block_comment = false
19+
processed_line += next_char
1820
i += 2
1921
} else {
2022
i++
@@ -51,11 +53,13 @@ export const shrink_sql = (content: string): string => {
5153
}
5254

5355
if (char == '-' && next_char == '-') {
56+
processed_line += line.substring(i)
5457
break
5558
}
5659

5760
if (char == '/' && next_char == '*') {
5861
is_in_block_comment = true
62+
processed_line += '/*'
5963
i += 2
6064
continue
6165
}

‎apps/editor/src/context/utils/shrink-file/test-cases/c-style/output.txt‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function test() {
22
// ...
33
}
4-
const str = "String with \"// quote";
4+
const str = "String with \"// quote"; // real comment
55
function add(a, b) {
66
// ...
77
}
@@ -24,14 +24,21 @@ const config = {
2424
env: "production",
2525
debug: false
2626
};
27+
// comment
28+
/* block */
2729
const str = "/* this is not a comment */";
2830
const str = "// this is not a comment";
2931
const str = '// this is not a comment';
3032
const str = `// this is not a comment`;
31-
const a = 1; const b = 2;
33+
const a = 1; /* comment */ const b = 2;
3234
const a = 1;
35+
/*
36+
This is a multi-line
37+
comment
38+
*/
3339
const b = 2;
34-
const a = 1;
40+
const a = 1; // This is a comment
41+
// This is a full line comment
3542
const b = 2;
3643
import {
3744
Readable,

‎apps/editor/src/context/utils/shrink-file/test-cases/css/output.txt‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
/* Header Styles */
12
header {
3+
color: white; /* Inline comment */
24
}
5+
/*
6+
Layout
7+
Styles
8+
*/
39
@media (min-width: 768px) {
410
header {
511
}

‎apps/editor/src/context/utils/shrink-file/test-cases/html/output.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<head>
44
<title>Test</title>
55
<script>
6+
// JS Comment
67
if (a < b) {
78
console.log("<div>");
89
}
@@ -11,11 +12,13 @@
1112
}
1213
</script>
1314
<style>
15+
/* CSS Comment */
1416
body { color: red; }
1517
</style>
1618
</head>
1719
<body>
1820
<div class="container">
21+
<!-- <p>Text</p> -->
1922
<span class="">Value</span>
2023
</div>
2124
</body>

0 commit comments

Comments
 (0)