-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
241 changed files
with
4,921 additions
and
147 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 |
---|---|---|
|
@@ -48,3 +48,4 @@ pnpm install | |
### Contributing | ||
|
||
See the [Developer Guide](./DEVELOPER.md) for more details. | ||
|
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
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,3 @@ | ||
# jscodeshift Plugin | ||
|
||
The `jscodeshift` plugin is a tool used for running codemods over JavaScript and TypeScript codebases. It leverages the `jscodeshift` library, which provides a simple API for transforming code using the power of abstract syntax trees (ASTs). |
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,2 @@ | ||
export * from './jscodeshift.adapter'; | ||
export * from './transforms'; |
47 changes: 47 additions & 0 deletions
47
packages/cli/src/codemods/plugins/jscodeshift/jscodeshift.adapter.ts
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,47 @@ | ||
import j, { Collection } from 'jscodeshift'; | ||
import { AstCliContext, AstTransform, NodePath } from '@ag-grid-devtools/ast'; | ||
|
||
export type JSCodeShiftTransformer = (root: Collection) => void | any; | ||
|
||
// Use https://astexplorer.net/ to iterate on your transformer | ||
// Parser: Typescript | ||
// Transform: jscodeshift | ||
// | ||
// NOTE: Less efficient than the raw visitor pattern, but: | ||
// * + easier to write (the tree is already parsed) | ||
// * + easier to reason about | ||
// * + easier to iterate over | ||
// * - multiple passes through parse/transform cycle | ||
export const jsCodeShiftTransform = ( | ||
...transforms: JSCodeShiftTransformer[] | ||
): AstTransform<AstCliContext> => { | ||
return (_babel) => ({ | ||
visitor: { | ||
Program: { | ||
exit(path: NodePath) { | ||
const root: Collection<any> = j((path.hub as any).file.ast); | ||
const getFirstNode = () => root.find(j.Program).get('body', 0).node; | ||
|
||
// save initial comment if any | ||
const firstNode = getFirstNode(); | ||
const { comments } = firstNode; | ||
|
||
// transform | ||
for (const transform of transforms) { | ||
transform(root); | ||
} | ||
|
||
// restore initial comment if any | ||
const firstNode2 = getFirstNode(); | ||
if (firstNode2 !== firstNode) { | ||
firstNode2.comments = comments; | ||
} | ||
|
||
// inject result back into babel AST | ||
const program = root.getAST()[0].node.program; | ||
path.replaceWith(program); | ||
}, | ||
}, | ||
}, | ||
}); | ||
}; |
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,4 @@ | ||
{ | ||
"name": "JSCodeshift", | ||
"description": "Transform using JSCodeshift" | ||
} |
1 change: 1 addition & 0 deletions
1
packages/cli/src/codemods/plugins/jscodeshift/transforms/index.ts
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 @@ | ||
export * from './multi-type-import-to-single'; |
63 changes: 63 additions & 0 deletions
63
packages/cli/src/codemods/plugins/jscodeshift/transforms/multi-type-import-to-single.ts
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 j, { Collection } from 'jscodeshift'; | ||
import { JSCodeShiftTransformer } from '../jscodeshift.adapter'; | ||
|
||
// Find old named imports and replace them with the new named import | ||
// remove the old import if no other named imports are present | ||
export const multiTypeImportToSingle = | ||
( | ||
oldPackage: string, | ||
oldImports: string[], | ||
newPackage: string, | ||
newImport: string, | ||
): JSCodeShiftTransformer => | ||
(root) => { | ||
// find old imports | ||
const importToBeInspected: Collection<any> = root | ||
.find(j.ImportDeclaration) | ||
.filter((path) => path.node.source.value === oldPackage); | ||
|
||
// get all named imports in old import | ||
const allSpecifiers = importToBeInspected.find(j.ImportSpecifier); | ||
|
||
// get all old named imports | ||
const importsToBeRemoved = allSpecifiers.filter((path) => | ||
oldImports.includes(path.node.imported.name), | ||
); | ||
|
||
const matches = importsToBeRemoved.length; | ||
const nonMatchingImports = allSpecifiers.length - matches; | ||
|
||
// remove old named imports, if any | ||
importsToBeRemoved.forEach((path) => path.replace()); | ||
|
||
// remove import line if no other named imports were present | ||
if (nonMatchingImports === 0) { | ||
const importSpecifiers = importToBeInspected.find(j.ImportSpecifier); | ||
if (importSpecifiers.length === 0) { | ||
importToBeInspected.forEach((path) => path.replace()); | ||
} | ||
} | ||
|
||
// no need to add new import if no old imports were found | ||
if (matches === 0) { | ||
return; | ||
} | ||
|
||
// construct new import | ||
const newImportDeclaration = j.importDeclaration( | ||
[j.importSpecifier(j.identifier(newImport))], | ||
j.stringLiteral(newPackage), | ||
'type', | ||
); | ||
|
||
// find attachment point | ||
const imports = root.find(j.ImportDeclaration); | ||
|
||
if (imports.length > 0) { | ||
// after imports | ||
imports.insertAfter(newImportDeclaration); | ||
} else { | ||
// top of file | ||
root.get().node.program.body.unshift(newImportDeclaration); | ||
} | ||
}; |
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
Oops, something went wrong.