Created
June 10, 2025 11:02
-
-
Save Farhan-Haseeb/3d73fc8a1ff0393588bef59b8e24b42a to your computer and use it in GitHub Desktop.
Custom ES Lint rule to avoid and fix two or more spaces in string or className.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| export default { | |
| meta: { | |
| type: 'problem', | |
| docs: { | |
| description: 'Disallow multiple consecutive spaces in string literals', | |
| category: 'Possible Errors', | |
| recommended: true, | |
| }, | |
| fixable: 'code', | |
| schema: [], | |
| messages: { | |
| noMultiSpace: 'Avoid using multiple consecutive spaces in string literals.', | |
| }, | |
| }, | |
| create(context) { | |
| return { | |
| Literal(node) { | |
| if (typeof node.value !== 'string') return; | |
| if (/\s{2,}/.test(node.value)) { | |
| context.report({ | |
| node, | |
| messageId: 'noMultiSpace', | |
| fix(fixer) { | |
| const fixed = node.value.replace(/\s{2,}/g, ' '); | |
| return fixer.replaceText(node, JSON.stringify(fixed)); | |
| }, | |
| }); | |
| } | |
| }, | |
| TemplateElement(node) { | |
| if (/\s{2,}/.test(node.value.raw)) { | |
| context.report({ | |
| node, | |
| messageId: 'noMultiSpace', | |
| fix(fixer) { | |
| const fixed = node.value.raw.replace(/\s{2,}/g, ' '); | |
| const rawText = '`' + fixed + '`'; | |
| return fixer.replaceText(node, rawText); | |
| }, | |
| }); | |
| } | |
| }, | |
| }; | |
| }, | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment