Skip to content

Commit 2059b50

Browse files
authored
fix: get twoslash to work properly with JSDoc (#1959)
* fix: get twoslash to work properly with JSDoc * fix * fix redactions * fixes * various annoying fixes * more * more
1 parent 6a6fd25 commit 2059b50

11 files changed

Lines changed: 75 additions & 23 deletions

File tree

apps/svelte.dev/content/blog/2023-03-09-zero-config-type-safety.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ This is where our automatic type generation comes in. Every route directory has
4444

4545
```js
4646
/// file: src/routes/blog/[slug]/+page.server.ts
47+
const database = {
48+
getPost(slug: string | undefined): Promise<string> {
49+
return Promise.resolve('hello world');
50+
}
51+
};
52+
// ---cut---
4753
---import type { ServerLoadEvent } from '@sveltejs/kit';---
4854
+++import type { PageServerLoadEvent } from './$types';+++
4955

@@ -133,7 +139,14 @@ src/
133139
Thanks to the automatic type generation we get advanced type safety. Wouldn't it be great though if we could just omit writing the types at all? As of today you can do exactly that:
134140

135141
```js
142+
// @errors: 7006
136143
/// file: src/routes/blog/[slug]/+page.server.ts
144+
const database = {
145+
getPost(slug: string | undefined): Promise<string> {
146+
return Promise.resolve('hello world');
147+
}
148+
};
149+
// ---cut---
137150
---import type { PageServerLoadEvent } from './$types';---
138151

139152
export async function load(event---: PageServerLoadEvent---) {

apps/svelte.dev/content/docs/kit/20-core-concepts/60-remote-functions.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,14 @@ In addition to declarative schema validation, you can programmatically mark fiel
524524
- It accepts multiple arguments that can be strings (for issues relating to the form as a whole — these will only show up in `fields.allIssues()`) or standard-schema-compliant issues (for those relating to a specific field). Use the `issue` parameter for type-safe creation of such issues:
525525
526526
```js
527+
// @errors: 18046
527528
/// file: src/routes/shop/data.remote.js
529+
// @filename: ambient.d.ts
530+
declare module '$lib/server/database' {
531+
export function buy(qty: number): Promise<void>
532+
}
533+
// @filename: index.js
534+
// ---cut---
528535
import * as v from 'valibot';
529536
import { invalid } from '@sveltejs/kit';
530537
import { form } from '$app/server';
@@ -1166,7 +1173,7 @@ As long as _you're_ not passing invalid data to your remote functions, there are
11661173
In the second case, we don't want to give the attacker any help, so SvelteKit will generate a generic [400 Bad Request](https://http.dog/400) response. You can control the message by implementing the [`handleValidationError`](hooks#Server-hooks-handleValidationError) server hook, which, like [`handleError`](hooks#Shared-hooks-handleError), must return an [`App.Error`](errors#Type-safety) (which defaults to `{ message: string }`):
11671174
11681175
```js
1169-
/// file: src/hooks.server.ts
1176+
/// file: src/hooks.server.js
11701177
/** @type {import('@sveltejs/kit').HandleValidationError} */
11711178
export function handleValidationError({ event, issues }) {
11721179
return {
@@ -1193,13 +1200,25 @@ Inside `query`, `form` and `command` you can use [`getRequestEvent`]($app-server
11931200
11941201
```ts
11951202
/// file: user.remote.ts
1203+
// @filename: ambient.d.ts
1204+
interface User {
1205+
name: string;
1206+
avatar: string;
1207+
}
1208+
1209+
declare module '$lib/server/database' {
1210+
export function findUser(sessionId: string | undefined): Promise<User | null>;
1211+
}
1212+
1213+
// @filename: index.js
1214+
// ---cut---
11961215
import { getRequestEvent, query } from '$app/server';
11971216
import { findUser } from '$lib/server/database';
11981217

11991218
export const getProfile = query(async () => {
12001219
const user = await getUser();
12011220

1202-
return {
1221+
return user && {
12031222
name: user.name,
12041223
avatar: user.avatar
12051224
};

apps/svelte.dev/content/docs/kit/25-build-and-deploy/50-adapter-static.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This will prerender your entire site as a collection of static files. If you'd l
1212
Install with `npm i -D @sveltejs/adapter-static`, then add the adapter to your `svelte.config.js`:
1313

1414
```js
15+
// @errors: 2307
1516
/// file: svelte.config.js
1617
import adapter from '@sveltejs/adapter-static';
1718

@@ -56,6 +57,7 @@ Some platforms have zero-config support (more to come in future):
5657
On these platforms, you should omit the adapter options so that `adapter-static` can provide the optimal configuration:
5758

5859
```js
60+
// @errors: 2307
5961
/// file: svelte.config.js
6062
import adapter from '@sveltejs/adapter-static';
6163

@@ -81,7 +83,7 @@ The directory to write static assets (the contents of `static`, plus client-side
8183

8284
### fallback
8385

84-
To create a [single page app (SPA)](single-page-apps) you must specify the name of the fallback page to be generated by SvelteKit, which is used as the entry point for URLs that have not been prerendered. This is commonly `200.html`, but can vary depending on your deployment platform. You should avoid `index.html` where possible to avoid conflicting with a prerendered homepage.
86+
To create a [single page app (SPA)](single-page-apps) you must specify the name of the fallback page to be generated by SvelteKit, which is used as the entry point for URLs that have not been prerendered. This is commonly `200.html`, but can vary depending on your deployment platform. You should avoid `index.html` where possible to avoid conflicting with a prerendered homepage.
8587

8688
> This option has large negative performance and SEO impacts. It is only recommended in certain circumstances such as wrapping the site in a mobile app. See the [single page apps](single-page-apps) documentation for more details and alternatives.
8789
@@ -102,7 +104,7 @@ You'll also want to generate a fallback `404.html` page to replace the default 4
102104
A config for GitHub Pages might look like the following:
103105

104106
```js
105-
// @errors: 2322
107+
// @errors: 2307 2322
106108
/// file: svelte.config.js
107109
import adapter from '@sveltejs/adapter-static';
108110

apps/svelte.dev/content/docs/kit/25-build-and-deploy/55-single-page-apps.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ export const ssr = false;
2020
If you don't have any server-side logic (i.e. `+page.server.js`, `+layout.server.js` or `+server.js` files) you can use [`adapter-static`](adapter-static) to create your SPA. Install `adapter-static` with `npm i -D @sveltejs/adapter-static` and add it to your `svelte.config.js` with the `fallback` option:
2121

2222
```js
23+
// @errors: 2307
2324
/// file: svelte.config.js
2425
import adapter from '@sveltejs/adapter-static';
2526

apps/svelte.dev/content/docs/kit/25-build-and-deploy/60-adapter-cloudflare.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ This adapter will be installed by default when you use [`adapter-auto`](adapter-
1818
Install with `npm i -D @sveltejs/adapter-cloudflare`, then add the adapter to your `svelte.config.js`:
1919

2020
```js
21+
// @errors: 2307
2122
/// file: svelte.config.js
2223
import adapter from '@sveltejs/adapter-cloudflare';
2324

apps/svelte.dev/content/docs/kit/25-build-and-deploy/80-adapter-netlify.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ This adapter will be installed by default when you use [`adapter-auto`](adapter-
1212
Install with `npm i -D @sveltejs/adapter-netlify`, then add the adapter to your `svelte.config.js`:
1313

1414
```js
15+
// @errors: 2307
1516
/// file: svelte.config.js
1617
import adapter from '@sveltejs/adapter-netlify';
1718

@@ -54,6 +55,7 @@ New projects will use the current Node LTS version by default. However, if you'r
5455
SvelteKit supports [Netlify Edge Functions](https://docs.netlify.com/build/edge-functions/overview/). If you pass the option `edge: true` to the `adapter` function, server-side rendering will happen in a Deno-based edge function that's deployed close to the site visitor. If set to `false` (the default), the site will deploy to Node-based Netlify Functions.
5556

5657
```js
58+
// @errors: 2307
5759
/// file: svelte.config.js
5860
import adapter from '@sveltejs/adapter-netlify';
5961

apps/svelte.dev/content/docs/kit/30-advanced/68-observability.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ To view your first trace, you'll need to set up a local collector. We'll use [Ja
7575
- Create `src/instrumentation.server.js` with the following:
7676

7777
```js
78+
// @errors: 2307
7879
/// file: src/instrumentation.server.js
7980
import { NodeSDK } from '@opentelemetry/sdk-node';
8081
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
@@ -98,4 +99,4 @@ Now, server-side requests will begin generating traces, which you can view in Ja
9899
99100
## `@opentelemetry/api`
100101
101-
SvelteKit uses `@opentelemetry/api` to generate its spans. This is declared as an optional peer dependency so that users not needing traces see no impact on install size or runtime performance. In most cases, if you're configuring your application to collect SvelteKit's spans, you'll end up installing a library like `@opentelemetry/sdk-node` or `@vercel/otel`, which in turn depend on `@opentelemetry/api`, which will satisfy SvelteKit's dependency as well. If you see an error from SvelteKit telling you it can't find `@opentelemetry/api`, it may just be because you haven't set up your trace collection yet. If you _have_ done that and are still seeing the error, you can install `@opentelemetry/api` yourself.
102+
SvelteKit uses `@opentelemetry/api` to generate its spans. This is declared as an optional peer dependency so that users not needing traces see no impact on install size or runtime performance. In most cases, if you're configuring your application to collect SvelteKit's spans, you'll end up installing a library like `@opentelemetry/sdk-node` or `@vercel/otel`, which in turn depend on `@opentelemetry/api`, which will satisfy SvelteKit's dependency as well. If you see an error from SvelteKit telling you it can't find `@opentelemetry/api`, it may just be because you haven't set up your trace collection yet. If you _have_ done that and are still seeing the error, you can install `@opentelemetry/api` yourself.

apps/svelte.dev/content/docs/kit/40-best-practices/10-accessibility.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,13 @@ If your content is available in multiple languages, you should set the `lang` at
6666
6767
```js
6868
/// file: src/hooks.server.js
69-
/**
70-
* @param {import('@sveltejs/kit').RequestEvent} event
71-
*/
72-
function get_lang(event) {
69+
// @filename: utils.ts
70+
export function get_lang(event: import('@sveltejs/kit').RequestEvent) {
7371
return 'en';
7472
}
73+
74+
// @filename: hooks.server.js
75+
import { get_lang } from './utils';
7576
// ---cut---
7677
/** @type {import('@sveltejs/kit').Handle} */
7778
export function handle({ event, resolve }) {

apps/svelte.dev/src/lib/server/renderer.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export const render_content = (
4545
injected.push(
4646
`declare module '$env/dynamic/private' { export const env: Record<string, string> }`,
4747
`declare module '$env/dynamic/public' { export const env: Record<string, string> }`,
48-
`declare module '$env/static/private' { export const API_KEY: string }`,
48+
`declare module '$env/static/private' { export const API_KEY: string; export const VERCEL_COMMIT_REF: string }`,
4949
`declare module '$env/static/public' { export const PUBLIC_BASE_URL: string }`
5050
);
5151
}
@@ -56,8 +56,10 @@ export const render_content = (
5656
`import type * as Kit from '@sveltejs/kit';`,
5757
`export type PageLoad = Kit.Load<Record<string, any>>;`,
5858
`export type PageServerLoad = Kit.ServerLoad<Record<string, any>>;`,
59+
`export type PageServerLoadEvent = Parameters<PageServerLoad>[0];`,
5960
`export type LayoutLoad = Kit.Load<Record<string, any>>;`,
6061
`export type LayoutServerLoad = Kit.ServerLoad<Record<string, any>>;`,
62+
`export type LayoutServerLoadEvent = Parameters<LayoutServerLoad>[0];`,
6163
`export type RequestHandler = Kit.RequestHandler<Record<string, any>>;`,
6264
`export type Action = Kit.Action<Record<string, any>>;`,
6365
`export type Actions = Kit.Actions<Record<string, any>>;`,

packages/site-kit/src/lib/docs/Tooltip.svelte

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
font: var(--sk-font-body-small);
9696
}
9797
98-
.tags {
98+
.twoslash-popup-docs-tags {
9999
display: grid;
100100
grid-template-columns: auto 1fr;
101101
column-gap: 1rem;
@@ -105,10 +105,6 @@
105105
.param {
106106
font: var(--sk-font-mono);
107107
}
108-
109-
.tag {
110-
min-width: 8rem;
111-
}
112108
}
113109
114110
/* Disable highlight styles inside tooltips (popup content) */

0 commit comments

Comments
 (0)