|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + */ |
| 4 | +'use strict' |
| 5 | + |
| 6 | +// ------------------------------------------------------------------------------ |
| 7 | +// Requirements |
| 8 | +// ------------------------------------------------------------------------------ |
| 9 | + |
| 10 | +const RuleTester = require('eslint').RuleTester |
| 11 | +const rule = require('../../../lib/rules/no-v-for-template-key') |
| 12 | + |
| 13 | +// ------------------------------------------------------------------------------ |
| 14 | +// Tests |
| 15 | +// ------------------------------------------------------------------------------ |
| 16 | + |
| 17 | +const tester = new RuleTester({ |
| 18 | + parser: require.resolve('vue-eslint-parser'), |
| 19 | + parserOptions: { ecmaVersion: 2015 } |
| 20 | +}) |
| 21 | + |
| 22 | +tester.run('no-v-for-template-key', rule, { |
| 23 | + valid: [ |
| 24 | + { |
| 25 | + filename: 'test.vue', |
| 26 | + code: '' |
| 27 | + }, |
| 28 | + { |
| 29 | + filename: 'test.vue', |
| 30 | + code: '<template><template></template></template>' |
| 31 | + }, |
| 32 | + { |
| 33 | + filename: 'test.vue', |
| 34 | + code: '<template><div key="foo"></div></template>' |
| 35 | + }, |
| 36 | + { |
| 37 | + filename: 'test.vue', |
| 38 | + code: '<template><div v-bind:key="foo"></div></template>' |
| 39 | + }, |
| 40 | + { |
| 41 | + filename: 'test.vue', |
| 42 | + code: '<template><div :key="foo"></div></template>' |
| 43 | + }, |
| 44 | + { |
| 45 | + filename: 'test.vue', |
| 46 | + code: '<template><div><template key="foo"></template></div></template>' |
| 47 | + }, |
| 48 | + { |
| 49 | + filename: 'test.vue', |
| 50 | + code: |
| 51 | + '<template><div><template v-bind:key="foo"></template></div></template>' |
| 52 | + }, |
| 53 | + { |
| 54 | + filename: 'test.vue', |
| 55 | + code: '<template><div><template :key="foo"></template></div></template>' |
| 56 | + }, |
| 57 | + { |
| 58 | + filename: 'test.vue', |
| 59 | + code: |
| 60 | + '<template><template v-slot="item" :key="item.id"><div /></template></template>' |
| 61 | + }, |
| 62 | + { |
| 63 | + filename: 'test.vue', |
| 64 | + code: |
| 65 | + '<template><template v-for="item in list"><template :key="item.id"><div /></template></template></template>' |
| 66 | + } |
| 67 | + ], |
| 68 | + invalid: [ |
| 69 | + { |
| 70 | + filename: 'test.vue', |
| 71 | + code: |
| 72 | + '<template><template v-for="item in list" :key="item.id"><div /></template></template>', |
| 73 | + errors: [ |
| 74 | + "'<template v-for>' cannot be keyed. Place the key on real elements instead." |
| 75 | + ] |
| 76 | + }, |
| 77 | + { |
| 78 | + filename: 'test.vue', |
| 79 | + code: |
| 80 | + '<template><template v-for="(item, i) in list" :key="i"><div /></template></template>', |
| 81 | + errors: [ |
| 82 | + "'<template v-for>' cannot be keyed. Place the key on real elements instead." |
| 83 | + ] |
| 84 | + }, |
| 85 | + { |
| 86 | + filename: 'test.vue', |
| 87 | + code: |
| 88 | + '<template><template v-for="item in list" :key="foo + item.id"><div /></template></template>', |
| 89 | + errors: [ |
| 90 | + "'<template v-for>' cannot be keyed. Place the key on real elements instead." |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + filename: 'test.vue', |
| 95 | + code: |
| 96 | + '<template><template v-for="item in list" key="foo"><div /></template></template>', |
| 97 | + errors: [ |
| 98 | + "'<template v-for>' cannot be keyed. Place the key on real elements instead." |
| 99 | + ] |
| 100 | + } |
| 101 | + ] |
| 102 | +}) |
0 commit comments