-
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(codemod): add
suid-imports
action
- Loading branch information
Showing
4 changed files
with
132 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@suid/codemod": minor | ||
--- | ||
|
||
Add `suid-imports` action |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import transformSuidImports from "../transforms/transformSuidImports"; | ||
import applyTransforms from "../utils/applyTransforms.js"; | ||
import colorize from "../utils/colorize.js"; | ||
import fg from "fast-glob"; | ||
import { readFile, writeFile } from "fs/promises"; | ||
import micromatch from "micromatch"; | ||
import { normalize, relative } from "path"; | ||
|
||
export default async function suidImports(options: { | ||
cwd: string; | ||
filters: string[]; | ||
write: boolean; | ||
}) { | ||
const inputPath = normalize(options.cwd); | ||
const inPattern = inputPath.replaceAll("\\", "/") + "/**/*"; | ||
const entries = fg.stream(inPattern, { | ||
dot: true, | ||
ignore: ["**/node_modules/**"], | ||
}); | ||
|
||
let files = 0; | ||
for await (const entry of entries) { | ||
const path = normalize(entry.toString()); | ||
const validExt = /\.[jt]sx?$/.test(path); | ||
if (!validExt) continue; | ||
|
||
const rpath = relative(inputPath, path); | ||
const matches = | ||
!options.filters.length || !!micromatch([rpath], options.filters).length; | ||
|
||
if (!matches) continue; | ||
|
||
console.log(`+ ${colorize(rpath)}`); | ||
files++; | ||
|
||
const contents = (await readFile(path)).toString(); | ||
let imports = 0; | ||
const newContents = applyTransforms(contents, [ | ||
async (source) => { | ||
return await transformSuidImports(source, { | ||
format: "named", | ||
onImport(module) { | ||
imports++; | ||
console.log(`# ${colorize(module, "yellow")} `); | ||
}, | ||
}); | ||
}, | ||
]); | ||
if (options.write && imports) await writeFile(path, newContents); | ||
} | ||
if (!files) throw new Error(`No files found`); | ||
} |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import type { SourceFile } from "ts-morph"; | ||
|
||
function createImportsMap() { | ||
const map: Record<string, Record<string, string>> = {}; | ||
|
||
return { | ||
map, | ||
add(module: string, named: string, alias: string) { | ||
if (!map[module]) map[module] = {}; | ||
map[module][named] = alias; | ||
}, | ||
}; | ||
} | ||
|
||
export default async function transformSuidImports( | ||
source: SourceFile, | ||
options: { | ||
format: "named"; | ||
onImport?: (moduleSpecifier: string) => false | void; | ||
dryRun?: boolean; | ||
} | ||
) { | ||
const allImports = source.getImportDeclarations(); | ||
const imports = createImportsMap(); | ||
|
||
for (const node of allImports) { | ||
const alias = node.getDefaultImport()?.getText(); | ||
const moduleSpecifier = node.getModuleSpecifier()?.getLiteralText(); | ||
if (!alias || !moduleSpecifier) continue; | ||
const levels = moduleSpecifier.split("/"); | ||
const [importScope, importModule, importPath] = levels; | ||
|
||
if (importScope === "@suid" && levels.length === 3) { | ||
if (importModule === "material") { | ||
imports.add("@suid/material", importPath, alias); | ||
if (options.onImport) { | ||
if (options.onImport(moduleSpecifier) !== false) { | ||
if (!node.getNamedImports().length) { | ||
node.remove(); | ||
} else { | ||
node.removeDefaultImport(); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (!options.dryRun) | ||
for (const moduleSpecifier in imports.map) { | ||
const importNode = source.addImportDeclaration({ | ||
moduleSpecifier, | ||
}); | ||
const names = Object.keys(imports.map[moduleSpecifier]).sort(); | ||
for (const name of names) { | ||
const alias = imports.map[moduleSpecifier][name]; | ||
importNode.addNamedImport({ | ||
name, | ||
alias: name === alias ? undefined : alias, | ||
}); | ||
} | ||
} | ||
} |