diff --git a/src/migrator/migratorManager.ts b/src/migrator/migratorManager.ts index 0a08a0d..2539168 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({ @@ -195,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; 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({ 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({