@@ -7,6 +7,10 @@ import { getIdentifierUsages } from './identifiers'
77import { parse } from './babel'
88import { pascalize } from './utils'
99
10+ const ELEMENT : NodeTypes . ELEMENT = 1
11+ const DIRECTIVE : NodeTypes . DIRECTIVE = 7
12+ const SIMPLE_EXPRESSION : NodeTypes . SIMPLE_EXPRESSION = 4
13+
1014const multilineCommentsRE = / \/ \* \s ( .| [ \r \n ] ) * ?\* \/ / gm
1115const singlelineCommentsRE = / \/ \/ \s .* / g
1216
@@ -32,29 +36,25 @@ const BUILD_IN_DIRECTIVES = new Set([
3236 // 'ref',
3337] )
3438
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
3942
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
4245
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
4649 ? undefined
4750 : arg . isStatic
4851 ? JSON . stringify ( arg . content )
4952 : 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,
5858 }
5959}
6060
@@ -123,7 +123,7 @@ export function parseSFC(code: string, id?: string, options?: ScriptSetupTransfo
123123 }
124124
125125 if ( key . startsWith ( 'v-' ) ) {
126- const parsedDirective = parseDirective ( key )
126+ const parsedDirective = parseDirective ( name , key , value )
127127 if ( parsedDirective && ! BUILD_IN_DIRECTIVES . has ( parsedDirective . name ) )
128128 directives . add ( camelize ( parsedDirective . name ) )
129129 if ( parsedDirective ?. argExpression )
0 commit comments