Skip to content

Commit

Permalink
fix codemod parsing files with macro
Browse files Browse the repository at this point in the history
  • Loading branch information
reidbarber authored and devongovett committed Sep 30, 2024
1 parent 82f0c99 commit 7d054d4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 2 deletions.
1 change: 1 addition & 0 deletions packages/dev/codemods/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"execa": "^5.1.1",
"jscodeshift": "^0.15.2",
"react": "^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0",
"recast": "^0.23.9",
"ts-node": "^10.9.2",
"uuid": "^9.0.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,12 @@ import { style } from "@react-spectrum/s2/style" with { type: "macro" };
})}>Test</RSPButton>"
`;

exports[`Should leave existing style macros unaffected 1`] = `
"import { style } from "@react-spectrum/s2/style" with { type: "macro" };
<TextField label="Name" styles={style({width: 160})} />"
`;

exports[`Updates left/right layout values to be start/end 1`] = `
"import { Button } from "@react-spectrum/s2";
import { style } from "@react-spectrum/s2/style" with { type: "macro" };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,10 @@ import {Button, Flex} from '@adobe/react-spectrum';
<Button justifySelf="right">Test</Button>
</>
`);

test('Should leave existing style macros unaffected', `
import { TextField } from "@react-spectrum/s2";
import { style } from "@react-spectrum/s2/style" with { type: "macro" };
<TextField label="Name" styles={style({width: 160})} />
`);
10 changes: 8 additions & 2 deletions packages/dev/codemods/src/s1-to-s2/src/codemods/codemod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {changes as changesJSON} from './changes';
import {functionMap} from './transforms';
import {getComponents} from '../getComponents';
import {iconMap} from '../iconMap';
import {parse as recastParse} from 'recast';
import * as t from '@babel/types';
import {transformStyleProps} from './styleProps';
import traverse, {Binding, NodePath} from '@babel/traverse';
Expand Down Expand Up @@ -32,7 +33,13 @@ interface Options {
}

export default function transformer(file: FileInfo, api: API, options: Options) {
let j = api.jscodeshift;
let j = api.jscodeshift.withParser({
parse(source: string) {
return recastParse(source, {
parser: require('recast/parsers/babel')
});
}
});
let root = j(file.source);
let componentsToTransform = options.components ? new Set(options.components.split(',').filter(s => availableComponents.has(s))) : availableComponents;

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

transformer.parser = 'tsx';
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6829,6 +6829,7 @@ __metadata:
jscodeshift: "npm:^0.15.2"
react: "npm:^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
react-dom: "npm:^16.8.0 || ^17.0.0-rc.1 || ^18.0.0 || ^19.0.0"
recast: "npm:^0.23.9"
ts-node: "npm:^10.9.2"
typescript: "npm:^5.5.0"
uuid: "npm:^9.0.1"
Expand Down

0 comments on commit 7d054d4

Please sign in to comment.