Browse Source

whitespace-only-lines

CrazyDoctor 2 years ago
parent
commit
b0cb1b190b
1 changed files with 49 additions and 9 deletions
  1. 49 9
      index.js

+ 49 - 9
index.js

@@ -205,7 +205,7 @@ module.exports = {
 				};
 			},
 		},
-		'todo-comments': {
+		'todo': {
 			meta: {
 				type: 'suggestion',
 				docs: {
@@ -218,16 +218,55 @@ module.exports = {
 			create: function (context) {
 				const sourceCode = context.getSourceCode();
 				const comments = sourceCode.getAllComments();
-				for (const comment of comments) {
-					if (comment.value.toLowerCase().includes('todo')) {
-						context.report({
-							node: comment,
-							message: comment.value
+
+				return {
+					Program: function(node) {
+						comments.forEach(comment => {
+							if (comment.value.toLowerCase().includes('todo')) {
+								context.report({
+									node,
+									loc: comment.loc,
+									message: comment.value.trim(),
+								});
+							}
 						});
-					}
-				}
+					},
+				};
 			}
 		},
+		'whitespace-only-lines': {
+			meta: {
+				type: 'suggestion',
+				docs: {
+					description: 'Disallow empty lines with only whitespace',
+					category: 'Z8 Stylistic Issues',
+					recommended: true
+				},
+				fixable: 'code',
+				schema: []
+			},
+			create: function (context) {
+				const sourceCode = context.getSourceCode();
+
+				return {
+					Program: function (node) {
+						const lines = sourceCode.lines;
+						lines.forEach((line, index) => {
+							if (/^\s+$/.test(line)) {
+								context.report({
+									node: node,
+									loc: {line: index + 1, column: 0},
+									message: 'Empty line with only whitespace',
+									fix: function(fixer) {
+										return fixer.replaceTextRange([sourceCode.getIndexFromLoc({line: index, column: 0}), sourceCode.getIndexFromLoc({line: index, column: line.length})], '');
+									}
+								});
+							}
+						});
+					}
+				};
+			}
+		}
 	},
 };
 
@@ -253,7 +292,8 @@ module.exports = {
  * 		'space-infix-ops': ['error'],
  * 		'indent': ['error', 'tab'],
  * 		'no-console': ['warn'],
- *		'z8/todo-comments': ['warn']
+ *		'z8/todo': ['warn'],
+ *		'z8/whitespace-only-lines': ['error']
  * 	},
  * 	'parserOptions': {
  * 		'ecmaVersion': 2022