Skip to content

Commit

Permalink
ci[patch]: Make notebook validation script handle default imports (#6302
Browse files Browse the repository at this point in the history
)

* Make notebook validation script handle default imports

* Format
  • Loading branch information
jacoblee93 authored Aug 1, 2024
1 parent 64cdc86 commit fa376c6
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions docs/core_docs/scripts/validate_notebook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,38 @@ export function extract(filepath: string) {

// Deduplicate imports
const importDeclarations = sourceFile.getImportDeclarations();
const uniqueImports = new Map<string, Set<string>>();
const uniqueImports = new Map<
string,
{ default?: string; named: Set<string> }
>();

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());
});
});

// Remove all existing imports
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();
}
Expand Down

0 comments on commit fa376c6

Please sign in to comment.