index.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. module.exports = {
  2. rules: {
  3. 'no-spaces-in-conditionals': {
  4. meta: {
  5. docs: {
  6. description: 'Disallow spaces in conditionals',
  7. category: 'Z8 Stylistic Issues',
  8. recommended: true,
  9. },
  10. fixable: 'code',
  11. schema: [],
  12. },
  13. create: function (context) {
  14. return {
  15. IfStatement: function (node) {
  16. const tokens = context.getSourceCode().getTokens(node);
  17. const firstToken = tokens[0];
  18. const secondToken = tokens[1];
  19. if (
  20. 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. });
  30. }
  31. },
  32. };
  33. },
  34. },
  35. 'no-strict-equality': {
  36. meta: {
  37. docs: {
  38. description: 'Disallow strict equality operator',
  39. category: 'Z8 Stylistic Issues',
  40. recommended: true,
  41. },
  42. fixable: 'code',
  43. schema: [],
  44. },
  45. create: function (context) {
  46. return {
  47. BinaryExpression: function (node) {
  48. if (node.operator === '===') {
  49. context.report({
  50. node: node,
  51. message: 'Avoid using strict equality operator \'===\' (use loose equality operator \'==\' instead)',
  52. });
  53. }
  54. },
  55. };
  56. },
  57. },
  58. 'no-double-quotes': {
  59. meta: {
  60. docs: {
  61. description: 'Disallow double quotes',
  62. category: 'Z8 Stylistic Issues',
  63. recommended: true,
  64. },
  65. fixable: 'code',
  66. schema: [],
  67. },
  68. create: function (context) {
  69. return {
  70. Literal: function (node) {
  71. if (typeof node.value === 'string' && node.raw.includes('"')) {
  72. context.report({
  73. node: node,
  74. message: 'Avoid using double quotes (use single quotes instead)',
  75. });
  76. }
  77. },
  78. };
  79. },
  80. },
  81. 'no-arrow-functions': {
  82. meta: {
  83. docs: {
  84. description: 'Disallow arrow functions',
  85. category: 'Z8 Stylistic Issues',
  86. recommended: true,
  87. },
  88. fixable: 'code',
  89. schema: [],
  90. },
  91. create: function (context) {
  92. return {
  93. ArrowFunctionExpression: function (node) {
  94. context.report({
  95. node: node,
  96. message: 'Avoid using arrow functions',
  97. });
  98. },
  99. };
  100. },
  101. },
  102. 'no-function-declaration': {
  103. meta: {
  104. docs: {
  105. description: 'Disallow function declaration',
  106. category: 'Z8 Stylistic Issues',
  107. recommended: true,
  108. },
  109. fixable: 'code',
  110. schema: [],
  111. },
  112. create: function (context) {
  113. return {
  114. FunctionDeclaration: function (node) {
  115. context.report({
  116. node: node,
  117. message: 'Use function expression instead of function declaration',
  118. });
  119. },
  120. VariableDeclarator: function (node) {
  121. if (
  122. node.init &&
  123. node.init.type === 'FunctionExpression' &&
  124. node.init.id &&
  125. node.init.id.type === 'Identifier' &&
  126. node.id &&
  127. node.id.type === 'Identifier' &&
  128. node.init.id.name === node.id.name
  129. ) {
  130. context.report({
  131. node: node,
  132. message: 'Use function expression instead of function declaration',
  133. });
  134. }
  135. },
  136. };
  137. },
  138. },
  139. },
  140. };