Skip to content

Commit f47258c

Browse files
authored
don't blindly ignore errors in docs (#8070)
* dont blindly ignore errors in docs - closes #8050 * fixes
1 parent 513ff59 commit f47258c

File tree

7 files changed

+59
-23
lines changed

7 files changed

+59
-23
lines changed

documentation/docs/30-advanced/40-service-workers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Inside the service worker you have access to the [`$service-worker` module](/doc
2121
The following example caches the built app and any files in `static` eagerly, and caches all other requests as they happen. This would make each page work offline once visited.
2222

2323
```js
24-
// @ts-nocheck Official TS Service Worker typings are still a work in progress.
24+
// @errors: 2339
2525
import { build, files, version } from '$service-worker';
2626

2727
// Create a unique cache name for this deployment

documentation/docs/30-advanced/50-server-only-modules.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ export const atlantisCoordinates = [/* redacted */];
2626
```
2727

2828
```js
29-
// @errors: 2307 7006
29+
// @errors: 2307 7006 7005
3030
/// file: src/routes/utils.js
3131
export { atlantisCoordinates } from '$lib/server/secrets.js';
3232

documentation/docs/60-appendix/10-migrating.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ declare module 'html-minifier';
160160
// @filename: index.js
161161
// ---cut---
162162
import { minify } from 'html-minifier';
163-
import { prerendering } from '$app/environment';
163+
import { building } from '$app/environment';
164164

165165
const minification_options = {
166166
collapseBooleanAttributes: true,
@@ -183,16 +183,16 @@ const minification_options = {
183183

184184
/** @type {import('@sveltejs/kit').Handle} */
185185
export async function handle({ event, resolve }) {
186-
const response = await resolve(event);
187-
188-
if (prerendering && response.headers.get('content-type') === 'text/html') {
189-
return new Response(minify(await response.text(), minification_options), {
190-
status: response.status,
191-
headers: response.headers
192-
});
193-
}
194-
195-
return response;
186+
let page = '';
187+
188+
return resolve(event, {
189+
transformPageChunk: ({ html, done }) => {
190+
html += page;
191+
if (done) {
192+
return building ? minify(page, minification_options) : page;
193+
}
194+
}
195+
});
196196
}
197197
```
198198

packages/kit/types/ambient.d.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,17 @@ declare module '$app/forms' {
160160
* Usage:
161161
*
162162
* ```js
163-
* const response = await fetch('/form?/action', { method: 'POST', body: formData });
164-
* const result = deserialize(await response.text());
163+
* import { deserialize } from '$app/forms';
164+
*
165+
* async function handleSubmit(event) {
166+
* const response = await fetch('/form?/action', {
167+
* method: 'POST',
168+
* body: new FormData(event.target)
169+
* });
170+
*
171+
* const result = deserialize(await response.text());
172+
* // ...
173+
* }
165174
* ```
166175
*/
167176
export function deserialize<
@@ -220,6 +229,8 @@ declare module '$app/navigation' {
220229
*
221230
* ```ts
222231
* // Example: Match '/path' regardless of the query parameters
232+
* import { invalidate } from '$app/navigation';
233+
*
223234
* invalidate((url) => url.pathname === '/path');
224235
* ```
225236
* @param url The invalidated URL
@@ -356,6 +367,7 @@ declare module '@sveltejs/kit/hooks' {
356367
* /// file: src/hooks.server.js
357368
* import { sequence } from '@sveltejs/kit/hooks';
358369
*
370+
* /// type: import('@sveltejs/kit').Handle
359371
* async function first({ event, resolve }) {
360372
* console.log('first pre-processing');
361373
* const result = await resolve(event, {
@@ -369,6 +381,7 @@ declare module '@sveltejs/kit/hooks' {
369381
* return result;
370382
* }
371383
*
384+
* /// type: import('@sveltejs/kit').Handle
372385
* async function second({ event, resolve }) {
373386
* console.log('second pre-processing');
374387
* const result = await resolve(event, {

packages/kit/types/index.d.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -464,8 +464,7 @@ export interface KitConfig {
464464
* }
465465
*
466466
* // otherwise fail the build
467-
* throw new Error(message);
468-
* }
467+
* throw new Error(message);
469468
* }
470469
* }
471470
* }

packages/kit/types/private.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,12 @@ export interface PrerenderHttpErrorHandler {
192192
path: string;
193193
referrer: string | null;
194194
referenceType: 'linked' | 'fetched';
195+
message: string;
195196
}): void;
196197
}
197198

198199
export interface PrerenderMissingIdHandler {
199-
(details: { path: string; id: string; referrers: string[] }): void;
200+
(details: { path: string; id: string; referrers: string[]; message: string }): void;
200201
}
201202

202203
export type PrerenderHttpErrorHandlerValue = 'fail' | 'warn' | 'ignore' | PrerenderHttpErrorHandler;

sites/kit.svelte.dev/src/lib/docs/server/index.js

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,28 @@ export async function read_file(file) {
119119
} else if (language === 'js' || language === 'ts') {
120120
try {
121121
const injected = [];
122-
if (source.includes('$app/') || source.includes('@sveltejs/kit/')) {
122+
if (
123+
source.includes('$app/') ||
124+
source.includes('$service-worker') ||
125+
source.includes('@sveltejs/kit/')
126+
) {
123127
injected.push(
124128
`// @filename: ambient-kit.d.ts`,
125129
`/// <reference types="@sveltejs/kit" />`
126130
);
127131
}
132+
133+
if (source.includes('$env/')) {
134+
// TODO we're hardcoding static env vars that are used in code examples
135+
// in the types, which isn't... totally ideal, but will do for now
136+
injected.push(
137+
`declare module '$env/dynamic/private' { export const env: Record<string, string> }`,
138+
`declare module '$env/dynamic/public' { export const env: Record<string, string> }`,
139+
`declare module '$env/static/private' { export const API_KEY: string }`,
140+
`declare module '$env/static/public' { export const PUBLIC_BASE_URL: string }`
141+
);
142+
}
143+
128144
if (source.includes('./$types') && !source.includes('@filename: $types.d.ts')) {
129145
const params = parse_route_id(options.file || `+page.${language}`)
130146
.params.map((param) => `${param.name}: string`)
@@ -142,11 +158,18 @@ export async function read_file(file) {
142158
`export type Actions = Kit.Actions<{${params}}>;`
143159
);
144160
}
145-
if (!options.file) {
146-
// No named file -> assume that the code is not meant to be type checked
147-
// If we don't do this, twoslash would throw errors for e.g. some snippets in `types/ambient.d.ts`
148-
injected.push('// @noErrors');
161+
162+
// special case — we need to make allowances for code snippets coming
163+
// from e.g. ambient.d.ts
164+
if (file.endsWith('30-modules.md')) {
165+
injected.push('// @errors: 7006 7031');
166+
}
167+
168+
// another special case
169+
if (source.includes('$lib/types')) {
170+
injected.push(`declare module '$lib/types' { export interface User {} }`);
149171
}
172+
150173
if (injected.length) {
151174
const injected_str = injected.join('\n');
152175
if (source.includes('// @filename:')) {

0 commit comments

Comments
 (0)