From 11eb88594cb903cf7a95c3eef87ac03449ec8a3f Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Wed, 31 Jul 2024 16:32:58 -0700 Subject: [PATCH 1/2] Make notebook validation script handle default imports --- docs/core_docs/scripts/validate_notebook.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/docs/core_docs/scripts/validate_notebook.ts b/docs/core_docs/scripts/validate_notebook.ts index 6f26d940be3a..ad66a39cc8df 100644 --- a/docs/core_docs/scripts/validate_notebook.ts +++ b/docs/core_docs/scripts/validate_notebook.ts @@ -15,15 +15,19 @@ export function extract(filepath: string) { // Deduplicate imports const importDeclarations = sourceFile.getImportDeclarations(); - const uniqueImports = new Map>(); + const uniqueImports = new Map }>(); 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,10 +35,11 @@ export function extract(filepath: string) { importDeclarations.forEach((importDecl) => importDecl.remove()); // Add deduplicated imports at the top - uniqueImports.forEach((namedImports, moduleSpecifier) => { + uniqueImports.forEach(({ default: defaultImport, named }, moduleSpecifier) => { sourceFile.addImportDeclaration({ moduleSpecifier, - namedImports: Array.from(namedImports), + defaultImport, + namedImports: Array.from(named), }); }); From ff216e6c74d30837088004f07513afb757b00e65 Mon Sep 17 00:00:00 2001 From: jacoblee93 Date: Wed, 31 Jul 2024 16:49:51 -0700 Subject: [PATCH 2/2] Format --- docs/core_docs/scripts/validate_notebook.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/docs/core_docs/scripts/validate_notebook.ts b/docs/core_docs/scripts/validate_notebook.ts index ad66a39cc8df..776684ebc73f 100644 --- a/docs/core_docs/scripts/validate_notebook.ts +++ b/docs/core_docs/scripts/validate_notebook.ts @@ -15,7 +15,10 @@ 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(); @@ -35,13 +38,15 @@ export function extract(filepath: string) { importDeclarations.forEach((importDecl) => importDecl.remove()); // Add deduplicated imports at the top - uniqueImports.forEach(({ default: defaultImport, named }, moduleSpecifier) => { - sourceFile.addImportDeclaration({ - moduleSpecifier, - defaultImport, - namedImports: Array.from(named), - }); - }); + uniqueImports.forEach( + ({ default: defaultImport, named }, moduleSpecifier) => { + sourceFile.addImportDeclaration({ + moduleSpecifier, + defaultImport, + namedImports: Array.from(named), + }); + } + ); return sourceFile.getFullText(); }