-
Notifications
You must be signed in to change notification settings - Fork 20
/
Watch.ts
68 lines (64 loc) · 2.23 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import { ASTConverter, ASTResultKind, ReferenceKind } from '../types'
import type ts from 'typescript'
import { copySyntheticComments, createIdentifier } from '../../utils'
const watchDecoratorName = 'Watch'
export const convertWatch: ASTConverter<ts.MethodDeclaration> = (node, options) => {
if (!node.decorators) {
return false
}
const decorator = node.decorators.find((el) => (el.expression as ts.CallExpression).expression.getText() === watchDecoratorName)
if (decorator) {
const tsModule = options.typescript
const decoratorArguments = (decorator.expression as ts.CallExpression).arguments
if (decoratorArguments.length > 1) {
const keyName = (decoratorArguments[0] as ts.StringLiteral).text
const watchArguments = decoratorArguments[1]
const method = tsModule.createArrowFunction(
node.modifiers,
node.typeParameters,
node.parameters,
node.type,
tsModule.createToken(tsModule.SyntaxKind.EqualsGreaterThanToken),
node.body ?? tsModule.createBlock([], false)
)
const watchOptions: ts.PropertyAssignment[] = []
if (tsModule.isObjectLiteralExpression(watchArguments)) {
watchArguments.properties.forEach((el) => {
if (!tsModule.isPropertyAssignment(el)) return
watchOptions.push(el)
})
}
return {
tag: 'Watch',
kind: ASTResultKind.COMPOSITION,
imports: [{
named: ['watch'],
external: (options.compatible) ? '@vue/composition-api' : 'vue'
}],
reference: ReferenceKind.VARIABLE,
attributes: [keyName],
nodes: [
tsModule.createExpressionStatement(
copySyntheticComments(
tsModule,
tsModule.createCall(
tsModule.createIdentifier('watch'),
undefined,
[
tsModule.createPropertyAccess(
tsModule.createThis(),
createIdentifier(tsModule, keyName)
),
method,
tsModule.createObjectLiteral(watchOptions)
]
),
node
)
)
] as ts.Statement[]
}
}
}
return false
}