@@ -4,9 +4,10 @@ import {
4
4
createSimpleExpression ,
5
5
SimpleExpressionNode
6
6
} from '@vue/compiler-dom'
7
- import { isPlainObject , isString , toDisplayString } from '@vue/shared'
7
+ import { isObject , isString , toDisplayString } from '@vue/shared'
8
8
import { I18n , Locale } from 'vue-i18n'
9
- import { evaluateValue } from './utils'
9
+ import { evaluateValue } from './transpiler'
10
+ import { report , ReportCodes } from './report'
10
11
11
12
// TODO: should be imported from vue-i18n
12
13
type VTDirectiveValue = {
@@ -16,58 +17,94 @@ type VTDirectiveValue = {
16
17
choice ?: number
17
18
}
18
19
20
+ // TODO: should be used from shared libs
19
21
export const isNumber = ( val : unknown ) : val is number =>
20
22
typeof val === 'number' && isFinite ( val )
21
23
22
24
export function defineTransformVT ( i18n : I18n ) : DirectiveTransform {
23
- return ( dir , node , context ) => {
25
+ return ( dir , node /* , context*/ ) => {
24
26
const { exp, loc } = dir
25
- // console.log('v-t trans', dir)
26
- // console.log('v-t trans node', node)
27
27
if ( ! exp ) {
28
- // TODO: throw error
28
+ // TODO: throw error with context.OnError
29
+ // NOTE: We need to support from @vue /compiler-core
30
+ // https://github.com/vuejs/vue-next/issues/1147
31
+ report ( ReportCodes . UNEXPECTED_DIRECTIVE_EXPRESSION , {
32
+ mode : 'error' ,
33
+ args : [ node . loc . source || '' ] ,
34
+ loc : node . loc
35
+ } )
29
36
}
30
- if ( node . children . length ) {
31
- // TODO: throw error
37
+ if ( node . children . length > 0 ) {
38
+ // TODO: throw error with context.OnError
39
+ // NOTE: We need to support from @vue /compiler-core
40
+ // https://github.com/vuejs/vue-next/issues/1147
41
+ report ( ReportCodes . ORVERRIDE_ELEMENT_CHILDREN , {
42
+ mode : 'error' ,
43
+ args : [ node . loc . source || '' ] ,
44
+ loc : node . loc
45
+ } )
46
+ node . children . length = 0
32
47
}
33
48
34
- const { status, value } = evaluateValue (
35
- ( exp as SimpleExpressionNode ) . content
36
- )
37
- if ( status === 'ng' ) {
38
- // TODO: throw error
39
- }
49
+ const simpleExp = exp as SimpleExpressionNode
50
+ if ( simpleExp . isConstant ) {
51
+ if ( dir . modifiers . includes ( 'preserve' ) ) {
52
+ report ( ReportCodes . NOT_SUPPORTED_PRESERVE , {
53
+ args : [ node . loc . source || '' ] ,
54
+ loc : node . loc
55
+ } )
56
+ }
40
57
41
- const parsedValue = parseValue ( value )
42
- const translated = i18n . global . t ( ...makeParams ( parsedValue ) )
43
- if ( translated ) {
44
- ; ( exp as SimpleExpressionNode ) . content = toDisplayString (
45
- `${ JSON . stringify ( translated ) } `
46
- )
47
- }
58
+ const { status, value } = evaluateValue ( simpleExp . content )
59
+ if ( status === 'ng' ) {
60
+ report ( ReportCodes . FAILED_VALUE_EVALUATION , {
61
+ args : [ node . loc . source || '' ] ,
62
+ loc : node . loc
63
+ } )
64
+ return { props : [ ] }
65
+ }
66
+
67
+ const [ parsedValue , parseStatus ] = parseValue ( value )
68
+ if ( parseStatus !== ReportCodes . SUCCESS ) {
69
+ report ( parseStatus , { args : [ node . loc . source || '' ] , loc : node . loc } )
70
+ return { props : [ ] }
71
+ }
72
+
73
+ const translated = i18n . global . t ( ...makeParams ( parsedValue ! ) )
74
+ if ( translated ) {
75
+ simpleExp . content = toDisplayString ( `${ JSON . stringify ( translated ) } ` )
76
+ }
48
77
49
- return {
50
- props : [
51
- createObjectProperty (
52
- createSimpleExpression ( `textContent` , true , loc ) ,
53
- exp || createSimpleExpression ( '' , true )
54
- )
55
- ]
78
+ // success!
79
+ return {
80
+ props : [
81
+ createObjectProperty (
82
+ createSimpleExpression ( `textContent` , true , loc ) ,
83
+ exp || createSimpleExpression ( '' , true )
84
+ )
85
+ ]
86
+ }
87
+ } else {
88
+ report ( ReportCodes . NOT_SUPPORTED_BINDING_PRE_TRANSLATION , {
89
+ args : [ node . loc . source || '' ] ,
90
+ loc : node . loc
91
+ } )
92
+ return { props : [ ] }
56
93
}
57
94
}
58
95
}
59
96
60
97
// TODO: should be defined typing ...
61
- function parseValue ( value : unknown ) : VTDirectiveValue {
98
+ function parseValue ( value : unknown ) : [ VTDirectiveValue | null , ReportCodes ] {
62
99
if ( isString ( value ) ) {
63
- return { path : value }
64
- } else if ( isPlainObject ( value ) ) {
100
+ return [ { path : value } , ReportCodes . SUCCESS ]
101
+ } else if ( isObject ( value ) ) {
65
102
if ( ! ( 'path' in value ) ) {
66
- throw new Error ( 'TODO' )
103
+ return [ null , ReportCodes . REQUIRED_PARAMETER ]
67
104
}
68
- return value as VTDirectiveValue
105
+ return [ value as VTDirectiveValue , ReportCodes . SUCCESS ]
69
106
} else {
70
- throw new Error ( 'TODO' )
107
+ return [ null , ReportCodes . INVALID_PARAMETER_TYPE ]
71
108
}
72
109
}
73
110
0 commit comments