forked from deecewan/eslint-plugin-snakecase
-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
68 additions
and
52 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]) | ||
|
||
|
@@ -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"]` | ||
|
||
|
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
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" | ||
}, | ||
|
@@ -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,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" | ||
}, | ||
|
@@ -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" | ||
} | ||
|
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
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; | ||
|
@@ -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()); | ||
} | ||
}); | ||
} | ||
} | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|