Skip to content

Instantly share code, notes, and snippets.

@OliverJAsh
Created December 3, 2020 17:48
Show Gist options
  • Save OliverJAsh/023f2e36f4e47c6862b3d78e7f077b5c to your computer and use it in GitHub Desktop.
Save OliverJAsh/023f2e36f4e47c6862b3d78e7f077b5c to your computer and use it in GitHub Desktop.
ts-morph: Convert named imports to namespace import
// https://gist.github.com/OliverJAsh/5de515ad1f81b88409c13cd548c20893
// https://twitter.com/OliverJAsh/status/1334537098469265413
const { Project } = require('ts-morph');
const project = new Project({
tsConfigFilePath: 'tsconfig.app.no-references.json',
});
const PATH_TO_MATCH = '/Users/oliverash/Development/unsplash-web/shared/helpers/booleans.ts';
project.getSourceFiles().forEach((sourceFile) => {
sourceFile.getImportDeclarations().forEach((importDec) => {
if (importDec.wasForgotten()) {
return
}
const importSourceFile = importDec.getModuleSpecifierSourceFile();
if (importSourceFile !== undefined) {
const path = importSourceFile.getFilePath();
if (path === PATH_TO_MATCH) {
const edits = project.getLanguageService().getEditsForRefactor(
sourceFile,
{},
importDec,
"Convert import",
"Convert named imports to namespace import",
{}
);
if (edits != null) {
edits.applyChanges();
}
}
}
});
});
project.save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment