Skip to content

Commit c9feef6

Browse files
authored
chore: add type annotations to Inspector.svelte (#1372)
1 parent 02981fd commit c9feef6

2 files changed

Lines changed: 97 additions & 64 deletions

File tree

‎packages/vite-plugin-svelte/src/plugins/inspector/runtime/Inspector.svelte‎

Lines changed: 93 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,37 @@
1-
<svelte:options runes={true} />
2-
3-
<script>
4-
// do not use TS here so that this component works in non-ts projects too
1+
<script lang="ts">
52
import { onMount } from 'svelte';
6-
73
import options from 'virtual:svelte-inspector-options';
8-
const toggle_combo = options.toggleKeyCombo?.toLowerCase().split('-');
9-
const escape_keys = options.escapeKeys?.map((k) => k.toLowerCase());
10-
const nav_keys = Object.values(options.navKeys).map((k) => k?.toLowerCase());
11-
const open_key = options.openKey?.toLowerCase();
12-
const open_menu_key = options.openMenuKey?.toLowerCase();
4+
5+
// Copied from Svelte's internal types
6+
interface DevStackEntry {
7+
file: string;
8+
type: 'component' | 'if' | 'each' | 'await' | 'key' | 'render';
9+
line: number;
10+
column: number;
11+
parent: DevStackEntry | null;
12+
componentTag?: string;
13+
}
14+
15+
interface Metadata {
16+
loc: Loc;
17+
parent?: DevStackEntry;
18+
}
19+
20+
interface Loc {
21+
file: string;
22+
line: number;
23+
column: number;
24+
}
25+
26+
interface ElementWithMetadata extends Element {
27+
__svelte_meta: Metadata;
28+
}
29+
30+
const toggle_combo = options.toggleKeyCombo.toLowerCase().split('-');
31+
const escape_keys = options.escapeKeys.map((k) => k.toLowerCase());
32+
const nav_keys = Object.values(options.navKeys).map((k) => k.toLowerCase());
33+
const open_key = options.openKey.toLowerCase();
34+
const open_menu_key = options.openMenuKey.toLowerCase();
1335
1436
let enabled = $state(false);
1537
let has_opened = $state(false);
@@ -27,53 +49,58 @@
2749
)}`;
2850
2951
// overlay positioning
30-
let x = $state(),
31-
y = $state(),
32-
w = $state(),
33-
h = $state();
52+
let x = $state(0),
53+
y = $state(0),
54+
w = $state(0),
55+
h = $state(0);
3456
35-
let active_el = $state();
57+
let active_el = $state<ElementWithMetadata | undefined>();
3658
37-
let menu = $state(null);
59+
let menu = $state<{ x: number; y: number; stack: Array<Loc & { tag: string }> } | null>(null);
3860
39-
let hold_start_ts = $state();
61+
let hold_start_ts = $state<number | null>(null);
4062
4163
let show_toggle = $derived(
4264
options.showToggleButton === 'always' || (options.showToggleButton === 'active' && enabled)
4365
);
4466
45-
function mousemove(e) {
67+
function mousemove(e: MouseEvent) {
4668
if (menu) return;
4769
x = e.x;
4870
y = e.y;
4971
}
5072
51-
function find_selectable_parent(el, include_self = false) {
73+
function find_selectable_parent(el: Element | undefined, include_self = false) {
74+
let candidate: Element | undefined = el;
75+
5276
if (!include_self) {
53-
el = el.parentNode;
77+
candidate = (candidate?.parentNode as Element) ?? undefined;
5478
}
55-
while (el) {
56-
if (is_selectable(el)) {
57-
return el;
79+
80+
while (candidate) {
81+
if (is_selectable(candidate)) {
82+
return candidate;
5883
}
59-
el = el.parentNode;
84+
85+
candidate = candidate.parentNode as Element;
6086
}
6187
}
6288
63-
function find_selectable_child(el) {
89+
function find_selectable_child(el: Element) {
6490
return [...el.querySelectorAll('*')].find(is_selectable);
6591
}
6692
67-
function find_selectable_sibling(el, prev = false) {
93+
function find_selectable_sibling(el: Element, prev = false) {
94+
let candidate: Element | null = el;
6895
do {
69-
el = prev ? el.previousElementSibling : el.nextElementSibling;
70-
if (is_selectable(el)) {
71-
return el;
96+
candidate = prev ? candidate.previousElementSibling : candidate.nextElementSibling;
97+
if (is_selectable(candidate)) {
98+
return candidate;
7299
}
73-
} while (el);
100+
} while (candidate);
74101
}
75102
76-
function find_selectable_for_nav(key) {
103+
function find_selectable_for_nav(key: string) {
77104
const el = active_el;
78105
if (!el) {
79106
return find_selectable_child(document?.body);
@@ -92,8 +119,9 @@
92119
}
93120
}
94121
95-
function is_selectable(el) {
96-
const file = el?.__svelte_meta?.loc?.file;
122+
function is_selectable(el: Element | null): el is ElementWithMetadata {
123+
if (!el) return false;
124+
const file = (el as ElementWithMetadata)?.__svelte_meta?.loc?.file;
97125
if (!file || file.includes('node_modules/')) {
98126
return false; // no file or 3rd party
99127
}
@@ -104,13 +132,13 @@
104132
return true;
105133
}
106134
107-
function mouseover({ target }) {
135+
function mouseover(e: MouseEvent) {
108136
if (menu) return;
109-
const el = find_selectable_parent(target, true);
137+
const el = find_selectable_parent(e.target as ElementWithMetadata, true);
110138
activate(el, false);
111139
}
112140
113-
function activate(el, set_bubble_pos = true) {
141+
function activate(el: ElementWithMetadata | undefined, set_bubble_pos = true) {
114142
if (options.customStyles && el !== active_el) {
115143
if (active_el) {
116144
active_el.classList.remove('svelte-inspector-active-target');
@@ -122,13 +150,13 @@
122150
123151
active_el = el;
124152
if (set_bubble_pos) {
125-
const pos = el.getBoundingClientRect();
153+
const pos = el!.getBoundingClientRect();
126154
x = Math.ceil(pos.left);
127155
y = Math.ceil(pos.bottom - 20);
128156
}
129157
}
130158
131-
function open_editor(e) {
159+
function open_editor(e: Event) {
132160
if (menu) return;
133161
134162
if (active_el) {
@@ -137,9 +165,9 @@
137165
}
138166
}
139167
140-
function get_stack(target) {
141-
const el = find_selectable_parent(target, true);
142-
const meta = el?.__svelte_meta;
168+
function get_stack(target: ElementWithMetadata) {
169+
const el = find_selectable_parent(target, true)!;
170+
const meta = el.__svelte_meta;
143171
144172
const stack = [
145173
{
@@ -150,33 +178,34 @@
150178
}
151179
];
152180
153-
let entry = meta;
154-
while ((entry = entry.parent)) {
181+
let entry: Metadata | DevStackEntry | undefined = meta;
182+
183+
while ((entry = entry.parent ?? undefined)) {
155184
if (entry.type !== 'component') continue;
156185
if (/(^|\/)(node_modules|\.svelte-kit)\//.test(entry.file)) continue;
157186
158187
stack.push({
159188
file: entry.file,
160189
line: entry.line,
161190
column: entry.column,
162-
tag: entry.componentTag
191+
tag: entry.componentTag!
163192
});
164193
}
165194
166195
return stack;
167196
}
168197
169-
function on_contextmenu(e) {
198+
function on_contextmenu(e: Event) {
170199
e.preventDefault();
171200
172-
active_el ??= find_selectable_parent(e.target);
201+
active_el ??= find_selectable_parent(e.target as Element);
173202
if (!active_el) return;
174203
175204
stop(e);
176-
menu = { x: e.clientX, y: e.clientY, stack: get_stack(active_el) };
205+
menu = { x, y, stack: get_stack(active_el) };
177206
}
178207
179-
function send(loc) {
208+
function send(loc: Loc) {
180209
fetch(
181210
`${options.__internal.base}/__open-in-editor?file=${encodeURIComponent(
182211
`${loc.file}:${loc.line}:${loc.column + 1}`
@@ -189,7 +218,7 @@
189218
}
190219
}
191220
192-
function is_active(key, e) {
221+
function is_active(key: string, e: KeyboardEvent) {
193222
switch (key) {
194223
case 'shift':
195224
case 'control':
@@ -201,41 +230,41 @@
201230
}
202231
}
203232
204-
function is_combo(e) {
233+
function is_combo(e: KeyboardEvent) {
205234
return toggle_combo?.every((k) => is_active(k, e));
206235
}
207236
208-
function is_escape(e) {
237+
function is_escape(e: KeyboardEvent) {
209238
return escape_keys?.some((k) => is_active(k, e));
210239
}
211240
212-
function is_toggle(e) {
241+
function is_toggle(e: KeyboardEvent) {
213242
return toggle_combo?.some((k) => is_active(k, e));
214243
}
215244
216-
function is_nav(e) {
245+
function is_nav(e: KeyboardEvent) {
217246
return nav_keys?.some((k) => is_active(k, e));
218247
}
219248
220-
function is_open(e) {
249+
function is_open(e: KeyboardEvent) {
221250
return open_key && is_active(open_key, e);
222251
}
223252
224-
function is_open_menu(e) {
253+
function is_open_menu(e: KeyboardEvent) {
225254
return open_menu_key && is_active(open_menu_key, e);
226255
}
227256
228257
function is_holding() {
229258
return hold_start_ts && Date.now() - hold_start_ts > 250;
230259
}
231260
232-
function stop(e) {
261+
function stop(e: Event) {
233262
e.preventDefault();
234263
e.stopPropagation();
235264
e.stopImmediatePropagation();
236265
}
237266
238-
function keydown(e) {
267+
function keydown(e: KeyboardEvent) {
239268
if (e.repeat || e.key == null || (!enabled && !is_toggle(e))) {
240269
return;
241270
}
@@ -265,7 +294,7 @@
265294
}
266295
}
267296
268-
function keyup(e) {
297+
function keyup(e: KeyboardEvent) {
269298
if (e.repeat || e.key == null || !enabled) {
270299
return;
271300
}
@@ -286,7 +315,7 @@
286315
}
287316
}
288317
289-
function listeners(body, enabled) {
318+
function listeners(body: HTMLBodyElement, enabled: boolean) {
290319
const l = enabled ? body.addEventListener : body.removeEventListener;
291320
l('mousemove', mousemove);
292321
l('mouseover', mouseover);
@@ -296,7 +325,7 @@
296325
297326
function enable() {
298327
enabled = true;
299-
const b = document.body;
328+
const b = document.body as HTMLBodyElement;
300329
if (options.customStyles) {
301330
b.classList.add('svelte-inspector-enabled');
302331
}
@@ -308,7 +337,7 @@
308337
const hov = innermost_hover_el();
309338
let el = find_selectable_parent(hov, true);
310339
if (!el) {
311-
const act = document.activeElement;
340+
const act = document.activeElement ?? undefined;
312341
el = find_selectable_parent(act, true);
313342
}
314343
if (!el) {
@@ -334,13 +363,13 @@
334363
has_opened = false;
335364
hold_start_ts = null;
336365
menu = null;
337-
const b = document.body;
366+
const b = document.body as HTMLBodyElement;
338367
listeners(b, enabled);
339368
if (options.customStyles) {
340369
b.classList.remove('svelte-inspector-enabled');
341370
active_el?.classList.remove('svelte-inspector-active-target');
342371
}
343-
active_el = null;
372+
active_el = undefined;
344373
}
345374
346375
function visibilityChange() {
@@ -446,7 +475,7 @@
446475
</li>
447476
{/each}
448477
</ul>
449-
{:else}
478+
{:else if active_el}
450479
{@const loc = active_el.__svelte_meta.loc}
451480

452481
<div class="svelte-inspector-grid">
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
declare module 'virtual:svelte-inspector-options' {
2+
const options: Required<import('../../../public.d.ts').InspectorOptions>;
3+
export default options;
4+
}

0 commit comments

Comments
 (0)