@@ -14,6 +14,10 @@ function yoptReplaceAll(str: string, search: string, replacement: string) {
1414 return str . replace ( re , replacement ) ;
1515}
1616
17+ /**Add commentMore actions
18+ * @param text текст, по которому следует пройтисьAdd commentMore actions
19+ * @param to язык текста ('ys' or 'js')
20+ */
1721function iterateText ( text : string , to : 'js' | 'ys' = 'ys' ) {
1822 const langCol = to === 'ys' ? 1 : 0 ;
1923 const dick = dictionary ;
@@ -25,15 +29,23 @@ function iterateText(text: string, to: 'js' | 'ys' = 'ys') {
2529 return text ;
2630}
2731
32+ /**Add commentMore actions
33+ * Переводит YoptaScript в JavaScript и обратно
34+ * @param text Строка для перевода
35+ * @param lang Язык строки (ys/js)
36+ * @returns {string } Переведённый текстAdd commentMore actions
37+ */
2838export function compile ( text : string , lang : 'js' | 'ys' = 'ys' ) : string {
39+ /* text - текст для реплейсаAdd commentMore actions
40+ * lang - язык текста ('ys' or 'js')Add commentMore actions
41+ */
2942 interface Literals {
3043 [ key : string ] : string ;
3144 }
3245
3346 const commentRegExp = / ( (?: \/ \* (?: [ ^ * ] | (?: \* + [ ^ * \/ ] ) ) * \* + \/ ) | (?: \/ \/ .* ) ) / g;
3447 const tmpToken = 'ys_' + new Date ( ) . getTime ( ) + '_' ;
3548
36- // 1) Extract and stash out real string literals
3749 const rStringLiterals : Literals = { } ;
3850 text = text . replace (
3951 / \" (?: \\ .| [ ^ \" \\ ] ) * \" | \' (?: \\ .| [ ^ \' \\ ] ) * \' / g,
@@ -44,9 +56,7 @@ export function compile(text: string, lang: 'js' | 'ys' = 'ys'): string {
4456 }
4557 ) ;
4658
47- // 2) Extract and stash out JSX text nodes (between > and <) so we don't yoptify them
4859 const rJsxTextLiterals : Literals = { } ;
49- // 2) Extract only literal text between opening and closing JSX tags
5060 text = text . replace (
5161 / ( < [ A - Z a - z ] [ ^ > ] * > ) ( [ \s \S ] + ?) (? = < \/ [ A - Z a - z ] ) / g,
5262 ( match , openTag , content , offset ) => {
@@ -56,21 +66,19 @@ export function compile(text: string, lang: 'js' | 'ys' = 'ys'): string {
5666 }
5767 ) ;
5868
59- // 3) Extract comments to restore later
6069 const commentsArray = text . match ( commentRegExp ) || [ ] ;
6170
62- // 4) Perform language iteration/replacement
6371 text = iterateText ( text , lang ) ;
6472
65- // 5) Restore comments
73+ // comeback comments
6674 text = text . replace ( commentRegExp , ( ) => commentsArray . shift ( ) || '' ) ;
6775
68- // 6) Restore original string literals
76+ // comeback strings
6977 for ( const key in rStringLiterals ) {
7078 text = text . replace ( key , rStringLiterals [ key ] ) ;
7179 }
7280
73- // 7) Restore JSX text nodes untouched
81+ // comeback jsx
7482 for ( const key in rJsxTextLiterals ) {
7583 text = text . replace ( key , rJsxTextLiterals [ key ] ) ;
7684 }
0 commit comments