Browse Source

plugin init

CrazyDoctor 2 years ago
parent
commit
8cf938d8bc
2 changed files with 151 additions and 0 deletions
  1. 141 0
      index.js
  2. 10 0
      package.json

+ 141 - 0
index.js

@@ -0,0 +1,141 @@
+module.exports = {
+	rules: {
+		'no-spaces-in-conditionals': {
+			meta: {
+				docs: {
+					description: 'Disallow spaces in conditionals',
+					category: 'Z8 Stylistic Issues',
+					recommended: true,
+				},
+				fixable: 'code',
+				schema: [],
+			},
+			create: function (context) {
+				return {
+					IfStatement: function (node) {
+						const tokens = context.getSourceCode().getTokens(node);
+						const firstToken = tokens[0];
+						const secondToken = tokens[1];
+
+						if (
+							firstToken.type === 'Keyword' &&
+							firstToken.value === 'if' &&
+							secondToken.type === 'Punctuator' &&
+							secondToken.value === '(' &&
+							secondToken.range[0] - firstToken.range[1] > 0
+						) {
+							context.report({
+								node: node,
+								message: 'Do not use spaces between \'if\' and its condition',
+							});
+						}
+					},
+				};
+			},
+		},
+		'no-strict-equality': {
+			meta: {
+				docs: {
+					description: 'Disallow strict equality operator',
+					category: 'Z8 Stylistic Issues',
+					recommended: true,
+				},
+				fixable: 'code',
+				schema: [],
+			},
+			create: function (context) {
+				return {
+					BinaryExpression: function (node) {
+						if (node.operator === '===') {
+							context.report({
+								node: node,
+								message: 'Avoid using strict equality operator \'===\' (use loose equality operator \'==\' instead)',
+							});
+						}
+					},
+				};
+			},
+		},
+		'no-double-quotes': {
+			meta: {
+				docs: {
+					description: 'Disallow double quotes',
+					category: 'Z8 Stylistic Issues',
+					recommended: true,
+				},
+				fixable: 'code',
+				schema: [],
+			},
+			create: function (context) {
+				return {
+					Literal: function (node) {
+						if (typeof node.value === 'string' && node.raw.includes('"')) {
+							context.report({
+								node: node,
+								message: 'Avoid using double quotes (use single quotes instead)',
+							});
+						}
+					},
+				};
+			},
+		},
+		'no-arrow-functions': {
+			meta: {
+				docs: {
+					description: 'Disallow arrow functions',
+					category: 'Z8 Stylistic Issues',
+					recommended: true,
+				},
+				fixable: 'code',
+				schema: [],
+			},
+			create: function (context) {
+				return {
+					ArrowFunctionExpression: function (node) {
+						context.report({
+							node: node,
+							message: 'Avoid using arrow functions',
+						});
+					},
+				};
+			},
+		},
+		'no-function-declaration': {
+			meta: {
+				docs: {
+					description: 'Disallow function declaration',
+					category: 'Z8 Stylistic Issues',
+					recommended: true,
+				},
+				fixable: 'code',
+				schema: [],
+			},
+			create: function (context) {
+				return {
+					FunctionDeclaration: function (node) {
+						context.report({
+							node: node,
+							message: 'Use function expression instead of function declaration',
+						});
+					},
+					VariableDeclarator: function (node) {
+						if (
+							node.init &&
+							node.init.type === 'FunctionExpression' &&
+							node.init.id &&
+							node.init.id.type === 'Identifier' &&
+							node.id &&
+							node.id.type === 'Identifier' &&
+							node.init.id.name === node.id.name
+						) {
+							context.report({
+								node: node,
+								message: 'Use function expression instead of function declaration',
+							});
+						}
+					},
+				};
+			},
+		},
+	},
+};

+ 10 - 0
package.json

@@ -0,0 +1,10 @@
+{
+  "name": "eslint-plugin-z8",
+  "version": "1.0.0",
+  "private": true,
+  "main": "index.js",
+  "peerDependencies": {
+    "eslint": "8.38.0"
+  },
+  "author": "Oleg Karataev (CrazyDoctor)"
+}