forked from getyourguide/vue-class-migrator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwatch.ts
38 lines (33 loc) · 1.25 KB
/
watch.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
import { Decorator, SyntaxKind } from 'ts-morph';
import { stringNodeToSTring } from '../utils';
import type MigrationManager from '../migratorManager';
// @Watcher
export default (migrationManager: MigrationManager) => {
const { clazz } = migrationManager;
const watchers = clazz.getMethods().filter((m) => m.getDecorator('Watch'));
watchers.forEach((watcher) => {
const watcherName = watcher.getName();
const watcherDecorators: Decorator[] = watcher
.getDecorators()
.filter((decorator) => decorator.getName() === 'Watch');
watcherDecorators.forEach((watcherDecorator) => {
const decoratorArgs = watcherDecorator.getArguments();
const watchPath = stringNodeToSTring(decoratorArgs[0]);
const watchOptions = decoratorArgs[1]
?.asKindOrThrow(SyntaxKind.ObjectLiteralExpression)
.getText();
migrationManager.addWatch({
watchPath,
watchOptions,
handlerMethod: watcherName,
});
});
migrationManager.addMethod({
methodName: watcherName,
parameters: watcher.getParameters().map((p) => p.getStructure()),
isAsync: watcher.isAsync(),
returnType: watcher.getReturnTypeNode()?.getText(),
statements: watcher.getBodyText() ?? '',
});
});
};