@@ -7,6 +7,10 @@ import { getIdentifierUsages } from './identifiers'
7
7
import { parse } from './babel'
8
8
import { pascalize } from './utils'
9
9
10
+ const ELEMENT : NodeTypes . ELEMENT = 1
11
+ const DIRECTIVE : NodeTypes . DIRECTIVE = 7
12
+ const SIMPLE_EXPRESSION : NodeTypes . SIMPLE_EXPRESSION = 4
13
+
10
14
const multilineCommentsRE = / \/ \* \s ( .| [ \r \n ] ) * ?\* \/ / gm
11
15
const singlelineCommentsRE = / \/ \/ \s .* / g
12
16
@@ -32,29 +36,25 @@ const BUILD_IN_DIRECTIVES = new Set([
32
36
// 'ref',
33
37
] )
34
38
35
- const parseDirective = ( attr : string ) => {
36
- try {
37
- const elementNode = baseCompile ( `<a ${ attr } ></a>` ) . ast . children [ 0 ]
38
- if ( elementNode ?. type !== NodeTypes . ELEMENT ) return undefined
39
+ function parseDirective ( comp : string , attr : string , body : string ) {
40
+ const elementNode = baseCompile ( `<${ comp } ${ attr } ="${ body } " />` ) . ast . children [ 0 ]
41
+ if ( elementNode ?. type !== ELEMENT ) return undefined
39
42
40
- const directiveNode = elementNode . props [ 0 ]
41
- if ( directiveNode ?. type !== NodeTypes . DIRECTIVE ) return undefined
43
+ const directiveNode = elementNode . props [ 0 ]
44
+ if ( directiveNode ?. type !== DIRECTIVE ) return undefined
42
45
43
- const { arg, modifiers, name } = directiveNode
44
- const argExpression
45
- = arg ?. type !== NodeTypes . SIMPLE_EXPRESSION
46
+ const { arg, modifiers, name } = directiveNode
47
+ const argExpression
48
+ = arg ?. type !== SIMPLE_EXPRESSION
46
49
? undefined
47
50
: arg . isStatic
48
51
? JSON . stringify ( arg . content )
49
52
: arg . content
50
- return {
51
- argExpression,
52
- modifiers,
53
- name,
54
- }
55
- }
56
- catch ( error ) {
57
- return undefined
53
+
54
+ return {
55
+ argExpression,
56
+ modifiers,
57
+ name,
58
58
}
59
59
}
60
60
@@ -123,7 +123,7 @@ export function parseSFC(code: string, id?: string, options?: ScriptSetupTransfo
123
123
}
124
124
125
125
if ( key . startsWith ( 'v-' ) ) {
126
- const parsedDirective = parseDirective ( key )
126
+ const parsedDirective = parseDirective ( name , key , value )
127
127
if ( parsedDirective && ! BUILD_IN_DIRECTIVES . has ( parsedDirective . name ) )
128
128
directives . add ( camelize ( parsedDirective . name ) )
129
129
if ( parsedDirective ?. argExpression )
0 commit comments