style-prop-object.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * @fileoverview Enforce style prop value is an object
  3. * @author David Petersen
  4. */
  5. 'use strict';
  6. // ------------------------------------------------------------------------------
  7. // Requirements
  8. // ------------------------------------------------------------------------------
  9. const rule = require('../../../lib/rules/style-prop-object');
  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('style-prop-object', rule, {
  23. valid: [
  24. {
  25. code: '<div style={{ color: "red" }} />'
  26. },
  27. {
  28. code: '<Hello style={{ color: "red" }} />'
  29. },
  30. {
  31. code: [
  32. 'function redDiv() {',
  33. ' const styles = { color: "red" };',
  34. ' return <div style={styles} />;',
  35. '}'
  36. ].join('\n')
  37. },
  38. {
  39. code: [
  40. 'function redDiv() {',
  41. ' const styles = { color: "red" };',
  42. ' return <Hello style={styles} />;',
  43. '}'
  44. ].join('\n')
  45. },
  46. {
  47. code: [
  48. 'const styles = { color: "red" };',
  49. 'function redDiv() {',
  50. ' return <div style={styles} />;',
  51. '}'
  52. ].join('\n')
  53. },
  54. {
  55. code: [
  56. 'function redDiv(props) {',
  57. ' return <div style={props.styles} />;',
  58. '}'
  59. ].join('\n')
  60. },
  61. {
  62. code: [
  63. 'import styles from \'./styles\';',
  64. 'function redDiv() {',
  65. ' return <div style={styles} />;',
  66. '}'
  67. ].join('\n')
  68. },
  69. {
  70. code: [
  71. 'import mystyles from \'./styles\';',
  72. 'const styles = Object.assign({ color: \'red\' }, mystyles);',
  73. 'function redDiv() {',
  74. ' return <div style={styles} />;',
  75. '}'
  76. ].join('\n'),
  77. parserOptions: {
  78. ecmaVersion: 2018,
  79. sourceType: 'module',
  80. ecmaFeatures: {
  81. jsx: true
  82. }
  83. }
  84. },
  85. {
  86. code: [
  87. 'const otherProps = { style: { color: "red" } };',
  88. 'const { a, b, ...props } = otherProps;',
  89. '<div {...props} />'
  90. ].join('\n')
  91. },
  92. {
  93. code: [
  94. 'const styles = Object.assign({ color: \'red\' }, mystyles);',
  95. 'React.createElement("div", { style: styles });'
  96. ].join('\n'),
  97. parserOptions: Object.assign({sourceType: 'module'}, parserOptions)
  98. },
  99. {
  100. code: '<div style></div>'
  101. },
  102. {
  103. code: [
  104. 'React.createElement(MyCustomElem, {',
  105. ' [style]: true',
  106. '}, \'My custom Elem\')'
  107. ].join('\n')
  108. },
  109. {
  110. code: [
  111. 'let style;',
  112. '<div style={style}></div>'
  113. ].join('\n')
  114. },
  115. {
  116. code: [
  117. 'let style = null;',
  118. '<div style={style}></div>'
  119. ].join('\n')
  120. },
  121. {
  122. code: [
  123. 'let style = undefined;',
  124. '<div style={style}></div>'
  125. ].join('\n')
  126. },
  127. {
  128. code: '<div style={undefined}></div>'
  129. },
  130. {
  131. code: [
  132. 'const props = { style: undefined };',
  133. '<div {...props} />'
  134. ].join('\n')
  135. },
  136. {
  137. code: [
  138. 'const otherProps = { style: undefined };',
  139. 'const { a, b, ...props } = otherProps;',
  140. '<div {...props} />'
  141. ].join('\n')
  142. },
  143. {
  144. code: [
  145. 'React.createElement("div", {',
  146. ' style: undefined',
  147. '})'
  148. ].join('\n')
  149. },
  150. {
  151. code: [
  152. 'let style;',
  153. 'React.createElement("div", {',
  154. ' style',
  155. '})'
  156. ].join('\n')
  157. },
  158. {
  159. code: '<div style={null}></div>'
  160. },
  161. {
  162. code: [
  163. 'const props = { style: null };',
  164. '<div {...props} />'
  165. ].join('\n')
  166. },
  167. {
  168. code: [
  169. 'const otherProps = { style: null };',
  170. 'const { a, b, ...props } = otherProps;',
  171. '<div {...props} />'
  172. ].join('\n')
  173. },
  174. {
  175. code: [
  176. 'React.createElement("div", {',
  177. ' style: null',
  178. '})'
  179. ].join('\n')
  180. },
  181. {
  182. code: [
  183. 'const MyComponent = (props) => {',
  184. ' React.createElement(MyCustomElem, {',
  185. ' ...props',
  186. ' });',
  187. '};'
  188. ].join('\n')
  189. }
  190. ],
  191. invalid: [
  192. {
  193. code: '<div style="color: \'red\'" />',
  194. errors: [{
  195. message: 'Style prop value must be an object',
  196. line: 1,
  197. column: 6,
  198. type: 'JSXAttribute'
  199. }]
  200. },
  201. {
  202. code: '<Hello style="color: \'red\'" />',
  203. errors: [{
  204. message: 'Style prop value must be an object',
  205. line: 1,
  206. column: 8,
  207. type: 'JSXAttribute'
  208. }]
  209. },
  210. {
  211. code: '<div style={true} />',
  212. errors: [{
  213. message: 'Style prop value must be an object',
  214. line: 1,
  215. column: 6,
  216. type: 'JSXAttribute'
  217. }]
  218. },
  219. {
  220. code: [
  221. 'const styles = \'color: "red"\';',
  222. 'function redDiv2() {',
  223. ' return <div style={styles} />;',
  224. '}'
  225. ].join('\n'),
  226. errors: [{
  227. message: 'Style prop value must be an object',
  228. line: 3,
  229. column: 22,
  230. type: 'Identifier'
  231. }]
  232. },
  233. {
  234. code: [
  235. 'const styles = \'color: "red"\';',
  236. 'function redDiv2() {',
  237. ' return <Hello style={styles} />;',
  238. '}'
  239. ].join('\n'),
  240. errors: [{
  241. message: 'Style prop value must be an object',
  242. line: 3,
  243. column: 24,
  244. type: 'Identifier'
  245. }]
  246. },
  247. {
  248. code: [
  249. 'const styles = true;',
  250. 'function redDiv() {',
  251. ' return <div style={styles} />;',
  252. '}'
  253. ].join('\n'),
  254. errors: [{
  255. message: 'Style prop value must be an object',
  256. line: 3,
  257. column: 22,
  258. type: 'Identifier'
  259. }]
  260. }
  261. ]
  262. });