vFor.spec.ts 21.5 KB
Newer Older
fxy060608's avatar
fxy060608 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
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 = {}
) {
fxy060608's avatar
fxy060608 已提交
17 18 19 20 21 22
  const { ast } = compile(template, {
    generatorOpts: {
      concise: true,
    },
    ...options,
  })
fxy060608's avatar
fxy060608 已提交
23 24 25 26 27 28 29 30 31 32 33 34

  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" />`,
35 36 37 38 39 40 41 42
        `<view wx:for="{{a}}" wx:for-item="index" wx:for-index="i0"/>`,
        `(_ctx, _cache) => {
  return { a: _f(5, (index, k0, i0) => { return {}; }) }
}`
      )
      assert(
        `<view v-for="index of 5" />`,
        `<view wx:for="{{a}}" wx:for-item="index" wx:for-index="i0"/>`,
fxy060608's avatar
fxy060608 已提交
43
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
44
  return { a: _f(5, (index, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
45 46 47 48 49 50 51 52
}`
      )
    })
    test(`value`, () => {
      assert(
        `<view v-for="(item) in items" />`,
        `<view wx:for="{{a}}" wx:for-item="item"/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
53
  return { a: _f(_ctx.items, (item, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
54 55 56 57 58 59 60 61
}`
      )
    })
    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 已提交
62
  return { a: _f(_ctx.items, ({ id, value }, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
63 64 65 66 67 68 69 70
}`
      )
    })
    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 已提交
71
  return { a: _f(_ctx.items, ([id, value], k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
72 73 74 75 76 77
}`
      )
    })
    test(`value and key`, () => {
      assert(
        `<view v-for="(item, key) in items" />`,
fxy060608's avatar
fxy060608 已提交
78
        `<view wx:for="{{a}}" wx:for-item="item"/>`,
fxy060608's avatar
fxy060608 已提交
79
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
80
  return { a: _f(_ctx.items, (item, key, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
81 82 83 84 85 86
}`
      )
    })
    test(`value, key and index`, () => {
      assert(
        `<view v-for="(item, key, index) in items" />`,
fxy060608's avatar
fxy060608 已提交
87
        `<view wx:for="{{a}}" wx:for-item="item"/>`,
fxy060608's avatar
fxy060608 已提交
88
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
89
  return { a: _f(_ctx.items, (item, key, index) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
90 91 92 93 94 95 96 97
}`
      )
    })
    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 已提交
98
  return { a: _f(_ctx.items, (value, k0, index) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
99 100 101 102 103 104 105 106
}`
      )
    })
    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 已提交
107
  return { a: _f(_ctx.items, (v0, k0, index) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
108 109 110 111 112 113 114 115
}`
      )
    })
    test(`unbracketed value`, () => {
      assert(
        `<view v-for="item in items" />`,
        `<view wx:for="{{a}}" wx:for-item="item"/>`,
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
116
  return { a: _f(_ctx.items, (item, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
117 118 119 120 121 122
}`
      )
    })
    test(`unbracketed value and key`, () => {
      assert(
        `<view v-for="item, key in items" />`,
fxy060608's avatar
fxy060608 已提交
123
        `<view wx:for="{{a}}" wx:for-item="item"/>`,
fxy060608's avatar
fxy060608 已提交
124
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
125
  return { a: _f(_ctx.items, (item, key, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
126 127 128 129 130 131
}`
      )
    })
    test(`unbracketed value, key and index`, () => {
      assert(
        `<view v-for="value, key, index in items" />`,
fxy060608's avatar
fxy060608 已提交
132
        `<view wx:for="{{a}}" wx:for-item="value"/>`,
fxy060608's avatar
fxy060608 已提交
133
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
134
  return { a: _f(_ctx.items, (value, key, index) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
135 136 137 138 139 140 141 142
}`
      )
    })
    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 已提交
143
  return { a: _f(_ctx.items, (value, k0, index) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
144 145 146 147 148 149 150 151
}`
      )
    })
    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 已提交
152
  return { a: _f(_ctx.items, (v0, k0, index) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
153 154 155
}`
      )
    })
fxy060608's avatar
fxy060608 已提交
156 157 158 159 160
    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 已提交
161
  return { a: _f(_ctx.items, (item, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
162 163 164 165 166 167
}`
      )
    })
    test(`template v-for w/ <slot/>`, () => {
      assert(
        `<template v-for="item in items"><slot/></template>`,
fxy060608's avatar
fxy060608 已提交
168
        `<block wx:for="{{a}}" wx:for-item="item"><slot/></block>`,
fxy060608's avatar
fxy060608 已提交
169
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
170
  return { a: _f(_ctx.items, (item, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
171 172 173 174 175 176 177 178 179
}`
      )
    })
    // #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 已提交
180
  return { a: _f(_ctx.items, (item, k0, i0) => { return { a: item.id, b: item.id }; }) }
fxy060608's avatar
fxy060608 已提交
181 182 183 184 185 186
}`
      )
    })
    test(`v-for on <slot/>`, () => {
      assert(
        `<slot v-for="item in items"></slot>`,
fxy060608's avatar
fxy060608 已提交
187
        `<slot wx:for="{{a}}" wx:for-item="item"></slot>`,
fxy060608's avatar
fxy060608 已提交
188
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
189
  return { a: _f(_ctx.items, (item, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
190 191 192 193 194 195
}`
      )
    })
    test(`keyed v-for`, () => {
      assert(
        `<view v-for="(item) in items" :key="item" />`,
196
        `<view wx:for="{{a}}" wx:for-item="item" wx:key="a"/>`,
fxy060608's avatar
fxy060608 已提交
197
        `(_ctx, _cache) => {
198
  return { a: _f(_ctx.items, (item, k0, i0) => { return { a: item }; }) }
fxy060608's avatar
fxy060608 已提交
199 200 201 202 203 204
}`
      )
    })
    test(`keyed template v-for`, () => {
      assert(
        `<template v-for="item in items" :key="item">hello<view/></template>`,
205
        `<block wx:for="{{a}}" wx:for-item="item" wx:key="a">hello<view/></block>`,
fxy060608's avatar
fxy060608 已提交
206
        `(_ctx, _cache) => {
207
  return { a: _f(_ctx.items, (item, k0, i0) => { return { a: item }; }) }
fxy060608's avatar
fxy060608 已提交
208 209 210 211 212 213
}`
      )
    })
    test(`v-if + v-for`, () => {
      assert(
        `<view v-if="ok" v-for="i in list"/>`,
fxy060608's avatar
fxy060608 已提交
214
        `<block wx:if="{{a}}"><view wx:for="{{b}}" wx:for-item="i"/></block>`,
fxy060608's avatar
fxy060608 已提交
215
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
216
  return _e({ a: _ctx.ok }, _ctx.ok ? { b: _f(_ctx.list, (i, k0, i0) => { return {}; }) } : {})
fxy060608's avatar
fxy060608 已提交
217 218 219 220 221 222 223
}`
      )
    })
    // 1637
    test(`v-if + v-for on <template>`, () => {
      assert(
        `<template v-if="ok" v-for="i in list"/>`,
fxy060608's avatar
fxy060608 已提交
224
        `<block wx:if="{{a}}"><block wx:for="{{b}}" wx:for-item="i"/></block>`,
fxy060608's avatar
fxy060608 已提交
225
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
226
  return _e({ a: _ctx.ok }, _ctx.ok ? { b: _f(_ctx.list, (i, k0, i0) => { return {}; }) } : {})
fxy060608's avatar
fxy060608 已提交
227 228 229 230 231 232
}`
      )
    })
    test(`v-for on element with custom directive`, () => {
      // <view v-for="i in list" v-foo/>
    })
fxy060608's avatar
fxy060608 已提交
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 471 472 473 474 475 476 477 478 479 480 481 482
  })

  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 已提交
483
      const { node } = parseWithForTransform(`<view v-for="i in list"/>`, {
fxy060608's avatar
fxy060608 已提交
484 485 486 487 488 489 490 491 492 493 494
        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 已提交
495
        `<view v-for="i in list.concat([foo])"/>`,
496 497 498 499
        {
          prefixIdentifiers: true,
          skipTransformIdentifier: true,
        }
fxy060608's avatar
fxy060608 已提交
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515
      )
      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 已提交
516
        `<view v-for="i in list">{{ i }}{{ j }}</view>`,
fxy060608's avatar
fxy060608 已提交
517 518
        { prefixIdentifiers: true, skipTransformIdentifier: true }
      )
fxy060608's avatar
fxy060608 已提交
519 520
      const view = node as ElementNode
      expect((view.children[0] as InterpolationNode).content).toMatchObject({
fxy060608's avatar
fxy060608 已提交
521 522 523
        type: NodeTypes.SIMPLE_EXPRESSION,
        content: `i`,
      })
fxy060608's avatar
fxy060608 已提交
524
      expect((view.children[1] as InterpolationNode).content).toMatchObject({
fxy060608's avatar
fxy060608 已提交
525 526 527 528 529 530 531
        type: NodeTypes.SIMPLE_EXPRESSION,
        content: `_ctx.j`,
      })
    })

    test('should not prefix v-for aliases (multiple)', () => {
      const { node } = parseWithForTransform(
fxy060608's avatar
fxy060608 已提交
532
        `<view v-for="(i, j, k) in list">{{ i + j + k }}{{ l }}</view>`,
fxy060608's avatar
fxy060608 已提交
533 534
        { prefixIdentifiers: true, skipTransformIdentifier: true }
      )
fxy060608's avatar
fxy060608 已提交
535 536
      const view = node as ElementNode
      expect((view.children[0] as InterpolationNode).content).toMatchObject({
fxy060608's avatar
fxy060608 已提交
537 538 539 540 541 542 543 544 545
        type: NodeTypes.COMPOUND_EXPRESSION,
        children: [
          { content: `i` },
          ` + `,
          { content: `j` },
          ` + `,
          { content: `k` },
        ],
      })
fxy060608's avatar
fxy060608 已提交
546
      expect((view.children[1] as InterpolationNode).content).toMatchObject({
fxy060608's avatar
fxy060608 已提交
547 548 549 550 551 552 553
        type: NodeTypes.SIMPLE_EXPRESSION,
        content: `_ctx.l`,
      })
    })

    test('should prefix id outside of v-for', () => {
      const { node } = parseWithForTransform(
fxy060608's avatar
fxy060608 已提交
554
        `<view><view v-for="i in list" />{{ i }}</view>`,
fxy060608's avatar
fxy060608 已提交
555 556 557 558 559 560 561 562 563 564
        { 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 已提交
565 566 567
        `<view v-for="i in list">
          <view v-for="i in list">{{ i + j }}</view>{{ i }}
        </view>`,
fxy060608's avatar
fxy060608 已提交
568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
        { 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 已提交
589
        `<view v-for="({ foo = bar, baz: [qux = quux] }) in list">
fxy060608's avatar
fxy060608 已提交
590
          {{ foo + bar + baz + qux + quux }}
fxy060608's avatar
fxy060608 已提交
591
        </view>`,
fxy060608's avatar
fxy060608 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
        { 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 已提交
608 609
      const view = node as ElementNode
      expect((view.children[0] as InterpolationNode).content).toMatchObject({
fxy060608's avatar
fxy060608 已提交
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
        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 已提交
627 628
        `<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 已提交
629
        `(_ctx, _cache) => {
fxy060608's avatar
fxy060608 已提交
630
  return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _ctx.itemKey(item) }; }) }
fxy060608's avatar
fxy060608 已提交
631 632 633 634 635 636 637 638 639 640
}`
      )
    })

    // #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 已提交
641
  return { a: _f(_ctx.items, (item, k0, i0) => { return { a: _ctx.itemKey(item) }; }) }
fxy060608's avatar
fxy060608 已提交
642 643 644 645 646 647 648 649 650
}`
      )
    })

    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 已提交
651
  return { a: _f(_ctx.items, (item, k0, i0) => { return {}; }) }
fxy060608's avatar
fxy060608 已提交
652 653 654 655 656
}`
      )
    })
  })
})