vFor.spec.ts 21.3 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
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(
        `<view v-for="index in 5" />`,
        `<view wx:for="{{a}}" wx:for-item="index"/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
32
  return { a: _vFor(5, (index, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
33 34 35 36 37 38 39 40
}`
      )
    })
    test(`value`, () => {
      assert(
        `<view v-for="(item) in items" />`,
        `<view wx:for="{{a}}" wx:for-item="item"/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
41
  return { a: _vFor(_ctx.items, (item, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
42 43 44 45 46 47 48 49
}`
      )
    })
    test('object de-structured value', () => {
      assert(
        `<view v-for="({ id, value }) in items" />`,
        `<view wx:for="{{a}}" wx:for-item="v0"/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
50
  return { a: _vFor(_ctx.items, ({ id, value }, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
51 52 53 54 55 56 57 58
}`
      )
    })
    test('array de-structured value', () => {
      assert(
        `<view v-for="([ id, value ]) in items" />`,
        `<view wx:for="{{a}}" wx:for-item="v0"/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
59
  return { a: _vFor(_ctx.items, ([id, value], k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
60 61 62 63 64 65
}`
      )
    })
    test(`value and key`, () => {
      assert(
        `<view v-for="(item, key) in items" />`,
fxy060608's avatar
fxy060608 已提交
66
        `<view wx:for="{{a}}" wx:for-item="item"/>`,
fxy060608's avatar
fxy060608 已提交
67
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
68
  return { a: _vFor(_ctx.items, (item, key, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
69 70 71 72 73 74
}`
      )
    })
    test(`value, key and index`, () => {
      assert(
        `<view v-for="(item, key, index) in items" />`,
fxy060608's avatar
fxy060608 已提交
75
        `<view wx:for="{{a}}" wx:for-item="item"/>`,
fxy060608's avatar
fxy060608 已提交
76
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
77
  return { a: _vFor(_ctx.items, (item, key, index) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
78 79 80 81 82 83 84 85
}`
      )
    })
    test(`skipped key`, () => {
      assert(
        `<view v-for="(value,,index) in items" />`,
        `<view wx:for="{{a}}" wx:for-item="value"/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
86
  return { a: _vFor(_ctx.items, (value, k0, index) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
87 88 89 90 91 92 93 94
}`
      )
    })
    test(`skipped value and key`, () => {
      assert(
        `<view v-for="(,,index) in items" />`,
        `<view wx:for="{{a}}" wx:for-item="v0"/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
95
  return { a: _vFor(_ctx.items, (v0, k0, index) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
96 97 98 99 100 101 102 103
}`
      )
    })
    test(`unbracketed value`, () => {
      assert(
        `<view v-for="item in items" />`,
        `<view wx:for="{{a}}" wx:for-item="item"/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
104
  return { a: _vFor(_ctx.items, (item, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
105 106 107 108 109 110
}`
      )
    })
    test(`unbracketed value and key`, () => {
      assert(
        `<view v-for="item, key in items" />`,
fxy060608's avatar
fxy060608 已提交
111
        `<view wx:for="{{a}}" wx:for-item="item"/>`,
fxy060608's avatar
fxy060608 已提交
112
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
113
  return { a: _vFor(_ctx.items, (item, key, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
114 115 116 117 118 119
}`
      )
    })
    test(`unbracketed value, key and index`, () => {
      assert(
        `<view v-for="value, key, index in items" />`,
fxy060608's avatar
fxy060608 已提交
120
        `<view wx:for="{{a}}" wx:for-item="value"/>`,
fxy060608's avatar
fxy060608 已提交
121
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
122
  return { a: _vFor(_ctx.items, (value, key, index) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
123 124 125 126 127 128 129 130
}`
      )
    })
    test(`unbracketed skipped key`, () => {
      assert(
        `<view v-for="value, , index in items" />`,
        `<view wx:for="{{a}}" wx:for-item="value"/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
131
  return { a: _vFor(_ctx.items, (value, k0, index) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
132 133 134 135 136 137 138 139
}`
      )
    })
    test(`unbracketed skipped value and key`, () => {
      assert(
        `<view v-for=", , index in items" />`,
        `<view wx:for="{{a}}" wx:for-item="v0"/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
140
  return { a: _vFor(_ctx.items, (v0, k0, index) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
141 142 143
}`
      )
    })
fxy060608's avatar
fxy060608 已提交
144 145 146 147 148
    test(`template v-for`, () => {
      assert(
        `<template v-for="item in items">hello<view/></template>`,
        `<block wx:for="{{a}}" wx:for-item="item">hello<view/></block>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
149
  return { a: _vFor(_ctx.items, (item, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
150 151 152 153 154 155 156 157
}`
      )
    })
    test(`template v-for w/ <slot/>`, () => {
      assert(
        `<template v-for="item in items"><slot/></template>`,
        `<block wx:for="{{a}}" wx:for-item="item"><slot/></block>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
158
  return { a: _vFor(_ctx.items, (item, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
159 160 161 162 163 164 165 166 167
}`
      )
    })
    // #1907 TODO 待优化
    test(`template v-for key injection with single child`, () => {
      assert(
        `<template v-for="item in items" :key="item.id"><view :id="item.id" /></template>`,
        `<block wx:for="{{a}}" wx:for-item="item" wx:key="b"><view id="{{item.a}}"/></block>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
168
  return { a: _vFor(_ctx.items, (item, k0, i0) => { return { a: item.id, b: item.id }; }) }
fxy060608's avatar
fxy060608 已提交
169 170 171 172 173 174 175 176
}`
      )
    })
    test(`v-for on <slot/>`, () => {
      assert(
        `<slot v-for="item in items"></slot>`,
        `<slot wx:for="{{a}}" wx:for-item="item"></slot>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
177
  return { a: _vFor(_ctx.items, (item, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
178 179 180 181 182 183 184 185
}`
      )
    })
    test(`keyed v-for`, () => {
      assert(
        `<view v-for="(item) in items" :key="item" />`,
        `<view wx:for="{{a}}" wx:for-item="item" wx:key="*this"/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
186
  return { a: _vFor(_ctx.items, (item, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
187 188 189 190 191 192 193 194
}`
      )
    })
    test(`keyed template v-for`, () => {
      assert(
        `<template v-for="item in items" :key="item">hello<view/></template>`,
        `<block wx:for="{{a}}" wx:for-item="item" wx:key="*this">hello<view/></block>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
195
  return { a: _vFor(_ctx.items, (item, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
196 197 198 199 200 201
}`
      )
    })
    test(`v-if + v-for`, () => {
      assert(
        `<view v-if="ok" v-for="i in list"/>`,
fxy060608's avatar
fxy060608 已提交
202
        `<view wx:if="{{a}}" wx:for="{{b}}" wx:for-item="i"/>`,
fxy060608's avatar
fxy060608 已提交
203
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
204
  return _extend({ a: _ctx.ok }, _ctx.ok ? { b: _vFor(_ctx.list, (i, k0, i0) => { return {}; }) } : {})
fxy060608's avatar
fxy060608 已提交
205 206 207 208 209 210 211
}`
      )
    })
    // 1637
    test(`v-if + v-for on <template>`, () => {
      assert(
        `<template v-if="ok" v-for="i in list"/>`,
fxy060608's avatar
fxy060608 已提交
212
        `<block wx:if="{{a}}" wx:for="{{b}}" wx:for-item="i"/>`,
fxy060608's avatar
fxy060608 已提交
213
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
214
  return _extend({ a: _ctx.ok }, _ctx.ok ? { b: _vFor(_ctx.list, (i, k0, i0) => { return {}; }) } : {})
fxy060608's avatar
fxy060608 已提交
215 216 217 218 219 220
}`
      )
    })
    test(`v-for on element with custom directive`, () => {
      // <view v-for="i in list" v-foo/>
    })
fxy060608's avatar
fxy060608 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
  })

  describe('errors', () => {
    test('missing expression', () => {
      const onError = jest.fn()
      parseWithForTransform('<view v-for />', { onError })

      expect(onError).toHaveBeenCalledTimes(1)
      expect(onError).toHaveBeenCalledWith(
        expect.objectContaining({
          code: ErrorCodes.X_V_FOR_NO_EXPRESSION,
        })
      )
    })
    test('empty expression', () => {
      const onError = jest.fn()
      parseWithForTransform('<view v-for="" />', { onError })

      expect(onError).toHaveBeenCalledTimes(1)
      expect(onError).toHaveBeenCalledWith(
        expect.objectContaining({
          code: ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION,
        })
      )
    })

    test('invalid expression', () => {
      const onError = jest.fn()
      parseWithForTransform('<view v-for="items" />', { onError })

      expect(onError).toHaveBeenCalledTimes(1)
      expect(onError).toHaveBeenCalledWith(
        expect.objectContaining({
          code: ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION,
        })
      )
    })

    test('missing source', () => {
      const onError = jest.fn()
      parseWithForTransform('<view v-for="item in" />', { onError })

      expect(onError).toHaveBeenCalledTimes(1)
      expect(onError).toHaveBeenCalledWith(
        expect.objectContaining({
          code: ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION,
        })
      )
    })

    test('missing value', () => {
      const onError = jest.fn()
      parseWithForTransform('<view v-for="in items" />', { onError })

      expect(onError).toHaveBeenCalledTimes(1)
      expect(onError).toHaveBeenCalledWith(
        expect.objectContaining({
          code: ErrorCodes.X_V_FOR_MALFORMED_EXPRESSION,
        })
      )
    })
    test('<template v-for> key placement', () => {
      const onError = jest.fn()
      parseWithForTransform(
        `
      <template v-for="item in items">
        <view :key="item.id"/>
      </template>`,
        { onError }
      )

      expect(onError).toHaveBeenCalledTimes(1)
      expect(onError).toHaveBeenCalledWith(
        expect.objectContaining({
          code: ErrorCodes.X_V_FOR_TEMPLATE_KEY_PLACEMENT,
        })
      )

      // should not warn on nested v-for keys
      parseWithForTransform(
        `
      <template v-for="item in items">
        <view v-for="c in item.children" :key="c.id"/>
      </template>`,
        { onError }
      )
      expect(onError).toHaveBeenCalledTimes(1)
    })
  })

  describe('source location', () => {
    test('value & source', () => {
      const source = '<view v-for="item in items" />'
      const {
        node: { vFor: forNode },
      } = parseWithForTransform(source)

      const itemOffset = source.indexOf('item')
      const value = forNode.value as SimpleExpressionNode
      expect(forNode.valueAlias).toBe('item')
      expect(value.loc.start.offset).toBe(itemOffset)
      expect(value.loc.start.line).toBe(1)
      expect(value.loc.start.column).toBe(itemOffset + 1)
      expect(value.loc.end.line).toBe(1)
      expect(value.loc.end.column).toBe(itemOffset + 1 + `item`.length)

      const itemsOffset = source.indexOf('items')
      expect((forNode.source as SimpleExpressionNode).content).toBe('items')
      expect(forNode.source.loc.start.offset).toBe(itemsOffset)
      expect(forNode.source.loc.start.line).toBe(1)
      expect(forNode.source.loc.start.column).toBe(itemsOffset + 1)
      expect(forNode.source.loc.end.line).toBe(1)
      expect(forNode.source.loc.end.column).toBe(
        itemsOffset + 1 + `items`.length
      )
    })

    test('bracketed value', () => {
      const source = '<view v-for="( item ) in items" />'
      const {
        node: { vFor: forNode },
      } = parseWithForTransform(source)

      const itemOffset = source.indexOf('item')
      const value = forNode.value as SimpleExpressionNode
      expect(value.content).toBe('item')
      expect(value.loc.start.offset).toBe(itemOffset)
      expect(value.loc.start.line).toBe(1)
      expect(value.loc.start.column).toBe(itemOffset + 1)
      expect(value.loc.end.line).toBe(1)
      expect(value.loc.end.column).toBe(itemOffset + 1 + `item`.length)

      const itemsOffset = source.indexOf('items')
      expect((forNode.source as SimpleExpressionNode).content).toBe('items')
      expect(forNode.source.loc.start.offset).toBe(itemsOffset)
      expect(forNode.source.loc.start.line).toBe(1)
      expect(forNode.source.loc.start.column).toBe(itemsOffset + 1)
      expect(forNode.source.loc.end.line).toBe(1)
      expect(forNode.source.loc.end.column).toBe(
        itemsOffset + 1 + `items`.length
      )
    })

    test('de-structured value', () => {
      const source = '<view v-for="(  { id, key }) in items" />'
      const {
        node: { vFor: forNode },
      } = parseWithForTransform(source)

      const value = forNode.value as SimpleExpressionNode
      const valueIndex = source.indexOf('{ id, key }')
      expect(value.content).toBe('{ id, key }')
      expect(value.loc.start.offset).toBe(valueIndex)
      expect(value.loc.start.line).toBe(1)
      expect(value.loc.start.column).toBe(valueIndex + 1)
      expect(value.loc.end.line).toBe(1)
      expect(value.loc.end.column).toBe(valueIndex + 1 + '{ id, key }'.length)

      const itemsOffset = source.indexOf('items')
      expect((forNode.source as SimpleExpressionNode).content).toBe('items')
      expect(forNode.source.loc.start.offset).toBe(itemsOffset)
      expect(forNode.source.loc.start.line).toBe(1)
      expect(forNode.source.loc.start.column).toBe(itemsOffset + 1)
      expect(forNode.source.loc.end.line).toBe(1)
      expect(forNode.source.loc.end.column).toBe(
        itemsOffset + 1 + `items`.length
      )
    })

    test('bracketed value, key, index', () => {
      const source = '<view v-for="( item, key, index ) in items" />'
      const {
        node: { vFor: forNode },
      } = parseWithForTransform(source)

      const itemOffset = source.indexOf('item')
      const value = forNode.value as SimpleExpressionNode
      expect(value.content).toBe('item')
      expect(value.loc.start.offset).toBe(itemOffset)
      expect(value.loc.start.line).toBe(1)
      expect(value.loc.start.column).toBe(itemOffset + 1)
      expect(value.loc.end.line).toBe(1)
      expect(value.loc.end.column).toBe(itemOffset + 1 + `item`.length)

      const keyOffset = source.indexOf('key')
      const key = forNode.key as SimpleExpressionNode
      expect(key.content).toBe('key')
      expect(key.loc.start.offset).toBe(keyOffset)
      expect(key.loc.start.line).toBe(1)
      expect(key.loc.start.column).toBe(keyOffset + 1)
      expect(key.loc.end.line).toBe(1)
      expect(key.loc.end.column).toBe(keyOffset + 1 + `key`.length)

      const indexOffset = source.indexOf('index')
      const index = forNode.index as SimpleExpressionNode
      expect(index.content).toBe('index')
      expect(index.loc.start.offset).toBe(indexOffset)
      expect(index.loc.start.line).toBe(1)
      expect(index.loc.start.column).toBe(indexOffset + 1)
      expect(index.loc.end.line).toBe(1)
      expect(index.loc.end.column).toBe(indexOffset + 1 + `index`.length)

      const itemsOffset = source.indexOf('items')
      expect((forNode.source as SimpleExpressionNode).content).toBe('items')
      expect(forNode.source.loc.start.offset).toBe(itemsOffset)
      expect(forNode.source.loc.start.line).toBe(1)
      expect(forNode.source.loc.start.column).toBe(itemsOffset + 1)
      expect(forNode.source.loc.end.line).toBe(1)
      expect(forNode.source.loc.end.column).toBe(
        itemsOffset + 1 + `items`.length
      )
    })

    test('skipped key', () => {
      const source = '<view v-for="( item,, index ) in items" />'
      const {
        node: { vFor: forNode },
      } = parseWithForTransform(source)

      const itemOffset = source.indexOf('item')
      const value = forNode.value as SimpleExpressionNode
      expect(value.content).toBe('item')
      expect(value.loc.start.offset).toBe(itemOffset)
      expect(value.loc.start.line).toBe(1)
      expect(value.loc.start.column).toBe(itemOffset + 1)
      expect(value.loc.end.line).toBe(1)
      expect(value.loc.end.column).toBe(itemOffset + 1 + `item`.length)

      const indexOffset = source.indexOf('index')
      const index = forNode.index as SimpleExpressionNode
      expect(index.content).toBe('index')
      expect(index.loc.start.offset).toBe(indexOffset)
      expect(index.loc.start.line).toBe(1)
      expect(index.loc.start.column).toBe(indexOffset + 1)
      expect(index.loc.end.line).toBe(1)
      expect(index.loc.end.column).toBe(indexOffset + 1 + `index`.length)

      const itemsOffset = source.indexOf('items')
      expect((forNode.source as SimpleExpressionNode).content).toBe('items')
      expect(forNode.source.loc.start.offset).toBe(itemsOffset)
      expect(forNode.source.loc.start.line).toBe(1)
      expect(forNode.source.loc.start.column).toBe(itemsOffset + 1)
      expect(forNode.source.loc.end.line).toBe(1)
      expect(forNode.source.loc.end.column).toBe(
        itemsOffset + 1 + `items`.length
      )
    })
  })
  describe('prefixIdentifiers: true', () => {
    test('should prefix v-for source', () => {
fxy060608's avatar
fxy060608 已提交
471
      const { node } = parseWithForTransform(`<view v-for="i in list"/>`, {
fxy060608's avatar
fxy060608 已提交
472 473 474 475 476 477 478 479 480 481 482
        prefixIdentifiers: true,
        skipTransformIdentifier: true,
      })
      expect(node.vFor.source).toMatchObject({
        type: NodeTypes.SIMPLE_EXPRESSION,
        content: `_ctx.list`,
      })
    })

    test('should prefix v-for source w/ complex expression', () => {
      const { node } = parseWithForTransform(
fxy060608's avatar
fxy060608 已提交
483
        `<view v-for="i in list.concat([foo])"/>`,
484 485 486 487
        {
          prefixIdentifiers: true,
          skipTransformIdentifier: true,
        }
fxy060608's avatar
fxy060608 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
      )
      expect(node.vFor.source).toMatchObject({
        type: NodeTypes.COMPOUND_EXPRESSION,
        children: [
          { content: `_ctx.list` },
          `.`,
          { content: `concat` },
          `([`,
          { content: `_ctx.foo` },
          `])`,
        ],
      })
    })

    test('should not prefix v-for alias', () => {
      const { node } = parseWithForTransform(
fxy060608's avatar
fxy060608 已提交
504
        `<view v-for="i in list">{{ i }}{{ j }}</view>`,
fxy060608's avatar
fxy060608 已提交
505 506
        { prefixIdentifiers: true, skipTransformIdentifier: true }
      )
fxy060608's avatar
fxy060608 已提交
507 508
      const view = node as ElementNode
      expect((view.children[0] as InterpolationNode).content).toMatchObject({
fxy060608's avatar
fxy060608 已提交
509 510 511
        type: NodeTypes.SIMPLE_EXPRESSION,
        content: `i`,
      })
fxy060608's avatar
fxy060608 已提交
512
      expect((view.children[1] as InterpolationNode).content).toMatchObject({
fxy060608's avatar
fxy060608 已提交
513 514 515 516 517 518 519
        type: NodeTypes.SIMPLE_EXPRESSION,
        content: `_ctx.j`,
      })
    })

    test('should not prefix v-for aliases (multiple)', () => {
      const { node } = parseWithForTransform(
fxy060608's avatar
fxy060608 已提交
520
        `<view v-for="(i, j, k) in list">{{ i + j + k }}{{ l }}</view>`,
fxy060608's avatar
fxy060608 已提交
521 522
        { prefixIdentifiers: true, skipTransformIdentifier: true }
      )
fxy060608's avatar
fxy060608 已提交
523 524
      const view = node as ElementNode
      expect((view.children[0] as InterpolationNode).content).toMatchObject({
fxy060608's avatar
fxy060608 已提交
525 526 527 528 529 530 531 532 533
        type: NodeTypes.COMPOUND_EXPRESSION,
        children: [
          { content: `i` },
          ` + `,
          { content: `j` },
          ` + `,
          { content: `k` },
        ],
      })
fxy060608's avatar
fxy060608 已提交
534
      expect((view.children[1] as InterpolationNode).content).toMatchObject({
fxy060608's avatar
fxy060608 已提交
535 536 537 538 539 540 541
        type: NodeTypes.SIMPLE_EXPRESSION,
        content: `_ctx.l`,
      })
    })

    test('should prefix id outside of v-for', () => {
      const { node } = parseWithForTransform(
fxy060608's avatar
fxy060608 已提交
542
        `<view><view v-for="i in list" />{{ i }}</view>`,
fxy060608's avatar
fxy060608 已提交
543 544 545 546 547 548 549 550 551 552
        { prefixIdentifiers: true, skipTransformIdentifier: true }
      )
      expect((node.children[1] as InterpolationNode).content).toMatchObject({
        type: NodeTypes.SIMPLE_EXPRESSION,
        content: `_ctx.i`,
      })
    })

    test('nested v-for', () => {
      const { node } = parseWithForTransform(
fxy060608's avatar
fxy060608 已提交
553 554 555
        `<view v-for="i in list">
          <view v-for="i in list">{{ i + j }}</view>{{ i }}
        </view>`,
fxy060608's avatar
fxy060608 已提交
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
        { prefixIdentifiers: true, skipTransformIdentifier: true }
      )
      const outerDiv = node as ElementNode
      const innerExp = (outerDiv.children[0] as ElementNode)
        .children[0] as InterpolationNode
      expect(innerExp.content).toMatchObject({
        type: NodeTypes.COMPOUND_EXPRESSION,
        children: [{ content: 'i' }, ` + `, { content: `_ctx.j` }],
      })

      // when an inner v-for shadows a variable of an outer v-for and exit,
      // it should not cause the outer v-for's alias to be removed from known ids
      const outerExp = outerDiv.children[1] as InterpolationNode
      expect(outerExp.content).toMatchObject({
        type: NodeTypes.SIMPLE_EXPRESSION,
        content: `i`,
      })
    })

    test('v-for aliases w/ complex expressions', () => {
      const { node } = parseWithForTransform(
fxy060608's avatar
fxy060608 已提交
577
        `<view v-for="({ foo = bar, baz: [qux = quux] }) in list">
fxy060608's avatar
fxy060608 已提交
578
          {{ foo + bar + baz + qux + quux }}
fxy060608's avatar
fxy060608 已提交
579
        </view>`,
fxy060608's avatar
fxy060608 已提交
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
        { prefixIdentifiers: true, skipTransformIdentifier: true }
      )
      expect(node.vFor.value).toMatchObject({
        type: NodeTypes.COMPOUND_EXPRESSION,
        children: [
          `{ `,
          { content: `foo` },
          ` = `,
          { content: `_ctx.bar` },
          `, baz: [`,
          { content: `qux` },
          ` = `,
          { content: `_ctx.quux` },
          `] }`,
        ],
      })
fxy060608's avatar
fxy060608 已提交
596 597
      const view = node as ElementNode
      expect((view.children[0] as InterpolationNode).content).toMatchObject({
fxy060608's avatar
fxy060608 已提交
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
        type: NodeTypes.COMPOUND_EXPRESSION,
        children: [
          { content: `foo` },
          ` + `,
          { content: `_ctx.bar` },
          ` + `,
          { content: `_ctx.baz` },
          ` + `,
          { content: `qux` },
          ` + `,
          { content: `_ctx.quux` },
        ],
      })
    })

    test('element v-for key expression prefixing', () => {
      assert(
fxy060608's avatar
fxy060608 已提交
615 616
        `<view v-for="item in items" :key="itemKey(item)">test</view>`,
        `<view wx:for="{{a}}" wx:for-item="item" wx:key="a">test</view>`,
fxy060608's avatar
fxy060608 已提交
617
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
618
  return { a: _vFor(_ctx.items, (item, k0, i0) => { return { a: _ctx.itemKey(item) }; }) }
fxy060608's avatar
fxy060608 已提交
619 620 621 622 623 624 625 626 627 628
}`
      )
    })

    // #2085
    test('template v-for key expression prefixing', () => {
      assert(
        `<template v-for="item in items" :key="itemKey(item)">test</template>`,
        `<block wx:for="{{a}}" wx:for-item="item" wx:key="a">test</block>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
629
  return { a: _vFor(_ctx.items, (item, k0, i0) => { return { a: _ctx.itemKey(item) }; }) }
fxy060608's avatar
fxy060608 已提交
630 631 632 633 634 635 636 637 638
}`
      )
    })

    test('template v-for key no prefixing on attribute key', () => {
      assert(
        `<template v-for="item in items" key="key">test</template>`,
        `<block wx:for="{{a}}" wx:for-item="item" key="key">test</block>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
639
  return { a: _vFor(_ctx.items, (item, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
640 641 642 643 644
}`
      )
    })
  })
})