jsx-indent-props.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /**
  2. * @fileoverview Validate props indentation in JSX
  3. * @author Yannick Croissant
  4. */
  5. 'use strict';
  6. // ------------------------------------------------------------------------------
  7. // Requirements
  8. // ------------------------------------------------------------------------------
  9. const rule = require('../../../lib/rules/jsx-indent-props');
  10. const RuleTester = require('eslint').RuleTester;
  11. const parserOptions = {
  12. ecmaVersion: 2018,
  13. sourceType: 'module',
  14. ecmaFeatures: {
  15. jsx: true
  16. }
  17. };
  18. // ------------------------------------------------------------------------------
  19. // Tests
  20. // ------------------------------------------------------------------------------
  21. const ruleTester = new RuleTester({parserOptions});
  22. ruleTester.run('jsx-indent-props', rule, {
  23. valid: [{
  24. code: [
  25. '<App foo',
  26. '/>'
  27. ].join('\n')
  28. }, {
  29. code: [
  30. '<App',
  31. ' foo',
  32. '/>'
  33. ].join('\n'),
  34. options: [2]
  35. }, {
  36. code: [
  37. '<App',
  38. 'foo',
  39. '/>'
  40. ].join('\n'),
  41. options: [0]
  42. }, {
  43. code: [
  44. ' <App',
  45. 'foo',
  46. ' />'
  47. ].join('\n'),
  48. options: [-2]
  49. }, {
  50. code: [
  51. '<App',
  52. '\tfoo',
  53. '/>'
  54. ].join('\n'),
  55. options: ['tab']
  56. }, {
  57. code: [
  58. '<App/>'
  59. ].join('\n'),
  60. options: ['first']
  61. }, {
  62. code: [
  63. '<App aaa',
  64. ' b',
  65. ' cc',
  66. '/>'
  67. ].join('\n'),
  68. options: ['first']
  69. }, {
  70. code: [
  71. '<App aaa',
  72. ' b',
  73. ' cc',
  74. '/>'
  75. ].join('\n'),
  76. options: ['first']
  77. }, {
  78. code: [
  79. 'const test = <App aaa',
  80. ' b',
  81. ' cc',
  82. ' />'
  83. ].join('\n'),
  84. options: ['first']
  85. }, {
  86. code: [
  87. '<App aaa x',
  88. ' b y',
  89. ' cc',
  90. '/>'
  91. ].join('\n'),
  92. options: ['first']
  93. }, {
  94. code: [
  95. 'const test = <App aaa x',
  96. ' b y',
  97. ' cc',
  98. ' />'
  99. ].join('\n'),
  100. options: ['first']
  101. }, {
  102. code: [
  103. '<App aaa',
  104. ' b',
  105. '>',
  106. ' <Child c',
  107. ' d/>',
  108. '</App>'
  109. ].join('\n'),
  110. options: ['first']
  111. }, {
  112. code: [
  113. '<Fragment>',
  114. ' <App aaa',
  115. ' b',
  116. ' cc',
  117. ' />',
  118. ' <OtherApp a',
  119. ' bbb',
  120. ' c',
  121. ' />',
  122. '</Fragment>'
  123. ].join('\n'),
  124. options: ['first']
  125. }, {
  126. code: [
  127. '<App',
  128. ' a',
  129. ' b',
  130. '/>'
  131. ].join('\n'),
  132. options: ['first']
  133. }],
  134. invalid: [{
  135. code: [
  136. '<App',
  137. ' foo',
  138. '/>'
  139. ].join('\n'),
  140. output: [
  141. '<App',
  142. ' foo',
  143. '/>'
  144. ].join('\n'),
  145. errors: [{message: 'Expected indentation of 4 space characters but found 2.'}]
  146. }, {
  147. code: [
  148. '<App',
  149. ' foo',
  150. '/>'
  151. ].join('\n'),
  152. output: [
  153. '<App',
  154. ' foo',
  155. '/>'
  156. ].join('\n'),
  157. options: [2],
  158. errors: [{message: 'Expected indentation of 2 space characters but found 4.'}]
  159. }, {
  160. code: [
  161. '<App',
  162. ' foo',
  163. '/>'
  164. ].join('\n'),
  165. output: [
  166. '<App',
  167. '\tfoo',
  168. '/>'
  169. ].join('\n'),
  170. options: ['tab'],
  171. errors: [{message: 'Expected indentation of 1 tab character but found 0.'}]
  172. }, {
  173. code: [
  174. '<App',
  175. '\t\t\tfoo',
  176. '/>'
  177. ].join('\n'),
  178. output: [
  179. '<App',
  180. '\tfoo',
  181. '/>'
  182. ].join('\n'),
  183. options: ['tab'],
  184. errors: [{message: 'Expected indentation of 1 tab character but found 3.'}]
  185. }, {
  186. code: [
  187. '<App a',
  188. ' b',
  189. '/>'
  190. ].join('\n'),
  191. output: [
  192. '<App a',
  193. ' b',
  194. '/>'
  195. ].join('\n'),
  196. options: ['first'],
  197. errors: [{message: 'Expected indentation of 5 space characters but found 2.'}]
  198. }, {
  199. code: [
  200. '<App a',
  201. ' b',
  202. '/>'
  203. ].join('\n'),
  204. output: [
  205. '<App a',
  206. ' b',
  207. '/>'
  208. ].join('\n'),
  209. options: ['first'],
  210. errors: [{message: 'Expected indentation of 6 space characters but found 3.'}]
  211. }, {
  212. code: [
  213. '<App',
  214. ' a',
  215. ' b',
  216. '/>'
  217. ].join('\n'),
  218. output: [
  219. '<App',
  220. ' a',
  221. ' b',
  222. '/>'
  223. ].join('\n'),
  224. options: ['first'],
  225. errors: [{message: 'Expected indentation of 6 space characters but found 3.'}]
  226. }, {
  227. code: [
  228. '<App',
  229. ' a',
  230. ' b',
  231. ' c',
  232. '/>'
  233. ].join('\n'),
  234. output: [
  235. '<App',
  236. ' a',
  237. ' b',
  238. ' c',
  239. '/>'
  240. ].join('\n'),
  241. options: ['first'],
  242. errors: [
  243. {message: 'Expected indentation of 2 space characters but found 1.'},
  244. {message: 'Expected indentation of 2 space characters but found 3.'}
  245. ]
  246. }]
  247. });