Skip to content

Commit 6e214f6

Browse files
committed
Add new arrowFunction config option
1 parent 42f3789 commit 6e214f6

10 files changed

Lines changed: 89 additions & 55 deletions

File tree

src/config/Config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export class Config implements IConfig {
2929

3030
cssModules: boolean;
3131

32+
arrowFunction: boolean;
33+
3234
prefixes: ConfigPrefixes;
3335

3436
ext: ConfigExt;
@@ -52,6 +54,7 @@ export class Config implements IConfig {
5254
this.wrapFolder = this.config.wrapFolder;
5355
this.cssModules = this.config.cssModules;
5456
this.exportType = this.config.exportType;
57+
this.arrowFunction = this.config.arrowFunction;
5558
}
5659

5760
private setFilesExtension() {

src/config/default.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
"fileNameCase": "pascal",
77
"path": "src/components",
88
"cssModules": true,
9-
"exportType": "named"
9+
"exportType": "default",
10+
"arrowFunction": true
1011
}

src/core/temlpates/components/ComponentTemplate.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ export class ComponentTemplate extends TemplateBase implements Template {
3333
body.push(t.emptyStatement());
3434
}
3535

36-
const component = c.component(this.vars.componentName, c.generateHooks(this.vars.hooks));
36+
const functionContainer = config.arrowFunction
37+
? c.arrowFunctionDeclaration
38+
: c.regularFunctionDeclaration;
39+
const component = functionContainer(
40+
this.vars.componentName,
41+
[t.identifier('props')],
42+
c.component(this.vars.componentName, c.generateHooks(this.vars.hooks))
43+
);
3744

3845
if (config.exportType === 'named') {
3946
body.push(t.exportNamedDeclaration(component));

src/core/temlpates/components/HOCTemplate.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,21 @@ export class HOCTemplate extends TemplateBase implements Template {
1616
body.push(t.emptyStatement());
1717
}
1818

19-
const hoc = c.hoc(this.vars.componentName, c.generateHooks(this.vars.hooks));
19+
const functionContainer = config.arrowFunction
20+
? c.arrowFunctionDeclaration
21+
: c.regularFunctionDeclaration;
22+
23+
const hocBody = c.hocBody(c.generateHooks(this.vars.hooks));
24+
25+
const functionExpression = config.arrowFunction
26+
? t.arrowFunctionExpression([t.identifier('props')], hocBody)
27+
: t.functionExpression(null, [t.identifier('props')], hocBody);
28+
29+
const hoc = functionContainer(
30+
this.vars.componentName,
31+
[t.identifier('WrappedComponent')],
32+
[t.returnStatement(functionExpression)]
33+
);
2034

2135
if (config.exportType === 'named') {
2236
body.push(t.exportNamedDeclaration(hoc));

src/core/temlpates/components/HookTemplate.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,15 @@ export class HookTemplate extends TemplateBase implements Template {
2020
body.push(t.emptyStatement());
2121
}
2222

23-
const hook = c.hook(this.vars.componentName, c.generateHooks(this.vars.hooks));
23+
const functionContainer = config.arrowFunction
24+
? c.arrowFunctionDeclaration
25+
: c.regularFunctionDeclaration;
26+
27+
const hook = functionContainer(
28+
this.vars.componentName,
29+
[],
30+
[...c.generateHooks(this.vars.hooks), t.returnStatement(t.nullLiteral())]
31+
);
2432

2533
if (config.exportType === 'named') {
2634
body.push(t.exportNamedDeclaration(hook));
Lines changed: 23 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,16 @@
11
import * as t from '@babel/types';
22

33
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+
[]
1911
)
2012
),
21-
]);
13+
];
2214
};
2315

2416
export const test = (componentName: string) => {
@@ -56,38 +48,19 @@ export const test = (componentName: string) => {
5648
);
5749
};
5850

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+
]);
9366
};

src/core/temlpates/shared/utils.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,27 @@ export const importNamed = (imports: t.ImportSpecifier[], from: string) => {
1414
return t.importDeclaration(imports, t.stringLiteral(from));
1515
};
1616

17+
export const arrowFunctionDeclaration = (
18+
name: string,
19+
params: t.Identifier[],
20+
body: t.Statement[]
21+
) => {
22+
return t.variableDeclaration('const', [
23+
t.variableDeclarator(
24+
t.identifier(name),
25+
t.arrowFunctionExpression(params, t.blockStatement(body))
26+
),
27+
]);
28+
};
29+
30+
export const regularFunctionDeclaration = (
31+
name: string,
32+
params: t.Identifier[],
33+
body: t.Statement[]
34+
) => {
35+
return t.functionDeclaration(t.identifier(name), params, t.blockStatement(body));
36+
};
37+
1738
export const importDefault = (name, from, specs = []) => {
1839
return t.importDeclaration(
1940
[t.importDefaultSpecifier(t.identifier(name)), ...specs],
@@ -25,7 +46,7 @@ export const propTypes = (componentName: string) => {
2546
return t.expressionStatement(
2647
t.assignmentExpression(
2748
'=',
28-
t.memberExpression(t.identifier(componentName), t.identifier('propTypes'), false),
49+
t.memberExpression(t.identifier(componentName), t.identifier('propTypes')),
2950
t.objectExpression([])
3051
)
3152
);

src/init/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export async function initConfigFile() {
2727
path: answers.path,
2828
wrapFolder: answers.wrapFolder,
2929
cssModules: answers.cssModules,
30+
arrowFunction: answers.arrowFunction,
3031
};
3132

3233
const content = JSON.stringify(config, null, 2);

src/init/questions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export const question = [
3636
name: 'wrapFolder',
3737
message: 'Should create a wrap folder?',
3838
},
39+
{
40+
type: 'confirm',
41+
name: 'arrowFunction',
42+
message: 'Use arrow function syntax?',
43+
},
3944
{
4045
type: 'list',
4146
name: 'fileNameCase',

src/types/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export interface IConfig {
1111
wrapFolder: boolean;
1212
cssModules: boolean;
1313
exportType: ExportType;
14+
arrowFunction: boolean;
1415
}
1516

1617
interface IBaseVariables {

0 commit comments

Comments
 (0)