Browse Source

for, while, switch statements; fixers; package.json update

CrazyDoctor 2 years ago
parent
commit
0e374b41cd
2 changed files with 109 additions and 7 deletions
  1. 105 7
      index.js
  2. 4 0
      package.json

+ 105 - 7
index.js

@@ -1,9 +1,10 @@
 module.exports = {
 	rules: {
-		'no-spaces-in-conditionals': {
+		// auto fixable
+		'no-spaces-in-control-flow-statements': {
 			meta: {
 				docs: {
-					description: 'Disallow spaces in conditionals',
+					description: 'Disallow spaces in control flow statements',
 					category: 'Z8 Stylistic Issues',
 					recommended: true,
 				},
@@ -17,8 +18,7 @@ module.exports = {
 						const firstToken = tokens[0];
 						const secondToken = tokens[1];
 
-						if (
-							firstToken.type === 'Keyword' &&
+						if (firstToken.type === 'Keyword' &&
 							firstToken.value === 'if' &&
 							secondToken.type === 'Punctuator' &&
 							secondToken.value === '(' &&
@@ -27,6 +27,72 @@ module.exports = {
 							context.report({
 								node: node,
 								message: 'Do not use spaces between \'if\' and its condition',
+								fix: function(fixer) {
+									return fixer.replaceTextRange([firstToken.range[1], secondToken.range[0]], '(');
+								}
+							});
+						}
+					},
+					ForStatement: function (node) {
+						const tokens = context.getSourceCode().getTokens(node);
+						const firstToken = tokens[0];
+						const secondToken = tokens[1];
+
+						if (
+							firstToken.type === 'Keyword' &&
+							firstToken.value === 'for' &&
+							secondToken.type === 'Punctuator' &&
+							secondToken.value === '(' &&
+							secondToken.range[0] - firstToken.range[1] > 0
+						) {
+							context.report({
+								node: node,
+								message: 'Do not use spaces between \'for\' and its condition',
+								fix: function(fixer) {
+									return fixer.replaceTextRange([firstToken.range[1], secondToken.range[0]], '(');
+								}
+							});
+						}
+					},
+					WhileStatement: function (node) {
+						const tokens = context.getSourceCode().getTokens(node);
+						const firstToken = tokens[0];
+						const secondToken = tokens[1];
+
+						if (
+							firstToken.type === 'Keyword' &&
+							firstToken.value === 'while' &&
+							secondToken.type === 'Punctuator' &&
+							secondToken.value === '(' &&
+							secondToken.range[0] - firstToken.range[1] > 0
+						) {
+							context.report({
+								node: node,
+								message: 'Do not use spaces between \'while\' and its condition',
+								fix: function(fixer) {
+									return fixer.replaceTextRange([firstToken.range[1], secondToken.range[0]], '(');
+								}
+							});
+						}
+					},
+					SwitchStatement: function (node) {
+						const tokens = context.getSourceCode().getTokens(node);
+						const firstToken = tokens[0];
+						const secondToken = tokens[1];
+
+						if (
+							firstToken.type === 'Keyword' &&
+							firstToken.value === 'switch' &&
+							secondToken.type === 'Punctuator' &&
+							secondToken.value === '(' &&
+							secondToken.range[0] - firstToken.range[1] > 0
+						) {
+							context.report({
+								node: node,
+								message: 'Do not use spaces between \'switch\' and its expression',
+								fix: function(fixer) {
+									return fixer.replaceTextRange([firstToken.range[1], secondToken.range[0]], '(');
+								}
 							});
 						}
 					},
@@ -56,6 +122,7 @@ module.exports = {
 				};
 			},
 		},
+		// auto fixable
 		'no-double-quotes': {
 			meta: {
 				docs: {
@@ -73,12 +140,15 @@ module.exports = {
 							context.report({
 								node: node,
 								message: 'Avoid using double quotes (use single quotes instead)',
+								fix: function(fixer) {
+									return fixer.replaceTextRange([node.start+1, node.end-1], "'" + node.value + "'");
+								}
 							});
 						}
 					},
 				};
 			},
-		},
+		}
 		'no-arrow-functions': {
 			meta: {
 				docs: {
@@ -86,7 +156,6 @@ module.exports = {
 					category: 'Z8 Stylistic Issues',
 					recommended: true,
 				},
-				fixable: 'code',
 				schema: [],
 			},
 			create: function (context) {
@@ -107,7 +176,6 @@ module.exports = {
 					category: 'Z8 Stylistic Issues',
 					recommended: true,
 				},
-				fixable: 'code',
 				schema: [],
 			},
 			create: function (context) {
@@ -139,3 +207,33 @@ module.exports = {
 		},
 	},
 };
+
+/**
+ * Preferable .eslintrc.js:
+ * 
+ * module.exports = {
+ * 	'plugins': [
+ * 		'z8'
+ * 	],
+ * 	'rules': {
+ * 		'z8/no-spaces-in-control-flow-statements': ['error'],
+ * 		'z8/no-strict-equality': ['error'],
+ * 		'z8/no-double-quotes': ['error'],
+ * 		'z8/no-arrow-functions': ['error'],
+ * 		'z8/no-function-declaration': ['error'],
+ * 		'object-shorthand': ['error', 'never'],
+ * 		'object-curly-spacing': ['error', 'always'],
+ * 		'space-before-blocks': ['error', 'always'],
+ * 		'func-call-spacing': ['error', 'never'],
+ * 		'space-before-function-paren': ['error', 'never'],
+ * 		'semi': ['error', 'always'],
+ * 		'space-infix-ops': ['error'],
+ * 		"indent": ['error', 'tab'],
+ * 		'no-console': ['warn'],
+ * 	},
+ * 	'parserOptions': {
+ * 		'ecmaVersion': 2022
+ * 	}
+ * }
+ * 
+ */

+ 4 - 0
package.json

@@ -6,5 +6,9 @@
   "peerDependencies": {
     "eslint": "8.38.0"
   },
+  "repository": {
+    "type": "git",
+    "url": "https://git.crazydoctor.org/dz/eslint-plugin-z8.git"
+  },
   "author": "Oleg Karataev (CrazyDoctor)"
 }