forked from getyourguide/vue-class-migrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetters.ts
43 lines (39 loc) · 1.68 KB
/
getters.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { SyntaxKind } from 'ts-morph';
import { extractPropertiesWithDecorator, stringNodeToSTring } from '../utils';
import type MigrationManager from '../migratorManager';
const supportedGetterOptions = ['namespace']; // @Getter("", {...})
export default (migrationManager: MigrationManager) => {
// Vuex getters are computed properties
const { clazz } = migrationManager;
const vuexGetters = extractPropertiesWithDecorator(clazz, 'Getter');
if (vuexGetters.length) {
vuexGetters.forEach((vuexGetter) => {
const decoratorArgs = vuexGetter.getDecoratorOrThrow('Getter').getArguments();
const getterMethodName = decoratorArgs[0]
? stringNodeToSTring(decoratorArgs[0])
: vuexGetter.getName();
const getterOptions = decoratorArgs[1]?.asKindOrThrow(SyntaxKind.ObjectLiteralExpression);
const namespace = getterOptions?.getProperty('namespace')
?.asKindOrThrow(SyntaxKind.PropertyAssignment)
.getInitializerIfKindOrThrow(SyntaxKind.StringLiteral)
.getLiteralText();
getterOptions?.getProperties().forEach((prop) => {
if (
prop.isKind(SyntaxKind.PropertyAssignment)
&& !supportedGetterOptions.includes(prop.getName())
) {
throw new Error(`@Getter option ${prop.getName()} not supported.`);
}
});
const propertyType = vuexGetter.getTypeNode()?.getText();
const getterName = (
namespace ? [namespace, getterMethodName].join('/') : getterMethodName
);
migrationManager.addComputedProp({
name: vuexGetter.getName(),
returnType: propertyType,
statements: `return this.$store.getters["${getterName}"];`,
});
});
}
};