Skip to content

Commit 63aeb58

Browse files
committed
Update JSX shrinker to use empty braces for stripped blocks and remove trailing semicolons
- fix `apps/editor/src/context/utils/shrink-file/languages/jsx.ts` - fix `apps/editor/src/context/utils/shrink-file/languages/jsx.ts` - fix [Pasted text]
1 parent 0578e9a commit 63aeb58

2 files changed

Lines changed: 11 additions & 33 deletions

File tree

  • apps/editor/src/context/utils/shrink-file

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,11 @@
11
export const shrink_jsx = (content: string): string => {
2-
// Split by newline to process line by line, handling both LF and CRLF
32
const lines = content.split(/\r?\n/)
43
const result: string[] = []
54
let is_in_block_comment = false
65
let skip_body_depth = 0
7-
// Buffer to track context for stripping bodies (e.g. "class Foo ")
86
let last_code_buffer = ''
97

108
for (const line of lines) {
11-
// Capture indentation for use in body stripping
12-
const indent_match = line.match(/^\s*/)
13-
const current_indent = indent_match ? indent_match[0] : ''
14-
159
let processed_line = ''
1610
let i = 0
1711
let is_in_string: false | '"' | "'" | '`' = false
@@ -61,7 +55,7 @@ export const shrink_jsx = (content: string): string => {
6155
}
6256

6357
if (char == '/' && next_char == '/') {
64-
break // Ignore the rest of the line
58+
break
6559
}
6660

6761
if (char == '/' && next_char == '*') {
@@ -70,34 +64,28 @@ export const shrink_jsx = (content: string): string => {
7064
continue
7165
}
7266

73-
// Handle Function Body Stripping
7467
if (char == '{') {
7568
if (skip_body_depth > 0) {
76-
// Already skipping, just deepen the level
7769
skip_body_depth++
7870
last_code_buffer += char
7971
i++
8072
continue
8173
} else {
82-
// Check if we should preserve this block (class, interface, etc.)
83-
// We look at last_code_buffer for keywords
8474
const is_keeper =
85-
/(^|\s)(class|interface|enum|namespace|type|import|export)(\s|$)/.test(
75+
/(^|\s)(class|interface|enum|namespace|type)(\s|$)/.test(
8676
last_code_buffer
87-
)
88-
// Also preserve object literals (preceded by =, :, or open paren for destructuring)
77+
) || /(^|\s)(import|export|default)\s*$/.test(last_code_buffer)
8978
const is_object_literal = /(=|:|\()\s*$/.test(
9079
last_code_buffer.trimEnd()
9180
)
9281

9382
if (!is_keeper && !is_object_literal) {
9483
skip_body_depth = 1
95-
processed_line += `{\n${current_indent} // ...\n${current_indent}}`
84+
processed_line += `{}`
9685
last_code_buffer += '{}'
9786
i++
9887
continue
9988
} else {
100-
// Keeper: reset buffer to avoid false positives in nested scopes
10189
last_code_buffer = ''
10290
}
10391
}
@@ -117,16 +105,12 @@ export const shrink_jsx = (content: string): string => {
117105
i++
118106
}
119107

120-
// We trim the line to actually "shrink" the file.
121-
// If a line contained only comments, it becomes empty and is skipped.
122108
const trimmed = processed_line.trim()
123109
if (trimmed) {
124-
result.push(processed_line.trimEnd())
110+
result.push(processed_line.trimEnd().replace(/;+$/, ''))
125111
}
126112

127-
// Add a space to buffer for newline to avoid concatenation issues (e.g. "class Foo" + "{")
128113
last_code_buffer += ' '
129-
// Limit buffer size to avoid memory issues, keeping enough for context
130114
if (last_code_buffer.length > 200) {
131115
last_code_buffer = last_code_buffer.slice(-200)
132116
}
Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,13 @@
1-
import React from 'react';
1+
import React from 'react'
22
interface Props {
3-
title: string;
4-
}
5-
export const MyComponent: React.FC<Props> = ({ title }) => {
6-
// ...
7-
}
8-
function Header() {
9-
// ...
3+
title: string
104
}
5+
export const MyComponent: React.FC<Props> = ({ title }) => {}
6+
function Header() {}
117
class OldComponent extends React.Component {
12-
render() {
13-
// ...
14-
}
8+
render() {}
159
}
1610
import {
1711
useState,
1812
useEffect
19-
} from 'react';
13+
} from 'react'

0 commit comments

Comments
 (0)