no-this-in-sfc.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /**
  2. * @fileoverview Report "this" being used in stateless functional components.
  3. */
  4. 'use strict';
  5. // ------------------------------------------------------------------------------
  6. // Constants
  7. // ------------------------------------------------------------------------------
  8. const ERROR_MESSAGE = 'Stateless functional components should not use this';
  9. // ------------------------------------------------------------------------------
  10. // Requirements
  11. // ------------------------------------------------------------------------------
  12. const rule = require('../../../lib/rules/no-this-in-sfc');
  13. const RuleTester = require('eslint').RuleTester;
  14. const parserOptions = {
  15. ecmaVersion: 2018,
  16. sourceType: 'module',
  17. ecmaFeatures: {
  18. jsx: true
  19. }
  20. };
  21. const ruleTester = new RuleTester({parserOptions});
  22. ruleTester.run('no-this-in-sfc', rule, {
  23. valid: [{
  24. code: `
  25. function Foo(props) {
  26. const { foo } = props;
  27. return <div bar={foo} />;
  28. }`
  29. }, {
  30. code: `
  31. function Foo({ foo }) {
  32. return <div bar={foo} />;
  33. }`
  34. }, {
  35. code: `
  36. class Foo extends React.Component {
  37. render() {
  38. const { foo } = this.props;
  39. return <div bar={foo} />;
  40. }
  41. }`
  42. }, {
  43. code: `
  44. const Foo = createReactClass({
  45. render: function() {
  46. return <div>{this.props.foo}</div>;
  47. }
  48. });`
  49. }, {
  50. code: `
  51. const Foo = React.createClass({
  52. render: function() {
  53. return <div>{this.props.foo}</div>;
  54. }
  55. });`,
  56. settings: {react: {createClass: 'createClass'}}
  57. }, {
  58. code: `
  59. function foo(bar) {
  60. this.bar = bar;
  61. this.props = 'baz';
  62. this.getFoo = function() {
  63. return this.bar + this.props;
  64. }
  65. }
  66. `
  67. }, {
  68. code: `
  69. function Foo(props) {
  70. return props.foo ? <span>{props.bar}</span> : null;
  71. }`
  72. }, {
  73. code: `
  74. function Foo(props) {
  75. if (props.foo) {
  76. return <div>{props.bar}</div>;
  77. }
  78. return null;
  79. }`
  80. }, {
  81. code: `
  82. function Foo(props) {
  83. if (props.foo) {
  84. something();
  85. }
  86. return null;
  87. }`
  88. }, {
  89. code: 'const Foo = (props) => <span>{props.foo}</span>'
  90. }, {
  91. code: 'const Foo = ({ foo }) => <span>{foo}</span>'
  92. }, {
  93. code: 'const Foo = (props) => props.foo ? <span>{props.bar}</span> : null;'
  94. }, {
  95. code: 'const Foo = ({ foo, bar }) => foo ? <span>{bar}</span> : null;'
  96. }],
  97. invalid: [{
  98. code: `
  99. function Foo(props) {
  100. const { foo } = this.props;
  101. return <div>{foo}</div>;
  102. }`,
  103. errors: [{message: ERROR_MESSAGE}]
  104. }, {
  105. code: `
  106. function Foo(props) {
  107. return <div>{this.props.foo}</div>;
  108. }`,
  109. errors: [{message: ERROR_MESSAGE}]
  110. }, {
  111. code: `
  112. function Foo(props) {
  113. return <div>{this.state.foo}</div>;
  114. }`,
  115. errors: [{message: ERROR_MESSAGE}]
  116. }, {
  117. code: `
  118. function Foo(props) {
  119. const { foo } = this.state;
  120. return <div>{foo}</div>;
  121. }`,
  122. errors: [{message: ERROR_MESSAGE}]
  123. }, {
  124. code: `
  125. function Foo(props) {
  126. return props.foo ? <div>{this.props.bar}</div> : null;
  127. }`,
  128. errors: [{message: ERROR_MESSAGE}]
  129. }, {
  130. code: `
  131. function Foo(props) {
  132. if (props.foo) {
  133. return <div>{this.props.bar}</div>;
  134. }
  135. return null;
  136. }`,
  137. errors: [{message: ERROR_MESSAGE}]
  138. }, {
  139. code: `
  140. function Foo(props) {
  141. if (this.props.foo) {
  142. something();
  143. }
  144. return null;
  145. }`,
  146. errors: [{message: ERROR_MESSAGE}]
  147. }, {
  148. code: 'const Foo = (props) => <span>{this.props.foo}</span>',
  149. errors: [{message: ERROR_MESSAGE}]
  150. }, {
  151. code: 'const Foo = (props) => this.props.foo ? <span>{props.bar}</span> : null;',
  152. errors: [{message: ERROR_MESSAGE}]
  153. }, {
  154. code: `
  155. function Foo(props) {
  156. function onClick(bar) {
  157. this.props.onClick();
  158. }
  159. return <div onClick={onClick}>{this.props.foo}</div>;
  160. }`,
  161. errors: [{message: ERROR_MESSAGE}, {message: ERROR_MESSAGE}]
  162. }]
  163. });