Skip to content

Commit 15482ac

Browse files
Release v33.0.0 (#106)
2 parents b4cb82d + 12ae31a commit 15482ac

File tree

241 files changed

+4921
-147
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

241 files changed

+4921
-147
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,4 @@ pnpm install
4848
### Contributing
4949

5050
See the [Developer Guide](./DEVELOPER.md) for more details.
51+

packages/cli/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "@ag-grid-devtools/cli",
4-
"version": "32.2.1",
4+
"version": "33.0.0",
55
"license": "MIT",
66
"description": "AG Grid developer toolkit",
77
"author": "AG Grid <[email protected]>",
@@ -78,6 +78,8 @@
7878
},
7979
"types": "./index.d.ts",
8080
"dependencies": {
81+
"@types/jscodeshift": "0.12.0",
82+
"jscodeshift": "17.1.1",
8183
"@typescript-eslint/parser": "7.18.0",
8284
"eslint": "8.57.0",
8385
"tsx": "4.19.1"
@@ -94,12 +96,14 @@
9496
"@ag-grid-devtools/worker-utils": "workspace:*",
9597
"@types/diff": "5.2.2",
9698
"@types/graceful-fs": "4.1.9",
99+
"@types/jscodeshift": "0.12.0",
97100
"@types/node": "22.7.3",
98101
"@types/semver": "7.5.8",
99102
"diff": "5.2.0",
100103
"glob": "11.0.0",
101104
"graceful-fs": "4.2.11",
102105
"ignore": "5.3.2",
106+
"jscodeshift": "17.1.1",
103107
"prettier": "3.3.3",
104108
"semver": "7.6.3",
105109
"vite-plugin-dts": "4.0.3",

packages/cli/src/codemods/lib.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ import { expect, test } from 'vitest';
22

33
import * as lib from './lib';
44

5-
const versions: Array<string> = ['31.0.0', '31.1.0', '31.2.0', '31.3.0', '32.0.0', '32.2.0'];
5+
const versions: Array<string> = [
6+
'31.0.0',
7+
'31.1.0',
8+
'31.2.0',
9+
'31.3.0',
10+
'32.0.0',
11+
'32.2.0',
12+
'33.0.0',
13+
];
614

715
test('module exports', () => {
816
expect({ ...lib }).toEqual({
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# jscodeshift Plugin
2+
3+
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).
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from './jscodeshift.adapter';
2+
export * from './transforms';
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import j, { Collection } from 'jscodeshift';
2+
import { AstCliContext, AstTransform, NodePath } from '@ag-grid-devtools/ast';
3+
4+
export type JSCodeShiftTransformer = (root: Collection) => void | any;
5+
6+
// Use https://astexplorer.net/ to iterate on your transformer
7+
// Parser: Typescript
8+
// Transform: jscodeshift
9+
//
10+
// NOTE: Less efficient than the raw visitor pattern, but:
11+
// * + easier to write (the tree is already parsed)
12+
// * + easier to reason about
13+
// * + easier to iterate over
14+
// * - multiple passes through parse/transform cycle
15+
export const jsCodeShiftTransform = (
16+
...transforms: JSCodeShiftTransformer[]
17+
): AstTransform<AstCliContext> => {
18+
return (_babel) => ({
19+
visitor: {
20+
Program: {
21+
exit(path: NodePath) {
22+
const root: Collection<any> = j((path.hub as any).file.ast);
23+
const getFirstNode = () => root.find(j.Program).get('body', 0).node;
24+
25+
// save initial comment if any
26+
const firstNode = getFirstNode();
27+
const { comments } = firstNode;
28+
29+
// transform
30+
for (const transform of transforms) {
31+
transform(root);
32+
}
33+
34+
// restore initial comment if any
35+
const firstNode2 = getFirstNode();
36+
if (firstNode2 !== firstNode) {
37+
firstNode2.comments = comments;
38+
}
39+
40+
// inject result back into babel AST
41+
const program = root.getAST()[0].node.program;
42+
path.replaceWith(program);
43+
},
44+
},
45+
},
46+
});
47+
};
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name": "JSCodeshift",
3+
"description": "Transform using JSCodeshift"
4+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './multi-type-import-to-single';
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import j, { Collection } from 'jscodeshift';
2+
import { JSCodeShiftTransformer } from '../jscodeshift.adapter';
3+
4+
// Find old named imports and replace them with the new named import
5+
// remove the old import if no other named imports are present
6+
export const multiTypeImportToSingle =
7+
(
8+
oldPackage: string,
9+
oldImports: string[],
10+
newPackage: string,
11+
newImport: string,
12+
): JSCodeShiftTransformer =>
13+
(root) => {
14+
// find old imports
15+
const importToBeInspected: Collection<any> = root
16+
.find(j.ImportDeclaration)
17+
.filter((path) => path.node.source.value === oldPackage);
18+
19+
// get all named imports in old import
20+
const allSpecifiers = importToBeInspected.find(j.ImportSpecifier);
21+
22+
// get all old named imports
23+
const importsToBeRemoved = allSpecifiers.filter((path) =>
24+
oldImports.includes(path.node.imported.name),
25+
);
26+
27+
const matches = importsToBeRemoved.length;
28+
const nonMatchingImports = allSpecifiers.length - matches;
29+
30+
// remove old named imports, if any
31+
importsToBeRemoved.forEach((path) => path.replace());
32+
33+
// remove import line if no other named imports were present
34+
if (nonMatchingImports === 0) {
35+
const importSpecifiers = importToBeInspected.find(j.ImportSpecifier);
36+
if (importSpecifiers.length === 0) {
37+
importToBeInspected.forEach((path) => path.replace());
38+
}
39+
}
40+
41+
// no need to add new import if no old imports were found
42+
if (matches === 0) {
43+
return;
44+
}
45+
46+
// construct new import
47+
const newImportDeclaration = j.importDeclaration(
48+
[j.importSpecifier(j.identifier(newImport))],
49+
j.stringLiteral(newPackage),
50+
'type',
51+
);
52+
53+
// find attachment point
54+
const imports = root.find(j.ImportDeclaration);
55+
56+
if (imports.length > 0) {
57+
// after imports
58+
imports.insertAfter(newImportDeclaration);
59+
} else {
60+
// top of file
61+
root.get().node.program.body.unshift(newImportDeclaration);
62+
}
63+
};

packages/cli/src/codemods/test/runners/transform.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ export function loadTransformScenarios(
1717
options: {
1818
transforms: Array<AstTransform<AstCliContext> | AstTransformWithOptions<AstCliContext>>;
1919
vitest: ExampleVitestHelpers;
20+
test?: (filename: string) => boolean;
2021
userConfig?: UserConfig;
2122
},
2223
): void {
2324
const { transforms, vitest, userConfig } = options;
2425
return loadAstTransformExampleScenarios(scenariosPath, {
2526
vitest,
27+
test: options.test,
2628
runner: (input) => {
2729
const { source, errors, warnings } = transformFileAst(input.source, transforms, {
2830
filename: input.path,

0 commit comments

Comments
 (0)