Skip to content

Commit

Permalink
[New] Add suggestions to no-unescaped-entities
Browse files Browse the repository at this point in the history
  • Loading branch information
StyleShit committed Sep 24, 2024
1 parent 4ecf034 commit 1210a14
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ module.exports = [
| [no-string-refs](docs/rules/no-string-refs.md) | Disallow using string references | ☑️ | | | | |
| [no-this-in-sfc](docs/rules/no-this-in-sfc.md) | Disallow `this` from being used in stateless functional components | | | | | |
| [no-typos](docs/rules/no-typos.md) | Disallow common typos | | | | | |
| [no-unescaped-entities](docs/rules/no-unescaped-entities.md) | Disallow unescaped HTML entities from appearing in markup | ☑️ | | | | |
| [no-unescaped-entities](docs/rules/no-unescaped-entities.md) | Disallow unescaped HTML entities from appearing in markup | ☑️ | | | 💡 | |
| [no-unknown-property](docs/rules/no-unknown-property.md) | Disallow usage of unknown DOM property | ☑️ | | 🔧 | | |
| [no-unsafe](docs/rules/no-unsafe.md) | Disallow usage of unsafe lifecycle methods | | ☑️ | | | |
| [no-unstable-nested-components](docs/rules/no-unstable-nested-components.md) | Disallow creating unstable components inside components | | | | | |
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-unescaped-entities.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

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

💡 This rule is manually fixable by [editor suggestions](https://eslint.org/docs/latest/use/core-concepts#rule-suggestions).

<!-- end auto-generated rule header -->

This rule prevents characters that you may have meant as JSX escape characters
Expand Down
12 changes: 12 additions & 0 deletions lib/rules/no-unescaped-entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const docsUrl = require('../util/docsUrl');
const getSourceCode = require('../util/eslint').getSourceCode;
const jsxUtil = require('../util/jsx');
const report = require('../util/report');
const getMessageData = require('../util/message');

// ------------------------------------------------------------------------------
// Rule Definition
Expand All @@ -34,11 +35,13 @@ const DEFAULTS = [{
const messages = {
unescapedEntity: 'HTML entity, `{{entity}}` , must be escaped.',
unescapedEntityAlts: '`{{entity}}` can be escaped with {{alts}}.',
replaceWithAlt: 'Replace with `{{alt}}`.',
};

/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
hasSuggestions: true,
docs: {
description: 'Disallow unescaped HTML entities from appearing in markup',
category: 'Possible Errors',
Expand Down Expand Up @@ -117,6 +120,15 @@ module.exports = {
entity: entities[j].char,
alts: entities[j].alternatives.map((alt) => `\`${alt}\``).join(', '),
},
suggest: entities[j].alternatives.map((alt) => Object.assign(
getMessageData('replaceWithAlt', messages.replaceWithAlt),
{
data: { alt },
fix(fixer) {
fixer.replaceText(node, alt);
},
}
)),
});
}
}
Expand Down
43 changes: 43 additions & 0 deletions tests/lib/rules/no-unescaped-entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ 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 <>&gt; babel-eslint</>;
}
});
`,
},
],
},
],
},
Expand Down Expand Up @@ -327,6 +340,36 @@ ruleTester.run('no-unescaped-entities', rule, {
data: { entity: '"', alts: '`&quot;`, `&ldquo;`, `&#34;`, `&rdquo;`' },
line: 2,
column: 30,
suggestions: [
{
messageId: 'replaceWithAlt',
data: { alt: '&quot;' },
output: `
<script>window.foo = &quot;bar"</script>
`,
},
{
messageId: 'replaceWithAlt',
data: { alt: '&ldquo;' },
output: `
<script>window.foo = &ldquo;bar"</script>
`,
},
{
messageId: 'replaceWithAlt',
data: { alt: '&#34;' },
output: `
<script>window.foo = &#34;bar"</script>
`,
},
{
messageId: 'replaceWithAlt',
data: { alt: '&rdquo;' },
output: `
<script>window.foo = &rdquo;bar"</script>
`,
},
],
},
{
messageId: 'unescapedEntityAlts',
Expand Down

0 comments on commit 1210a14

Please # to comment.