Skip to content

Commit db7fe33

Browse files
authored
convert REPL components to typescript (#47)
* convert stuff to ts * more * more
1 parent 7016c18 commit db7fe33

18 files changed

+184
-325
lines changed

packages/repl/src/lib/Checkbox.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
<script>
2-
export let checked = false;
1+
<script lang="ts">
2+
let { checked = $bindable() }: { checked: boolean } = $props();
33
</script>
44

55
<input type="checkbox" bind:checked />

packages/repl/src/lib/CodeMirror.svelte

Lines changed: 31 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
export const cursorIndex = writable(0);
33
</script>
44

5-
<script>
5+
<script lang="ts">
66
import { historyField } from '@codemirror/commands';
77
import { EditorState, Range, StateEffect, StateEffectType, StateField } from '@codemirror/state';
88
import { Decoration, EditorView } from '@codemirror/view';
@@ -15,33 +15,28 @@
1515
import Message from './Message.svelte';
1616
import { svelteTheme } from './theme.js';
1717
import { autocomplete } from './autocomplete.js';
18+
import type { LintSource } from '@codemirror/lint';
19+
import type { Extension } from '@codemirror/state';
20+
import { CompletionContext } from '@codemirror/autocomplete';
21+
import type { Lang } from './types';
1822
19-
/** @type {import('@codemirror/lint').LintSource | undefined} */
20-
export let diagnostics = undefined;
21-
23+
export let diagnostics: LintSource | undefined = undefined;
2224
export let readonly = false;
2325
export let tab = true;
2426
export let vim = false;
2527
26-
/** @type {ReturnType<typeof createEventDispatcher<{ change: { value: string } }>>} */
27-
const dispatch = createEventDispatcher();
28+
const dispatch: ReturnType<typeof createEventDispatcher<{ change: { value: string } }>> =
29+
createEventDispatcher();
2830
2931
let code = '';
3032
31-
/** @type {import('./types').Lang} */
32-
let lang = 'svelte';
33+
let lang: Lang = 'svelte';
3334
34-
/**
35-
* @param {{ code: string; lang: import('./types').Lang }} options
36-
*/
37-
export async function set(options) {
35+
export async function set(options: { code: string; lang: Lang }) {
3836
update(options);
3937
}
4038
41-
/**
42-
* @param {{ code?: string; lang?: import('./types').Lang }} options
43-
*/
44-
export async function update(options) {
39+
export async function update(options: { code?: string; lang?: Lang }) {
4540
await isReady;
4641
4742
if (!$cmInstance.view) return;
@@ -65,15 +60,11 @@
6560
}
6661
}
6762
68-
/**
69-
* @param {number} pos
70-
*/
71-
export function setCursor(pos) {
63+
export function setCursor(pos: number) {
7264
cursor_pos = pos;
7365
}
7466
75-
/** @type {(...val: any) => void} */
76-
let fulfil_module_editor_ready;
67+
let fulfil_module_editor_ready: (...val: any) => void;
7768
export const isReady = new Promise((f) => (fulfil_module_editor_ready = f));
7869
7970
export function resize() {
@@ -88,10 +79,7 @@
8879
return $cmInstance.view?.state.toJSON({ history: historyField });
8980
}
9081
91-
/**
92-
* @param {any} state
93-
*/
94-
export function setEditorState(state) {
82+
export function setEditorState(state: any) {
9583
if (!$cmInstance.view) return;
9684
9785
$cmInstance.view.setState(
@@ -113,8 +101,7 @@
113101
});
114102
}
115103
116-
/** @type {StateEffectType<Range<Decoration>[]>} */
117-
const addMarksDecoration = StateEffect.define();
104+
const addMarksDecoration: StateEffectType<Range<Decoration>[]> = StateEffect.define();
118105
119106
// This value must be added to the set of extensions to enable this
120107
const markField = StateField.define({
@@ -136,13 +123,15 @@
136123
provide: (f) => EditorView.decorations.from(f)
137124
});
138125
139-
/**
140-
* @param {object} param0
141-
* @param {number} param0.from
142-
* @param {number} param0.to
143-
* @param {string} [param0.className]
144-
*/
145-
export function markText({ from, to, className = 'mark-text' }) {
126+
export function markText({
127+
from,
128+
to,
129+
className = 'mark-text'
130+
}: {
131+
from: number;
132+
to: number;
133+
className?: string;
134+
}) {
146135
const executedMark = Decoration.mark({
147136
class: className
148137
});
@@ -163,23 +152,20 @@
163152
164153
const cmInstance = withCodemirrorInstance();
165154
166-
/** @type {number} */
167-
let w;
168-
/** @type {number} */
169-
let h;
155+
let w: number;
156+
let h: number;
170157
171158
let marked = false;
172159
173160
let updating_externally = false;
174161
175-
/** @type {import('@codemirror/state').Extension[]} */
176-
let extensions = [];
162+
let extensions: Extension[] = [];
177163
178164
/**
179165
* update the extension if and when vim changes
180-
* @param {boolean} vimEnabled if vim it's included in the set of extensions
166+
* @param vimEnabled if vim it's included in the set of extensions
181167
*/
182-
async function getExtensions(vimEnabled) {
168+
async function getExtensions(vimEnabled: boolean) {
183169
let extensions = [watcher];
184170
if (vimEnabled) {
185171
const { vim } = await import('@replit/codemirror-vim').then((vimModule) => ({
@@ -221,13 +207,11 @@
221207
const { files, selected } = get_repl_context();
222208
223209
const svelte_rune_completions = svelteLanguage.data.of({
224-
/** @param {import('@codemirror/autocomplete').CompletionContext} context */
225-
autocomplete: (context) => autocomplete(context, $selected, $files)
210+
autocomplete: (context: CompletionContext) => autocomplete(context, $selected, $files)
226211
});
227212
228213
const js_rune_completions = javascriptLanguage.data.of({
229-
/** @param {import('@codemirror/autocomplete').CompletionContext} context */
230-
autocomplete: (context) => autocomplete(context, $selected, $files)
214+
autocomplete: (context: CompletionContext) => autocomplete(context, $selected, $files)
231215
});
232216
</script>
233217

packages/repl/src/lib/Input/ComponentSelector.svelte

Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,20 @@
1-
<script>
1+
<script lang="ts">
22
import { get_repl_context } from '$lib/context.js';
33
import { get_full_filename } from '$lib/utils.js';
44
import { createEventDispatcher, tick } from 'svelte';
55
import RunesInfo from './RunesInfo.svelte';
66
import Migrate from './Migrate.svelte';
7+
import type { File } from '$lib/types';
78
8-
/** @type {boolean} */
9-
export let show_modified;
9+
export let show_modified: boolean;
10+
export let runes: boolean;
1011
11-
/** @type {boolean} */
12-
export let runes;
13-
14-
/** @type {ReturnType<typeof createEventDispatcher<{
15-
* remove: { files: import('$lib/types').File[]; diff: import('$lib/types').File },
16-
* add: { files: import('$lib/types').File[]; diff: import('$lib/types').File },
17-
* }>>} */
18-
const dispatch = createEventDispatcher();
12+
const dispatch: ReturnType<
13+
typeof createEventDispatcher<{
14+
remove: { files: import('$lib/types').File[]; diff: import('$lib/types').File };
15+
add: { files: import('$lib/types').File[]; diff: import('$lib/types').File };
16+
}>
17+
> = createEventDispatcher();
1918
2019
const {
2120
files,
@@ -27,21 +26,17 @@
2726
EDITOR_STATE_MAP
2827
} = get_repl_context();
2928
30-
/** @type {string | null} */
31-
let editing_name = null;
32-
29+
let editing_name: string | null = null;
3330
let input_value = '';
3431
35-
/** @param {string} filename */
36-
function select_file(filename) {
32+
function select_file(filename: string) {
3733
if ($selected_name !== filename) {
3834
editing_name = null;
3935
handle_select(filename);
4036
}
4137
}
4238
43-
/** @param {import('$lib/types').File} file */
44-
function edit_tab(file) {
39+
function edit_tab(file: File) {
4540
if ($selected_name === get_full_filename(file)) {
4641
editing_name = get_full_filename(file);
4742
input_value = file.name;
@@ -110,10 +105,7 @@
110105
rebundle();
111106
}
112107
113-
/**
114-
* @param {string} filename
115-
*/
116-
function remove(filename) {
108+
function remove(filename: string) {
117109
const file = $files.find((val) => get_full_filename(val) === filename);
118110
const idx = $files.findIndex((val) => get_full_filename(val) === filename);
119111
@@ -133,8 +125,7 @@
133125
handle_select($selected_name);
134126
}
135127
136-
/** @param {FocusEvent & { currentTarget: HTMLInputElement }} event */
137-
async function select_input(event) {
128+
async function select_input(event: FocusEvent & { currentTarget: HTMLInputElement }) {
138129
await tick();
139130
140131
event.currentTarget.select();
@@ -165,31 +156,25 @@
165156
$files = $files;
166157
}
167158
168-
/** @param {import('$lib/types').File} editing */
169-
function is_file_name_used(editing) {
159+
function is_file_name_used(editing: File) {
170160
return $files.find(
171161
(file) => JSON.stringify(file) !== JSON.stringify($selected) && file.name === editing.name
172162
);
173163
}
174164
175165
// drag and drop
176-
/** @type {string | null} */
177-
let from = null;
178-
179-
/** @type {string | null} */
180-
let over = null;
166+
let from: string | null = null;
167+
let over: string | null = null;
181168
182-
/** @param {DragEvent & { currentTarget: HTMLDivElement }} event */
183-
function dragStart(event) {
169+
function dragStart(event: DragEvent & { currentTarget: HTMLDivElement }) {
184170
from = event.currentTarget.id;
185171
}
186172
187173
function dragLeave() {
188174
over = null;
189175
}
190176
191-
/** @param {DragEvent & { currentTarget: HTMLDivElement }} event */
192-
function dragOver(event) {
177+
function dragOver(event: DragEvent & { currentTarget: HTMLDivElement }) {
193178
over = event.currentTarget.id;
194179
}
195180

packages/repl/src/lib/Input/Migrate.svelte

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<script>
1+
<script lang="ts">
22
import { get_repl_context } from '$lib/context.js';
33
44
const { migrate } = get_repl_context();

packages/repl/src/lib/Input/ModuleEditor.svelte

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
<script>
1+
<script lang="ts">
22
import { get_repl_context } from '$lib/context.js';
33
import CodeMirror from '../CodeMirror.svelte';
4+
import type { CompileError, Warning } from 'svelte/compiler';
45
5-
/** @type {import('svelte/compiler').CompileError} */
6-
export let error;
7-
8-
/** @type {import('svelte/compiler').Warning[]} */
9-
export let warnings;
10-
11-
/** @type {boolean} */
12-
export let vim;
6+
export let error: CompileError;
7+
export let warnings: Warning[];
8+
export let vim: boolean;
139
1410
export function focus() {
1511
$module_editor?.focus();

packages/repl/src/lib/Input/RunesInfo.svelte

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,21 @@
1-
<script>
1+
<script lang="ts">
22
import { get_repl_context } from '$lib/context.js';
33
4-
/** @type {boolean} */
5-
export let runes;
4+
let { runes }: { runes: boolean } = $props();
65
7-
let open = false;
6+
let open = $state(false);
87
98
const { selected_name } = get_repl_context();
109
</script>
1110

1211
<svelte:window
13-
on:keydown={(e) => {
12+
onkeydown={(e) => {
1413
if (e.key === 'Escape') open = false;
1514
}}
1615
/>
1716

1817
<div class="container">
19-
<button class:active={runes} class:open on:click={() => (open = !open)}>
18+
<button class:active={runes} class:open onclick={() => (open = !open)}>
2019
<svg viewBox="0 0 24 24">
2120
<path d="M9.4,1H19l-5.9,7.7h8L8.3,23L11,12.6H3.5L9.4,1z" />
2221
</svg>
@@ -27,7 +26,7 @@
2726
{#if open}
2827
<!-- svelte-ignore a11y_click_events_have_key_events a11y_no_static_element_interactions
2928
(This is taken care of by the <svelte:window> above) -->
30-
<div class="modal-backdrop" on:click={() => (open = false)}></div>
29+
<div class="modal-backdrop" onclick={() => (open = false)}></div>
3130
<div class="popup">
3231
{#if $selected_name.endsWith('.svelte.js')}
3332
<p>

packages/repl/src/lib/InputOutputToggle.svelte

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
<script>
2-
export let checked = false;
3-
1+
<script lang="ts">
42
import Checkbox from './Checkbox.svelte';
3+
4+
let { checked = $bindable() }: { checked: boolean } = $props();
55
</script>
66

77
<!-- svelte-ignore a11y_label_has_associated_control -->

packages/repl/src/lib/Message.svelte

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,16 @@
1-
<script>
1+
<script lang="ts">
22
import { slide } from 'svelte/transition';
33
import { get_repl_context } from './context.js';
4+
import type { MessageDetails } from './types.js';
45
5-
/** @type {'info' | 'warning' | 'error'} */
6-
export let kind = 'info';
7-
8-
/** @type {import('./types').MessageDetails | undefined} */
9-
export let details = undefined;
10-
11-
/** @type {string | undefined} */
12-
export let filename = undefined;
13-
6+
export let kind: 'info' | 'warning' | 'error' = 'info';
7+
export let details: MessageDetails | undefined = undefined;
8+
export let filename: string | undefined = undefined;
149
export let truncate = false;
1510
1611
const { go_to_warning_pos } = get_repl_context();
1712
18-
/** @param {import('./types').MessageDetails} details */
19-
function message(details) {
13+
function message(details: MessageDetails) {
2014
let str = details.message || '[missing message]';
2115
2216
let loc = [];

0 commit comments

Comments
 (0)