From b3608b79aa0f412e06779e42ac49e5c97734c8a2 Mon Sep 17 00:00:00 2001 From: Olivier Adam Date: Fri, 8 Dec 2023 16:14:09 +0100 Subject: [PATCH 1/3] improve migrator * remove $refs from data * better typing for data * better typing for props --- src/migrator/migratorManager.ts | 4 ++++ src/migrator/vue-class-component/migrate-data.ts | 12 +++--------- 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/migrator/migratorManager.ts b/src/migrator/migratorManager.ts index 0a08a0d..f421fbb 100644 --- a/src/migrator/migratorManager.ts +++ b/src/migrator/migratorManager.ts @@ -94,6 +94,10 @@ export default class MigrationManager { } if (propNode.isKind(SyntaxKind.ObjectLiteralExpression)) { propObject = addPropertyObject(propsObject, propName, propNode.getText()); + // is tsType is not null, assume that it is more correct than the vue type + if (tsType) { + propObject.getProperty('type')?.remove(); + } if (!propObject.getProperty('type')) { propObject .addPropertyAssignment({ diff --git a/src/migrator/vue-class-component/migrate-data.ts b/src/migrator/vue-class-component/migrate-data.ts index 1faa490..43911f9 100644 --- a/src/migrator/vue-class-component/migrate-data.ts +++ b/src/migrator/vue-class-component/migrate-data.ts @@ -105,18 +105,12 @@ export default (clazz: ClassDeclaration, mainObject: ObjectLiteralExpression) => if (classPropertyData.length || clazzDataMethod) { const { dataMethod, returnObject } = getDataMethod(clazz, mainObject); classPropertyData.forEach((propertyData) => { + if (propertyData.getName() == '$refs') {return;} const typeNode = propertyData.getTypeNode()?.getText(); if (typeNode) { - dataMethod.insertVariableStatement(0, { - declarationKind: VariableDeclarationKind.Const, - declarations: [{ + returnObject.addPropertyAssignment({ name: propertyData.getName(), - type: typeNode, - initializer: propertyData.getInitializer()?.getText() ?? 'undefined', - }], - }); - returnObject.addShorthandPropertyAssignment({ - name: propertyData.getName(), + initializer: (propertyData.getInitializer()?.getText() ?? 'undefined') + " as " + typeNode, }); } else { returnObject.addPropertyAssignment({ From 7c2fa2bd01fa3e7c130922062bd392335ac528db Mon Sep 17 00:00:00 2001 From: Olivier Adam Date: Wed, 13 Dec 2023 10:46:55 +0100 Subject: [PATCH 2/3] hack to put props first --- src/migrator/utils.ts | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/migrator/utils.ts b/src/migrator/utils.ts index e82e58a..f570cf1 100644 --- a/src/migrator/utils.ts +++ b/src/migrator/utils.ts @@ -7,12 +7,24 @@ import type { } from 'ts-morph'; import { SyntaxKind } from 'ts-morph'; -export const addPropertyObject = (mainObject: ObjectLiteralExpression, propName: string, initializer = '{}'): ObjectLiteralExpression => mainObject - .addPropertyAssignment({ - name: propName, - initializer, - }) - .getFirstDescendantByKindOrThrow(SyntaxKind.ObjectLiteralExpression); +export const addPropertyObject = (mainObject: ObjectLiteralExpression, propName: string, initializer = '{}'): ObjectLiteralExpression => { + if (propName === 'props') { + // put props as first property + return mainObject + .insertPropertyAssignment(0, { + name: propName, + initializer, + }) + .getFirstDescendantByKindOrThrow(SyntaxKind.ObjectLiteralExpression); + } else { + return mainObject + .addPropertyAssignment({ + name: propName, + initializer, + }) + .getFirstDescendantByKindOrThrow(SyntaxKind.ObjectLiteralExpression); + } +}; export const addPropertyArray = (mainObject: ObjectLiteralExpression, propName: string, initializer = '[]'): ArrayLiteralExpression => mainObject .addPropertyAssignment({ From 828ac63b1c943b9a3b1405eb16ecfa70a1590347 Mon Sep 17 00:00:00 2001 From: Olivier Adam Date: Wed, 13 Dec 2023 18:30:14 +0100 Subject: [PATCH 3/3] safer fallback --- src/migrator/migratorManager.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/migrator/migratorManager.ts b/src/migrator/migratorManager.ts index f421fbb..2539168 100644 --- a/src/migrator/migratorManager.ts +++ b/src/migrator/migratorManager.ts @@ -199,7 +199,7 @@ export default class MigrationManager { boolean: 'Boolean', number: 'Number', }; - let fallbackType = 'Object'; + let fallbackType = 'null as unknown'; fallbackType = isArray ? 'Array' : fallbackType; fallbackType = isFunction ? 'Function' : fallbackType;