diff --git a/packages/uni-mp-compiler/__tests__/codegen.spec.ts b/packages/uni-mp-compiler/__tests__/codegen.spec.ts index 2007e759b1543a0cdbb861b52b564a50a4e4c459..3f681d15487527a2fd611b6123fcf0d376550441 100644 --- a/packages/uni-mp-compiler/__tests__/codegen.spec.ts +++ b/packages/uni-mp-compiler/__tests__/codegen.spec.ts @@ -4,11 +4,11 @@ describe('compiler: codegen', () => { test('module mode preamble', () => { assert( ``, - ``, + ``, `import { vOn as _vOn, vFor as _vFor } from "vue" export function render(_ctx, _cache) { - return { a: _vFor(_ctx.items, item => { return { a: _vOn(_ctx.onClick) }; }) } + return { a: _vFor(_ctx.items, item => { return {}; }), b: _vOn(_ctx.onClick) } }`, { inline: false, mode: 'module', prefixIdentifiers: false } ) @@ -17,11 +17,11 @@ export function render(_ctx, _cache) { test('module mode preamble w/ optimizeImports: true', () => { assert( ``, - ``, + ``, `import { vOn as _vOn, vFor as _vFor } from "vue" export function render(_ctx, _cache) { - return { a: _vFor(_ctx.items, item => { return { a: _vOn(_ctx.onClick) }; }) } + return { a: _vFor(_ctx.items, item => { return {}; }), b: _vOn(_ctx.onClick) } }`, { inline: false, mode: 'module' } ) @@ -30,14 +30,14 @@ export function render(_ctx, _cache) { test('function mode preamble', () => { assert( ``, - ``, + ``, `const _Vue = Vue return function render(_ctx, _cache) { with (_ctx) { const { vOn: _vOn, vFor: _vFor } = _Vue - return { a: _vFor(items, item => { return { a: _vOn(onClick) }; }) } + return { a: _vFor(items, item => { return {}; }), b: _vOn(onClick) } } }`, { inline: false, mode: 'function', prefixIdentifiers: false } @@ -46,11 +46,11 @@ return function render(_ctx, _cache) { test('function mode preamble w/ prefixIdentifiers: true', () => { assert( ``, - ``, + ``, `const { vOn: _vOn, vFor: _vFor } = Vue return function render(_ctx, _cache) { - return { a: _vFor(_ctx.items, item => { return { a: _vOn(_ctx.onClick) }; }) } + return { a: _vFor(_ctx.items, item => { return {}; }), b: _vOn(_ctx.onClick) } }`, { inline: false, mode: 'function' } ) diff --git a/packages/uni-mp-compiler/__tests__/scope.spec.ts b/packages/uni-mp-compiler/__tests__/scope.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..c2dad134f05c341fb3b58132219f9ced623719b1 --- /dev/null +++ b/packages/uni-mp-compiler/__tests__/scope.spec.ts @@ -0,0 +1,56 @@ +import { assert } from './testUtils' + +describe('compiler: scope', () => { + test('v-for', () => { + assert( + `{{item.title}}`, + `{{item.a}}`, + `(_ctx, _cache) => { + return { a: _vFor(_ctx.items, item => { return { a: _toDisplayString(item.title), b: item.id, c: item.isRed, d: _vOn($event => _ctx.onClick(item)) }; }), b: _vOn(_ctx.longpress) } +}` + ) + }) + test('v-for + v-for', () => { + assert( + `{{item.title}}{{handle(foo)}}{{item.id}}{{item1.title}}`, + `{{item.a}}{{b}}{{item.c}}{{item1.a}}`, + `(_ctx, _cache) => { + return { a: _vFor(_ctx.items, item => { return { a: _toDisplayString(item.title), b: _vFor(item.list, item1 => { return { a: _toDisplayString(item1.title), b: item1.id, c: _vOn($event => _ctx.longpress(item1)) }; }), c: _toDisplayString(item.id), d: _vOn($event => _ctx.onClick(item)), e: item.id }; }), b: _toDisplayString(_ctx.handle(_ctx.foo)) } +}` + ) + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _vFor(_ctx.items, item => { return { a: item.id }; }), b: _vFor(_ctx.item1, item1 => { return { a: item1.title }; }) } +}` + ) + }) + test('v-for + v-if', () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _vFor(_ctx.items, item => { return { ...(true ? { a: _ctx.id } : {}) }; }) } +}` + ) + }) + test('v-if', () => { + assert( + `{{ok}}{{ok1}}{{ok2}}{{ok3}}`, + `{{b}}{{d}}{{f}}{{g}}`, + `(_ctx, _cache) => { + return { a: _ctx.ok, ...(_ctx.ok ? { b: _toDisplayString(_ctx.ok) } : _ctx.ok1 ? { d: _toDisplayString(_ctx.ok1) } : _ctx.ok2 ? { f: _toDisplayString(_ctx.ok2) } : { g: _toDisplayString(_ctx.ok3) }), c: _ctx.ok1, e: _ctx.ok2 } +}` + ) + }) + test('v-if + v-for', () => { + assert( + ``, + ``, + `(_ctx, _cache) => { + return { a: _ctx.ok, ...(_ctx.ok ? { b: _vFor(_ctx.items, item => { return { a: item.id, b: item.title }; }), c: _ctx.foo, d: _vOn(_ctx.onClick) } : _ctx.ok1 ? { f: _vFor(_ctx.items, item => { return { a: item.id, b: item.title }; }), g: _ctx.foo, h: _vOn(_ctx.onClick) } : { i: _vFor(_ctx.items, item => { return { a: item.id, b: item.title }; }), j: _ctx.foo, k: _vOn(_ctx.onClick) }), e: _ctx.ok1 } +}` + ) + }) +}) diff --git a/packages/uni-mp-compiler/__tests__/test.spec.ts b/packages/uni-mp-compiler/__tests__/test.spec.ts index c51865fe03b49bb422dcf1a5c6d6c78bb77e74e2..e4d7daa737a1c0a87c28e034259a3f9b0fdeaffa 100644 --- a/packages/uni-mp-compiler/__tests__/test.spec.ts +++ b/packages/uni-mp-compiler/__tests__/test.spec.ts @@ -7,7 +7,7 @@ function assert( template: string, templateCode: string, renderCode: string, - options: CompilerOptions + options: CompilerOptions = {} ) { const res = compile(template, { filename: 'foo.vue', @@ -31,14 +31,13 @@ function assert( } describe('compiler', () => { - test('should wrap as function if expression is inline statement', () => { + test('scope', () => { assert( - ``, - ``, + ``, + ``, `(_ctx, _cache) => { - return { a: _normalizeClass({ red: _ctx.red }) } -}`, - {} + return { a: _vFor(_ctx.items, item => { return { a: item.id }; }), b: _vFor(_ctx.item1, item1 => { return { a: item1.title }; }) } +}` ) }) }) diff --git a/packages/uni-mp-compiler/__tests__/vFor.spec.ts b/packages/uni-mp-compiler/__tests__/vFor.spec.ts index 0b1a3093a5f513e27df6d7eff1243844bcfa5a0b..a8fb2c7dc09fe25035ed82c4e2c1f2a91a24a2ef 100644 --- a/packages/uni-mp-compiler/__tests__/vFor.spec.ts +++ b/packages/uni-mp-compiler/__tests__/vFor.spec.ts @@ -199,9 +199,9 @@ describe(`compiler: v-for`, () => { test(`v-if + v-for`, () => { assert( ``, - ``, + ``, `(_ctx, _cache) => { - return { b: _ctx.ok, ...(_ctx.ok ? { a: _vFor(_ctx.list, i => { return {}; }) } : {}) } + return { a: _ctx.ok, ...(_ctx.ok ? { b: _vFor(_ctx.list, i => { return {}; }) } : {}) } }` ) }) @@ -209,9 +209,9 @@ describe(`compiler: v-for`, () => { test(`v-if + v-for on