diff --git a/CHANGELOG.md b/CHANGELOG.md index a04b3a5..e0f9990 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +# v1.2.0 (October 08, 2018) + +* Feature: eslint --fix param, auto replace camelCase to snake_case + + # v1.1.0 (October 01, 2018) * Feature: whitelist array on `eslintrc.json` diff --git a/INSTALL.md b/INSTALL.md index 377e6d9..becbb78 100755 --- a/INSTALL.md +++ b/INSTALL.md @@ -1,14 +1,7 @@ #### Setup -1. Add dependencies on `package.json` -``` -"dependencies": { - "eslint": "^4.19.1", -}, -"devDependencies": { - "eslint-plugin-snakecasejs": "^1.1.0", -} -``` -2. Create `.eslintrc.json` file with: +1. Install eslint: `npm install eslint -g` +2. Install snakecasejs plugin in your project: `npm install eslint-plugin-snakecasejs --save-dev` +3. Create `.eslintrc.json` file with: ``` { "plugins": [ @@ -20,4 +13,6 @@ "snakecasejs/whitelist": [] } } -``` \ No newline at end of file +``` +4. If it works add a star :star: at this project :heart: +5. If you want to help me: **[donate on paypal](http://paypal.ptkdev.io)** or become a **[backer on patreon](http://patreon.ptkdev.io)**. \ No newline at end of file diff --git a/ISSUE_TEMPLATE.md b/ISSUE_TEMPLATE.md index 163d953..c2b1556 100644 --- a/ISSUE_TEMPLATE.md +++ b/ISSUE_TEMPLATE.md @@ -2,10 +2,9 @@ Replace the values below with your own: -- **Plugin Version:** v1.0.0 +- **Plugin Version:** v1.2.0 - **Node Version:** v8.9 (Plugin require >= 7.6) - **Operating System:** Ubuntu 14.04 -- **Browser:** Google Chrome 64 ### Expected Behavior diff --git a/README.md b/README.md index add5cc1..d6ad578 100755 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ # ESLint Plugin snakecasejs -[![](https://img.shields.io/badge/license-MIT-brightgreen.svg)](#) [![](https://img.shields.io/badge/powered%20by-eslint-46aef7.svg)](https://www.npmjs.com/package/eslint) [![](https://img.shields.io/badge/version-v1.1.0-lightgrey.svg)](https://github.com/ptkdev/eslint-plugin-snakecasejs/releases) [![](https://img.shields.io/badge/chat%20on-slack-orange.svg)](http://slack.ptkdev.io) [![](https://img.shields.io/badge/chat%20on-discord-7289da.svg)](http://discord.ptkdev.io) [![](https://img.shields.io/badge/blog-medium-2AE176.svg)](http://blog.ptkdev.io) [![](https://img.shields.io/badge/twitter-ptkdevio-2AA3EF.svg)](https://twitter.com/ptkdevio) +[![](https://img.shields.io/badge/license-MIT-brightgreen.svg)](#) [![](https://img.shields.io/badge/powered%20by-eslint-46aef7.svg)](https://www.npmjs.com/package/eslint) [![](https://img.shields.io/badge/version-v1.2.0-lightgrey.svg)](https://github.com/ptkdev/eslint-plugin-snakecasejs/releases) [![](https://img.shields.io/badge/chat%20on-slack-orange.svg)](http://slack.ptkdev.io) [![](https://img.shields.io/badge/chat%20on-discord-7289da.svg)](http://discord.ptkdev.io) [![](https://img.shields.io/badge/blog-medium-2AE176.svg)](http://blog.ptkdev.io) [![](https://img.shields.io/badge/twitter-ptkdevio-2AA3EF.svg)](https://twitter.com/ptkdevio) [![](https://img.shields.io/badge/donate-patreon-F87668.svg)](http://patreon.ptkdev.io) [![](https://img.shields.io/badge/donate-paypal-46AFE0.svg)](http://paypal.ptkdev.io) [![](https://img.shields.io/badge/buy%20me-coffee-4B788C.svg)](http://coffee.ptkdev.io) [![](https://img.shields.io/badge/help-support@ptkdev.io-fbbc05.svg)](mailto:support@ptkdev.io) @@ -12,21 +12,15 @@ This is to enforce a style of snake_case in your project, rather than just disab ## Features * [✓] Easy to use * [✓] MIT License -* [✓] Compatible with last version of ESLint +* [✓] Compatible with latest version of ESLint * [✓] Compatible with CamelCase (Class name or similar) +* [✓] Compatible with --fix (convert automatically camelCase to snake_case) * [✓] Whitelist array for methods or variables with camelCase syntax from other libraries/npm package. ## Fast setup -1. Add dependencies on `package.json` -``` -"dependencies": { - "eslint": "^4.19.1", -}, -"devDependencies": { - "eslint-plugin-snakecasejs": "^1.1.0", -} -``` -2. Create `.eslintrc.json` file with: +1. Install eslint: `npm install eslint -g` +2. Install snakecasejs plugin: `npm install eslint-plugin-snakecasejs --save-dev` +3. Create `.eslintrc.json` file with: ``` { "plugins": [ @@ -39,15 +33,19 @@ This is to enforce a style of snake_case in your project, rather than just disab } } ``` -3. If it works add a star :star: at this project :heart: -4. If you want to help me: **[donate on paypal](http://paypal.ptkdev.io)** or become a **[backer on patreon](http://patreon.ptkdev.io)**. +4. If it works add a star :star: at this project :heart: +5. If you want to help me: **[donate on paypal](http://paypal.ptkdev.io)** or become a **[backer on patreon](http://patreon.ptkdev.io)**. NOTE: switch `error` to `warn` if you don't need snake_case as mandatory rules. For advanced configuration see [INSTALL.md](https://github.com/ptkdev/eslint-plugin-snakecasejs/blob/master/INSTALL.md). -## Ignore words, variables, methods, class name (whitelist) -Add rules: `"snakecasejs/whitelist": []` with array of variables or method with camelCase syntax. +## --fix +This eslint param fix simple rules of linter and with this plugin convert all camelCase variables or function name to snake_case. --fix support whitelist. Run this command in your project dir: +- `eslint ./ --cache --ignore-pattern .gitignore --fix` + +## Whitelist +Ignore words, variables, methods, class name without snake_case syntax. Add rules: `"snakecasejs/whitelist": []` with array of variables or method with camelCase that you can not convert to snake_case (external library naming convention). Plugin ignore check on this words. Example: `"snakecasejs/whitelist": ["externalPath","setNumber"]` diff --git a/package.json b/package.json index 0c8870b..a652e8e 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "eslint-plugin-snakecasejs", - "description": "Eslint plugin to enforce a style of snake_case in your project, rather than just disabling camelcase.", - "version": "1.1.0-5", - "main": "index.js", + "description": "Eslint plugin to enforce a style of snake_case in your project, rather than just disabling camelCase.", + "version": "1.2.0", + "main": "plugin.js", "author": "Patryk Rzucidlo [@ptkdev] (https://ptkdev.it)", "author_original": "David Buchan-Swanson ", "license": "MIT", - "homepage": "https://github.com/ptkdev", + "homepage": "https://ptkdev.io", "bugs": { "url": "https://github.com/ptkdev/eslint-plugin-snakecasejs/issues" }, @@ -15,7 +15,7 @@ "url": "https://github.com/ptkdev/eslint-plugin-snakecasejs.git" }, "scripts": { - "start": "node index.js", + "start": "node plugin.js", "test": "node test.js", "lint": "eslint ./ --cache --ignore-pattern .gitignore --fix", "precommit-lint-check": "echo 'Pre-commit lint checks...' && exit 0" @@ -30,8 +30,8 @@ "snakecase", "snakecase-javascript", "underscore-syntax", - "programming style", - "coding conventions", + "programming-style", + "conventions", "syntax", "coding-style", "eslint", @@ -39,9 +39,6 @@ "eslint-plugin-snakecase", "eslint-plugin-snakecasejs" ], - "dependencies": { - "eslint": "^4.19.1" - }, "engines": { "node": ">=7.6.0" }, @@ -50,7 +47,7 @@ "eslint-plugin-import": "^2.11.0", "eslint-plugin-node": "^6.0.1", "eslint-plugin-promise": "^3.7.0", - "eslint-plugin-snakecasejs": "^1.1.0", + "eslint-plugin-snakecasejs": "latest", "eslint-plugin-standard": "^3.1.0", "pre-commit": "^1.2.2" } diff --git a/index.js b/plugin.js similarity index 73% rename from index.js rename to plugin.js index 154dd9a..9679608 100644 --- a/index.js +++ b/plugin.js @@ -1,14 +1,11 @@ /** - * snakecasejs + * ESLint: snakecasejs * ===================== * This is to enforce a style of snake_case in your project, rather than just disabling camelcase. * * @author: Patryk Rzucidlo [@ptkdev] (https://ptkdev.it) * @original: David Buchan-Swanson * @license: This code and contributions have 'MIT License' - * @version: 1.1.0 - * @changelog: 1.0.0 initial release - * 1.1.0 add whitelist * */ let eslintrc = null; @@ -18,14 +15,32 @@ try { eslintrc = []; } -if(typeof eslintrc.rules["snakecasejs/whitelist"] == "undefined"){ +/** + * Whitelist exist? + * ===================== + * Set empry array if whitelist is undefined in eslintrc + * + */ +if (typeof eslintrc.rules["snakecasejs/whitelist"] == "undefined") { eslintrc.rules["snakecasejs/whitelist"] = []; } -function is_class_usage(node) { +/** + * Detect var and function + * ===================== + * Filter code, find funcrions name and variables + * + */ +function filter(node) { return ["FunctionDeclaration", "NewExpression", "MemberExpression"].indexOf(node.parent.type) > -1; } +/** + * Detect function and variables name + * ===================== + * Filter code, find functions name and variables + * + */ module.exports = { rules: { snakecasejs: { @@ -36,22 +51,31 @@ module.exports = { return { Identifier(node) { var name = node.name; + + // ignore javascript language function var array_system_var = ["parseInt", "parseFloat", "isNaN", "isFinite", "decodeURI", "decodeURIComponent", "encodeURI", "toString", "toLocaleString", "valueOf", "hasOwnProperty", "isPrototypeOf", "propertyIsEnumerable", "indexOf", "forEach", "charAt", "charCodeAt", "endsWith", "lastIndexOf", "startsWith", "toLowerCase", "toLocaleLowerCase", "toUpperCase", "toLocaleUpperCase", "toFixed", "toPrecision", "setInterval", "clearInterval", "setTimeout", "toDateString", "toTimeString", "getTime", "getFullYear", "getUTCFullYear", "getMonth", "getUTCMonth", "getDate", "getUTCDate", "getDay", "getUTCDay", "getHours", "getUTCHours", "getMinutes", "getUTCMinutes", "getSeconds", "getUTCSeconds", "getMilliseconds", "getUTCMilliseconds", "getTimeZoneOffset", "setTime", "setMilliseconds", "setUTCMilliseconds", "setSeconds", "setUTCSeconds", "setMinutes", "setUTCMinutes", "setHours", "setUTCHours", "setDate", "setUTCDate", "setMonth", "setUTCMonth", "setFullYear", "setUTCFullYear", "toUTCString", "toISOString", "toJSON"]; + var split = name.split(/(?=[A-Z])/); var split_az = name.split(/(?=[a-z])/); + + // detect camelCase if ((split.length > 1 && split_az.length > 1) && !array_system_var.includes(name) && !eslintrc.rules["snakecasejs/whitelist"].includes(name)) { - if (is_class_usage(node)) { + if (filter(node)) { return true; } + + // error message on ide context.report({ message: "Identifiers must be snake_case: {{ identifier }}", node: node, data: { identifier: node.name, }, - // fix(fixer) { - // return fixer.replaceText(node, split.map(function(piece){ return piece.replace('_', '').toLowerCase(); }).join('_')); - // } + + // --fix eslint param: detect camelCase and convert in snake_case + fix(fixer) { + return fixer.replaceText(node, node.name.split(/(?=[A-Z])/).join("_").toLowerCase()); + } }); } } diff --git a/test.js b/test.js index 985f651..e18a33f 100644 --- a/test.js +++ b/test.js @@ -6,8 +6,6 @@ * @author: Patryk Rzucidlo [@ptkdev] (https://ptkdev.it) * @original: David Buchan-Swanson * @license: This code and contributions have 'MIT License' - * @version: 1.0.0 - * @changelog: 1.0.0 initial release * */ var rule_tester = require("eslint").RuleTester;