Skip to content

Commit 335ad9a

Browse files
authored
Allow warnings/errors in jscodeshift mods (#113)
* add plugin-jscodeshift * add v34 * add v34 transforms * fix import * fix manifests * support error and warning messages in jscodeshift-based transforms * fix build * remove autosize codemod
1 parent deadf5e commit 335ad9a

File tree

19 files changed

+131
-15
lines changed

19 files changed

+131
-15
lines changed

packages/ast/src/types/transform.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ export interface FsContext extends TransformContext {
4747
}
4848

4949
export interface AstCliContext extends FsContext {
50-
warn(node: NodePath<AstNode> | null, message: string): void;
51-
fail(node: NodePath<AstNode> | null, message: string): void;
50+
warn(node: NodePath<AstNode> | Error | null, message: string): void;
51+
fail(node: NodePath<AstNode> | Error | null, message: string): void;
5252
}
5353

5454
export type AstTransformResult = {

packages/cli/src/codemods/plugins/jscodeshift/jscodeshift.adapter.ts

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import j, { Collection } from 'jscodeshift';
2-
import { AstCliContext, AstTransform, NodePath } from '@ag-grid-devtools/ast';
2+
import { AstCliContext, AstTransform, NodePath, Node } from '@ag-grid-devtools/ast';
33

4-
export type JSCodeShiftTransformer = (root: Collection) => void | any;
4+
export type ErrorSpec = { path: j.ASTPath<j.Node>; message: string };
5+
6+
export type JSCodeShiftTransformer = (root: Collection) => void | {
7+
errors: ErrorSpec[];
8+
warnings: ErrorSpec[];
9+
};
510

611
// Use https://astexplorer.net/ to iterate on your transformer
712
// Parser: Typescript
@@ -15,11 +20,16 @@ export type JSCodeShiftTransformer = (root: Collection) => void | any;
1520
export const jsCodeShiftTransform = (
1621
...transforms: JSCodeShiftTransformer[]
1722
): AstTransform<AstCliContext> => {
23+
const errors: Error[] = [];
24+
const warnings: Error[] = [];
25+
let source: any;
26+
1827
return (_babel) => ({
1928
visitor: {
2029
Program: {
2130
exit(path: NodePath) {
22-
const root: Collection<any> = j((path.hub as any).file.ast);
31+
source = (path.hub as any).file.ast;
32+
const root: Collection<any> = j(source);
2333
const getFirstNode = () => root.find(j.Program).get('body', 0).node;
2434

2535
// save initial comment if any
@@ -28,7 +38,21 @@ export const jsCodeShiftTransform = (
2838

2939
// transform
3040
for (const transform of transforms) {
31-
transform(root);
41+
const result = transform(root);
42+
if (result?.errors) {
43+
errors.push(
44+
...result.errors.map((error) =>
45+
path.hub.buildError(error.path.node as Node, error.message),
46+
),
47+
);
48+
}
49+
if (result?.warnings) {
50+
warnings.push(
51+
...result.warnings.map((warning) =>
52+
path.hub.buildError(warning.path.node as Node, warning.message),
53+
),
54+
);
55+
}
3256
}
3357

3458
// restore initial comment if any
@@ -43,5 +67,14 @@ export const jsCodeShiftTransform = (
4367
},
4468
},
4569
},
70+
post(_file) {
71+
for (const warning of warnings) {
72+
this.opts.warn(warning, warning.message);
73+
}
74+
75+
for (const error of errors) {
76+
this.opts.fail(error, error.message);
77+
}
78+
},
4679
});
4780
};

packages/cli/src/codemods/transforms/transform-modules-to-packages-v33/transformers/package-transforms.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const packageLicenseManager: JSCodeShiftTransformer = (root) => {
4747

4848
if (alreadyExists) {
4949
// This package file already has a ModuleRegistry import so looks like it has already been transformed
50-
return root.toSource();
50+
return;
5151
}
5252

5353
const usingCharts: UsingCharts = process.env.AG_USING_CHARTS as any;

packages/cli/src/codemods/transforms/transform-sparklines-options-v33-0/transformers/sparkline-chart-type-subobject.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { JSCodeShiftTransformer } from '../../../plugins/jscodeshift';
44
const chartTypeKeys = ['area', 'bar', 'column', 'line'];
55

66
// find [chart-type] keys, and merge their contents into the parent object
7-
export const chartTypeSubobject: JSCodeShiftTransformer = (root) =>
7+
export const chartTypeSubobject: JSCodeShiftTransformer = (root) => {
88
root
99
.find(j.ObjectProperty, { key: { name: 'cellRendererParams' } })
1010
.find(j.ObjectProperty, { key: { name: 'sparklineOptions' } })
@@ -17,3 +17,4 @@ export const chartTypeSubobject: JSCodeShiftTransformer = (root) =>
1717
});
1818
path.replace();
1919
});
20+
};

packages/cli/src/codemods/transforms/transform-sparklines-options-v33-0/transformers/sparkline-column.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import j from 'jscodeshift';
22
import { JSCodeShiftTransformer } from '../../../plugins/jscodeshift';
33

44
// update bar/column types to bar with matching direction
5-
export const columnToVerticalBarTransform: JSCodeShiftTransformer = (root) =>
5+
export const columnToVerticalBarTransform: JSCodeShiftTransformer = (root) => {
66
root
77
.find(j.ObjectProperty, { key: { name: 'cellRendererParams' } })
88
.find(j.ObjectProperty, { key: { name: 'sparklineOptions' } })
@@ -14,3 +14,4 @@ export const columnToVerticalBarTransform: JSCodeShiftTransformer = (root) =>
1414
path.replace(j.objectProperty(j.identifier('type'), j.stringLiteral('bar')));
1515
path.insertAfter(j.objectProperty(j.identifier('direction'), j.stringLiteral(direction)));
1616
});
17+
};
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import j from 'jscodeshift';
22
import { JSCodeShiftTransformer } from '../../../plugins/jscodeshift';
33

4-
export const removeCrosshairs: JSCodeShiftTransformer = (root) =>
4+
export const removeCrosshairs: JSCodeShiftTransformer = (root) => {
55
root
66
.find(j.ObjectProperty, { key: { name: 'cellRendererParams' } })
77
.find(j.ObjectProperty, { key: { name: 'sparklineOptions' } })
88
.find(j.ObjectProperty, { key: { name: 'crosshairs' } })
99
.forEach((path) => path.replace());
10+
};

packages/cli/src/codemods/transforms/transform-sparklines-options-v33-0/transformers/sparkline-types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import j from 'jscodeshift';
22
import { JSCodeShiftTransformer } from '../../../plugins/jscodeshift';
33
import { newType, oldTypes } from './constants';
44

5-
export const replaceTypes: JSCodeShiftTransformer = (root) =>
5+
export const replaceTypes: JSCodeShiftTransformer = (root) => {
66
root
77
.find(j.TSTypeReference)
88
.filter((path) => oldTypes.includes((path.value.typeName as any).name))
99
.forEach((path) => path.replace(j.tsTypeReference(j.identifier(newType))));
10+
};

packages/codemod-utils/src/transform/js.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@ function transformJsFile<S>(
6565
const transformContext: AstTransformContext<AstCliContext> = {
6666
filename,
6767
opts: {
68-
warn(node: NodePath<AstNode> | null, message: string) {
68+
warn(node, message) {
6969
const error = createSourceCodeError(node, message);
7070
uniqueErrors.set(error.message, { error, fatal: false });
7171
},
72-
fail(node: NodePath<AstNode> | null, message: string) {
72+
fail(node, message) {
7373
const error = createSourceCodeError(node, message);
7474
uniqueErrors.set(error.message, { error, fatal: true });
7575
},
@@ -99,6 +99,14 @@ function transformJsFile<S>(
9999
};
100100
}
101101

102-
function createSourceCodeError(node: NodePath<AstNode> | null, message: string): Error {
103-
return node ? node.buildCodeFrameError(message) : new SyntaxError(message);
102+
function createSourceCodeError(node: NodePath<AstNode> | Error | null, message: string): Error {
103+
if (!node) {
104+
return new SyntaxError(message);
105+
}
106+
107+
if ('hub' in node) {
108+
return node.buildCodeFrameError(message);
109+
}
110+
111+
return node;
104112
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { dirname, join } from 'node:path';
2+
import { fileURLToPath } from 'node:url';
3+
import { describe, expect, onTestFinished, test } from 'vitest';
4+
import { loadTransformScenarios } from '../../test/runners/transform';
5+
6+
import <%= identifier %> from '.';
7+
8+
const __dirname = dirname(fileURLToPath(import.meta.url));
9+
10+
describe(<%= identifier %>, () => {
11+
const scenariosPath = join(__dirname, './__fixtures__/scenarios');
12+
loadTransformScenarios(scenariosPath, {
13+
transforms: [<%= identifier %>],
14+
vitest: { describe, expect, test, onTestFinished },
15+
});
16+
});
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).

0 commit comments

Comments
 (0)