Created
April 5, 2023 08:27
-
-
Save devunt/8d2df8f1c9960cfc60b12d248d7f55a8 to your computer and use it in GitHub Desktop.
This file contains 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
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