@@ -4,9 +4,10 @@ import {
44 createSimpleExpression ,
55 SimpleExpressionNode
66} from '@vue/compiler-dom'
7- import { isPlainObject , isString , toDisplayString } from '@vue/shared'
7+ import { isObject , isString , toDisplayString } from '@vue/shared'
88import { I18n , Locale } from 'vue-i18n'
9- import { evaluateValue } from './utils'
9+ import { evaluateValue } from './transpiler'
10+ import { report , ReportCodes } from './report'
1011
1112// TODO: should be imported from vue-i18n
1213type VTDirectiveValue = {
@@ -16,58 +17,94 @@ type VTDirectiveValue = {
1617 choice ?: number
1718}
1819
20+ // TODO: should be used from shared libs
1921export const isNumber = ( val : unknown ) : val is number =>
2022 typeof val === 'number' && isFinite ( val )
2123
2224export function defineTransformVT ( i18n : I18n ) : DirectiveTransform {
23- return ( dir , node , context ) => {
25+ return ( dir , node /* , context*/ ) => {
2426 const { exp, loc } = dir
25- // console.log('v-t trans', dir)
26- // console.log('v-t trans node', node)
2727 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+ } )
2936 }
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
3247 }
3348
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+ }
4057
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+ }
4877
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 : [ ] }
5693 }
5794 }
5895}
5996
6097// TODO: should be defined typing ...
61- function parseValue ( value : unknown ) : VTDirectiveValue {
98+ function parseValue ( value : unknown ) : [ VTDirectiveValue | null , ReportCodes ] {
6299 if ( isString ( value ) ) {
63- return { path : value }
64- } else if ( isPlainObject ( value ) ) {
100+ return [ { path : value } , ReportCodes . SUCCESS ]
101+ } else if ( isObject ( value ) ) {
65102 if ( ! ( 'path' in value ) ) {
66- throw new Error ( 'TODO' )
103+ return [ null , ReportCodes . REQUIRED_PARAMETER ]
67104 }
68- return value as VTDirectiveValue
105+ return [ value as VTDirectiveValue , ReportCodes . SUCCESS ]
69106 } else {
70- throw new Error ( 'TODO' )
107+ return [ null , ReportCodes . INVALID_PARAMETER_TYPE ]
71108 }
72109}
73110
0 commit comments