MCPcopy Index your code
hub / github.com/material-shell/material-shell / convertImports

Function convertImports

scripts/transpile.ts:178–241  ·  view source on GitHub ↗
(text: string)

Source from the content-addressed store, hash-verified

176/// and
177/// const { a, b, c } = imports.gi.Source;
178function convertImports(text: string) {
179 const giImports = [
180 ['clutter', "'gi://Clutter'"],
181 ['cogl', "'gi://Cogl'"],
182 ['gdk', "'gi://Gdk'"],
183 ['gio', "'gi://Gio'"],
184 ['glib', "'gi://GLib'"],
185 ['gnomedesktop', "'gi://GnomeDesktop'"],
186 ['gobject', "'gi://GObject'"],
187 ['gtk', "'gi://Gtk'"],
188 ['meta', "'gi://Meta'"],
189 ['shell', "'gi://Shell'"],
190 ['soup', "'gi://Soup'"],
191 ['st', "'gi://St'"],
192 ['gjs', 'imports'],
193 ['ui', "'resource:///org/gnome/shell/ui/main.js'"],
194 ];
195
196 const regexes: [RegExp, string][] = giImports.map((x) => {
197 const [name, importpath] = x;
198 return [
199 new RegExp(
200 'import \\* as ([\\w\\d]+) from [\'"]' + name + '[\'"];',
201 'g'
202 ),
203 'import $1 from ' + importpath + ';',
204 ];
205 });
206
207 const regexes2: [RegExp, string][] = giImports.map((x) => {
208 const [name, importpath] = x;
209 return [
210 new RegExp(
211 'import \\{([^\\}]+)\\} from [\'"]' + name + '[\'"];',
212 'g'
213 ),
214 'import {$1} from ' + importpath + ';',
215 ];
216 });
217
218 const regexes3: [RegExp, string][] = giImports.map((x) => {
219 const [name, importpath] = x;
220 return [
221 new RegExp(`(const {.+) as (.+} = ${importpath};)`, 'g'),
222 '$1: $2',
223 ];
224 });
225
226 for (let regex of regexes) {
227 text = text.replace(regex[0], regex[1]);
228 }
229
230 for (let regex of regexes2) {
231 text = text.replace(regex[0], regex[1]);
232 }
233
234 for (let i = 0; i < 3; i++) {
235 for (let regex of regexes3) {

Callers 1

testFunction · 0.85

Calls 2

mapMethod · 0.65
replaceMethod · 0.65

Tested by 1

testFunction · 0.68