Skip to content

Commit 7d054d4

Browse files
reidbarberdevongovett
authored andcommitted
fix codemod parsing files with macro
1 parent 82f0c99 commit 7d054d4

File tree

5 files changed

+23
-2
lines changed

5 files changed

+23
-2
lines changed

packages/dev/codemods/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
"execa": "^5.1.1",
3434
"jscodeshift": "^0.15.2",
3535
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0",
36+
"recast": "^0.23.9",
3637
"ts-node": "^10.9.2",
3738
"uuid": "^9.0.1"
3839
},

packages/dev/codemods/src/s1-to-s2/__tests__/__snapshots__/styleProps.test.ts.snap

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,12 @@ import { style } from "@react-spectrum/s2/style" with { type: "macro" };
225225
})}>Test</RSPButton>"
226226
`;
227227

228+
exports[`Should leave existing style macros unaffected 1`] = `
229+
"import { style } from "@react-spectrum/s2/style" with { type: "macro" };
230+
231+
<TextField label="Name" styles={style({width: 160})} />"
232+
`;
233+
228234
exports[`Updates left/right layout values to be start/end 1`] = `
229235
"import { Button } from "@react-spectrum/s2";
230236
import { style } from "@react-spectrum/s2/style" with { type: "macro" };

packages/dev/codemods/src/s1-to-s2/__tests__/styleProps.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,3 +199,10 @@ import {Button, Flex} from '@adobe/react-spectrum';
199199
<Button justifySelf="right">Test</Button>
200200
</>
201201
`);
202+
203+
test('Should leave existing style macros unaffected', `
204+
import { TextField } from "@react-spectrum/s2";
205+
import { style } from "@react-spectrum/s2/style" with { type: "macro" };
206+
207+
<TextField label="Name" styles={style({width: 160})} />
208+
`);

packages/dev/codemods/src/s1-to-s2/src/codemods/codemod.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {changes as changesJSON} from './changes';
55
import {functionMap} from './transforms';
66
import {getComponents} from '../getComponents';
77
import {iconMap} from '../iconMap';
8+
import {parse as recastParse} from 'recast';
89
import * as t from '@babel/types';
910
import {transformStyleProps} from './styleProps';
1011
import traverse, {Binding, NodePath} from '@babel/traverse';
@@ -32,7 +33,13 @@ interface Options {
3233
}
3334

3435
export default function transformer(file: FileInfo, api: API, options: Options) {
35-
let j = api.jscodeshift;
36+
let j = api.jscodeshift.withParser({
37+
parse(source: string) {
38+
return recastParse(source, {
39+
parser: require('recast/parsers/babel')
40+
});
41+
}
42+
});
3643
let root = j(file.source);
3744
let componentsToTransform = options.components ? new Set(options.components.split(',').filter(s => availableComponents.has(s))) : availableComponents;
3845

@@ -277,4 +284,3 @@ export default function transformer(file: FileInfo, api: API, options: Options)
277284
return root.toSource().replace(/assert\s*\{\s*type:\s*"macro"\s*\}/g, 'with { type: "macro" }');
278285
}
279286

280-
transformer.parser = 'tsx';

yarn.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6829,6 +6829,7 @@ __metadata:
68296829
jscodeshift: "npm:^0.15.2"
68306830
react: "npm:^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
68316831
react-dom: "npm:^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
6832+
recast: "npm:^0.23.9"
68326833
ts-node: "npm:^10.9.2"
68336834
typescript: "npm:^5.5.0"
68346835
uuid: "npm:^9.0.1"

0 commit comments

Comments
 (0)