@@ -2,6 +2,7 @@ import type { DirectiveTransform } from '../transform'
22import {
33 type ExpressionNode ,
44 NodeTypes ,
5+ type SimpleExpressionNode ,
56 createObjectProperty ,
67 createSimpleExpression ,
78} from '../ast'
@@ -17,10 +18,45 @@ export const transformBind: DirectiveTransform = (dir, _node, context) => {
1718 const { modifiers, loc } = dir
1819 const arg = dir . arg !
1920
20- // :arg is replaced by :arg="arg"
2121 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 )
2460 exp = dir . exp = createSimpleExpression ( propName , false , arg . loc )
2561 if ( ! __BROWSER__ ) {
2662 exp = dir . exp = processExpression ( exp , context )
@@ -57,16 +93,6 @@ export const transformBind: DirectiveTransform = (dir, _node, context) => {
5793 }
5894 }
5995
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-
7096 return {
7197 props : [ createObjectProperty ( arg , exp ) ] ,
7298 }
0 commit comments