Skip to content

Commit b806216

Browse files
authored
Merge pull request #50 from imslepov/feat/use-function-declaration
feat: convert methods as function declaration
2 parents 28fa88b + 75f08c2 commit b806216

File tree

2 files changed

+43
-19
lines changed

2 files changed

+43
-19
lines changed

src/options.ts

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export interface Vc2cOptions {
99
compatible: boolean
1010
setupPropsKey: string
1111
setupContextKey: string
12+
useFunctionDeclaration: boolean
1213
typescript: typeof ts
1314
vueTemplateCompiler: typeof vueTemplateCompiler
1415
eslintConfigFile: string
@@ -24,6 +25,7 @@ export function getDefaultVc2cOptions (tsModule: typeof ts = ts): Vc2cOptions {
2425
compatible: false,
2526
setupPropsKey: 'props',
2627
setupContextKey: 'context',
28+
useFunctionDeclaration: false,
2729
typescript: tsModule,
2830
vueTemplateCompiler: vueTemplateCompiler,
2931
eslintConfigFile: '.eslintrc.js',

src/plugins/vue-class-component/Method.ts

+41-19
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,9 @@ export const convertMethod: ASTConverter<ts.MethodDeclaration> = (node, options)
66
const tsModule = options.typescript
77
const methodName = node.name.getText()
88

9-
const outputMethod = tsModule.createArrowFunction(
10-
node.modifiers,
11-
node.typeParameters,
12-
node.parameters,
13-
node.type,
14-
tsModule.createToken(tsModule.SyntaxKind.EqualsGreaterThanToken),
15-
node.body ?? tsModule.createBlock([])
16-
)
9+
const result = options.useFunctionDeclaration
10+
? convertMethodAsFunctionDeclaration(tsModule, node)
11+
: convertMethodAsArrowFunction(tsModule, node)
1712

1813
return {
1914
tag: 'Method',
@@ -24,19 +19,46 @@ export const convertMethod: ASTConverter<ts.MethodDeclaration> = (node, options)
2419
nodes: [
2520
copySyntheticComments(
2621
tsModule,
27-
tsModule.createVariableStatement(
28-
undefined,
29-
tsModule.createVariableDeclarationList([
30-
tsModule.createVariableDeclaration(
31-
tsModule.createIdentifier(methodName),
32-
undefined,
33-
outputMethod
34-
)
35-
],
36-
tsModule.NodeFlags.Const)
37-
),
22+
result,
3823
node
3924
)
4025
] as ts.Statement[]
4126
}
4227
}
28+
29+
const convertMethodAsFunctionDeclaration = (tsModule: typeof ts, node: ts.MethodDeclaration): ts.FunctionDeclaration => {
30+
return tsModule.createFunctionDeclaration(
31+
undefined,
32+
undefined,
33+
node.asteriskToken,
34+
node.name.getText(),
35+
node.typeParameters,
36+
node.parameters,
37+
node.type,
38+
node.body ?? tsModule.createBlock([])
39+
)
40+
}
41+
42+
const convertMethodAsArrowFunction = (tsModule: typeof ts, node: ts.MethodDeclaration): ts.VariableStatement => {
43+
const methodName = node.name.getText()
44+
const outputMethod = tsModule.createArrowFunction(
45+
undefined,
46+
node.typeParameters,
47+
node.parameters,
48+
node.type,
49+
tsModule.createToken(tsModule.SyntaxKind.EqualsGreaterThanToken),
50+
node.body ?? tsModule.createBlock([])
51+
)
52+
53+
return tsModule.createVariableStatement(
54+
undefined,
55+
tsModule.createVariableDeclarationList([
56+
tsModule.createVariableDeclaration(
57+
tsModule.createIdentifier(methodName),
58+
undefined,
59+
outputMethod
60+
)
61+
],
62+
tsModule.NodeFlags.Const)
63+
)
64+
}

0 commit comments

Comments
 (0)