From e7b0c8813930fa35c3ebf4ce8c6a56a1c75eb5c9 Mon Sep 17 00:00:00 2001 From: Rich Harris Date: Mon, 27 May 2024 15:11:03 +0200 Subject: [PATCH] tidy up types (#622) --- src/lib/client/adapters/webcontainer/index.js | 36 +++++++------------ src/lib/types/index.d.ts | 11 ++++++ src/routes/tutorial/[slug]/adapter.js | 2 +- src/routes/tutorial/[slug]/state.js | 15 +------- 4 files changed, 26 insertions(+), 38 deletions(-) diff --git a/src/lib/client/adapters/webcontainer/index.js b/src/lib/client/adapters/webcontainer/index.js index 9528f5ed4..1d984d195 100644 --- a/src/lib/client/adapters/webcontainer/index.js +++ b/src/lib/client/adapters/webcontainer/index.js @@ -5,10 +5,6 @@ import * as yootils from 'yootils'; import { escape_html, get_depth } from '../../../utils.js'; import { ready } from '../common/index.js'; -/** - * @typedef {import("../../../../routes/tutorial/[slug]/state.js").CompilerWarning} CompilerWarning - */ - const converter = new AnsiToHtml({ fg: 'var(--sk-text-3)' }); @@ -21,7 +17,7 @@ let vm; * @param {import('svelte/store').Writable} error * @param {import('svelte/store').Writable<{ value: number, text: string }>} progress * @param {import('svelte/store').Writable} logs - * @param {import('svelte/store').Writable>} warnings + * @param {import('svelte/store').Writable>} warnings * @returns {Promise} */ export async function create(base, error, progress, logs, warnings) { @@ -48,9 +44,9 @@ export async function create(base, error, progress, logs, warnings) { } }); - /** @type {Record} */ + /** @type {Record} */ let $warnings; - warnings.subscribe((value) => $warnings = value); + warnings.subscribe((value) => ($warnings = value)); /** @type {any} */ let timeout; @@ -67,21 +63,19 @@ export async function create(base, error, progress, logs, warnings) { if (chunk === '\x1B[1;1H') { // clear screen logs.set([]); - } else if (chunk?.startsWith('svelte:warnings:')) { - /** @type {CompilerWarning} */ + /** @type {import('$lib/types').Warning} */ const warn = JSON.parse(chunk.slice(16)); const current = $warnings[warn.filename]; if (!current) { $warnings[warn.filename] = [warn]; - // the exact same warning may be given multiple times in a row - } else if (!current.some((s) => (s.code === warn.code && s.pos === warn.pos))) { + // the exact same warning may be given multiple times in a row + } else if (!current.some((s) => s.code === warn.code && s.pos === warn.pos)) { current.push(warn); } schedule_to_update_warning(100); - } else { const log = converter.toHtml(escape_html(chunk)).replace(/\n/g, '
'); logs.update(($logs) => [...$logs, log]); @@ -179,16 +173,14 @@ export async function create(base, error, progress, logs, warnings) { // Don't delete the node_modules folder when switching from one exercise to another // where, as this crashes the dev server. const to_delete = [ - ...Array.from(current_stubs.keys()).filter( - (s) => !s.startsWith('/node_modules') - ), + ...Array.from(current_stubs.keys()).filter((s) => !s.startsWith('/node_modules')), ...force_delete ]; // initialize warnings of written files to_write .filter((stub) => stub.type === 'file' && $warnings[stub.name]) - .forEach((stub) => $warnings[stub.name] = []); + .forEach((stub) => ($warnings[stub.name] = [])); // remove warnings of deleted files to_delete .filter((stubname) => $warnings[stubname]) @@ -201,8 +193,8 @@ export async function create(base, error, progress, logs, warnings) { // For some reason, server-ready is fired again when the vite dev server is restarted. // We need to wait for it to finish before we can continue, else we might // request files from Vite before it's ready, leading to a timeout. - const will_restart = launched && - (to_write.some(is_config) || to_delete.some(is_config_path)); + const will_restart = + launched && (to_write.some(is_config) || to_delete.some(is_config_path)); const promise = will_restart ? wait_for_restart_vite() : Promise.resolve(); for (const file of to_delete) { @@ -223,14 +215,13 @@ export async function create(base, error, progress, logs, warnings) { }); }, update: (file) => { - let queue = q_per_file.get(file.name); if (queue) { queue.push(file); return Promise.resolve(false); } - q_per_file.set(file.name, queue = [file]); + q_per_file.set(file.name, (queue = [file])); return q.add(async () => { /** @type {import('@webcontainer/api').FileSystemTree} */ @@ -257,10 +248,9 @@ export async function create(base, error, progress, logs, warnings) { const will_restart = is_config(file); while (queue && queue.length > 0) { - // if the file is updated many times rapidly, get the most recently updated one const file = /** @type {import('$lib/types').FileStub} */ (queue.pop()); - queue.length = 0 + queue.length = 0; tree[basename] = to_file(file); @@ -280,7 +270,7 @@ export async function create(base, error, progress, logs, warnings) { await new Promise((f) => setTimeout(f, 50)); } - q_per_file.delete(file.name) + q_per_file.delete(file.name); return will_restart; }); diff --git a/src/lib/types/index.d.ts b/src/lib/types/index.d.ts index 029dc7d8d..43ddef53c 100644 --- a/src/lib/types/index.d.ts +++ b/src/lib/types/index.d.ts @@ -78,3 +78,14 @@ export interface EditingConstraints { create: Set; remove: Set; } + +// TODO replace with `Warning` from `svelte/compiler` +export interface Warning { + code: string; + start: { line: number; column: number; character: number }; + end: { line: number; column: number; character: number }; + pos: number; + filename: string; + frame: string; + message: string; +} diff --git a/src/routes/tutorial/[slug]/adapter.js b/src/routes/tutorial/[slug]/adapter.js index 891a3eddb..f133d4cc6 100644 --- a/src/routes/tutorial/[slug]/adapter.js +++ b/src/routes/tutorial/[slug]/adapter.js @@ -15,7 +15,7 @@ export const error = writable(null); /** @type {import('svelte/store').Writable} */ export const logs = writable([]); -/** @type {import('svelte/store').Writable>} */ +/** @type {import('svelte/store').Writable>} */ export const warnings = writable({}); /** @type {Promise} */ diff --git a/src/routes/tutorial/[slug]/state.js b/src/routes/tutorial/[slug]/state.js index 629a4d863..e321c3892 100644 --- a/src/routes/tutorial/[slug]/state.js +++ b/src/routes/tutorial/[slug]/state.js @@ -6,19 +6,6 @@ import * as adapter from './adapter.js'; * @typedef {import('svelte/store').Writable} Writable */ -// TODO would be nice if svelte exported this type (maybe it does already?) -/** - * @typedef {{ - * code: string; - * start: { line: number, column: number, character: number }; - * end: { line: number, column: number, character: number }; - * pos: number; - * filename: string; - * frame: string; - * message: string; - * }} CompilerWarning - */ - /** @type {Writable} */ export const files = writable([]); @@ -98,4 +85,4 @@ export function create_directories(name, files) { } return directories; -} \ No newline at end of file +}