Skip to content

Commit

Permalink
Allow duplicate imports in notebooks
Browse files Browse the repository at this point in the history
  • Loading branch information
jacoblee93 committed Jul 30, 2024
1 parent cb5c83e commit 3947956
Showing 1 changed file with 36 additions and 9 deletions.
45 changes: 36 additions & 9 deletions docs/core_docs/scripts/validate_notebook.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,44 @@
import * as fs from "node:fs";
import * as ts from "typescript";
import { Project } from "ts-morph";

export function extract(filepath: string) {
const cells = JSON.parse(fs.readFileSync(filepath).toString()).cells;
const code = cells
.map((cell: Record<string, any>) => {
if (cell.cell_type === "code") {
return cell.source.join("");
}
return "";
})
.join("\n");
return code;
const project = new Project({ useInMemoryFileSystem: true });
const sourceFile = project.createSourceFile("temp.ts", "");

cells.forEach((cell: Record<string, any>) => {
if (cell.cell_type === "code") {
sourceFile.addStatements(cell.source.join(""));
}
});

// Deduplicate imports
const importDeclarations = sourceFile.getImportDeclarations();
const uniqueImports = new Map<string, Set<string>>();

importDeclarations.forEach(importDecl => {
const moduleSpecifier = importDecl.getModuleSpecifierValue();
if (!uniqueImports.has(moduleSpecifier)) {
uniqueImports.set(moduleSpecifier, new Set());
}
importDecl.getNamedImports().forEach(namedImport => {
uniqueImports.get(moduleSpecifier)!.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),
});
});

return sourceFile.getFullText();
}

let [pathname, ...args] = process.argv.slice(2);
Expand Down

0 comments on commit 3947956

Please sign in to comment.