@@ -40,9 +40,10 @@ class MigrationError extends Error {
40
40
*/
41
41
function migrate_css ( state ) {
42
42
if ( ! state . analysis . css . ast ?. start ) return ;
43
- let code = state . str
43
+ const css_contents = state . str
44
44
. snip ( state . analysis . css . ast . start , /** @type {number } */ ( state . analysis . css . ast ?. end ) )
45
45
. toString ( ) ;
46
+ let code = css_contents ;
46
47
let starting = 0 ;
47
48
48
49
// since we already blank css we can't work directly on `state.str` so we will create a copy that we can update
@@ -56,23 +57,28 @@ function migrate_css(state) {
56
57
) {
57
58
let start = code . indexOf ( '(' ) + 1 ;
58
59
let is_global = false ;
60
+
59
61
const global_str = ':global' ;
60
62
const next_global = code . indexOf ( global_str ) ;
61
63
const str_between = code . substring ( start , next_global ) ;
62
64
if ( ! str_between . trim ( ) ) {
63
65
is_global = true ;
64
66
start += global_str . length ;
67
+ } else {
68
+ const prev_global = css_contents . lastIndexOf ( global_str , starting ) ;
69
+ if ( prev_global > - 1 ) {
70
+ const end =
71
+ find_closing_parenthesis ( css_contents . indexOf ( '(' , prev_global ) + 1 , css_contents ) -
72
+ starting ;
73
+ if ( end > start ) {
74
+ starting += end ;
75
+ code = code . substring ( end ) ;
76
+ continue ;
77
+ }
78
+ }
65
79
}
66
- let parenthesis = 1 ;
67
- let end = start ;
68
- let char = code [ end ] ;
69
- // find the closing parenthesis
70
- while ( parenthesis !== 0 && char ) {
71
- if ( char === '(' ) parenthesis ++ ;
72
- if ( char === ')' ) parenthesis -- ;
73
- end ++ ;
74
- char = code [ end ] ;
75
- }
80
+
81
+ const end = find_closing_parenthesis ( start , code ) ;
76
82
if ( start && end ) {
77
83
if ( ! is_global && ! code . startsWith ( ':not' ) ) {
78
84
str . prependLeft ( starting + start , ':global(' ) ;
@@ -89,6 +95,24 @@ function migrate_css(state) {
89
95
state . str . update ( state . analysis . css . ast ?. start , state . analysis . css . ast ?. end , str . toString ( ) ) ;
90
96
}
91
97
98
+ /**
99
+ * @param {number } start
100
+ * @param {string } code
101
+ */
102
+ function find_closing_parenthesis ( start , code ) {
103
+ let parenthesis = 1 ;
104
+ let end = start ;
105
+ let char = code [ end ] ;
106
+ // find the closing parenthesis
107
+ while ( parenthesis !== 0 && char ) {
108
+ if ( char === '(' ) parenthesis ++ ;
109
+ if ( char === ')' ) parenthesis -- ;
110
+ end ++ ;
111
+ char = code [ end ] ;
112
+ }
113
+ return end ;
114
+ }
115
+
92
116
/**
93
117
* Does a best-effort migration of Svelte code towards using runes, event attributes and render tags.
94
118
* May throw an error if the code is too complex to migrate automatically.
0 commit comments