|
1 | 1 | import * as t from '@babel/types'; |
2 | 2 |
|
3 | 3 | export const component = (componentName: string, contentStatement: t.Statement[] = []) => { |
4 | | - return t.variableDeclaration('const', [ |
5 | | - t.variableDeclarator( |
6 | | - t.identifier(componentName), |
7 | | - t.arrowFunctionExpression( |
8 | | - [t.identifier('props')], |
9 | | - t.blockStatement([ |
10 | | - ...contentStatement, |
11 | | - t.returnStatement( |
12 | | - t.jsxElement( |
13 | | - t.jsxOpeningElement(t.jsxIdentifier('div'), [], false), |
14 | | - t.jsxClosingElement(t.jsxIdentifier('div')), |
15 | | - [] |
16 | | - ) |
17 | | - ), |
18 | | - ]) |
| 4 | + return [ |
| 5 | + ...contentStatement, |
| 6 | + t.returnStatement( |
| 7 | + t.jsxElement( |
| 8 | + t.jsxOpeningElement(t.jsxIdentifier('div'), [], false), |
| 9 | + t.jsxClosingElement(t.jsxIdentifier('div')), |
| 10 | + [] |
19 | 11 | ) |
20 | 12 | ), |
21 | | - ]); |
| 13 | + ]; |
22 | 14 | }; |
23 | 15 |
|
24 | 16 | export const test = (componentName: string) => { |
@@ -56,38 +48,19 @@ export const test = (componentName: string) => { |
56 | 48 | ); |
57 | 49 | }; |
58 | 50 |
|
59 | | -export const hoc = (hocName: string, contentStatement: t.Statement[] = []) => { |
60 | | - return t.functionDeclaration( |
61 | | - t.identifier(hocName), |
62 | | - [t.identifier('WrappedComponent')], |
63 | | - t.blockStatement([ |
64 | | - t.returnStatement( |
65 | | - t.arrowFunctionExpression( |
66 | | - [t.identifier('props')], |
67 | | - t.blockStatement([ |
68 | | - ...contentStatement, |
69 | | - t.returnStatement( |
70 | | - t.jsxElement( |
71 | | - t.jsxOpeningElement( |
72 | | - t.jsxIdentifier('WrappedComponent'), |
73 | | - [t.jsxSpreadAttribute(t.identifier('props'))], |
74 | | - true |
75 | | - ), |
76 | | - null, |
77 | | - [] |
78 | | - ) |
79 | | - ), |
80 | | - ]) |
81 | | - ) |
82 | | - ), |
83 | | - ]) |
84 | | - ); |
85 | | -}; |
86 | | - |
87 | | -export const hook = (hookName: string, contentStatement: t.Statement[] = []) => { |
88 | | - return t.functionDeclaration( |
89 | | - t.identifier(hookName), |
90 | | - [], |
91 | | - t.blockStatement([...contentStatement, t.returnStatement(t.nullLiteral())]) |
92 | | - ); |
| 51 | +export const hocBody = (contentStatement: t.Statement[] = []) => { |
| 52 | + return t.blockStatement([ |
| 53 | + ...contentStatement, |
| 54 | + t.returnStatement( |
| 55 | + t.jsxElement( |
| 56 | + t.jsxOpeningElement( |
| 57 | + t.jsxIdentifier('WrappedComponent'), |
| 58 | + [t.jsxSpreadAttribute(t.identifier('props'))], |
| 59 | + true |
| 60 | + ), |
| 61 | + null, |
| 62 | + [] |
| 63 | + ) |
| 64 | + ), |
| 65 | + ]); |
93 | 66 | }; |
0 commit comments