Skip to content

Commit df6f8ce

Browse files
authored
Merge pull request #6 from eddeee888/add-function-expression-option
Implement function / function expression config
2 parents b2f328f + 3e9480c commit df6f8ce

File tree

6 files changed

+30
-7
lines changed

6 files changed

+30
-7
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@
2525
| `createReactComponent.testLibrary` | Specifies what testing library to import <br><br> Options: <br> - `react-testing-library` (_default_) <br> - `enzyme` |
2626
| `createReactComponent.testingLibrary.cleanup` | Specifies whether or not to generate cleanup when using react-testing-library <br><br> Options: <br> - `true` <br> - `false` (_default_) |
2727
| `createReactComponent.module` | Specifies whether or not to create a module <br><br> Options: <br> - `true` (_default_) <br> - `false` |
28+
| `createReactComponent.functionType` | Specifies whether to use a normal function or function expression for the component <br><br> Options <br> - `function` (default) <br> - `expression` |

package.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,18 @@
9292
"type": "boolean",
9393
"default": "true",
9494
"description": "Specifies whether or not to create the component as a module"
95+
},
96+
"createReactComponent.functionType": {
97+
"type": "string",
98+
"enum": [
99+
"function",
100+
"expression"
101+
],
102+
"enumDescriptions": [
103+
"Generate functions",
104+
"Generate function expressions"
105+
],
106+
"default": "function"
95107
}
96108
}
97109
}

src/extension.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { createFolder, createFile } from './file';
33
import { typescriptComponentTemplate, typescriptTestTemplate } from './template/typescriptTemplate';
44
import { javascriptComponentTemplate, javascriptTestTemplate } from './template/javascriptTemplate';
55
import { indexTemplate } from './template/indexTemplate';
6-
import TemplateOptions, { TestLibrary } from './template/templateOptions';
6+
import TemplateOptions, { TestLibrary, FunctionType } from './template/templateOptions';
77

88
export function activate(context: vscode.ExtensionContext) {
99
const disposable = vscode.commands.registerCommand('extension.createReactComponent', async (uri: vscode.Uri) => {
@@ -20,6 +20,7 @@ export function activate(context: vscode.ExtensionContext) {
2020
name: componentName,
2121
testLibrary: config.get('testingLibrary') as TestLibrary,
2222
cleanup: config.get('testingLibrary.cleanup') as boolean,
23+
functionType: config.get('functionType') as FunctionType
2324
};
2425

2526
const createModule = config.get('createModule') as boolean;

src/template/javascriptTemplate.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import TemplateOptions, { TestLibrary } from './templateOptions';
1+
import TemplateOptions, { TestLibrary, FunctionType } from './templateOptions';
22

3-
const javascriptComponentTemplate = ({ name }: TemplateOptions) => `import React from 'react';
3+
const javascriptComponentTemplate = ({ name, functionType }: TemplateOptions) => `import React from 'react';
44
5-
function ${name}({}) {
5+
${functionType === FunctionType.Function ? `function ${name}({}) {` : ''}
6+
${functionType === FunctionType.Expression ? `const ${name} = ({}) => {` : ''}
67
return <>${name}</>
78
};
89

src/template/templateOptions.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@ export enum TestLibrary {
22
ReactTestingLibrary = 'react-testing-library',
33
Enzyme = 'enzyme',
44
}
5+
6+
export enum FunctionType {
7+
Function = "function",
8+
Expression = "expression"
9+
}
10+
511
interface TemplateOptions {
612
name: string;
713
testLibrary: TestLibrary;
814
cleanup: boolean;
15+
functionType: FunctionType;
916
}
1017

1118
export default TemplateOptions;

src/template/typescriptTemplate.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import TemplateOptions, { TestLibrary } from './templateOptions';
1+
import TemplateOptions, { TestLibrary, FunctionType } from './templateOptions';
22

3-
const typescriptComponentTemplate = ({ name }: TemplateOptions) => `import React from 'react';
3+
const typescriptComponentTemplate = ({ name, functionType }: TemplateOptions) => `import React from 'react';
44
55
export interface ${name}Props {}
66
7-
function ${name}({ }: ${name}Props) {
7+
${functionType === FunctionType.Function ? `function ${name}({ }: ${name}Props) {` : ''}
8+
${functionType === FunctionType.Expression ? `const ${name}: React.FunctionComponent<${name}Props> = ({ }) => {` : ''}
89
return <>${name}</>
910
};
1011

0 commit comments

Comments
 (0)