The drop-em-dash-eslint-rule plugin enforces the use of plain hyphens in your code by automatically replacing em dashes. This helps maintain consistency in codebases, making them clearer and easier to read, while preventing AI-generated text pitfalls. Ideal for teams aiming for readability and uniformity.
Drop Em Dash ESLint Rule
The eslint-plugin-drop-em-dash project serves as an essential tool for developers looking to maintain clean and consistent codebases by disallowing the use of em dashes (Unicode character U+2014) and automatically fixing them to hyphens (U+002D). This initiative addresses the common issue of unwanted em dashes creeping into source codes from various sources, such as AI-generated text and word processors that automatically convert characters, potentially disrupting the readability and consistency of code.
Key Features
- Identifies and flags all occurrences of em dashes in code, comments, string literals, and JSX text.
- Autofixes flagged em dashes to hyphens instantly upon saving, ensuring that unwanted characters do not persist in the codebase.
- Designed to ignore other types of dashes, such as en dashes, avoiding unnecessary style conflicts.
Why Use This Plugin?
The presence of em dashes in code can lead to several drawbacks, including:
- Compatibility Issues: Tools and commands like
grepmay not recognize em dashes, complicating searches and refactoring. - Cluttered Diffs: Mixed punctuation styles can create confusion during code reviews.
- Rendering Problems: Different systems may render em dashes inconsistently, which can lead to unexpected behavior.
- Lack of Standardization: Em dashes introduce non-ASCII characters that may disrupt legacy systems, logs, or exports.
Usage
To integrate the plugin into an ESLint setup, include it in your configuration:
For ESLint 9+
import dropEmDash from 'eslint-plugin-drop-em-dash';
export default [...dropEmDash.configs['flat/recommended']];
For Legacy Formats
You can also use the plugin with types like .eslintrc:
{
"extends": ["plugin:drop-em-dash/recommended"]
}
Example
Once the plugin is set up, running eslint . --fix converts any instance of an em dash in the code. For example:
const title = 'Pricing - overview'; // best plan - for teams
This will be corrected to:
const title = 'Pricing - overview'; // best plan - for teams
Installation Without npm
For those using other package managers:
pnpm add -D eslint eslint-plugin-drop-em-dash
yarn add -D eslint eslint-plugin-drop-em-dash
Additional Resources
Comprehensive documentation can be found at: Full Docs
For testing and contributions, refer to the project's testing suite and contribution guidelines.
This ESLint plugin stands as a simple yet effective solution for ensuring consistent use of ASCII characters in the code, contributing significantly to improved code quality and maintainability.
No comments yet.
Sign in to be the first to comment.