Skip to content

Commit 33b67a4

Browse files
authored
fix(no-useless-mustaches): escape < in fixes (#2389)
1 parent 5227eed commit 33b67a4

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

lib/rules/no-useless-mustaches.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -142,8 +142,9 @@ module.exports = {
142142
// It doesn't autofix because another rule like indent or eol space might remove spaces.
143143
return null
144144
}
145+
const escaped = text.replace(/</g, '&lt;')
145146

146-
return fixer.replaceText(node, text.replace(/\\([\S\s])/g, '$1'))
147+
return fixer.replaceText(node, escaped.replace(/\\([\S\s])/g, '$1'))
147148
}
148149
})
149150
}

tests/lib/rules/no-useless-mustaches.js

+48
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,54 @@ tester.run('no-useless-mustaches', rule, {
186186
'Unexpected mustache interpolation with a string literal value.'
187187
]
188188
},
189+
{
190+
code: `
191+
<template>
192+
{{ '&lt;' }}
193+
{{ '&gt;' }}
194+
{{ '&amp;' }}
195+
{{ '&#8212;' }}
196+
</template>
197+
`,
198+
output: `
199+
<template>
200+
&lt;
201+
&gt;
202+
&amp;
203+
&#8212;
204+
</template>
205+
`,
206+
errors: [
207+
'Unexpected mustache interpolation with a string literal value.',
208+
'Unexpected mustache interpolation with a string literal value.',
209+
'Unexpected mustache interpolation with a string literal value.',
210+
'Unexpected mustache interpolation with a string literal value.'
211+
]
212+
},
213+
{
214+
code: `
215+
<template>
216+
{{ '<' }}
217+
{{ '<<' }}
218+
{{ 'can be < anywhere' }}
219+
{{ '<tag>' }}
220+
</template>
221+
`,
222+
output: `
223+
<template>
224+
&lt;
225+
&lt;&lt;
226+
can be &lt; anywhere
227+
&lt;tag>
228+
</template>
229+
`,
230+
errors: [
231+
'Unexpected mustache interpolation with a string literal value.',
232+
'Unexpected mustache interpolation with a string literal value.',
233+
'Unexpected mustache interpolation with a string literal value.',
234+
'Unexpected mustache interpolation with a string literal value.'
235+
]
236+
},
189237
{
190238
code: `
191239
<template>

0 commit comments

Comments
 (0)