@@ -2,6 +2,7 @@ import type { DirectiveTransform } from '../transform'
2
2
import {
3
3
type ExpressionNode ,
4
4
NodeTypes ,
5
+ type SimpleExpressionNode ,
5
6
createObjectProperty ,
6
7
createSimpleExpression ,
7
8
} from '../ast'
@@ -17,10 +18,45 @@ export const transformBind: DirectiveTransform = (dir, _node, context) => {
17
18
const { modifiers, loc } = dir
18
19
const arg = dir . arg !
19
20
20
- // :arg is replaced by :arg="arg"
21
21
let { exp } = dir
22
- if ( ! exp && arg . type === NodeTypes . SIMPLE_EXPRESSION ) {
23
- const propName = camelize ( arg . content )
22
+
23
+ // handle empty expression
24
+ if ( exp && exp . type === NodeTypes . SIMPLE_EXPRESSION && ! exp . content . trim ( ) ) {
25
+ if ( ! __BROWSER__ ) {
26
+ // #10280 only error against empty expression in non-browser build
27
+ // because :foo in in-DOM templates will be parsed into :foo="" by the
28
+ // browser
29
+ context . onError (
30
+ createCompilerError ( ErrorCodes . X_V_BIND_NO_EXPRESSION , loc ) ,
31
+ )
32
+ return {
33
+ props : [
34
+ createObjectProperty ( arg , createSimpleExpression ( '' , true , loc ) ) ,
35
+ ] ,
36
+ }
37
+ } else {
38
+ exp = undefined
39
+ }
40
+ }
41
+
42
+ // same-name shorthand - :arg is expanded to :arg="arg"
43
+ if ( ! exp ) {
44
+ if ( arg . type !== NodeTypes . SIMPLE_EXPRESSION || ! arg . isStatic ) {
45
+ // only simple expression is allowed for same-name shorthand
46
+ context . onError (
47
+ createCompilerError (
48
+ ErrorCodes . X_V_BIND_INVALID_SAME_NAME_ARGUMENT ,
49
+ arg . loc ,
50
+ ) ,
51
+ )
52
+ return {
53
+ props : [
54
+ createObjectProperty ( arg , createSimpleExpression ( '' , true , loc ) ) ,
55
+ ] ,
56
+ }
57
+ }
58
+
59
+ const propName = camelize ( ( arg as SimpleExpressionNode ) . content )
24
60
exp = dir . exp = createSimpleExpression ( propName , false , arg . loc )
25
61
if ( ! __BROWSER__ ) {
26
62
exp = dir . exp = processExpression ( exp , context )
@@ -57,16 +93,6 @@ export const transformBind: DirectiveTransform = (dir, _node, context) => {
57
93
}
58
94
}
59
95
60
- if (
61
- ! exp ||
62
- ( exp . type === NodeTypes . SIMPLE_EXPRESSION && ! exp . content . trim ( ) )
63
- ) {
64
- context . onError ( createCompilerError ( ErrorCodes . X_V_BIND_NO_EXPRESSION , loc ) )
65
- return {
66
- props : [ createObjectProperty ( arg , createSimpleExpression ( '' , true , loc ) ) ] ,
67
- }
68
- }
69
-
70
96
return {
71
97
props : [ createObjectProperty ( arg , exp ) ] ,
72
98
}
0 commit comments