Skip to content

Commit 95297ed

Browse files
StyleShitljharb
authored andcommitted
[New] no-unescaped-entities: add suggestions
Closes #3277
1 parent 7f3ac1b commit 95297ed

File tree

5 files changed

+294
-1
lines changed

5 files changed

+294
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ This change log adheres to standards from [Keep a CHANGELOG](https://keepachange
88

99
### Added
1010
* add type generation ([#3830][] @voxpelli)
11+
* [`no-unescaped-entities`]: add suggestions ([#3831][] @StyleShit)
1112

13+
[#3831]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3831
1214
[#3830]: https://github.com/jsx-eslint/eslint-plugin-react/pull/3830
1315

1416
## [7.36.1] - 2024.09.12

README.md

+1-1
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

+2
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

+22
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,25 @@ 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+
const lineToChange = i - node.loc.start.line;
129+
130+
const newText = node.raw.split('\n').map((line, idx) => {
131+
if (idx === lineToChange) {
132+
return line.slice(0, index) + alt + line.slice(index + 1);
133+
}
134+
135+
return line;
136+
}).join('\n');
137+
138+
return fixer.replaceText(node, newText);
139+
},
140+
}
141+
)),
120142
});
121143
}
122144
}

0 commit comments

Comments
 (0)