Skip to content

Commit c3fe44d

Browse files
committed
fix overload duplication
1 parent a3ded94 commit c3fe44d

File tree

7 files changed

+97
-98
lines changed

7 files changed

+97
-98
lines changed

apps/svelte.dev/content/docs/svelte/98-reference/21-svelte-compiler.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,6 @@ function parse(
102102

103103
</div>
104104

105-
## parse
106-
107-
The parse function parses a component, returning only its abstract syntax tree.
108-
109-
The `modern` option (`false` by default in Svelte 5) makes the parser return a modern AST instead of the legacy AST.
110-
`modern` will become `true` by default in Svelte 6, and the option will be removed in Svelte 7.
111-
112105
<div class="ts-block">
113106

114107
```ts

apps/svelte.dev/content/docs/svelte/98-reference/21-svelte-events.md

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,6 @@ function on<Type extends keyof WindowEventMap>(
3232

3333
</div>
3434

35-
## on
36-
37-
Attaches an event handler to the document and returns a function that removes the handler. Using this
38-
rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
39-
(with attributes like `onclick`), which use event delegation for performance reasons
40-
4135
<div class="ts-block">
4236

4337
```ts
@@ -55,12 +49,6 @@ function on<Type extends keyof DocumentEventMap>(
5549

5650
</div>
5751

58-
## on
59-
60-
Attaches an event handler to an element and returns a function that removes the handler. Using this
61-
rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
62-
(with attributes like `onclick`), which use event delegation for performance reasons
63-
6452
<div class="ts-block">
6553

6654
```ts
@@ -81,12 +69,6 @@ function on<
8169

8270
</div>
8371

84-
## on
85-
86-
Attaches an event handler to an element and returns a function that removes the handler. Using this
87-
rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
88-
(with attributes like `onclick`), which use event delegation for performance reasons
89-
9072
<div class="ts-block">
9173

9274
```ts
@@ -107,12 +89,6 @@ function on<
10789

10890
</div>
10991

110-
## on
111-
112-
Attaches an event handler to an element and returns a function that removes the handler. Using this
113-
rather than `addEventListener` will preserve the correct order relative to handlers added declaratively
114-
(with attributes like `onclick`), which use event delegation for performance reasons
115-
11692
<div class="ts-block">
11793

11894
```ts

apps/svelte.dev/content/docs/svelte/98-reference/21-svelte-store.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,6 @@ function derived<S extends Stores, T>(
3939

4040
</div>
4141

42-
## derived
43-
44-
Derived value store by synchronizing one or more readable stores and
45-
applying an aggregation function over its input values.
46-
4742
<div class="ts-block">
4843

4944
```ts
@@ -72,10 +67,6 @@ function fromStore<V>(store: Writable<V>): {
7267

7368
</div>
7469

75-
## fromStore
76-
77-
78-
7970
<div class="ts-block">
8071

8172
```ts
@@ -145,10 +136,6 @@ function toStore<V>(
145136

146137
</div>
147138

148-
## toStore
149-
150-
151-
152139
<div class="ts-block">
153140

154141
```ts

apps/svelte.dev/scripts/sync-docs/types.ts

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
11
import fs from 'node:fs';
22
import ts from 'typescript';
33
import { format } from 'prettier';
4-
import { type Modules } from '@sveltejs/site-kit/markdown';
54
import { strip_origin } from './utils';
6-
7-
interface Extracted {
8-
name: string;
9-
comment: string;
10-
markdown?: string;
11-
snippet: string;
12-
deprecated?: string | null;
13-
children: Extracted[];
14-
bullets?: string[];
15-
}
5+
import type { Modules, Declaration, TypeElement } from '@sveltejs/site-kit/markdown';
166

177
export async function read_types(base: string, modules: Modules) {
188
{
@@ -51,8 +41,8 @@ export async function read_types(base: string, modules: Modules) {
5141
}
5242

5343
export async function get_types(code: string, statements: ts.NodeArray<ts.Statement>) {
54-
const exports: Extracted[] = [];
55-
const types: Extracted[] = [];
44+
const exports: Declaration[] = [];
45+
const types: Declaration[] = [];
5646

5747
if (statements) {
5848
for (const statement of statements) {
@@ -116,7 +106,7 @@ export async function get_types(code: string, statements: ts.NodeArray<ts.Statem
116106
const i = code.indexOf('export', start);
117107
start = i + 6;
118108

119-
let children: Extracted[] = [];
109+
let children: TypeElement[] = [];
120110

121111
let snippet_unformatted = code.slice(start, statement.end).trim();
122112

@@ -162,12 +152,31 @@ export async function get_types(code: string, statements: ts.NodeArray<ts.Statem
162152
? exports
163153
: types;
164154

165-
collection.push({
166-
name,
167-
comment: cleanup_comment(comment),
155+
let declaration = collection.find((statement) => statement.name === name);
156+
157+
if (declaration) {
158+
// TODO tidy these up in the source
159+
if (cleanup_comment(comment) !== declaration.comment) {
160+
console.warn(`${name} overload has mismatched comment`);
161+
}
162+
163+
if (deprecated_notice !== declaration.deprecated) {
164+
console.warn(`${name} overload has mismatched deprecation notices`);
165+
}
166+
} else {
167+
declaration = {
168+
name,
169+
comment: cleanup_comment(comment),
170+
deprecated: deprecated_notice,
171+
overloads: []
172+
};
173+
174+
collection.push(declaration);
175+
}
176+
177+
declaration.overloads.push({
168178
snippet,
169-
children,
170-
deprecated: deprecated_notice
179+
children
171180
});
172181
}
173182
}
@@ -179,13 +188,13 @@ export async function get_types(code: string, statements: ts.NodeArray<ts.Statem
179188
return { types, exports };
180189
}
181190

182-
function munge_type_element(member: ts.TypeElement, depth = 1): Extracted | undefined {
191+
function munge_type_element(member: ts.TypeElement, depth = 1): TypeElement | undefined {
183192
// @ts-ignore
184193
const doc = member.jsDoc?.[0];
185194

186195
if (/private api|DO NOT USE/i.test(doc?.comment)) return;
187196

188-
const children: Extracted[] = [];
197+
const children: TypeElement[] = [];
189198

190199
// @ts-ignore
191200
const name = member.name?.escapedText ?? member.name?.getText() ?? 'unknown';

packages/site-kit/src/lib/components/Text.svelte

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,10 @@
105105
}
106106
}
107107
108+
.ts-block pre {
109+
margin: 0;
110+
}
111+
108112
p code {
109113
max-width: 100%;
110114
display: inline-flex;

packages/site-kit/src/lib/markdown/index.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@ export type Modules = Array<{
1616
name?: string;
1717
comment?: string;
1818
exempt?: boolean;
19-
types?: ModuleChild[];
20-
exports?: ModuleChild[];
19+
types?: Declaration[];
20+
exports?: Declaration[];
2121
}>;
22-
export interface ModuleChild {
22+
23+
export interface Declaration {
24+
name: string;
25+
comment: string;
26+
deprecated?: string | null;
27+
overloads: Array<{
28+
snippet: string;
29+
children: TypeElement[];
30+
}>;
31+
}
32+
33+
export interface TypeElement {
2334
name: string;
2435
snippet: string;
2536
comment: string;
26-
deprecated?: string;
27-
bullets?: string[];
28-
children?: ModuleChild[];
37+
bullets: string[];
38+
children: TypeElement[];
2939
}

packages/site-kit/src/lib/markdown/renderer.ts

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { mkdir, readFile, readdir, stat, writeFile } from 'node:fs/promises';
44
import path from 'node:path';
55
import ts from 'typescript';
66
import { SHIKI_LANGUAGE_MAP, escape, normalizeSlugify, transform } from './utils';
7-
import type { ModuleChild, Modules } from '.';
7+
import type { Declaration, TypeElement, Modules } from './index';
88

99
type MetadataKeys = 'file' | 'link' | 'copy';
1010
type SnippetOptions = Record<MetadataKeys, string | boolean | number | null>;
@@ -119,7 +119,7 @@ export async function render_content_markdown(
119119
{
120120
twoslashBanner,
121121
modules = [],
122-
cacheCodeSnippets = true,
122+
cacheCodeSnippets = false,
123123
resolveTypeLinks
124124
}: RenderContentOptions = {}
125125
) {
@@ -593,7 +593,11 @@ export async function replace_export_type_placeholders(content: string, modules:
593593

594594
return (
595595
exported
596-
?.map((exportVal) => `<div class="ts-block">${fence(exportVal.snippet, 'dts')}</div>`)
596+
?.map((declaration) =>
597+
declaration.overloads
598+
.map((overload) => `<div class="ts-block">${fence(overload.snippet, 'dts')}</div>`)
599+
.join('\n\n')
600+
)
597601
.join('\n\n') ?? ''
598602
);
599603
});
@@ -627,12 +631,16 @@ export async function replace_export_type_placeholders(content: string, modules:
627631
}
628632

629633
return `## ${module.name}\n\n${import_block}\n\n${module.comment}\n\n${module.exports
630-
.map((type) => {
631-
const markdown =
632-
`<div class="ts-block">${fence(type.snippet)}` +
633-
type.children?.map((v) => stringify(v)).join('\n\n') +
634-
`</div>`;
635-
return `### ${type.name}\n\n${type.comment}\n\n${markdown}`;
634+
.map((declaration) => {
635+
const markdown = declaration.overloads
636+
.map(
637+
(overload) =>
638+
`<div class="ts-block">${fence(overload.snippet)}` +
639+
overload.children?.map((v) => stringify(v)).join('\n\n') +
640+
`</div>`
641+
)
642+
.join('\n\n');
643+
return `### ${declaration.name}\n\n${declaration.comment}\n\n${markdown}`;
636644
})
637645
.join('\n\n')}`;
638646
})
@@ -661,12 +669,16 @@ export async function replace_export_type_placeholders(content: string, modules:
661669
}
662670

663671
return `${import_block}\n\n${module.comment}\n\n${module.exports
664-
.map((type) => {
665-
const markdown =
666-
`<div class="ts-block">${fence(type.snippet, 'dts')}` +
667-
type.children?.map((val) => stringify(val, 'dts')).join('\n\n') +
668-
`</div>`;
669-
return `### ${type.name}\n\n${type.comment}\n\n${markdown}`;
672+
.map((declaration) => {
673+
const markdown = declaration.overloads
674+
.map(
675+
(overload) =>
676+
`<div class="ts-block">${fence(overload.snippet, 'dts')}` +
677+
overload.children?.map((val) => stringify(val, 'dts')).join('\n\n') +
678+
`</div>`
679+
)
680+
.join('\n\n');
681+
return `### ${declaration.name}\n\n${declaration.comment}\n\n${markdown}`;
670682
})
671683
.join('\n\n')}`;
672684
});
@@ -696,12 +708,16 @@ function stringify_module(module: Modules[0]) {
696708
content += `${module.comment}\n\n`;
697709
}
698710

699-
for (const e of module.exports || []) {
700-
const markdown =
701-
`<div class="ts-block">${fence(e.snippet)}` +
702-
e.children?.map((v) => stringify(v)).join('\n\n') +
703-
`</div>`;
704-
content += `## ${e.name}\n\n${e.comment}\n\n${markdown}\n\n`;
711+
for (const declaration of module.exports || []) {
712+
const markdown = declaration.overloads
713+
.map(
714+
(overload) =>
715+
`<div class="ts-block">${fence(overload.snippet)}` +
716+
overload.children?.map((v) => stringify(v)).join('\n\n') +
717+
`</div>`
718+
)
719+
.join('\n\n');
720+
content += `## ${declaration.name}\n\n${declaration.comment}\n\n${markdown}\n\n`;
705721
}
706722

707723
for (const t of module.types || []) {
@@ -711,7 +727,7 @@ function stringify_module(module: Modules[0]) {
711727
return content;
712728
}
713729

714-
function stringify_type(t: ModuleChild) {
730+
function stringify_type(t: Declaration) {
715731
let content = '';
716732

717733
if (t.deprecated) {
@@ -722,14 +738,18 @@ function stringify_type(t: ModuleChild) {
722738
content += `${t.comment}\n\n`;
723739
}
724740

725-
if (t.children || t.snippet) {
726-
const children = t.children?.map((val) => stringify(val, 'dts')).join('\n\n');
727-
content += `<div class="ts-block">${fence(t.snippet, 'dts')}` + children + `\n</div>\n\n`;
741+
for (const overload of t.overloads) {
742+
if (overload.children || overload.snippet) {
743+
const children = overload.children?.map((val) => stringify(val, 'dts')).join('\n\n');
744+
content +=
745+
`<div class="ts-block">${fence(overload.snippet, 'dts')}` + children + `\n</div>\n\n`;
746+
}
728747
}
748+
729749
return content;
730750
}
731751

732-
function stringify_expanded_type(type: ModuleChild) {
752+
function stringify_expanded_type(type: TypeElement) {
733753
return (
734754
type.comment +
735755
type.children
@@ -770,7 +790,7 @@ function fence(code: string, lang: keyof typeof SHIKI_LANGUAGE_MAP = 'ts') {
770790
/**
771791
* Helper function for {@link replace_export_type_placeholders}. Renders specifiv members to their markdown/html representation.
772792
*/
773-
function stringify(member: ModuleChild, lang: keyof typeof SHIKI_LANGUAGE_MAP = 'ts'): string {
793+
function stringify(member: TypeElement, lang: keyof typeof SHIKI_LANGUAGE_MAP = 'ts'): string {
774794
if (!member) return '';
775795

776796
// It's important to always use two newlines after a dom tag or else markdown does not render it properly

0 commit comments

Comments
 (0)