Created
December 3, 2020 17:48
-
-
Save OliverJAsh/023f2e36f4e47c6862b3d78e7f077b5c to your computer and use it in GitHub Desktop.
ts-morph: Convert named imports to namespace import
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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