index.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. module.exports = {
  2. rules: {
  3. // auto fixable
  4. 'no-spaces-in-control-flow-statements': {
  5. meta: {
  6. docs: {
  7. description: 'Disallow spaces in control flow statements',
  8. category: 'Z8 Stylistic Issues',
  9. recommended: true,
  10. },
  11. fixable: 'code',
  12. schema: [],
  13. },
  14. create: function (context) {
  15. return {
  16. IfStatement: function (node) {
  17. const tokens = context.getSourceCode().getTokens(node);
  18. const firstToken = tokens[0];
  19. const secondToken = tokens[1];
  20. if (firstToken.type === 'Keyword' &&
  21. firstToken.value === 'if' &&
  22. secondToken.type === 'Punctuator' &&
  23. secondToken.value === '(' &&
  24. secondToken.range[0] - firstToken.range[1] > 0
  25. ) {
  26. context.report({
  27. node: node,
  28. message: 'Do not use spaces between \'if\' and its condition',
  29. fix: function(fixer) {
  30. return fixer.replaceTextRange([firstToken.range[1], secondToken.range[0]], '');
  31. }
  32. });
  33. }
  34. },
  35. ForStatement: function (node) {
  36. const tokens = context.getSourceCode().getTokens(node);
  37. const firstToken = tokens[0];
  38. const secondToken = tokens[1];
  39. if (
  40. firstToken.type === 'Keyword' &&
  41. firstToken.value === 'for' &&
  42. secondToken.type === 'Punctuator' &&
  43. secondToken.value === '(' &&
  44. secondToken.range[0] - firstToken.range[1] > 0
  45. ) {
  46. context.report({
  47. node: node,
  48. message: 'Do not use spaces between \'for\' and its condition',
  49. fix: function(fixer) {
  50. return fixer.replaceTextRange([firstToken.range[1], secondToken.range[0]], '');
  51. }
  52. });
  53. }
  54. },
  55. WhileStatement: function (node) {
  56. const tokens = context.getSourceCode().getTokens(node);
  57. const firstToken = tokens[0];
  58. const secondToken = tokens[1];
  59. if (
  60. firstToken.type === 'Keyword' &&
  61. firstToken.value === 'while' &&
  62. secondToken.type === 'Punctuator' &&
  63. secondToken.value === '(' &&
  64. secondToken.range[0] - firstToken.range[1] > 0
  65. ) {
  66. context.report({
  67. node: node,
  68. message: 'Do not use spaces between \'while\' and its condition',
  69. fix: function(fixer) {
  70. return fixer.replaceTextRange([firstToken.range[1], secondToken.range[0]], '');
  71. }
  72. });
  73. }
  74. },
  75. SwitchStatement: function (node) {
  76. const tokens = context.getSourceCode().getTokens(node);
  77. const firstToken = tokens[0];
  78. const secondToken = tokens[1];
  79. if (
  80. firstToken.type === 'Keyword' &&
  81. firstToken.value === 'switch' &&
  82. secondToken.type === 'Punctuator' &&
  83. secondToken.value === '(' &&
  84. secondToken.range[0] - firstToken.range[1] > 0
  85. ) {
  86. context.report({
  87. node: node,
  88. message: 'Do not use spaces between \'switch\' and its expression',
  89. fix: function(fixer) {
  90. return fixer.replaceTextRange([firstToken.range[1], secondToken.range[0]], '');
  91. }
  92. });
  93. }
  94. },
  95. };
  96. },
  97. },
  98. 'no-strict-equality': {
  99. meta: {
  100. docs: {
  101. description: 'Disallow strict equality operator',
  102. category: 'Z8 Stylistic Issues',
  103. recommended: true,
  104. },
  105. fixable: 'code',
  106. schema: [],
  107. },
  108. create: function (context) {
  109. return {
  110. BinaryExpression: function (node) {
  111. if (
  112. (node.operator === '===' || node.operator === '!==') && (
  113. (node.right.type === 'Literal' && node.left.type === 'Literal') ||
  114. (node.right.type === 'Literal' && ![true, false].includes(node.right.value)) ||
  115. (node.left.type === 'Literal' && ![true, false].includes(node.left.value)) ||
  116. (node.right.type === 'Identifier' && node.left.type === 'Identifier' && !(node.right.name === "undefined" || node.left.name === "undefined"))
  117. )
  118. ) {
  119. context.report({
  120. node: node,
  121. message: 'Avoid using strict equality operator \''+node.operator+'\' (use loose equality operator \''+node.operator.substring(0, 2)+'\' instead)',
  122. });
  123. }
  124. },
  125. };
  126. },
  127. },
  128. // auto fixable
  129. 'no-double-quotes': {
  130. meta: {
  131. docs: {
  132. description: 'Disallow double quotes',
  133. category: 'Z8 Stylistic Issues',
  134. recommended: true,
  135. },
  136. fixable: 'code',
  137. schema: [],
  138. },
  139. create: function (context) {
  140. return {
  141. Literal: function (node) {
  142. if (typeof node.value === 'string' && node.raw[0] === '"') {
  143. context.report({
  144. node: node,
  145. message: 'Avoid using double quotes (use single quotes instead)',
  146. fix: function(fixer) {
  147. return fixer.replaceTextRange([node.start, node.end], "'" + node.value.replace(/'/g, '\\\'') + "'");
  148. }
  149. });
  150. }
  151. },
  152. };
  153. },
  154. },
  155. 'no-arrow-functions': {
  156. meta: {
  157. docs: {
  158. description: 'Disallow arrow functions',
  159. category: 'Z8 Stylistic Issues',
  160. recommended: true,
  161. },
  162. schema: [],
  163. },
  164. create: function (context) {
  165. return {
  166. ArrowFunctionExpression: function (node) {
  167. context.report({
  168. node: node,
  169. message: 'Avoid using arrow functions',
  170. });
  171. },
  172. };
  173. },
  174. },
  175. 'no-function-declaration': {
  176. meta: {
  177. docs: {
  178. description: 'Disallow function declaration',
  179. category: 'Z8 Stylistic Issues',
  180. recommended: true,
  181. },
  182. schema: [],
  183. },
  184. create: function (context) {
  185. return {
  186. FunctionDeclaration: function (node) {
  187. context.report({
  188. node: node,
  189. message: 'Use function expression instead of function declaration',
  190. });
  191. },
  192. VariableDeclarator: function (node) {
  193. if (
  194. node.init &&
  195. node.init.type === 'FunctionExpression' &&
  196. node.init.id &&
  197. node.init.id.type === 'Identifier' &&
  198. node.id &&
  199. node.id.type === 'Identifier' &&
  200. node.init.id.name === node.id.name
  201. ) {
  202. context.report({
  203. node: node,
  204. message: 'Use function expression instead of function declaration',
  205. });
  206. }
  207. },
  208. };
  209. },
  210. },
  211. 'todo': {
  212. meta: {
  213. type: 'suggestion',
  214. docs: {
  215. description: 'TODO tracking',
  216. category: 'Other',
  217. recommended: true
  218. },
  219. schema: []
  220. },
  221. create: function (context) {
  222. const sourceCode = context.getSourceCode();
  223. const comments = sourceCode.getAllComments();
  224. return {
  225. Program: function(node) {
  226. comments.forEach(comment => {
  227. if (comment.value.toLowerCase().includes('todo')) {
  228. context.report({
  229. node,
  230. loc: comment.loc,
  231. message: comment.value.trim(),
  232. });
  233. }
  234. });
  235. },
  236. };
  237. }
  238. },
  239. // auto fixable
  240. 'whitespace-only-lines': {
  241. meta: {
  242. type: 'suggestion',
  243. docs: {
  244. description: 'Disallow empty lines with only whitespace',
  245. category: 'Z8 Stylistic Issues',
  246. recommended: true
  247. },
  248. fixable: 'code',
  249. schema: []
  250. },
  251. create: function (context) {
  252. const sourceCode = context.getSourceCode();
  253. return {
  254. Program: function (node) {
  255. const lines = sourceCode.lines;
  256. lines.forEach((line, index) => {
  257. if (/^\s+$/.test(line)) {
  258. context.report({
  259. node: node,
  260. loc: {line: index + 1, column: 0},
  261. message: 'Empty line with only whitespace',
  262. fix: function(fixer) {
  263. return fixer.replaceTextRange([sourceCode.getIndexFromLoc({line: index + 1, column: 0}), sourceCode.getIndexFromLoc({line: index + 1, column: line.length})], '');
  264. }
  265. });
  266. }
  267. });
  268. }
  269. };
  270. }
  271. },
  272. // auto fixable
  273. 'prefer-var': {
  274. meta: {
  275. docs: {
  276. description: 'Disallow let/const usage',
  277. category: 'Z8 Stylistic Issues',
  278. recommended: true,
  279. },
  280. fixable: 'code',
  281. schema: [],
  282. },
  283. create: function(context) {
  284. return {
  285. VariableDeclaration(node) {
  286. if (node.kind !== 'var') {
  287. context.report({
  288. node: node,
  289. message: 'Prefer using `var` instead of `let` or `const`.',
  290. fix: function(fixer) {
  291. return fixer.replaceTextRange([node.start, node.start + node.kind.length], 'var');
  292. }
  293. });
  294. }
  295. }
  296. };
  297. }
  298. }
  299. },
  300. };
  301. /**
  302. * Preferable .eslintrc.js:
  303. *
  304. * module.exports = {
  305. * 'plugins': [
  306. * 'z8'
  307. * ],
  308. * 'rules': {
  309. * 'z8/no-spaces-in-control-flow-statements': ['error'],
  310. * 'z8/no-strict-equality': ['error'],
  311. * 'z8/no-double-quotes': ['error'],
  312. * 'z8/no-arrow-functions': ['error'],
  313. * 'z8/no-function-declaration': ['error'],
  314. * 'object-shorthand': ['error', 'never'],
  315. * 'object-curly-spacing': ['error', 'always'],
  316. * 'space-before-blocks': ['error', 'always'],
  317. * 'func-call-spacing': ['error', 'never'],
  318. * 'space-before-function-paren': ['error', 'never'],
  319. * 'semi': ['error', 'always'],
  320. * 'space-infix-ops': ['error'],
  321. * 'indent': ['error', 'tab'],
  322. * 'no-console': ['warn'],
  323. * 'z8/todo': ['warn'],
  324. * 'z8/whitespace-only-lines': ['error']
  325. * },
  326. * 'parserOptions': {
  327. * 'ecmaVersion': 2022,
  328. * 'sourceType': 'module'
  329. * }
  330. * }
  331. *
  332. */