@@ -78,7 +78,18 @@ export default async args => {
7878
7979 code = header + code ;
8080
81- // 4. Fix export pattern: async function Name { } export { Name };
81+ // 4. Add missing // BEGIN EXTRA CODE if // END EXTRA CODE exists without a BEGIN
82+ if ( code . includes ( '// END EXTRA CODE' ) && ! code . includes ( '// BEGIN EXTRA CODE' ) ) {
83+ // Find last import line, add BEGIN EXTRA CODE after it
84+ code = code . replace ( / ( ^ i m p o r t .+ $ ) / m, '$1\n\n// BEGIN EXTRA CODE' ) ;
85+ }
86+
87+ // 5. If no EXTRA CODE markers at all, add empty block after imports
88+ if ( ! code . includes ( '// BEGIN EXTRA CODE' ) && ! code . includes ( '// END EXTRA CODE' ) ) {
89+ code = code . replace ( / ( ^ i m p o r t .+ $ ) ( \r ? \n ) + (? = \/ ? \* \* | e x p o r t ) / m, '$1\n\n// BEGIN EXTRA CODE\n// END EXTRA CODE\n\n' ) ;
90+ }
91+
92+ // 6. Fix export pattern: async function Name { } export { Name };
8293 // → export async function Name { }
8394 const exportMatch = code . match ( / e x p o r t \s * \{ \s * ( \w + ) \s * \} ; ? \s * $ / m) ;
8495 if ( exportMatch ) {
@@ -88,6 +99,26 @@ export default async args => {
8899 code = code . replace ( / \n e x p o r t \s * \{ \s * \w + \s * \} ; ? \s * $ / m, "" ) ;
89100 }
90101
102+ // 7. Fix USER CODE marker indentation: Studio Pro uses tabs inside functions
103+ // Match any whitespace before the markers and replace with single tab
104+ code = code . replace ( / ^ [ \t ] + ( \/ \/ B E G I N U S E R C O D E ) / gm, "\t$1" ) ;
105+ code = code . replace ( / ^ [ \t ] + ( \/ \/ E N D U S E R C O D E ) / gm, "\t$1" ) ;
106+
107+ // 8. Ensure blank line before // BEGIN EXTRA CODE
108+ code = code . replace ( / ( [ ^ \r \n ] ) \r ? \n ( \/ \/ B E G I N E X T R A C O D E ) / g, "$1\r\n\r\n$2" ) ;
109+
110+ // 9. Ensure blank line after // END EXTRA CODE
111+ code = code . replace ( / ( \/ \/ E N D E X T R A C O D E ) \r ? \n ( [ ^ \r \n ] ) / g, "$1\r\n\r\n$2" ) ;
112+
113+ // 10. Normalize JSDoc formatting to match Studio Pro
114+ // - Empty JSDoc lines: ` *` (one space before asterisk, nothing after)
115+ // - Lines with text: ` * text` (one space before, one space after asterisk)
116+ code = code . replace ( / ^ [ \t ] + \* [ \t ] + $ / gm, " *" ) ; // Empty JSDoc lines: remove trailing whitespace
117+ code = code . replace ( / ^ [ \t ] + \* [ \t ] + ( .+ ) $ / gm, " * $1" ) ; // JSDoc with text: normalize to 1 space before and after asterisk
118+
119+ // 11. Normalize all line endings to CRLF (Studio Pro expects \r\n everywhere)
120+ code = code . replace ( / \r ? \n / g, "\r\n" ) ;
121+
91122 chunk . code = code ;
92123 }
93124 }
0 commit comments