1
1
import * as bt from '@babel/types'
2
2
import recast from 'recast'
3
- import { ASTElement , ASTExpression } from 'vue-template-compiler'
3
+ import { ASTElement , ASTExpression , ASTNode } from 'vue-template-compiler'
4
4
import buildParser from '../babel-parser'
5
- import { Documentation } from '../Documentation'
5
+ import { Documentation , ParamTag } from '../Documentation'
6
6
import { TemplateParserOptions } from '../parse-template'
7
+ import extractLeadingComment from '../utils/extractLeadingComment'
8
+ import getDoclets from '../utils/getDoclets'
9
+
10
+ const parser = buildParser ( { plugins : [ 'typescript' ] } )
7
11
8
12
const allowRE = / ^ ( v - b i n d | : ) /
9
13
export default function propTemplateHandler (
@@ -12,35 +16,50 @@ export default function propTemplateHandler(
12
16
options : TemplateParserOptions ,
13
17
) {
14
18
if ( options . functional ) {
15
- propsInAttributes ( templateAst , documentation )
16
- propsInInterpolation ( templateAst , documentation )
19
+ propsInAttributes ( templateAst , documentation , options )
20
+ propsInInterpolation ( templateAst , documentation , options )
17
21
}
18
22
}
19
23
20
- function propsInAttributes ( templateAst : ASTElement , documentation : Documentation ) {
24
+ function propsInAttributes (
25
+ templateAst : ASTElement ,
26
+ documentation : Documentation ,
27
+ options : TemplateParserOptions ,
28
+ ) {
21
29
const bindings = templateAst . attrsMap
22
30
const keys = Object . keys ( bindings )
23
31
for ( const key of keys ) {
24
32
// only look at expressions
25
33
if ( allowRE . test ( key ) ) {
26
34
const expression = bindings [ key ]
27
- getPropsFromExpression ( expression , documentation )
35
+ getPropsFromExpression ( templateAst . parent , templateAst , expression , documentation , options )
28
36
}
29
37
}
30
38
}
31
39
32
- function propsInInterpolation ( templateAst : ASTElement , documentation : Documentation ) {
40
+ function propsInInterpolation (
41
+ templateAst : ASTElement ,
42
+ documentation : Documentation ,
43
+ options : TemplateParserOptions ,
44
+ ) {
33
45
if ( templateAst . children ) {
34
46
templateAst . children
35
47
. filter ( c => c . type === 2 )
36
48
. forEach ( ( expr : ASTExpression ) => {
37
- getPropsFromExpression ( expr . expression , documentation )
49
+ getPropsFromExpression ( templateAst , expr , expr . expression , documentation , options )
38
50
} )
39
51
}
40
52
}
41
53
42
- function getPropsFromExpression ( expression : string , documentation : Documentation ) {
43
- const ast = buildParser ( { plugins : [ 'typescript' ] } ) . parse ( expression )
54
+ function getPropsFromExpression (
55
+ parentAst : ASTElement | undefined ,
56
+ item : ASTNode ,
57
+ expression : string ,
58
+ documentation : Documentation ,
59
+ options : TemplateParserOptions ,
60
+ ) {
61
+ const ast = parser . parse ( expression )
62
+ const propsFound : string [ ] = [ ]
44
63
recast . visit ( ast . program , {
45
64
visitMemberExpression ( path ) {
46
65
const obj = path . node ? path . node . object : undefined
@@ -54,9 +73,27 @@ function getPropsFromExpression(expression: string, documentation: Documentation
54
73
) {
55
74
const pName = propName . name
56
75
const p = documentation . getPropDescriptor ( pName )
76
+ propsFound . push ( pName )
57
77
p . type = { name : 'undefined' }
58
78
}
59
79
return false
60
80
} ,
61
81
} )
82
+ if ( propsFound . length ) {
83
+ const comment = extractLeadingComment ( parentAst , item , options . rootLeadingComment )
84
+ const doclets = getDoclets ( comment )
85
+ const propTags = doclets . tags && ( doclets . tags . filter ( d => d . title === 'prop' ) as ParamTag [ ] )
86
+ if ( propTags && propTags . length ) {
87
+ propsFound . forEach ( pName => {
88
+ const propTag = propTags . filter ( pt => pt . name === pName )
89
+ if ( propTag . length ) {
90
+ const p = documentation . getPropDescriptor ( pName )
91
+ p . type = propTag [ 0 ] . type
92
+ if ( typeof propTag [ 0 ] . description === 'string' ) {
93
+ p . description = propTag [ 0 ] . description
94
+ }
95
+ }
96
+ } )
97
+ }
98
+ }
62
99
}
0 commit comments