|
| 1 | +/** |
| 2 | + * @author Yosuke Ota |
| 3 | + * See LICENSE file in root directory for full license. |
| 4 | + */ |
| 5 | +'use strict' |
| 6 | + |
| 7 | +const RuleTester = require('eslint').RuleTester |
| 8 | +const rule = require('../../../lib/rules/no-useless-template-attributes') |
| 9 | + |
| 10 | +const tester = new RuleTester({ |
| 11 | + parser: require.resolve('vue-eslint-parser'), |
| 12 | + parserOptions: { |
| 13 | + ecmaVersion: 2020, |
| 14 | + sourceType: 'module' |
| 15 | + } |
| 16 | +}) |
| 17 | + |
| 18 | +tester.run('no-useless-template-attributes', rule, { |
| 19 | + valid: [ |
| 20 | + { |
| 21 | + filename: 'test.vue', |
| 22 | + code: ` |
| 23 | + <template> |
| 24 | + <template v-if="foo">...</template> |
| 25 | + <template v-else-if="bar">...</template> |
| 26 | + <template v-else>...</template> |
| 27 | + </template> |
| 28 | + ` |
| 29 | + }, |
| 30 | + { |
| 31 | + filename: 'test.vue', |
| 32 | + code: ` |
| 33 | + <template> |
| 34 | + <template v-for="e in list">...</template> |
| 35 | + </template> |
| 36 | + ` |
| 37 | + }, |
| 38 | + { |
| 39 | + filename: 'test.vue', |
| 40 | + code: ` |
| 41 | + <template> |
| 42 | + <template v-slot>...</template> |
| 43 | + </template> |
| 44 | + ` |
| 45 | + }, |
| 46 | + { |
| 47 | + filename: 'test.vue', |
| 48 | + code: ` |
| 49 | + <template> |
| 50 | + <CoolButton> |
| 51 | + <template slot="foo">...</template> |
| 52 | + </CoolButton> |
| 53 | + </template> |
| 54 | + ` |
| 55 | + }, |
| 56 | + { |
| 57 | + filename: 'test.vue', |
| 58 | + code: ` |
| 59 | + <template> |
| 60 | + <CoolButton> |
| 61 | + <template slot-scope="foo">...</template> |
| 62 | + </CoolButton> |
| 63 | + </template> |
| 64 | + ` |
| 65 | + }, |
| 66 | + { |
| 67 | + filename: 'test.vue', |
| 68 | + code: ` |
| 69 | + <template> |
| 70 | + <CoolButton> |
| 71 | + <template scope="foo">...</template> |
| 72 | + </CoolButton> |
| 73 | + </template> |
| 74 | + ` |
| 75 | + }, |
| 76 | + { |
| 77 | + filename: 'test.vue', |
| 78 | + code: ` |
| 79 | + <template> |
| 80 | + <!-- ignore --> |
| 81 | + <template foo="a">...</template> |
| 82 | + <template :foo="a">...</template> |
| 83 | + <template v-unknown="a">...</template> |
| 84 | + </template> |
| 85 | + ` |
| 86 | + }, |
| 87 | + // not template |
| 88 | + { |
| 89 | + filename: 'test.vue', |
| 90 | + code: ` |
| 91 | + <template> |
| 92 | + <div v-if="foo" class="heading">...</div> |
| 93 | + <div v-for="i in foo" :bar="i">...</div> |
| 94 | + <div v-slot:foo="foo" ref="input">...</div> |
| 95 | + <div v-if="foo" @click="click">...</div> |
| 96 | + </template> |
| 97 | + ` |
| 98 | + } |
| 99 | + ], |
| 100 | + invalid: [ |
| 101 | + { |
| 102 | + filename: 'test.vue', |
| 103 | + code: ` |
| 104 | + <template> |
| 105 | + <!-- ✓ GOOD --> |
| 106 | + <template v-if="foo">...</template> |
| 107 | + <template v-if="foo">...</template> |
| 108 | + <template v-else-if="foo">...</template> |
| 109 | + <template v-else>...</template> |
| 110 | + <template v-for="i in foo" :key="i">...</template> |
| 111 | + <template v-slot:foo>...</template> |
| 112 | + <!-- for Vue<=2.5 --> |
| 113 | + <template slot="foo">...</template> |
| 114 | + <template :slot="foo">...</template> |
| 115 | + <template slot-scope="param">...</template> |
| 116 | + <!-- for Vue<=2.4 --> |
| 117 | + <template scope="param">...</template> |
| 118 | +
|
| 119 | + <!-- ✗ BAD --> |
| 120 | + <template v-if="foo" class="heading">...</template> |
| 121 | + <template v-for="i in foo" :bar="i">...</template> |
| 122 | + <template v-slot:foo="foo" ref="input">...</template> |
| 123 | + <template v-if="foo" @click="click">...</template> |
| 124 | +
|
| 125 | + <!-- Ignore --> |
| 126 | + <template class="heading">...</template> |
| 127 | + <template :bar="i">...</template> |
| 128 | + <template ref="input">...</template> |
| 129 | + <template @click="click">...</template> |
| 130 | + </template> |
| 131 | + `, |
| 132 | + errors: [ |
| 133 | + { |
| 134 | + message: 'Unexpected useless attribute on `<template>`.', |
| 135 | + line: 18, |
| 136 | + column: 30 |
| 137 | + }, |
| 138 | + { |
| 139 | + message: 'Unexpected useless directive on `<template>`.', |
| 140 | + line: 19, |
| 141 | + column: 36 |
| 142 | + }, |
| 143 | + { |
| 144 | + message: 'Unexpected useless attribute on `<template>`.', |
| 145 | + line: 20, |
| 146 | + column: 36 |
| 147 | + }, |
| 148 | + { |
| 149 | + message: 'Unexpected useless directive on `<template>`.', |
| 150 | + line: 21, |
| 151 | + column: 30 |
| 152 | + } |
| 153 | + ] |
| 154 | + } |
| 155 | + ] |
| 156 | +}) |
0 commit comments