From 7a8d620726d4b67bd4560da99cb495fd57354e95 Mon Sep 17 00:00:00 2001 From: Puru Vijay Date: Sat, 15 Apr 2023 02:28:11 +0530 Subject: [PATCH] Make compile-types logic more robust --- .../scripts/type-gen/compile-types.js | 80 ++++++++++--------- .../type-gen/dts-sources/internal.d.ts | 1 - .../scripts/type-gen/dts-sources/runtime.d.ts | 1 - sites/svelte.dev/src/lib/server/docs/index.js | 4 + 4 files changed, 45 insertions(+), 41 deletions(-) delete mode 100644 sites/svelte.dev/scripts/type-gen/dts-sources/internal.d.ts delete mode 100644 sites/svelte.dev/scripts/type-gen/dts-sources/runtime.d.ts diff --git a/sites/svelte.dev/scripts/type-gen/compile-types.js b/sites/svelte.dev/scripts/type-gen/compile-types.js index a829e4dac8..9e2db89af7 100644 --- a/sites/svelte.dev/scripts/type-gen/compile-types.js +++ b/sites/svelte.dev/scripts/type-gen/compile-types.js @@ -23,9 +23,18 @@ export async function get_bundled_types() { const moduleName = (file === 'index.d.ts' ? 'svelte' : `svelte/${file}`).replace('.d.ts', ''); const code = await bundle.generate({ format: 'esm' }).then(({ output }) => output[0].code); - const [inlined_export_declaration_code, ts_source_file] = useExportDeclarations(code); - - codes.set(moduleName, { code: inlined_export_declaration_code, ts_source_file }); + const inlined_export_declaration_code = inlineExportDeclarations(code); + + codes.set(moduleName, { + code: inlined_export_declaration_code, + ts_source_file: ts.createSourceFile( + 'index.d.ts', + inlined_export_declaration_code, + ts.ScriptTarget.ESNext, + true, + ts.ScriptKind.TS + ) + }); // !IMPORTANT: This is for debugging purposes only. // !Do not remove until Svelte d.ts files are stable during v4/v5 @@ -38,9 +47,8 @@ export async function get_bundled_types() { /** * @param {string} str - * @returns {[string, ts.SourceFile]} */ -function useExportDeclarations(str) { +function inlineExportDeclarations(str) { const project = new Project(); const source_file = project.createSourceFile('index.d.ts', str, { overwrite: true }); @@ -51,11 +59,7 @@ function useExportDeclarations(str) { .map((e) => e.getAliasNode()?.getText() ?? e.getNameNode().getText()); // console.log(exportedSymbols); - if (exportedSymbols.length === 0) - return [ - str, - ts.createSourceFile('index.d.ts', str, ts.ScriptTarget.ESNext, true, ts.ScriptKind.TS) - ]; + if (exportedSymbols.length === 0) return str; const aliasedExportedSymbols = new Map(); const namedExports = exportDeclaration.getNamedExports(); @@ -76,39 +80,37 @@ function useExportDeclarations(str) { }); } - const magicStr = new MagicString(source_file.getFullText()); - - // Find all the identifiers from ewport declaration and prefix export before them - const identifiers = [ - ...new Set( - source_file - .getDescendantsOfKind(SyntaxKind.Identifier) - .filter((identifier) => exportedSymbols.includes(identifier.getText())) - .filter( - (value, index, self) => index === self.findIndex((t) => t.getText() === value.getText()) - ) - ) - ]; - - for (const identifier of identifiers) { - magicStr.appendLeft(identifier?.getFirstAncestor()?.getStartLinePos() ?? 0, 'export '); + { + // Get the symbols and their declarations + const exportedSymbols = exportDeclaration + .getNamedExports() + .map((exp) => exp.getSymbolOrThrow()); + + /** @type {import('ts-morph').ExportSpecifier[]} */ + // @ts-ignore + const exportedDeclarations = exportedSymbols.flatMap((symbol) => symbol.getDeclarations()); + + // Add 'export' keyword to the declarations + exportedDeclarations.forEach((declaration) => { + if (!declaration.getFirstDescendantByKind(SyntaxKind.ExportKeyword)) { + for (const target of declaration.getLocalTargetDeclarations()) { + if (target.isKind(SyntaxKind.VariableDeclaration)) { + target.getVariableStatement()?.setIsExported(true); + } else { + // @ts-ignore + target.setIsExported(true); + } + } + } + }); } - magicStr.remove(exportDeclaration?.getStart() ?? 0, exportDeclaration?.getEnd() ?? 0); + exportDeclaration.remove(); // In case it is export declare VERSION = '__VERSION__', replace it with svelte's real version - magicStr.replace('__VERSION__', VERSION); - - return [ - magicStr.toString() ?? str, - ts.createSourceFile( - 'index.d.ts', - magicStr.toString() ?? str, - ts.ScriptTarget.ESNext, - true, - ts.ScriptKind.TS - ) - ]; + const stringified = source_file.getFullText().replace('__VERSION__', VERSION); + + return stringified; } /** diff --git a/sites/svelte.dev/scripts/type-gen/dts-sources/internal.d.ts b/sites/svelte.dev/scripts/type-gen/dts-sources/internal.d.ts deleted file mode 100644 index 10f0475eea..0000000000 --- a/sites/svelte.dev/scripts/type-gen/dts-sources/internal.d.ts +++ /dev/null @@ -1 +0,0 @@ -export type * from 'svelte/internal'; diff --git a/sites/svelte.dev/scripts/type-gen/dts-sources/runtime.d.ts b/sites/svelte.dev/scripts/type-gen/dts-sources/runtime.d.ts deleted file mode 100644 index 2988db0db1..0000000000 --- a/sites/svelte.dev/scripts/type-gen/dts-sources/runtime.d.ts +++ /dev/null @@ -1 +0,0 @@ -export type * from 'svelte'; diff --git a/sites/svelte.dev/src/lib/server/docs/index.js b/sites/svelte.dev/src/lib/server/docs/index.js index d7b5fc20c4..844d78d892 100644 --- a/sites/svelte.dev/src/lib/server/docs/index.js +++ b/sites/svelte.dev/src/lib/server/docs/index.js @@ -128,6 +128,10 @@ export async function get_parsed_docs(docs_data, slug) { injected.push('// @esModuleInterop'); } + if (page.file.includes('svelte.md')) { + injected.push('// @errors: 2304'); + } + // Actions JSDoc examples are invalid. Too many errors, edge cases if (page.file.includes('svelte-action')) { injected.push('// @noErrors');