1
1
import {
2
2
ErrorCodes ,
3
+ NodeTypes ,
3
4
type SimpleExpressionNode ,
4
5
createCompilerError ,
5
6
createSimpleExpression ,
6
7
} from '@vue/compiler-dom'
7
8
import { camelize , isReservedProp } from '@vue/shared'
8
- import type { DirectiveTransform } from '../transform'
9
+ import type { DirectiveTransform , TransformContext } from '../transform'
9
10
11
+ // same-name shorthand - :arg is expanded to :arg="arg"
10
12
export function normalizeBindShorthand (
11
13
arg : SimpleExpressionNode ,
14
+ context : TransformContext ,
12
15
) : SimpleExpressionNode {
13
- // shorthand syntax https://github.com/vuejs/core/pull/9451
16
+ if ( arg . type !== NodeTypes . SIMPLE_EXPRESSION || ! arg . isStatic ) {
17
+ // only simple expression is allowed for same-name shorthand
18
+ context . options . onError (
19
+ createCompilerError (
20
+ ErrorCodes . X_V_BIND_INVALID_SAME_NAME_ARGUMENT ,
21
+ arg . loc ,
22
+ ) ,
23
+ )
24
+ return createSimpleExpression ( '' , true , arg . loc )
25
+ }
26
+
14
27
const propName = camelize ( arg . content )
15
28
const exp = createSimpleExpression ( propName , false , arg . loc )
16
29
exp . ast = null
17
30
return exp
18
31
}
19
32
20
33
export const transformVBind : DirectiveTransform = ( dir , node , context ) => {
21
- let { exp, loc, modifiers } = dir
34
+ const { loc, modifiers } = dir
35
+ let { exp } = dir
22
36
const arg = dir . arg !
23
37
24
38
if ( arg . isStatic && isReservedProp ( arg . content ) ) return
25
39
26
- if ( ! exp ) exp = normalizeBindShorthand ( arg )
40
+ if ( ! exp ) exp = normalizeBindShorthand ( arg , context )
27
41
28
42
let camel = false
29
43
if ( modifiers . includes ( 'camel' ) ) {
@@ -35,11 +49,15 @@ export const transformVBind: DirectiveTransform = (dir, node, context) => {
35
49
}
36
50
37
51
if ( ! exp . content . trim ( ) ) {
38
- context . options . onError (
39
- createCompilerError ( ErrorCodes . X_V_BIND_NO_EXPRESSION , loc ) ,
40
- )
41
- context . template += ` ${ arg . content } =""`
42
- return
52
+ if ( ! __BROWSER__ ) {
53
+ // #10280 only error against empty expression in non-browser build
54
+ // because :foo in in-DOM templates will be parsed into :foo="" by the
55
+ // browser
56
+ context . options . onError (
57
+ createCompilerError ( ErrorCodes . X_V_BIND_NO_EXPRESSION , loc ) ,
58
+ )
59
+ }
60
+ exp = createSimpleExpression ( '' , true , loc )
43
61
}
44
62
45
63
return {
0 commit comments