Skip to content

Instantly share code, notes, and snippets.

@Farhan-Haseeb
Created June 10, 2025 11:02
Show Gist options
  • Select an option

  • Save Farhan-Haseeb/3d73fc8a1ff0393588bef59b8e24b42a to your computer and use it in GitHub Desktop.

Select an option

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.
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