Skip to content

Commit 21335ec

Browse files
committed
support error and warning messages in jscodeshift-based transforms
1 parent 2c4af87 commit 21335ec

11 files changed

Lines changed: 83 additions & 111 deletions

File tree

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-grid-api-methods-v34-0/__fixtures__/scenarios/autoSizeColumns/input.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@ const gridApi = createGrid(document.body, {
77

88
const array = ['bar'];
99
const flag = true;
10+
const allArgs = [['baz'], false];
1011

1112
gridApi.autoSizeColumns(['foo'], true);
1213
gridApi?.autoSizeColumns(['foo'], false);
1314
gridApi.autoSizeColumns(array, flag);
1415
gridApi.autoSizeColumns(array);
16+
gridApi.autoSizeColumns(...allArgs);
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
module.exports = [];
1+
module.exports = [
2+
new SyntaxError(`Cannot support spread arguments
3+
4+
> | gridApi.autoSizeColumns(...allArgs);
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^`)
6+
];

packages/cli/src/codemods/transforms/transform-grid-api-methods-v34-0/__fixtures__/scenarios/autoSizeColumns/output.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const gridApi = createGrid(document.body, {
77

88
const array = ['bar'];
99
const flag = true;
10+
const allArgs = [['baz'], false];
1011

1112
gridApi.autoSizeColumns({
1213
colIds: ['foo'],
@@ -23,3 +24,4 @@ gridApi.autoSizeColumns({
2324
gridApi.autoSizeColumns({
2425
colIds: array
2526
});
27+
gridApi.autoSizeColumns(...allArgs);

packages/cli/src/codemods/transforms/transform-grid-api-methods-v34-0/jscodeshift.adapter.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

packages/cli/src/codemods/transforms/transform-grid-api-methods-v34-0/transforms/index.ts

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import j from 'jscodeshift';
2-
import { JSCodeShiftTransformer } from '../jscodeshift.adapter';
2+
import { ErrorSpec, JSCodeShiftTransformer } from '../../../plugins/jscodeshift';
3+
4+
export const transformAutoSizeColumnsArguments: JSCodeShiftTransformer = (root) => {
5+
const errors: ErrorSpec[] = [];
6+
const warnings: ErrorSpec[] = [];
37

4-
export const transformAutoSizeColumnsArguments: JSCodeShiftTransformer = (root) =>
58
root
69
.find(j.CallExpression, {
710
callee: {
@@ -14,7 +17,7 @@ export const transformAutoSizeColumnsArguments: JSCodeShiftTransformer = (root)
1417
const args = path.node.arguments;
1518

1619
if (args.some((a) => j.SpreadElement.check(a))) {
17-
// can't support spread arguments
20+
errors.push({ path, message: 'Cannot support spread arguments' });
1821
return;
1922
}
2023

@@ -31,7 +34,13 @@ export const transformAutoSizeColumnsArguments: JSCodeShiftTransformer = (root)
3134
path.node.arguments = [j.objectExpression(properties)];
3235
});
3336

34-
export const transformAutoSizeAllColumnsArguments: JSCodeShiftTransformer = (root) =>
37+
return { errors, warnings };
38+
};
39+
40+
export const transformAutoSizeAllColumnsArguments: JSCodeShiftTransformer = (root) => {
41+
const errors: ErrorSpec[] = [];
42+
const warnings: ErrorSpec[] = [];
43+
3544
root
3645
.find(j.CallExpression, {
3746
callee: {
@@ -48,7 +57,7 @@ export const transformAutoSizeAllColumnsArguments: JSCodeShiftTransformer = (roo
4857
}
4958

5059
if (args.some((a) => j.SpreadElement.check(a))) {
51-
// can't support spread arguments
60+
errors.push({ path, message: 'Cannot support spread arguments' });
5261
return;
5362
}
5463

@@ -59,3 +68,6 @@ export const transformAutoSizeAllColumnsArguments: JSCodeShiftTransformer = (roo
5968

6069
path.node.arguments = [j.objectExpression(properties)];
6170
});
71+
72+
return { errors, warnings };
73+
};

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: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
1-
export * from './jscodeshift.adapter';
21
export * from './transforms';

packages/codemods-tasks/templates/plugin-jscodeshift/jscodeshift.adapter.ts

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)