import { ElementNode, ErrorCodes, InterpolationNode, NodeTypes, SimpleExpressionNode, } from '@vue/compiler-core' import { compile } from '../src' import { CompilerOptions } from '../src/options' import { ForElementNode } from '../src/transforms/vFor' import { assert } from './testUtils' function parseWithForTransform( template: string, options: CompilerOptions = {} ) { const { ast } = compile(template, options) return { root: ast, node: ast.children[0] as ForElementNode, } } describe(`compiler: v-for`, () => { describe(`codegen`, () => { test(`number expression`, () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(5, (index, k0, i0) => { return {}; }) } }` ) }) test(`value`, () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (item, k0, i0) => { return {}; }) } }` ) }) test('object de-structured value', () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, ({ id, value }, k0, i0) => { return {}; }) } }` ) }) test('array de-structured value', () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, ([id, value], k0, i0) => { return {}; }) } }` ) }) test(`value and key`, () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (item, key, i0) => { return {}; }) } }` ) }) test(`value, key and index`, () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (item, key, index) => { return {}; }) } }` ) }) test(`skipped key`, () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (value, k0, index) => { return {}; }) } }` ) }) test(`skipped value and key`, () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (v0, k0, index) => { return {}; }) } }` ) }) test(`unbracketed value`, () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (item, k0, i0) => { return {}; }) } }` ) }) test(`unbracketed value and key`, () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (item, key, i0) => { return {}; }) } }` ) }) test(`unbracketed value, key and index`, () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (value, key, index) => { return {}; }) } }` ) }) test(`unbracketed skipped key`, () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (value, k0, index) => { return {}; }) } }` ) }) test(`unbracketed skipped value and key`, () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (v0, k0, index) => { return {}; }) } }` ) }) test(`template v-for`, () => { assert( ``, `hello`, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (item, k0, i0) => { return {}; }) } }` ) }) test(`template v-for w/ `, () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (item, k0, i0) => { return {}; }) } }` ) }) // #1907 TODO 待优化 test(`template v-for key injection with single child`, () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (item, k0, i0) => { return { a: item.id, b: item.id }; }) } }` ) }) test(`v-for on `, () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (item, k0, i0) => { return {}; }) } }` ) }) test(`keyed v-for`, () => { assert( ``, ``, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (item, k0, i0) => { return {}; }) } }` ) }) test(`keyed template v-for`, () => { assert( ``, `hello`, `(_ctx, _cache) => { return { a: _vFor(_ctx.items, (item, k0, i0) => { return {}; }) } }` ) }) test(`v-if + v-for`, () => { assert( ``, ``, `(_ctx, _cache) => { return _extend({ a: _ctx.ok }, _ctx.ok ? { b: _vFor(_ctx.list, (i, k0, i0) => { return {}; }) } : {}) }` ) }) // 1637 test(`v-if + v-for on