Skip to content

Commit e35ebe7

Browse files
committed
modularized version
1 parent bc346bd commit e35ebe7

File tree

6 files changed

+138
-29
lines changed

6 files changed

+138
-29
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
3+
export function Component() {
4+
return <span>Hi {name}</span>;
5+
}
6+
7+
Component.defaultProps = { name: 'Oleg' };
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import React from 'react';
2+
3+
export const Component = () => {
4+
return <span>Hi {name}</span>;
5+
};
6+
7+
Component.defaultProps = { name: 'Oleg' };
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { JSCodeshift } from 'jscodeshift';
2+
import { Collection } from 'jscodeshift/src/Collection';
3+
4+
export function moveDefaultPropsToFunctionDeclaration(
5+
source: Collection<any>,
6+
j: JSCodeshift,
7+
) {
8+
source
9+
.find(j.FunctionDeclaration, {
10+
id: { name: 'Component' },
11+
})
12+
.forEach(component => {
13+
// Find the defaultProps assignment expression
14+
const defaultProps = source.find(j.AssignmentExpression, {
15+
left: {
16+
object: { name: 'Component' },
17+
property: { name: 'defaultProps' },
18+
},
19+
});
20+
21+
if (defaultProps.length === 0) return;
22+
23+
// Extract the default props object
24+
const defaultValues = defaultProps.get('right').value.properties;
25+
26+
// Generate a new function parameter for each default prop
27+
const params = defaultValues.map((prop: any) => {
28+
const key = prop.key.name;
29+
const value = prop.value.value;
30+
// return j.objectPattern(`${key}=${JSON.stringify(value)}`);
31+
return j.objectPattern([
32+
j.objectProperty(
33+
j.identifier(key),
34+
j.assignmentPattern(j.identifier(key), j.literal(value)),
35+
),
36+
]);
37+
});
38+
39+
// Replace the original function declaration with a new one
40+
j(component).replaceWith(nodePath =>
41+
j.functionDeclaration(
42+
nodePath.node.id,
43+
params,
44+
nodePath.node.body,
45+
nodePath.node.generator,
46+
nodePath.node.async,
47+
),
48+
);
49+
// Remove the
50+
})
51+
.find(j.AssignmentExpression, {
52+
left: {
53+
object: { name: 'Component' },
54+
property: { name: 'defaultProps' },
55+
},
56+
})
57+
.toSource();
58+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { JSCodeshift } from 'jscodeshift';
2+
import { Collection } from 'jscodeshift/src/Collection';
3+
4+
export function removeDefaultPropsAssignment(
5+
source: Collection<any>,
6+
j: JSCodeshift,
7+
) {
8+
source
9+
.find(j.AssignmentExpression, {
10+
left: {
11+
object: { name: 'Component' },
12+
property: { name: 'defaultProps' },
13+
},
14+
})
15+
.remove();
16+
}
Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,46 @@
11
import { applyTransform } from '@codeshift/test-utils';
22
import * as transformer from './transform';
3+
import fs from 'fs';
4+
import path from 'path';
5+
6+
const input = fs.readFileSync(path.resolve(__dirname, 'input.tsx'), 'utf8');
7+
const input2 = fs.readFileSync(path.resolve(__dirname, 'input2.tsx'), 'utf8');
38

49
describe('react#remove-default-props transform', () => {
5-
it('should remove default props', async () => {
6-
const result = await applyTransform(
7-
transformer,
8-
`
9-
import React from 'react';
10-
11-
export const Greet = ({ name }) => <span>Hi {name}</span>;
12-
Greet.defaultProps = { name: 'Stranger' };
13-
`,
14-
{ parser: 'tsx' },
15-
);
10+
// it('should remove default props', async () => {
11+
// const result = await applyTransform(
12+
// transformer,
13+
// `
14+
// import React from 'react';
15+
16+
// export const Greet = ({ name }) => <span>Hi {name}</span>;
17+
// Greet.defaultProps = { name: 'Stranger' };
18+
// `,
19+
// { parser: 'tsx' },
20+
// );
21+
22+
// expect(result).toMatchInlineSnapshot(`
23+
// import React from 'react'; export const Greet = ({ name }) =>
24+
// <span>Hi {name}</span>;
25+
// `);
26+
// });
27+
28+
it('should replace default props for function component', async () => {
29+
const result = await applyTransform(transformer, input, { parser: 'tsx' });
30+
31+
expect(result).toMatchInlineSnapshot(`
32+
import React from 'react'; export function Component( { name: name = "Oleg"
33+
} ) { return
34+
<span>Hi {name}</span>; }
35+
`);
36+
});
37+
38+
it('should replace default props for arrow function component', async () => {
39+
const result = await applyTransform(transformer, input2, { parser: 'tsx' });
1640

1741
expect(result).toMatchInlineSnapshot(`
18-
import React from 'react'; export const Greet = ({ name }) =>
19-
<span>Hi {name}</span>;
42+
import React from 'react'; export const Component = () => { return
43+
<span>Hi {name}</span>; };
2044
`);
2145
});
2246
});
Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
1-
import { API, FileInfo, Options } from 'jscodeshift';
1+
import { FileInfo, API } from 'jscodeshift';
2+
import { moveDefaultPropsToFunctionDeclaration } from './motions/moveDefaultPropsToFunctionDeclaration';
3+
import { removeDefaultPropsAssignment } from './motions/removeDefaultPropsAssignment';
24

3-
export default function transformer(
4-
file: FileInfo,
5-
{ jscodeshift: j }: API,
6-
options: Options,
7-
) {
8-
const removePath = (path: any) => j(path).remove();
9-
const isAssigningDefaultProps = (e: any) =>
10-
e.node.left &&
11-
e.node.left.property &&
12-
e.node.left.property.name === 'defaultProps';
5+
export default function transformer(file: FileInfo, api: API) {
6+
const j = api.jscodeshift;
137

14-
return j(file.source)
15-
.find(j.AssignmentExpression)
16-
.filter(isAssigningDefaultProps)
17-
.forEach(removePath)
18-
.toSource(options.printOptions);
8+
const source = j(file.source);
9+
10+
// Find all function declarations with name "Component"
11+
moveDefaultPropsToFunctionDeclaration(source, j);
12+
13+
removeDefaultPropsAssignment(source, j);
14+
15+
return source.toSource();
1916
}

0 commit comments

Comments
 (0)