Skip to content

Commit

Permalink
Merge pull request #50 from imslepov/feat/use-function-declaration
Browse files Browse the repository at this point in the history
feat: convert methods as function declaration
  • Loading branch information
yoyo930021 authored Apr 13, 2024
2 parents 28fa88b + 75f08c2 commit b806216
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 19 deletions.
2 changes: 2 additions & 0 deletions src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export interface Vc2cOptions {
compatible: boolean
setupPropsKey: string
setupContextKey: string
useFunctionDeclaration: boolean
typescript: typeof ts
vueTemplateCompiler: typeof vueTemplateCompiler
eslintConfigFile: string
Expand All @@ -24,6 +25,7 @@ export function getDefaultVc2cOptions (tsModule: typeof ts = ts): Vc2cOptions {
compatible: false,
setupPropsKey: 'props',
setupContextKey: 'context',
useFunctionDeclaration: false,
typescript: tsModule,
vueTemplateCompiler: vueTemplateCompiler,
eslintConfigFile: '.eslintrc.js',
Expand Down
60 changes: 41 additions & 19 deletions src/plugins/vue-class-component/Method.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,9 @@ export const convertMethod: ASTConverter<ts.MethodDeclaration> = (node, options)
const tsModule = options.typescript
const methodName = node.name.getText()

const outputMethod = tsModule.createArrowFunction(
node.modifiers,
node.typeParameters,
node.parameters,
node.type,
tsModule.createToken(tsModule.SyntaxKind.EqualsGreaterThanToken),
node.body ?? tsModule.createBlock([])
)
const result = options.useFunctionDeclaration
? convertMethodAsFunctionDeclaration(tsModule, node)
: convertMethodAsArrowFunction(tsModule, node)

return {
tag: 'Method',
Expand All @@ -24,19 +19,46 @@ export const convertMethod: ASTConverter<ts.MethodDeclaration> = (node, options)
nodes: [
copySyntheticComments(
tsModule,
tsModule.createVariableStatement(
undefined,
tsModule.createVariableDeclarationList([
tsModule.createVariableDeclaration(
tsModule.createIdentifier(methodName),
undefined,
outputMethod
)
],
tsModule.NodeFlags.Const)
),
result,
node
)
] as ts.Statement[]
}
}

const convertMethodAsFunctionDeclaration = (tsModule: typeof ts, node: ts.MethodDeclaration): ts.FunctionDeclaration => {
return tsModule.createFunctionDeclaration(
undefined,
undefined,
node.asteriskToken,
node.name.getText(),
node.typeParameters,
node.parameters,
node.type,
node.body ?? tsModule.createBlock([])
)
}

const convertMethodAsArrowFunction = (tsModule: typeof ts, node: ts.MethodDeclaration): ts.VariableStatement => {
const methodName = node.name.getText()
const outputMethod = tsModule.createArrowFunction(
undefined,
node.typeParameters,
node.parameters,
node.type,
tsModule.createToken(tsModule.SyntaxKind.EqualsGreaterThanToken),
node.body ?? tsModule.createBlock([])
)

return tsModule.createVariableStatement(
undefined,
tsModule.createVariableDeclarationList([
tsModule.createVariableDeclaration(
tsModule.createIdentifier(methodName),
undefined,
outputMethod
)
],
tsModule.NodeFlags.Const)
)
}

0 comments on commit b806216

Please sign in to comment.