Skip to content

Commit 856643b

Browse files
feat: 🎸 vuejs#1492
1 parent c775584 commit 856643b

File tree

2 files changed

+53
-2
lines changed

2 files changed

+53
-2
lines changed

‎lib/rules/this-in-template.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ module.exports = {
2323
categories: ['vue3-recommended', 'recommended'],
2424
url: 'https://eslint.vuejs.org/rules/this-in-template.html'
2525
},
26-
fixable: null,
26+
fixable: 'code',
2727
schema: [
2828
{
2929
enum: ['always', 'never']
@@ -91,6 +91,10 @@ module.exports = {
9191
context.report({
9292
node,
9393
loc: node.loc,
94+
fix(fixer) {
95+
// node.parent should be some code like `this.test`, `this?.['result']`
96+
return fixer.replaceText(node.parent, propertyName)
97+
},
9498
message: "Unexpected usage of 'this'."
9599
})
96100
}
@@ -116,7 +120,10 @@ module.exports = {
116120
context.report({
117121
node: reference.id,
118122
loc: reference.id.loc,
119-
message: "Expected 'this'."
123+
message: "Expected 'this'.",
124+
fix(fixer) {
125+
return fixer.insertTextBefore(reference.id, 'this.')
126+
}
120127
})
121128
}
122129
}

‎tests/lib/rules/this-in-template.js

+44
Original file line numberDiff line numberDiff line change
@@ -121,41 +121,73 @@ function createInvalidTests(prefix, options, message, type) {
121121
return [
122122
{
123123
code: `<template><div>{{ ${prefix}foo }}</div></template><!-- ${comment} -->`,
124+
output: `<template><div>{{ ${suggestionPrefix(
125+
prefix,
126+
options
127+
)}foo }}</div></template><!-- ${comment} -->`,
124128
errors: [{ message, type }],
125129
options
126130
},
127131
{
128132
code: `<template><div>{{ ${prefix}foo() }}</div></template><!-- ${comment} -->`,
133+
output: `<template><div>{{ ${suggestionPrefix(
134+
prefix,
135+
options
136+
)}foo() }}</div></template><!-- ${comment} -->`,
129137
errors: [{ message, type }],
130138
options
131139
},
132140
{
133141
code: `<template><div>{{ ${prefix}foo.bar() }}</div></template><!-- ${comment} -->`,
142+
output: `<template><div>{{ ${suggestionPrefix(
143+
prefix,
144+
options
145+
)}foo.bar() }}</div></template><!-- ${comment} -->`,
134146
errors: [{ message, type }],
135147
options
136148
},
137149
{
138150
code: `<template><div :class="${prefix}foo"></div></template><!-- ${comment} -->`,
151+
output: `<template><div :class="${suggestionPrefix(
152+
prefix,
153+
options
154+
)}foo"></div></template><!-- ${comment} -->`,
139155
errors: [{ message, type }],
140156
options
141157
},
142158
{
143159
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} -->`,
144164
errors: [{ message, type }],
145165
options
146166
},
147167
{
148168
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} -->`,
149173
errors: [{ message, type }],
150174
options
151175
},
152176
{
153177
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} -->`,
154182
errors: [{ message, type }],
155183
options
156184
},
157185
{
158186
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} -->`,
159191
errors: [{ message, type }],
160192
options
161193
}
@@ -172,11 +204,13 @@ function createInvalidTests(prefix, options, message, type) {
172204
: [
173205
{
174206
code: `<template><div>{{ this['xs'] }}</div></template><!-- ${comment} -->`,
207+
output: `<template><div>{{ xs }}</div></template><!-- ${comment} -->`,
175208
errors: [{ message, type }],
176209
options
177210
},
178211
{
179212
code: `<template><div>{{ this['xs0AZ_foo'] }}</div></template><!-- ${comment} -->`,
213+
output: `<template><div>{{ xs0AZ_foo }}</div></template><!-- ${comment} -->`,
180214
errors: [{ message, type }],
181215
options
182216
}
@@ -225,13 +259,23 @@ ruleTester.run('this-in-template', rule, {
225259
.concat([
226260
{
227261
code: `<template><div v-if="fn(this.$foo)"></div></template><!-- never -->`,
262+
output: `<template><div v-if="fn($foo)"></div></template><!-- never -->`,
228263
errors: ["Unexpected usage of 'this'."],
229264
options: ['never']
230265
},
231266
{
232267
code: `<template><div :class="{ foo: this.$foo }"></div></template><!-- never -->`,
268+
output: `<template><div :class="{ foo: $foo }"></div></template><!-- never -->`,
233269
errors: ["Unexpected usage of 'this'."],
234270
options: ['never']
235271
}
236272
])
237273
})
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

Comments
 (0)