@@ -121,41 +121,73 @@ function createInvalidTests(prefix, options, message, type) {
121
121
return [
122
122
{
123
123
code : `<template><div>{{ ${ prefix } foo }}</div></template><!-- ${ comment } -->` ,
124
+ output : `<template><div>{{ ${ suggestionPrefix (
125
+ prefix ,
126
+ options
127
+ ) } foo }}</div></template><!-- ${ comment } -->`,
124
128
errors : [ { message, type } ] ,
125
129
options
126
130
} ,
127
131
{
128
132
code : `<template><div>{{ ${ prefix } foo() }}</div></template><!-- ${ comment } -->` ,
133
+ output : `<template><div>{{ ${ suggestionPrefix (
134
+ prefix ,
135
+ options
136
+ ) } foo() }}</div></template><!-- ${ comment } -->`,
129
137
errors : [ { message, type } ] ,
130
138
options
131
139
} ,
132
140
{
133
141
code : `<template><div>{{ ${ prefix } foo.bar() }}</div></template><!-- ${ comment } -->` ,
142
+ output : `<template><div>{{ ${ suggestionPrefix (
143
+ prefix ,
144
+ options
145
+ ) } foo.bar() }}</div></template><!-- ${ comment } -->`,
134
146
errors : [ { message, type } ] ,
135
147
options
136
148
} ,
137
149
{
138
150
code : `<template><div :class="${ prefix } foo"></div></template><!-- ${ comment } -->` ,
151
+ output : `<template><div :class="${ suggestionPrefix (
152
+ prefix ,
153
+ options
154
+ ) } foo"></div></template><!-- ${ comment } -->`,
139
155
errors : [ { message, type } ] ,
140
156
options
141
157
} ,
142
158
{
143
159
code : `<template><div :class="{foo: ${ prefix } foo}"></div></template><!-- ${ comment } -->` ,
160
+ output : `<template><div :class="{foo: ${ suggestionPrefix (
161
+ prefix ,
162
+ options
163
+ ) } foo}"></div></template><!-- ${ comment } -->`,
144
164
errors : [ { message, type } ] ,
145
165
options
146
166
} ,
147
167
{
148
168
code : `<template><div :class="{foo: ${ prefix } foo()}"></div></template><!-- ${ comment } -->` ,
169
+ output : `<template><div :class="{foo: ${ suggestionPrefix (
170
+ prefix ,
171
+ options
172
+ ) } foo()}"></div></template><!-- ${ comment } -->`,
149
173
errors : [ { message, type } ] ,
150
174
options
151
175
} ,
152
176
{
153
177
code : `<template><div v-if="${ prefix } foo"></div></template><!-- ${ comment } -->` ,
178
+ output : `<template><div v-if="${ suggestionPrefix (
179
+ prefix ,
180
+ options
181
+ ) } foo"></div></template><!-- ${ comment } -->`,
154
182
errors : [ { message, type } ] ,
155
183
options
156
184
} ,
157
185
{
158
186
code : `<template><div v-for="foo in ${ prefix } bar"></div></template><!-- ${ comment } -->` ,
187
+ output : `<template><div v-for="foo in ${ suggestionPrefix (
188
+ prefix ,
189
+ options
190
+ ) } bar"></div></template><!-- ${ comment } -->`,
159
191
errors : [ { message, type } ] ,
160
192
options
161
193
}
@@ -172,11 +204,13 @@ function createInvalidTests(prefix, options, message, type) {
172
204
: [
173
205
{
174
206
code : `<template><div>{{ this['xs'] }}</div></template><!-- ${ comment } -->` ,
207
+ output : `<template><div>{{ xs }}</div></template><!-- ${ comment } -->` ,
175
208
errors : [ { message, type } ] ,
176
209
options
177
210
} ,
178
211
{
179
212
code : `<template><div>{{ this['xs0AZ_foo'] }}</div></template><!-- ${ comment } -->` ,
213
+ output : `<template><div>{{ xs0AZ_foo }}</div></template><!-- ${ comment } -->` ,
180
214
errors : [ { message, type } ] ,
181
215
options
182
216
}
@@ -225,13 +259,23 @@ ruleTester.run('this-in-template', rule, {
225
259
. concat ( [
226
260
{
227
261
code : `<template><div v-if="fn(this.$foo)"></div></template><!-- never -->` ,
262
+ output : `<template><div v-if="fn($foo)"></div></template><!-- never -->` ,
228
263
errors : [ "Unexpected usage of 'this'." ] ,
229
264
options : [ 'never' ]
230
265
} ,
231
266
{
232
267
code : `<template><div :class="{ foo: this.$foo }"></div></template><!-- never -->` ,
268
+ output : `<template><div :class="{ foo: $foo }"></div></template><!-- never -->` ,
233
269
errors : [ "Unexpected usage of 'this'." ] ,
234
270
options : [ 'never' ]
235
271
}
236
272
] )
237
273
} )
274
+
275
+ function suggestionPrefix ( prefix , options ) {
276
+ if ( options [ 0 ] === 'always' && ! [ 'this.' , 'this?.' ] . includes ( prefix ) ) {
277
+ return 'this.'
278
+ } else {
279
+ return ''
280
+ }
281
+ }
0 commit comments