Skip to content

fix: correct display for warnings #376

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions content/tutorial/common/src/__client.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,4 @@ if (import.meta.hot) {
import.meta.hot.on('vite:beforeUpdate', (event) => {
post({ type: 'hmr', data: event.updates });
});

import.meta.hot.on('svelte:warnings', (data) => {
post({ type: 'warnings', data });
});
}
9 changes: 5 additions & 4 deletions content/tutorial/common/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ const config = {
},

vitePlugin: {
experimental: {
// This feature enables compile-time warnings to be
// visible in the learn.svelte.dev editor
sendWarningsToBrowser: true
// This enables compile-time warnings to be
// visible in the learn.svelte.dev editor
onwarn: (warning, defaultHandler) => {
console.log('svelte:warnings:%s', JSON.stringify(warning));
defaultHandler(warning);
}
}
};
Expand Down
50 changes: 49 additions & 1 deletion src/lib/client/adapters/webcontainer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ 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)'
});
Expand All @@ -17,9 +21,10 @@ let vm;
* @param {import('svelte/store').Writable<Error | null>} error
* @param {import('svelte/store').Writable<{ value: number, text: string }>} progress
* @param {import('svelte/store').Writable<string[]>} logs
* @param {import('svelte/store').Writable<Record<string, CompilerWarning[]>>} warnings
* @returns {Promise<import('$lib/types').Adapter>}
*/
export async function create(base, error, progress, logs) {
export async function create(base, error, progress, logs, warnings) {
if (/safari/i.test(navigator.userAgent) && !/chrome/i.test(navigator.userAgent)) {
throw new Error('WebContainers are not supported by Safari');
}
Expand All @@ -45,12 +50,40 @@ export async function create(base, error, progress, logs) {
}
});

/** @type {Record<string, CompilerWarning[]>} */
let $warnings;
warnings.subscribe((value) => $warnings = value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this there's a dependency loop between the adapter and the output. Can we move the warnings store into tutorial/[slug]/adapter.js (like the other stores) to decouple this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the review. I fixed it!


/** @type {any} */
let timeout;

/** @param {number} msec */
function schedule_to_update_warning(msec) {
clearTimeout(timeout);
timeout = setTimeout(() => warnings.set($warnings), msec);
}

const log_stream = () =>
new WritableStream({
write(chunk) {
if (chunk === '\x1B[1;1H') {
// clear screen
logs.set([]);

} else if (chunk?.startsWith('svelte:warnings:')) {
/** @type {CompilerWarning} */
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))) {
current.push(warn);
}

schedule_to_update_warning(100);

} else {
const log = converter.toHtml(escape_html(chunk)).replace(/\n/g, '<br>');
logs.update(($logs) => [...$logs, log]);
Expand Down Expand Up @@ -154,6 +187,17 @@ export async function create(base, error, progress, logs) {
...force_delete
];

// initialize warnings of written files
to_write
.filter((stub) => stub.type === 'file' && $warnings[stub.name])
.forEach((stub) => $warnings[stub.name] = []);
// remove warnings of deleted files
to_delete
.filter((stubname) => $warnings[stubname])
.forEach((stubname) => delete $warnings[stubname]);

warnings.set($warnings);

current_stubs = stubs_to_map(stubs);

// For some reason, server-ready is fired again when the vite dev server is restarted.
Expand Down Expand Up @@ -221,6 +265,10 @@ export async function create(base, error, progress, logs) {

tree[basename] = to_file(file);

// initialize warnings of this file
$warnings[file.name] = [];
schedule_to_update_warning(100);

await vm.mount(root);

current_stubs.set(file.name, file);
Expand Down
3 changes: 2 additions & 1 deletion src/routes/tutorial/[slug]/Editor.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
import { HighlightStyle } from '@codemirror/language';
import { syntaxHighlighting } from '@codemirror/language';
import { afterNavigate, beforeNavigate } from '$app/navigation';
import { files, selected_file, selected_name, update_file, warnings } from './state.js';
import { files, selected_file, selected_name, update_file } from './state.js';
import { warnings } from './adapter.js';
import './codemirror.css';

/** @type {HTMLDivElement} */
Expand Down
6 changes: 0 additions & 6 deletions src/routes/tutorial/[slug]/Output.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import Chrome from './Chrome.svelte';
import Loading from './Loading.svelte';
import { base, error, logs, progress, subscribe } from './adapter';
import { warnings } from './state';

/** @type {import('$lib/types').Exercise} */
export let exercise;
Expand Down Expand Up @@ -63,11 +62,6 @@
}, 1000);
} else if (e.data.type === 'ping-pause') {
clearTimeout(timeout);
} else if (e.data.type === 'warnings') {
warnings.update(($warnings) => ({
...$warnings,
[e.data.data.normalizedFilename]: e.data.data.allWarnings
}));
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/routes/tutorial/[slug]/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,17 @@ export const error = writable(null);
/** @type {import('svelte/store').Writable<string[]>} */
export const logs = writable([]);

/** @type {import('svelte/store').Writable<Record<string, import('./state').CompilerWarning[]>>} */
export const warnings = writable({});

/** @type {Promise<import('$lib/types').Adapter>} */
let ready = new Promise(() => {});

if (browser) {
ready = new Promise(async (fulfil, reject) => {
try {
const module = await import('$lib/client/adapters/webcontainer/index.js');
const adapter = await module.create(base, error, progress, logs);
const adapter = await module.create(base, error, progress, logs, warnings);

fulfil(adapter);
} catch (error) {
Expand Down
3 changes: 0 additions & 3 deletions src/routes/tutorial/[slug]/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ export const files = writable([]);
/** @type {Writable<Record<string, import('$lib/types').Stub>>} */
export const solution = writable({});

/** @type {Writable<Record<string, CompilerWarning[]>>} */
export const warnings = writable({});

/** @type {Writable<{ parent: string, type: 'file' | 'directory' } | null>} */
export const creating = writable(null);

Expand Down