# eslint代码规范校验插件 **Repository Path**: qiuwenwu91/mm_eslint ## Basic Information - **Project Name**: eslint代码规范校验插件 - **Description**: 这是一个用于校验代码是否符合规范的eslint插件 - **Primary Language**: NodeJS - **License**: ISC - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-12-18 - **Last Updated**: 2026-01-05 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # MM ESLint Plugin An ESLint plugin based on personal naming conventions, supporting naming convention detection for class names, function names, variable names, constant names, and more. ## Features - ✅ **Class Name Detection** - PascalCase naming convention - ✅ **Function Name Detection** - camelCase naming convention - ✅ **Method Name Detection** - camelCase naming convention - ✅ **Variable Name Detection** - snake_case naming convention - ✅ **Constant Name Detection** - UPPER_SNAKE_CASE naming convention - ✅ **Private Member Detection** - underscore-prefixed snake_case naming - ✅ **Parameter Name Detection** - snake_case naming convention - ✅ **Property Name Detection** - Multi-style naming based on value type - ✅ **Length Restrictions** - All names limited to 1-20 characters - ✅ **Single Word Preferred** - Encourages single word naming - ✅ **Word Length Limit** - Each word limited to maximum 8 characters - ✅ **Forbidden Words** - Avoids redundant words like Manager, Handler, etc. - ✅ **JSDoc Support** - Integrated JSDoc documentation requirements - ✅ **Naming Recommendations** - Recommends shorter alternative names while preserving semantic meaning ## Installation ### npm Installation ```bash npm install mm-eslint-plugin --save-dev ``` ### Local Installation If you want to use a local version, you can copy the plugin files to your project: ```bash # Copy plugin files to your project cp mm-eslint-plugin/index.js your-project/eslint-plugins/ ``` ## Quick Start ### 1. Complete Configuration Import the plugin in your ESLint configuration file: ```javascript // eslint.config.js const namingConventionPlugin = require("mm-eslint-plugin"); module.exports = [ { files: ["**/*.js"], plugins: { "naming-convention": namingConventionPlugin, jsdoc: require("eslint-plugin-jsdoc"), }, rules: { // Custom naming convention rules (priority) "naming-convention/class-name": "error", "naming-convention/function-name": "error", "naming-convention/method-name": "error", "naming-convention/variable-name": "warn", "naming-convention/constant-name": "error", "naming-convention/private-naming": "warn", "naming-convention/param-name": "warn", "naming-convention/property-name": "warn", // Disable conflicting default rules camelcase: "off", "id-match": "off", "new-cap": "off", "id-length": "off", "id-denylist": "off", "id-blacklist": "off", }, }, { // Code style rules rules: { "max-len": ["error", { code: 100 }], indent: ["error", 2], quotes: ["error", "single"], "no-tabs": "error", semi: ["error", "always"], }, }, { // Error handling rules rules: { "no-unused-vars": ["error", { args: "all" }], "no-unsafe-finally": "warn", "no-throw-literal": "error", }, }, { // Best practices rules: { "prefer-promise-reject-errors": "error", "no-param-reassign": "error", "prefer-object-spread": "error", "prefer-arrow-callback": "error", "max-lines-per-function": ["warn", { max: 40 }], complexity: ["warn", 10], }, }, { // JSDoc rules rules: { "jsdoc/require-jsdoc": ["warn", { publicOnly: true }], "jsdoc/check-alignment": "warn", "jsdoc/check-indentation": "warn", "jsdoc/check-param-names": "warn", "jsdoc/check-syntax": "warn", "jsdoc/check-tag-names": "warn", "jsdoc/check-types": "warn", "jsdoc/no-undefined-types": "warn", "jsdoc/require-description": "warn", "jsdoc/require-param": "warn", "jsdoc/require-param-description": "warn", "jsdoc/require-param-name": "warn", "jsdoc/require-param-type": "warn", "jsdoc/require-returns": "warn", "jsdoc/require-returns-check": "warn", "jsdoc/require-returns-description": "warn", "jsdoc/require-returns-type": "warn", "jsdoc/valid-types": "warn", }, }, ]; ``` ### 2. Command Line Usage ```bash # Validate a single file npx eslint your-file.js # Validate all JS files npx eslint "**/*.js" # Validate and auto-fix npx eslint --fix your-file.js ``` ## Naming Convention Rules ### Class Name Rule (class-name) - **Level**: error - **Requirement**: PascalCase naming convention - **Length**: 1-20 characters - **Word Length**: Each word ≤8 characters - **Examples**: `User`, `Product` - **Invalid Examples**: `UserConfiguration`, `ProductManagement` ### Function Name Rule (function-name) - **Level**: error - **Requirement**: camelCase naming convention - **Length**: 1-20 characters - **Word Length**: Each word ≤8 characters - **Preference**: Single word - **Examples**: `get`, `getName`, `process` - **Invalid Examples**: `getConfiguration`, `processInformation` ### Method Name Rule (method-name) - **Level**: error - **Requirement**: camelCase naming convention - **Length**: 1-20 characters - **Word Length**: Each word ≤8 characters - **Preference**: Single word - **Examples**: `add`, `setName`, `isValid` - **Invalid Examples**: `addConfiguration`, `validateAuthentication` ### Variable Name Rule (variable-name) - **Level**: warn - **Requirement**: snake_case naming convention - **Length**: 1-20 characters - **Examples**: `user_count`, `max_retry` - **Invalid Examples**: `userCount`, `MAX_RETRY` ### Constant Name Rule (constant-name) - **Level**: error - **Requirement**: UPPER_SNAKE_CASE naming convention - **Length**: 1-20 characters - **Examples**: `MAX_RETRY`, `DEFAULT_TIMEOUT` - **Invalid Examples**: `maxRetry`, `defaultTimeout` ### Private Member Rule (private-naming) - **Level**: warn - **Requirement**: underscore-prefixed snake_case naming - **Length**: 2-20 characters - **Examples**: `_user_count`, `_internal_data` - **Invalid Examples**: `userCount`, `_UserCount` ### Parameter Name Rule (param-name) - **Level**: warn - **Requirement**: snake_case naming convention - **Length**: 1-20 characters - **Examples**: `user_name`, `max_count` - **Invalid Examples**: `userName`, `maxCount` ### Property Name Rule (property-name) - **Level**: warn - **Requirement**: Multi-style naming based on value type - **Length**: 1-20 characters - **Examples**: `user_name` (string), `getUser` (function), `User` (class) ## Error Examples ```javascript // ❌ Invalid naming examples class UserConfiguration { // "Configuration" exceeds 8 characters const getConfiguration = function() {}; // "Configuration" exceeds 8 characters let maxRetryCount = 5; // Variable name should be snake_case const userCount = 10; // Constant name should be UPPER_SNAKE_CASE // ✅ Correct naming examples class User { constructor() { this._user_count = 0; // Private property correct } /** * Get user count * @returns {number} User count */ get() { // Method name correct (single word ≤8 chars) return this._user_count; } } const MAX_RETRY = 5; // Constant name correct let user_count = 10; // Variable name correct ``` ## Word Length Validation The plugin validates that each word in compound names does not exceed 8 characters: - **Valid**: `UserService` ("User"=4, "Service"=7) - **Invalid**: `UserConfiguration` ("Configuration"=13 > 8) - **Valid**: `getUser` ("get"=3, "User"=4) - **Invalid**: `getConfiguration` ("Configuration"=13 > 8) ## Configuration Options You can customize rule parameters through configuration: ````javascript // Custom configuration example const customConfig = { "class-name": { regex: /^[A-Z][a-zA-Z]*$/, min: 1, max: 25, // Extend maximum length single_word_len: 10, // Extend word length limit message: "Class name {name} must use PascalCase naming convention", }, }; const detector = new NamingDetector(customConfig); ## Naming Recommendation Feature The plugin includes an intelligent naming recommendation system that suggests shorter alternative names while preserving semantic meaning. ### Principle The core principle is **"shorten names without changing semantics"**. The recommendation system: - Recommends shorter words that maintain the same meaning - Avoids conceptual changes that would alter functionality - Ensures recommended words are always shorter than the original - Preserves programming-specific terminology and conceptual differences ### Configuration Enable the recommendation feature in your ESLint configuration: ```javascript // eslint.config.js const namingConventionPlugin = require("mm-eslint-plugin"); module.exports = [ { files: ["**/*.js"], plugins: { "naming-convention": namingConventionPlugin, }, rules: { "naming-convention/class-name": "error", "naming-convention/function-name": ["error", { recommen: true }], // Enable recommendations // ... other rules }, }, ]; ```` ### Available Recommendations The plugin provides recommendations for common programming operations: | Original Word | Recommended Word | Description | | ------------- | ---------------- | ------------------------- | | `calculate` | `calc` | Mathematical operations | | `generate` | `gen` | Data generation | | `initialize` | `init` | Initialization operations | | `execute` | `exec` | Command execution | | `process` | `run` | Data processing | | `retrieve` | `load` | Data loading | | `persist` | `save` | Data storage | | `compare` | `cmp` | Comparison operations | | `duplicate` | `copy` | Data copying | | `transfer` | `move` | Data movement | | `convert` | `to` | Type conversion | | `verify` | `check` | Validation checks | | `construct` | `create` | Object creation | | `handle` | `run` | Event handling | ### Examples ```javascript // ❌ Original (longer names) function calculateTotal() {} function generateUserData() {} function initializeSystem() {} function executeCommand() {} function processData() {} // ✅ Recommended (shorter names) function calcTotal() {} function genUserData() {} function initSystem() {} function execCommand() {} function runData() {} ``` ### Error Messages with Recommendations When the recommendation feature is enabled, ESLint will suggest alternative names: ```bash # ESLint output example function calculateTotal() {} # ^ Recommend using 'calc' instead of 'calculate' for shorter naming ``` ### Benefits 1. **Improved Readability**: Shorter names are easier to read and understand 2. **Consistent Codebase**: Standardized naming across the project 3. **Semantic Preservation**: Meaning is maintained while reducing verbosity 4. **Programming Best Practices**: Follows industry standards for concise naming ## Development ### Project Structure ``` mm-eslint-plugin/ ├── index.js # Main plugin file ├── package.json # npm configuration ├── eslint.config.js # ESLint configuration ├── test.js # Unit tests ├── example.js # Usage examples └── README.md # Documentation ``` ### Running Tests ```bash # Run unit tests npm test # Run ESLint checks npm run lint ``` ## License ISC License ## Contributing Welcome to submit Issues and Pull Requests to improve this plugin. ## Changelog ### v1.1.0 - Added word length validation (max 8 characters per word) - Added parameter name and property name detection - Integrated JSDoc documentation requirements - Enhanced configuration with complete ESLint rules ### v1.0.0 - Initial version release - Support for class name, function name, method name, variable name, constant name, and private member naming convention detection - Support for length restrictions and single word preference rules - Support for forbidden words detection