no-this-in-fetch-data.test.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /**
  2. * @fileoverview disallow `this` in `asyncData/fetch`
  3. * @author Xin Du <clark.duxin@gmail.com>
  4. */
  5. 'use strict'
  6. // ------------------------------------------------------------------------------
  7. // Requirements
  8. // ------------------------------------------------------------------------------
  9. const rule = require('../no-this-in-fetch-data')
  10. const RuleTester = require('eslint').RuleTester
  11. const parserOptions = {
  12. ecmaVersion: 2018,
  13. sourceType: 'module'
  14. }
  15. // ------------------------------------------------------------------------------
  16. // Tests
  17. // ------------------------------------------------------------------------------
  18. const ruleTester = new RuleTester()
  19. ruleTester.run('no-this-in-fetch-data', rule, {
  20. valid: [
  21. {
  22. filename: 'test.vue',
  23. code: `
  24. export default {
  25. ...foo,
  26. asyncData() {
  27. },
  28. fetch() {
  29. }
  30. }
  31. `,
  32. parserOptions
  33. }
  34. ],
  35. invalid: [
  36. {
  37. filename: 'test.vue',
  38. code: `
  39. export default {
  40. ...foo,
  41. asyncData() {
  42. if(this.$route.path === 'foo') {}
  43. }
  44. }
  45. `,
  46. errors: [{
  47. message: 'Unexpected this in asyncData.',
  48. type: 'ThisExpression'
  49. }],
  50. parserOptions
  51. },
  52. {
  53. filename: 'test.vue',
  54. code: `
  55. export default {
  56. ...foo,
  57. async asyncData() {
  58. if(this.$route.path === 'foo') {}
  59. }
  60. }
  61. `,
  62. errors: [{
  63. message: 'Unexpected this in asyncData.',
  64. type: 'ThisExpression'
  65. }],
  66. parserOptions
  67. },
  68. {
  69. filename: 'test.vue',
  70. code: `
  71. export default {
  72. ...foo,
  73. asyncData: () => {
  74. if(this.$route.path === 'foo') {}
  75. }
  76. }
  77. `,
  78. errors: [{
  79. message: 'Unexpected this in asyncData.',
  80. type: 'ThisExpression'
  81. }],
  82. parserOptions
  83. },
  84. {
  85. filename: 'test.vue',
  86. code: `
  87. export default {
  88. ...foo,
  89. asyncData: function test() {
  90. if(this.$route.path === 'foo') {}
  91. }
  92. }
  93. `,
  94. errors: [{
  95. message: 'Unexpected this in asyncData.',
  96. type: 'ThisExpression'
  97. }],
  98. parserOptions
  99. },
  100. {
  101. filename: 'test.vue',
  102. code: `
  103. export default {
  104. ...foo,
  105. fetch() {
  106. if(this.$route.path === 'foo') {}
  107. }
  108. }
  109. `,
  110. errors: [{
  111. message: 'Unexpected this in fetch.',
  112. type: 'ThisExpression'
  113. }],
  114. options: [{ methods: ['fetch'] }],
  115. parserOptions
  116. },
  117. {
  118. filename: 'test.vue',
  119. code: `
  120. export default {
  121. ...foo,
  122. async fetch() {
  123. if(this.$route.path === 'foo') {}
  124. }
  125. }
  126. `,
  127. errors: [{
  128. message: 'Unexpected this in fetch.',
  129. type: 'ThisExpression'
  130. }],
  131. options: [{ methods: ['fetch'] }],
  132. parserOptions
  133. },
  134. {
  135. filename: 'test.vue',
  136. code: `
  137. export default {
  138. ...foo,
  139. fetch: () => {
  140. if(this.$route.path === 'foo') {}
  141. }
  142. }
  143. `,
  144. errors: [{
  145. message: 'Unexpected this in fetch.',
  146. type: 'ThisExpression'
  147. }],
  148. options: [{ methods: ['fetch'] }],
  149. parserOptions
  150. },
  151. {
  152. filename: 'test.vue',
  153. code: `
  154. export default {
  155. ...foo,
  156. fetch: function test() {
  157. if(this.$route.path === 'foo') {}
  158. }
  159. }
  160. `,
  161. errors: [{
  162. message: 'Unexpected this in fetch.',
  163. type: 'ThisExpression'
  164. }],
  165. options: [{ methods: ['fetch'] }],
  166. parserOptions
  167. }
  168. ]
  169. })