Skip to content

Commit

Permalink
[Release] v1.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ptkdev committed Oct 8, 2018
1 parent d0d549a commit 3939218
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 52 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
17 changes: 6 additions & 11 deletions INSTALL.md
Original file line number Diff line number Diff line change
@@ -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": [
Expand All @@ -20,4 +13,6 @@
"snakecasejs/whitelist": []
}
}
```
```
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)**.
3 changes: 1 addition & 2 deletions ISSUE_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
30 changes: 14 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected])](mailto:[email protected])

Expand All @@ -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": [
Expand All @@ -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"]`

Expand Down
19 changes: 8 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -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] <[email protected]> (https://ptkdev.it)",
"author_original": "David Buchan-Swanson <[email protected]>",
"license": "MIT",
"homepage": "https://github.com/ptkdev",
"homepage": "https://ptkdev.io",
"bugs": {
"url": "https://github.com/ptkdev/eslint-plugin-snakecasejs/issues"
},
Expand All @@ -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"
Expand All @@ -30,18 +30,15 @@
"snakecase",
"snakecase-javascript",
"underscore-syntax",
"programming style",
"coding conventions",
"programming-style",
"conventions",
"syntax",
"coding-style",
"eslint",
"eslint-plugin",
"eslint-plugin-snakecase",
"eslint-plugin-snakecasejs"
],
"dependencies": {
"eslint": "^4.19.1"
},
"engines": {
"node": ">=7.6.0"
},
Expand All @@ -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"
}
Expand Down
44 changes: 34 additions & 10 deletions index.js → plugin.js
Original file line number Diff line number Diff line change
@@ -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] <[email protected]> (https://ptkdev.it)
* @original: David Buchan-Swanson <[email protected]>
* @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;
Expand All @@ -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: {
Expand All @@ -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());
}
});
}
}
Expand Down
2 changes: 0 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
* @author: Patryk Rzucidlo [@ptkdev] <[email protected]> (https://ptkdev.it)
* @original: David Buchan-Swanson <[email protected]>
* @license: This code and contributions have 'MIT License'
* @version: 1.0.0
* @changelog: 1.0.0 initial release
*
*/
var rule_tester = require("eslint").RuleTester;
Expand Down

0 comments on commit 3939218

Please sign in to comment.