diff --git a/docs/core_docs/scripts/validate_notebook.ts b/docs/core_docs/scripts/validate_notebook.ts index 6f26d940be3a..776684ebc73f 100644 --- a/docs/core_docs/scripts/validate_notebook.ts +++ b/docs/core_docs/scripts/validate_notebook.ts @@ -15,15 +15,22 @@ export function extract(filepath: string) { // Deduplicate imports const importDeclarations = sourceFile.getImportDeclarations(); - const uniqueImports = new Map>(); + const uniqueImports = new Map< + string, + { default?: string; named: Set } + >(); importDeclarations.forEach((importDecl) => { const moduleSpecifier = importDecl.getModuleSpecifierValue(); if (!uniqueImports.has(moduleSpecifier)) { - uniqueImports.set(moduleSpecifier, new Set()); + uniqueImports.set(moduleSpecifier, { named: new Set() }); + } + const defaultImport = importDecl.getDefaultImport(); + if (defaultImport) { + uniqueImports.get(moduleSpecifier)!.default = defaultImport.getText(); } importDecl.getNamedImports().forEach((namedImport) => { - uniqueImports.get(moduleSpecifier)!.add(namedImport.getText()); + uniqueImports.get(moduleSpecifier)!.named.add(namedImport.getText()); }); }); @@ -31,12 +38,15 @@ export function extract(filepath: string) { importDeclarations.forEach((importDecl) => importDecl.remove()); // Add deduplicated imports at the top - uniqueImports.forEach((namedImports, moduleSpecifier) => { - sourceFile.addImportDeclaration({ - moduleSpecifier, - namedImports: Array.from(namedImports), - }); - }); + uniqueImports.forEach( + ({ default: defaultImport, named }, moduleSpecifier) => { + sourceFile.addImportDeclaration({ + moduleSpecifier, + defaultImport, + namedImports: Array.from(named), + }); + } + ); return sourceFile.getFullText(); }