Skip to content

Commit

Permalink
fix tests for real this time?
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleShit committed Sep 24, 2024
1 parent c320b67 commit 39a7a8f
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 9 deletions.
13 changes: 10 additions & 3 deletions lib/rules/no-unescaped-entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,17 @@ module.exports = {
{
data: { alt },
fix(fixer) {
const rangeStart = start + index + 1;
const rangeEnd = rangeStart + 1;
const lineToChange = i - node.loc.start.line;

return fixer.replaceTextRange([rangeStart, rangeEnd], alt);
const newText = node.raw.split('\n').map((line, idx) => {
if (idx === lineToChange) {
return line.slice(0, index) + alt + line.slice(index + 1);
}

return line;
}).join('\n');

return fixer.replaceText(node, newText);
},
}
)),
Expand Down
125 changes: 119 additions & 6 deletions tests/lib/rules/no-unescaped-entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,19 @@ ruleTester.run('no-unescaped-entities', rule, {
{
messageId: 'unescapedEntityAlts',
data: { entity: '>', alts: '`>`' },
suggestions: [
{
messageId: 'replaceWithAlt',
data: { alt: '>' },
output: `
var Hello = createReactClass({
render: function() {
return <div>&gt; default parser</div>;
}
});
`,
},
],
},
],
},
Expand All @@ -152,6 +165,21 @@ ruleTester.run('no-unescaped-entities', rule, {
{
messageId: 'unescapedEntityAlts',
data: { entity: '>', alts: '`&gt;`' },
suggestions: [
{
messageId: 'replaceWithAlt',
data: { alt: '&gt;' },
output: `
var Hello = createReactClass({
render: function() {
return <div>first line is ok
so is second
and here are some bad entities: &gt;</div>
}
});
`,
},
],
},
],
},
Expand All @@ -167,14 +195,86 @@ ruleTester.run('no-unescaped-entities', rule, {
{
messageId: 'unescapedEntityAlts',
data: { entity: '\'', alts: '`&apos;`, `&lsquo;`, `&#39;`, `&rsquo;`' },
suggestions: [
{
messageId: 'replaceWithAlt',
data: { alt: '&apos;' },
output: `
var Hello = createReactClass({
render: function() {
return <div>Multiple errors: &apos;>> default parser</div>;
}
});
`,
},
{
messageId: 'replaceWithAlt',
data: { alt: '&lsquo;' },
output: `
var Hello = createReactClass({
render: function() {
return <div>Multiple errors: &lsquo;>> default parser</div>;
}
});
`,
},
{
messageId: 'replaceWithAlt',
data: { alt: '&#39;' },
output: `
var Hello = createReactClass({
render: function() {
return <div>Multiple errors: &#39;>> default parser</div>;
}
});
`,
},
{
messageId: 'replaceWithAlt',
data: { alt: '&rsquo;' },
output: `
var Hello = createReactClass({
render: function() {
return <div>Multiple errors: &rsquo;>> default parser</div>;
}
});
`,
},
],
},
{
messageId: 'unescapedEntityAlts',
data: { entity: '>', alts: '`&gt;`' },
suggestions: [
{
messageId: 'replaceWithAlt',
data: { alt: '&gt;' },
output: `
var Hello = createReactClass({
render: function() {
return <div>Multiple errors: '&gt;> default parser</div>;
}
});
`,
},
],
},
{
messageId: 'unescapedEntityAlts',
data: { entity: '>', alts: '`&gt;`' },
suggestions: [
{
messageId: 'replaceWithAlt',
data: { alt: '&gt;' },
output: `
var Hello = createReactClass({
render: function() {
return <div>Multiple errors: '>&gt; default parser</div>;
}
});
`,
},
],
},
],
},
Expand All @@ -190,6 +290,19 @@ ruleTester.run('no-unescaped-entities', rule, {
{
messageId: 'unescapedEntityAlts',
data: { entity: '}', alts: '`&#125;`' },
suggestions: [
{
messageId: 'replaceWithAlt',
data: { alt: '&#125;' },
output: `
var Hello = createReactClass({
render: function() {
return <div>{"Unbalanced braces - default parser"}&#125;</div>;
}
});
`,
},
],
},
],
},
Expand All @@ -212,12 +325,12 @@ ruleTester.run('no-unescaped-entities', rule, {
messageId: 'replaceWithAlt',
data: { alt: '&gt;' },
output: `
var Hello = createReactClass({
render: function() {
return <>&gt; babel-eslint</>;
}
});
`,
var Hello = createReactClass({
render: function() {
return <>&gt; babel-eslint</>;
}
});
`,
},
],
},
Expand Down

0 comments on commit 39a7a8f

Please sign in to comment.