Skip to content

Instantly share code, notes, and snippets.

@devunt
Created April 5, 2023 08:27
Show Gist options
  • Save devunt/8d2df8f1c9960cfc60b12d248d7f55a8 to your computer and use it in GitHub Desktop.
Save devunt/8d2df8f1c9960cfc60b12d248d7f55a8 to your computer and use it in GitHub Desktop.
import { ESLintUtils } from '@typescript-eslint/utils';
export default ESLintUtils.RuleCreator((name) => name)({
name: 'no-excessive-whitespace',
meta: {
type: 'layout',
fixable: 'whitespace',
docs: {
description: 'Find excessive whitespace in classnames',
recommended: false,
},
messages: {
'excessive-whitespace': 'excessive whitespace in classnames',
},
schema: [],
},
defaultOptions: [],
create: (context) => {
return {
JSXAttribute: async (node) => {
const {
name: { name },
value,
} = node;
if (name !== 'className' || !value) {
return;
}
if (value.type === 'Literal') {
const { value: className } = value;
if (typeof className !== 'string') {
return;
}
const normalizedClassName = className.trim().replace(/\s+/g, ' ');
if (className !== normalizedClassName) {
context.report({
node,
messageId: 'excessive-whitespace',
fix: (fixer) =>
fixer.replaceText(node, `'${normalizedClassName}'`),
});
return false;
}
}
return true;
},
};
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment