From 181b541b639e8f9d2f7037332e6eb4f0dee02095 Mon Sep 17 00:00:00 2001 From: Puru Vijay Date: Sun, 30 Apr 2023 17:19:02 +0530 Subject: [PATCH] Fix svelte component types --- site/content/docs/03-runtime/01-svelte.md | 4 ++ sites/svelte.dev/scripts/type-gen/index.js | 57 +++++++++++++++++++ sites/svelte.dev/src/lib/server/docs/index.js | 8 ++- .../src/routes/docs/[slug]/OnThisPage.svelte | 2 +- 4 files changed, 69 insertions(+), 2 deletions(-) diff --git a/site/content/docs/03-runtime/01-svelte.md b/site/content/docs/03-runtime/01-svelte.md index 07909554da..8d3a6d1f35 100644 --- a/site/content/docs/03-runtime/01-svelte.md +++ b/site/content/docs/03-runtime/01-svelte.md @@ -222,3 +222,7 @@ Events can be cancelable by passing a third parameter to the dispatch function. } ``` + +## Types + +> TYPES: svelte diff --git a/sites/svelte.dev/scripts/type-gen/index.js b/sites/svelte.dev/scripts/type-gen/index.js index c9b700b7c9..45348c99a2 100644 --- a/sites/svelte.dev/scripts/type-gen/index.js +++ b/sites/svelte.dev/scripts/type-gen/index.js @@ -319,6 +319,63 @@ const bundled_types = await get_bundled_types(); modules.sort((a, b) => (a.name < b.name ? -1 : 1)); +// Fix the duplicate/messed up types +// !NOTE: This relies on mutation of `modules` +$: { + const module_with_SvelteComponent = modules.find((m) => + m.types.filter((t) => t.name === 'SvelteComponent') + ); + + if (!module_with_SvelteComponent) break $; + + const svelte_comp_part = module_with_SvelteComponent?.types.filter( + (t) => t.name === 'SvelteComponent' + ); + + if (svelte_comp_part?.[1]) { + // Take the comment from [0], and insert into [1]. Then delete [0] + svelte_comp_part[1].comment = svelte_comp_part?.[0].comment; + delete svelte_comp_part[0]; + svelte_comp_part.reverse(); + svelte_comp_part.length = 1; + + module_with_SvelteComponent.types = module_with_SvelteComponent?.types.filter( + (t) => t.name !== 'SvelteComponent' + ); + + module_with_SvelteComponent.types.push(svelte_comp_part[0]); + module_with_SvelteComponent.types.sort((a, b) => (a.name < b.name ? -1 : 1)); + } +} + +// Fix the duplicate/messed up types +// !NOTE: This relies on mutation of `modules` +$: { + const module_with_SvelteComponentTyped = modules.find((m) => + m.types.filter((t) => t.name === 'SvelteComponentTyped') + ); + + if (!module_with_SvelteComponentTyped) break $; + + const svelte_comp_typed_part = module_with_SvelteComponentTyped?.types.filter( + (t) => t.name === 'SvelteComponentTyped' + ); + + if (svelte_comp_typed_part?.[1]) { + // Take the comment from [1], and insert into [0]. Then delete [1] + svelte_comp_typed_part[0].comment = svelte_comp_typed_part?.[1].comment; + delete svelte_comp_typed_part[1]; + svelte_comp_typed_part.length = 1; + + module_with_SvelteComponentTyped.types = module_with_SvelteComponentTyped?.types.filter( + (t) => t.name !== 'SvelteComponentTyped' + ); + + module_with_SvelteComponentTyped.types.push(svelte_comp_typed_part[0]); + module_with_SvelteComponentTyped.types.sort((a, b) => (a.name < b.name ? -1 : 1)); + } +} + try { fs.mkdirSync(new URL('../../src/lib/generated', import.meta.url), { recursive: true }); } catch {} diff --git a/sites/svelte.dev/src/lib/server/docs/index.js b/sites/svelte.dev/src/lib/server/docs/index.js index 844d78d892..572e18b074 100644 --- a/sites/svelte.dev/src/lib/server/docs/index.js +++ b/sites/svelte.dev/src/lib/server/docs/index.js @@ -110,7 +110,9 @@ export async function get_parsed_docs(docs_data, slug) { try { const injected = []; - if (source.includes('svelte')) { + console.log(page.file); + + if (/(svelte)/.test(source) || page.file.includes('typescript')) { injected.push( `// @filename: ambient.d.ts`, `/// `, @@ -137,6 +139,10 @@ export async function get_parsed_docs(docs_data, slug) { injected.push('// @noErrors'); } + if (page.file.includes('typescript')) { + injected.push('// @errors: 2304'); + } + if (injected.length) { const injected_str = injected.join('\n'); if (source.includes('// @filename:')) { diff --git a/sites/svelte.dev/src/routes/docs/[slug]/OnThisPage.svelte b/sites/svelte.dev/src/routes/docs/[slug]/OnThisPage.svelte index 09422dc344..2c70e79fef 100644 --- a/sites/svelte.dev/src/routes/docs/[slug]/OnThisPage.svelte +++ b/sites/svelte.dev/src/routes/docs/[slug]/OnThisPage.svelte @@ -41,7 +41,7 @@ function update() { content = document.querySelector('.content'); const { top } = content.getBoundingClientRect(); - headings = content.querySelectorAll('h2[id], h3[id]'); + headings = content.querySelectorAll('h2[id]'); positions = Array.from(headings).map((heading) => { const style = getComputedStyle(heading); return heading.getBoundingClientRect().top - parseFloat(style.scrollMarginTop) - top;