MCPcopy
hub / github.com/sveltejs/svelte / migrate_css

Function migrate_css

packages/svelte/src/compiler/migrate/index.js:41–96  ·  view source on GitHub ↗

* * @param {State} state

(state)

Source from the content-addressed store, hash-verified

39 * @param {State} state
40 */
41function migrate_css(state) {
42 if (!state.analysis.css.ast?.start) return;
43 const css_contents = state.str
44 .snip(state.analysis.css.ast.start, /** @type {number} */ (state.analysis.css.ast?.end))
45 .toString();
46 let code = css_contents;
47 let starting = 0;
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
50 const str = new MagicString(code);
51 while (code) {
52 if (
53 code.startsWith(':has') ||
54 code.startsWith(':is') ||
55 code.startsWith(':where') ||
56 code.startsWith(':not')
57 ) {
58 let start = code.indexOf('(') + 1;
59 let is_global = false;
60
61 const global_str = ':global';
62 const next_global = code.indexOf(global_str);
63 const str_between = code.substring(start, next_global);
64 if (!str_between.trim()) {
65 is_global = true;
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 }
79 }
80
81 const end = find_closing_parenthesis(start, code);
82 if (start && end) {
83 if (!is_global && !code.startsWith(':not')) {
84 str.prependLeft(starting + start, ':global(');
85 str.appendRight(starting + end - 1, ')');
86 }
87 starting += end - 1;
88 code = code.substring(end - 1);
89 continue;
90 }
91 }
92 starting++;
93 code = code.substring(1);
94 }
95 state.str.update(state.analysis.css.ast?.start, state.analysis.css.ast?.end, str.toString());
96}
97
98/**

Callers 1

migrateFunction · 0.85

Calls 3

find_closing_parenthesisFunction · 0.85
updateMethod · 0.65
toStringMethod · 0.45

Tested by

no test coverage detected