Skip to content

Commit 1210a14

Browse files
committed
[New] Add suggestions to no-unescaped-entities
Closes #3277
1 parent 4ecf034 commit 1210a14

File tree

4 files changed

+58
-1
lines changed

4 files changed

+58
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ module.exports = [
368368
| [no-string-refs](docs/rules/no-string-refs.md) | Disallow using string references | ☑️ | | | | |
369369
| [no-this-in-sfc](docs/rules/no-this-in-sfc.md) | Disallow `this` from being used in stateless functional components | | | | | |
370370
| [no-typos](docs/rules/no-typos.md) | Disallow common typos | | | | | |
371-
| [no-unescaped-entities](docs/rules/no-unescaped-entities.md) | Disallow unescaped HTML entities from appearing in markup | ☑️ | | | | |
371+
| [no-unescaped-entities](docs/rules/no-unescaped-entities.md) | Disallow unescaped HTML entities from appearing in markup | ☑️ | | | 💡 | |
372372
| [no-unknown-property](docs/rules/no-unknown-property.md) | Disallow usage of unknown DOM property | ☑️ | | 🔧 | | |
373373
| [no-unsafe](docs/rules/no-unsafe.md) | Disallow usage of unsafe lifecycle methods | | ☑️ | | | |
374374
| [no-unstable-nested-components](docs/rules/no-unstable-nested-components.md) | Disallow creating unstable components inside components | | | | | |

docs/rules/no-unescaped-entities.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
💼 This rule is enabled in the ☑️ `recommended` [config](https://github.com/jsx-eslint/eslint-plugin-react/#shareable-configs).
44

5+
💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).
6+
57
<!-- end auto-generated rule header -->
68

79
This rule prevents characters that you may have meant as JSX escape characters

lib/rules/no-unescaped-entities.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const docsUrl = require('../util/docsUrl');
99
const getSourceCode = require('../util/eslint').getSourceCode;
1010
const jsxUtil = require('../util/jsx');
1111
const report = require('../util/report');
12+
const getMessageData = require('../util/message');
1213

1314
// ------------------------------------------------------------------------------
1415
// Rule Definition
@@ -34,11 +35,13 @@ const DEFAULTS = [{
3435
const messages = {
3536
unescapedEntity: 'HTML entity, `{{entity}}` , must be escaped.',
3637
unescapedEntityAlts: '`{{entity}}` can be escaped with {{alts}}.',
38+
replaceWithAlt: 'Replace with `{{alt}}`.',
3739
};
3840

3941
/** @type {import('eslint').Rule.RuleModule} */
4042
module.exports = {
4143
meta: {
44+
hasSuggestions: true,
4245
docs: {
4346
description: 'Disallow unescaped HTML entities from appearing in markup',
4447
category: 'Possible Errors',
@@ -117,6 +120,15 @@ module.exports = {
117120
entity: entities[j].char,
118121
alts: entities[j].alternatives.map((alt) => `\`${alt}\``).join(', '),
119122
},
123+
suggest: entities[j].alternatives.map((alt) => Object.assign(
124+
getMessageData('replaceWithAlt', messages.replaceWithAlt),
125+
{
126+
data: { alt },
127+
fix(fixer) {
128+
fixer.replaceText(node, alt);
129+
},
130+
}
131+
)),
120132
});
121133
}
122134
}

tests/lib/rules/no-unescaped-entities.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,19 @@ ruleTester.run('no-unescaped-entities', rule, {
207207
{
208208
messageId: 'unescapedEntityAlts',
209209
data: { entity: '>', alts: '`&gt;`' },
210+
suggestions: [
211+
{
212+
messageId: 'replaceWithAlt',
213+
data: { alt: '&gt;' },
214+
output: `
215+
var Hello = createReactClass({
216+
render: function() {
217+
return <>&gt; babel-eslint</>;
218+
}
219+
});
220+
`,
221+
},
222+
],
210223
},
211224
],
212225
},
@@ -327,6 +340,36 @@ ruleTester.run('no-unescaped-entities', rule, {
327340
data: { entity: '"', alts: '`&quot;`, `&ldquo;`, `&#34;`, `&rdquo;`' },
328341
line: 2,
329342
column: 30,
343+
suggestions: [
344+
{
345+
messageId: 'replaceWithAlt',
346+
data: { alt: '&quot;' },
347+
output: `
348+
<script>window.foo = &quot;bar"</script>
349+
`,
350+
},
351+
{
352+
messageId: 'replaceWithAlt',
353+
data: { alt: '&ldquo;' },
354+
output: `
355+
<script>window.foo = &ldquo;bar"</script>
356+
`,
357+
},
358+
{
359+
messageId: 'replaceWithAlt',
360+
data: { alt: '&#34;' },
361+
output: `
362+
<script>window.foo = &#34;bar"</script>
363+
`,
364+
},
365+
{
366+
messageId: 'replaceWithAlt',
367+
data: { alt: '&rdquo;' },
368+
output: `
369+
<script>window.foo = &rdquo;bar"</script>
370+
`,
371+
},
372+
],
330373
},
331374
{
332375
messageId: 'unescapedEntityAlts',

0 commit comments

Comments
 (0)