Make compile-types logic more robust

pull/8452/head
Puru Vijay 3 years ago
parent 8fa8c25851
commit 7a8d620726

@ -23,9 +23,18 @@ export async function get_bundled_types() {
const moduleName = (file === 'index.d.ts' ? 'svelte' : `svelte/${file}`).replace('.d.ts', ''); 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 code = await bundle.generate({ format: 'esm' }).then(({ output }) => output[0].code);
const [inlined_export_declaration_code, ts_source_file] = useExportDeclarations(code); const inlined_export_declaration_code = inlineExportDeclarations(code);
codes.set(moduleName, { code: inlined_export_declaration_code, ts_source_file }); 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. // !IMPORTANT: This is for debugging purposes only.
// !Do not remove until Svelte d.ts files are stable during v4/v5 // !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 * @param {string} str
* @returns {[string, ts.SourceFile]}
*/ */
function useExportDeclarations(str) { function inlineExportDeclarations(str) {
const project = new Project(); const project = new Project();
const source_file = project.createSourceFile('index.d.ts', str, { overwrite: true }); 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()); .map((e) => e.getAliasNode()?.getText() ?? e.getNameNode().getText());
// console.log(exportedSymbols); // console.log(exportedSymbols);
if (exportedSymbols.length === 0) if (exportedSymbols.length === 0) return str;
return [
str,
ts.createSourceFile('index.d.ts', str, ts.ScriptTarget.ESNext, true, ts.ScriptKind.TS)
];
const aliasedExportedSymbols = new Map(); const aliasedExportedSymbols = new Map();
const namedExports = exportDeclaration.getNamedExports(); const namedExports = exportDeclaration.getNamedExports();
@ -76,39 +80,37 @@ function useExportDeclarations(str) {
}); });
} }
const magicStr = new MagicString(source_file.getFullText()); {
// Get the symbols and their declarations
// Find all the identifiers from ewport declaration and prefix export before them const exportedSymbols = exportDeclaration
const identifiers = [ .getNamedExports()
...new Set( .map((exp) => exp.getSymbolOrThrow());
source_file
.getDescendantsOfKind(SyntaxKind.Identifier) /** @type {import('ts-morph').ExportSpecifier[]} */
.filter((identifier) => exportedSymbols.includes(identifier.getText())) // @ts-ignore
.filter( const exportedDeclarations = exportedSymbols.flatMap((symbol) => symbol.getDeclarations());
(value, index, self) => index === self.findIndex((t) => t.getText() === value.getText())
) // Add 'export' keyword to the declarations
) exportedDeclarations.forEach((declaration) => {
]; if (!declaration.getFirstDescendantByKind(SyntaxKind.ExportKeyword)) {
for (const target of declaration.getLocalTargetDeclarations()) {
for (const identifier of identifiers) { if (target.isKind(SyntaxKind.VariableDeclaration)) {
magicStr.appendLeft(identifier?.getFirstAncestor()?.getStartLinePos() ?? 0, 'export '); 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 // In case it is export declare VERSION = '__VERSION__', replace it with svelte's real version
magicStr.replace('__VERSION__', VERSION); const stringified = source_file.getFullText().replace('__VERSION__', VERSION);
return [ return stringified;
magicStr.toString() ?? str,
ts.createSourceFile(
'index.d.ts',
magicStr.toString() ?? str,
ts.ScriptTarget.ESNext,
true,
ts.ScriptKind.TS
)
];
} }
/** /**

@ -1 +0,0 @@
export type * from 'svelte/internal';

@ -1 +0,0 @@
export type * from 'svelte';

@ -128,6 +128,10 @@ export async function get_parsed_docs(docs_data, slug) {
injected.push('// @esModuleInterop'); injected.push('// @esModuleInterop');
} }
if (page.file.includes('svelte.md')) {
injected.push('// @errors: 2304');
}
// Actions JSDoc examples are invalid. Too many errors, edge cases // Actions JSDoc examples are invalid. Too many errors, edge cases
if (page.file.includes('svelte-action')) { if (page.file.includes('svelte-action')) {
injected.push('// @noErrors'); injected.push('// @noErrors');

Loading…
Cancel
Save