From 86f512f119feb0e7734b011ffe4028c41f65218c Mon Sep 17 00:00:00 2001 From: fxy060608 Date: Tue, 25 Feb 2020 18:34:46 +0800 Subject: [PATCH] =?UTF-8?q?fix(v3):=20generate=20text=20with=20multiple=20?= =?UTF-8?q?statements=EF=BC=88https://ask.dcloud.net.cn/question/89423?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../compiler-app-plus-extra.service.spec.js | 24 +++++++++++-------- .../compiler-app-plus-extra.view.spec.js | 12 +++++++--- .../compiler-app-plus.service.spec.js | 24 +++++++++---------- .../__tests__/compiler-app-plus.view.spec.js | 24 +++++++++---------- .../uni-template-compiler/__tests__/demo.js | 4 ++-- .../lib/app/parser/text-parser.js | 2 +- .../uni-template-compiler/lib/app/service.js | 1 + .../uni-template-compiler/lib/app/util.js | 15 ++++++++---- .../uni-template-compiler/lib/app/view.js | 3 ++- 9 files changed, 64 insertions(+), 45 deletions(-) diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js index 9538ae89c..42642d27d 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.service.spec.js @@ -28,7 +28,7 @@ describe('codegen', () => { ) assertCodegen( '
{{item}}
', - `with(this){return _c('div',[_l((_$s(1,'f',{forItems:list,fill:true})),function(item,index,$20,$30){return [[_c('text',{key:_$s(("3-"+$30),'a-key',index+'_0'+'_0')},[_v((_$s(("3-"+$30),'t0',_s(item))))])]]})],2)}` + `with(this){return _c('div',[_l((_$s(1,'f',{forItems:list,fill:true})),function(item,index,$20,$30){return [[_c('text',{key:_$s(("3-"+$30),'a-key',index+'_0'+'_0')},[_v((_$s(("3-"+$30),'t0-0',_s(item))))])]]})],2)}` ) }) it('generate directive', () => { @@ -48,15 +48,15 @@ describe('codegen', () => { ) assertCodegen( '
', - `with(this){return _c('div',[_l((_$s(1,'f',{forItems:items,fill:true})),function(item,$10,$20,$30){return [_v((_$s(("1-"+$30),'t0',_s(text))))]})],2)}` + `with(this){return _c('div',[_l((_$s(1,'f',{forItems:items,fill:true})),function(item,$10,$20,$30){return [_v((_$s(("1-"+$30),'t0-0',_s(text))))]})],2)}` ) assertCodegen( '
', - `with(this){return _c('div',[_l((_$s(1,'f',{forItems:items})),function(item,$10,$20,$30){return [_c('span',{key:_$s(1,'f',{forIndex:$20,keyIndex:0,key:1+'-0'+$30})}),_v((_$s(("1-"+$30),'t0',_s(text))))]})],2)}` + `with(this){return _c('div',[_l((_$s(1,'f',{forItems:items})),function(item,$10,$20,$30){return [_c('span',{key:_$s(1,'f',{forIndex:$20,keyIndex:0,key:1+'-0'+$30})}),_v((_$s(("1-"+$30),'t1-0',_s(text))))]})],2)}` ) assertCodegen( '
', - `with(this){return _c('div',[_l((_$s(1,'f',{forItems:items,fill:true})),function(item,$10,$20,$30){return [_v((_$s(("1-"+$30),'t0',_s(text1)))+(_$s(("1-"+$30),'t1',_s(text2))))]})],2)}` + `with(this){return _c('div',[_l((_$s(1,'f',{forItems:items,fill:true})),function(item,$10,$20,$30){return [_v((_$s(("1-"+$30),'t0-0',_s(text1)))+(_$s(("1-"+$30),'t0-1',_s(text2))))]})],2)}` ) assertCodegen( '
', @@ -64,28 +64,32 @@ describe('codegen', () => { ) assertCodegen( '', - `with(this){return _c('view',[_l((_$s(1,'f',{forItems:arr,fill:true})),function(item,index,$20,$30){return [_v((_$s(("1-"+$30),'t0',_s(item))))]})],2)}` + `with(this){return _c('view',[_l((_$s(1,'f',{forItems:arr,fill:true})),function(item,index,$20,$30){return [_v((_$s(("1-"+$30),'t0-0',_s(item))))]})],2)}` ) assertCodegen( '{{item}}', - `with(this){return _c('view',[_l((_$s(1,'f',{forItems:arr,fill:true})),function(item,index,$20,$30){return [_v((_$s(("1-"+$30),'t0',_s(item))))]})],2)}` + `with(this){return _c('view',[_l((_$s(1,'f',{forItems:arr,fill:true})),function(item,index,$20,$30){return [_v((_$s(("1-"+$30),'t0-0',_s(item))))]})],2)}` ) assertCodegen( '{{item}}', - `with(this){return _c('view',[_l((_$s(1,'f',{forItems:arr,fill:true})),function(item,index,$20,$30){return [(_$s(("2-"+$30),'i',item==3))?[_v((_$s(("2-"+$30),'t0',_s(item))))]:_e()]})],2)}` + `with(this){return _c('view',[_l((_$s(1,'f',{forItems:arr,fill:true})),function(item,index,$20,$30){return [(_$s(("2-"+$30),'i',item==3))?[_v((_$s(("2-"+$30),'t0-0',_s(item))))]:_e()]})],2)}` ) }) it('generate text with multiple statements', () => { assertCodegen( `
A{{ d | e | f }}B{{text}}C
`, - `with(this){return _c('div',{attrs:{"id":_$s(0,'a-id','a'+b),"_i":0}},[_v((_$s(0,'t0',_s(_f("f")(_f("e")(d)))))+(_$s(0,'t1',_s(text))))])}` + `with(this){return _c('div',{attrs:{"id":_$s(0,'a-id','a'+b),"_i":0}},[_v((_$s(0,'t0-0',_s(_f("f")(_f("e")(d)))))+(_$s(0,'t0-1',_s(text))))])}` + ) + assertCodegen( + `{{obj.param1}}123123{{obj.param1}}123123{{obj.param1}} -{{obj.param3}}---{{obj.param3}} {{obj.param2}}aaaa{{obj.param2}}aaaa{{obj.param2}}`, + `with(this){return _c('view',[_v((_$s(0,'t0-0',_s(obj.param1)))+(_$s(0,'t0-1',_s(obj.param1)))+(_$s(0,'t0-2',_s(obj.param1)))),_c('text',[_v((_$s(1,'t0-0',_s(obj.param3)))+(_$s(1,'t0-1',_s(obj.param3))))]),_v((_$s(0,'t2-0',_s(obj.param2)))+(_$s(0,'t2-1',_s(obj.param2)))+(_$s(0,'t2-2',_s(obj.param2))))])}` ) }) it('generate v-slot', () => { assertCodegen( '{{ user.firstName }}', - `with(this){return _c('current-user',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function({ user }, _svm, _si){return [_v((_svm._$s(("0-"+_si),'t0',_s(user.firstName))))]}}])})}` + `with(this){return _c('current-user',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function({ user }, _svm, _si){return [_v((_svm._$s(("0-"+_si),'t0-0',_s(user.firstName))))]}}])})}` ) assertCodegen( 'ABCD', @@ -97,7 +101,7 @@ describe('codegen', () => { {{item.name}} `, - `with(this){return _c('current-user',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function({result}, _svm, _si){return _l((_svm._$s(("2-"+_si),'f',{forItems:result.list})),function(item,index,$20,$30){return _c('view',{key:_svm._$s(("2-"+_si),'f',{forIndex:$20,key:("2-"+_si)+'-'+$30}),attrs:{"_i":(("2-"+_si)+$30)}},[_v((_svm._$s((("2-"+_si)+$30),'t0',_s(item.name))))])})}}])})}` + `with(this){return _c('current-user',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function({result}, _svm, _si){return _l((_svm._$s(("2-"+_si),'f',{forItems:result.list})),function(item,index,$20,$30){return _c('view',{key:_svm._$s(("2-"+_si),'f',{forIndex:$20,key:("2-"+_si)+'-'+$30}),attrs:{"_i":(("2-"+_si)+$30)}},[_v((_svm._$s((("2-"+_si)+$30),'t0-0',_s(item.name))))])})}}])})}` ) }) diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js index 23b14375a..487a15611 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus-extra.view.spec.js @@ -32,19 +32,25 @@ describe('codegen', () => { it('generate events with multiple statements', () => { assertCodegen( '
A{{ d | e | f }}B{{text}}C
', - `with(this){return _c('div',{attrs:{"_i":0}},[_v("A"+(_$g(0,'t0'))+"B"+(_$g(0,'t1'))+"C")])}` + `with(this){return _c('div',{attrs:{"_i":0}},[_v("A"+(_$g(0,'t0-0'))+"B"+(_$g(0,'t0-1'))+"C")])}` ) }) it('generate slot fallback content', () => { assertCodegen( '
{{hi}}
', - `with(this){return _c('div',{attrs:{"_i":0}},[_t("default",[_c('div',{attrs:{"_i":2}},[_v((_$g(2,'t0')))])],{"_i":1})],2)}` + `with(this){return _c('div',{attrs:{"_i":0}},[_t("default",[_c('div',{attrs:{"_i":2}},[_v((_$g(2,'t0-0')))])],{"_i":1})],2)}` ) + }) + it('generate text with multiple statements', () => { + assertCodegen( + `{{obj.param1}}123123{{obj.param1}}123123{{obj.param1}} -{{obj.param3}}---{{obj.param3}} {{obj.param2}}aaaa{{obj.param2}}aaaa{{obj.param2}}`, + `with(this){return _c('v-uni-view',{attrs:{"_i":0}},[_v((_$g(0,'t0-0'))+"123123"+(_$g(0,'t0-1'))+"123123"+(_$g(0,'t0-2'))),_c('v-uni-text',{attrs:{"_i":1}},[_v("-"+(_$g(1,'t0-0'))+"---"+(_$g(1,'t0-1')))]),_v((_$g(0,'t2-0'))+"aaaa"+(_$g(0,'t2-1'))+"aaaa"+(_$g(0,'t2-2')))],1)}` + ) }) it('generate v-slot', () => { assertCodegen( '{{ user.firstName }}', - `with(this){return _c('current-user',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function({ user }, _svm, _si){return [_v((_svm._$g(("0-"+_si),'t0')))]}}])})}` + `with(this){return _c('current-user',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function({ user }, _svm, _si){return [_v((_svm._$g(("0-"+_si),'t0-0')))]}}])})}` ) }) it('generate keep-alive', () => { diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus.service.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus.service.spec.js index 5db90d294..4a92c3e00 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus.service.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus.service.spec.js @@ -27,14 +27,14 @@ describe('codegen', () => { it('generate filters', () => { assertCodegen( '
{{ d | e | f }}
', - `with(this){return _c('div',{attrs:{"id":_$s(0,'a-id',_f("c")(_f("b")(a))),"_i":0}},[_v((_$s(0,'t0',_s(_f("f")(_f("e")(d))))))])}` + `with(this){return _c('div',{attrs:{"id":_$s(0,'a-id',_f("c")(_f("b")(a))),"_i":0}},[_v((_$s(0,'t0-0',_s(_f("f")(_f("e")(d))))))])}` ) }) it('generate filters with no arguments', () => { assertCodegen( '
{{ d | e() }}
', - `with(this){return _c('div',[_v((_$s(0,'t0',_s(_f("e")(d)))))])}` + `with(this){return _c('div',[_v((_$s(0,'t0-0',_s(_f("e")(d)))))])}` ) }) @@ -162,7 +162,7 @@ describe('codegen', () => { it('generate template tag', () => { assertCodegen( '
', - `with(this){return _c('div',[[_c('p',[_v((_$s(2,'t0',_s(hello))))])]],2)}` + `with(this){return _c('div',[[_c('p',[_v((_$s(2,'t0-0',_s(hello))))])]],2)}` ) }) @@ -197,47 +197,47 @@ describe('codegen', () => { it('generate scoped slot', () => { assertCodegen( '', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function(bar, _svm, _si){return [_v((_svm._$s(("1-"+_si),'t0',_s(bar))))]}}])})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function(bar, _svm, _si){return [_v((_svm._$s(("1-"+_si),'t0-0',_s(bar))))]}}])})}` ) assertCodegen( '
{{ bar }}
', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function(bar, _svm, _si){return _c('div',{attrs:{"_i":("1-"+_si)}},[_v((_svm._$s(("1-"+_si),'t0',_s(bar))))])}}])})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function(bar, _svm, _si){return _c('div',{attrs:{"_i":("1-"+_si)}},[_v((_svm._$s(("1-"+_si),'t0-0',_s(bar))))])}}])})}` ) }) it('generate named scoped slot', () => { assertCodegen( '', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"foo",fn:function(bar, _svm, _si){return [_v((_svm._$s(("1-"+_si),'t0',_s(bar))))]}}])})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"foo",fn:function(bar, _svm, _si){return [_v((_svm._$s(("1-"+_si),'t0-0',_s(bar))))]}}])})}` ) assertCodegen( '
{{ bar }}
', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"foo",fn:function(bar, _svm, _si){return _c('div',{attrs:{"_i":("1-"+_si)}},[_v((_svm._$s(("1-"+_si),'t0',_s(bar))))])}}])})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"foo",fn:function(bar, _svm, _si){return _c('div',{attrs:{"_i":("1-"+_si)}},[_v((_svm._$s(("1-"+_si),'t0-0',_s(bar))))])}}])})}` ) }) it('generate dynamic scoped slot', () => { assertCodegen( '', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:foo,fn:function(bar, _svm, _si){return [_v((_svm._$s(("1-"+_si),'t0',_s(bar))))]}}],null,true)})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:foo,fn:function(bar, _svm, _si){return [_v((_svm._$s(("1-"+_si),'t0-0',_s(bar))))]}}],null,true)})}` ) }) it('generate scoped slot with multiline v-if', () => { assertCodegen( '', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function(bar, _svm, _si){return (_svm._$s(("1-"+_si),'i',\nshow\n))?[_v((_svm._$s(("1-"+_si),'t0',_s(bar))))]:undefined}}],null,true)})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function(bar, _svm, _si){return (_svm._$s(("1-"+_si),'i',\nshow\n))?[_v((_svm._$s(("1-"+_si),'t0-0',_s(bar))))]:undefined}}],null,true)})}` ) assertCodegen( '
{{ bar }}
', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"foo",fn:function(bar, _svm, _si){return (_svm._$s(("1-"+_si),'i',\nshow\n))?_c('div',{attrs:{"_i":("1-"+_si)}},[_v((_svm._$s(("1-"+_si),'t0',_s(bar))))]):_e()}}],null,true)})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"foo",fn:function(bar, _svm, _si){return (_svm._$s(("1-"+_si),'i',\nshow\n))?_c('div',{attrs:{"_i":("1-"+_si)}},[_v((_svm._$s(("1-"+_si),'t0-0',_s(bar))))]):_e()}}],null,true)})}` ) }) it('generate scoped slot with new slot syntax', () => { assertCodegen( '', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([(_$s(1,'i',show))?{key:"default",fn:function(bar, _svm, _si){return [_v((_svm._$s(("1-"+_si),'t0',_s(bar))))]}}:null],null,true)})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([(_$s(1,'i',show))?{key:"default",fn:function(bar, _svm, _si){return [_v((_svm._$s(("1-"+_si),'t0-0',_s(bar))))]}}:null],null,true)})}` ) }) @@ -625,7 +625,7 @@ describe('codegen', () => { // normalize type: 2 assertCodegen( '
', - `with(this){return _c('div',[_c('child',{attrs:{"_i":1}}),_l((_$s(2,'f',{forItems:list,fill:true})),function(item,$10,$20,$30){return [_v((_$s(("2-"+$30),'t0',_s(item))))]})],2)}` + `with(this){return _c('div',[_c('child',{attrs:{"_i":1}}),_l((_$s(2,'f',{forItems:list,fill:true})),function(item,$10,$20,$30){return [_v((_$s(("2-"+$30),'t0-0',_s(item))))]})],2)}` ) }) diff --git a/packages/uni-template-compiler/__tests__/compiler-app-plus.view.spec.js b/packages/uni-template-compiler/__tests__/compiler-app-plus.view.spec.js index f674c7805..6fe1baea7 100644 --- a/packages/uni-template-compiler/__tests__/compiler-app-plus.view.spec.js +++ b/packages/uni-template-compiler/__tests__/compiler-app-plus.view.spec.js @@ -27,14 +27,14 @@ describe('codegen', () => { it('generate filters', () => { assertCodegen( '
{{ d | e | f }}
', - `with(this){return _c('div',{attrs:{"id":_$g(0,'a-id'),"_i":0}},[_v((_$g(0,'t0')))])}` + `with(this){return _c('div',{attrs:{"id":_$g(0,'a-id'),"_i":0}},[_v((_$g(0,'t0-0')))])}` ) }) it('generate filters with no arguments', () => { assertCodegen( '
{{ d | e() }}
', - `with(this){return _c('div',{attrs:{"_i":0}},[_v((_$g(0,'t0')))])}` + `with(this){return _c('div',{attrs:{"_i":0}},[_v((_$g(0,'t0-0')))])}` ) }) @@ -162,7 +162,7 @@ describe('codegen', () => { it('generate template tag', () => { assertCodegen( '
', - `with(this){return _c('div',{attrs:{"_i":0}},[[_c('p',{attrs:{"_i":2}},[_v((_$g(2,'t0')))])]],2)}` + `with(this){return _c('div',{attrs:{"_i":0}},[[_c('p',{attrs:{"_i":2}},[_v((_$g(2,'t0-0')))])]],2)}` ) }) @@ -197,47 +197,47 @@ describe('codegen', () => { it('generate scoped slot', () => { assertCodegen( '', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function(bar, _svm, _si){return [_v((_svm._$g(("1-"+_si),'t0')))]}}])})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function(bar, _svm, _si){return [_v((_svm._$g(("1-"+_si),'t0-0')))]}}])})}` ) assertCodegen( '
{{ bar }}
', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function(bar, _svm, _si){return _c('div',{attrs:{"_i":("1-"+_si)}},[_v((_svm._$g(("1-"+_si),'t0')))])}}])})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function(bar, _svm, _si){return _c('div',{attrs:{"_i":("1-"+_si)}},[_v((_svm._$g(("1-"+_si),'t0-0')))])}}])})}` ) }) it('generate named scoped slot', () => { assertCodegen( '', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"foo",fn:function(bar, _svm, _si){return [_v((_svm._$g(("1-"+_si),'t0')))]}}])})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"foo",fn:function(bar, _svm, _si){return [_v((_svm._$g(("1-"+_si),'t0-0')))]}}])})}` ) assertCodegen( '
{{ bar }}
', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"foo",fn:function(bar, _svm, _si){return _c('div',{attrs:{"_i":("1-"+_si)}},[_v((_svm._$g(("1-"+_si),'t0')))])}}])})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"foo",fn:function(bar, _svm, _si){return _c('div',{attrs:{"_i":("1-"+_si)}},[_v((_svm._$g(("1-"+_si),'t0-0')))])}}])})}` ) }) it('generate dynamic scoped slot', () => { assertCodegen( '', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:foo,fn:function(bar, _svm, _si){return [_v((_svm._$g(("1-"+_si),'t0')))]}}],null,true)})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:foo,fn:function(bar, _svm, _si){return [_v((_svm._$g(("1-"+_si),'t0-0')))]}}],null,true)})}` ) }) it('generate scoped slot with multiline v-if', () => { assertCodegen( '', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function(bar, _svm, _si){return (_svm._$g(("1-"+_si),'i'))?[_v((_svm._$g(("1-"+_si),'t0')))]:undefined}}],null,true)})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"default",fn:function(bar, _svm, _si){return (_svm._$g(("1-"+_si),'i'))?[_v((_svm._$g(("1-"+_si),'t0-0')))]:undefined}}],null,true)})}` ) assertCodegen( '
{{ bar }}
', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"foo",fn:function(bar, _svm, _si){return (_svm._$g(("1-"+_si),'i'))?_c('div',{attrs:{"_i":("1-"+_si)}},[_v((_svm._$g(("1-"+_si),'t0')))]):_e()}}],null,true)})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([{key:"foo",fn:function(bar, _svm, _si){return (_svm._$g(("1-"+_si),'i'))?_c('div',{attrs:{"_i":("1-"+_si)}},[_v((_svm._$g(("1-"+_si),'t0-0')))]):_e()}}],null,true)})}` ) }) it('generate scoped slot with new slot syntax', () => { assertCodegen( '', - `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([(_$g(1,'i'))?{key:"default",fn:function(bar, _svm, _si){return [_v((_svm._$g(("1-"+_si),'t0')))]}}:null],null,true)})}` + `with(this){return _c('foo',{attrs:{"_i":0},scopedSlots:_u([(_$g(1,'i'))?{key:"default",fn:function(bar, _svm, _si){return [_v((_svm._$g(("1-"+_si),'t0-0')))]}}:null],null,true)})}` ) }) @@ -625,7 +625,7 @@ describe('codegen', () => { // normalize type: 2 assertCodegen( '
', - `with(this){return _c('div',{attrs:{"_i":0}},[_c('child',{attrs:{"_i":1}}),_l((_$g(2,'f')),function(item,$10,$20,$30){return [_v((_$g(("2-"+$30),'t0')))]})],2)}` + `with(this){return _c('div',{attrs:{"_i":0}},[_c('child',{attrs:{"_i":1}}),_l((_$g(2,'f')),function(item,$10,$20,$30){return [_v((_$g(("2-"+$30),'t0-0')))]})],2)}` ) }) diff --git a/packages/uni-template-compiler/__tests__/demo.js b/packages/uni-template-compiler/__tests__/demo.js index d97e014ea..2ee1fc227 100644 --- a/packages/uni-template-compiler/__tests__/demo.js +++ b/packages/uni-template-compiler/__tests__/demo.js @@ -18,8 +18,8 @@ const scopedPath = path.resolve(__dirname, '../../') const compiler = require('../lib') const res = compiler.compile( - ` -123d + ` +{{obj.param1}}123123{{obj.param1}}123123{{obj.param1}} -{{obj.param3}}---{{obj.param3}} {{obj.param2}}aaaa{{obj.param2}}aaaa{{obj.param2}} `, { miniprogram: true, resourcePath: '/User/fxy/Documents/test.wxml', diff --git a/packages/uni-template-compiler/lib/app/parser/text-parser.js b/packages/uni-template-compiler/lib/app/parser/text-parser.js index f6f55f948..d0d6a74d5 100644 --- a/packages/uni-template-compiler/lib/app/parser/text-parser.js +++ b/packages/uni-template-compiler/lib/app/parser/text-parser.js @@ -43,7 +43,7 @@ module.exports = function parseText ( } // tag token const exp = parseFilters(match[1].trim()) - tokens.push(`(${state.genVar('t' + (state.index++), '_s(' + exp + ')')})`) + tokens.push(`(${state.genVar('t' + (state.childIndex) + '-' + (state.index++), '_s(' + exp + ')')})`) rawTokens.push({ '@binding': exp }) diff --git a/packages/uni-template-compiler/lib/app/service.js b/packages/uni-template-compiler/lib/app/service.js index f7cfad5ff..0eb71e660 100644 --- a/packages/uni-template-compiler/lib/app/service.js +++ b/packages/uni-template-compiler/lib/app/service.js @@ -119,6 +119,7 @@ function transformNode (el, parent, state, isScopedSlot) { pid = getNewId(pid, '_si') } return parseText(el, parent, { + childIndex: state.childIndex || 0, index: 0, service: true, // {{content}} diff --git a/packages/uni-template-compiler/lib/app/util.js b/packages/uni-template-compiler/lib/app/util.js index 8e52704d7..72424eb38 100644 --- a/packages/uni-template-compiler/lib/app/util.js +++ b/packages/uni-template-compiler/lib/app/util.js @@ -104,7 +104,7 @@ function updateForIterator (el, state) { function updateForEleId (el, state) { updateForIterator(el, state) - if (el.for) { + if (el.for) { const it = el.$parentIterator3 ? (el.$parentIterator3 + '+' + "'-'" + '+' + el.iterator3) : el.iterator3 updateEleId(el, it, state) } @@ -194,11 +194,18 @@ function hasOwn (obj, key) { function traverseNode (el, parent, state, isScopedSlot) { state.transformNode(el, parent, state, isScopedSlot) - el.children && el.children.forEach(child => traverseNode(child, el, state, isScopedSlot)) + el.children && el.children.forEach((child, index) => { + state.childIndex = index + traverseNode(child, el, state, isScopedSlot) + }) el.ifConditions && el.ifConditions.forEach((con, index) => { - index !== 0 && traverseNode(con.block, el, state, isScopedSlot) + if (index !== 0) { + state.childIndex = index + traverseNode(con.block, el, state, isScopedSlot) + } }) - el.scopedSlots && Object.values(el.scopedSlots).forEach(slot => { + el.scopedSlots && Object.values(el.scopedSlots).forEach((slot, index) => { + state.childIndex = index slot.slotScope = `${slot.slotScope}, _svm, _si` traverseNode(slot, el, state, true) }) diff --git a/packages/uni-template-compiler/lib/app/view.js b/packages/uni-template-compiler/lib/app/view.js index 7f7f179e4..01ff75c84 100644 --- a/packages/uni-template-compiler/lib/app/view.js +++ b/packages/uni-template-compiler/lib/app/view.js @@ -114,7 +114,8 @@ function transformNode (el, parent, state, isScopedSlot) { if (isScopedSlot && String(pid).indexOf('_si') === -1) { pid = getNewId(pid, '_si') } - return parseText(el, parent, { + return parseText(el, parent, { + childIndex: state.childIndex || 0, index: 0, view: true, // {{content}} -- GitLab