rules.test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. const { RuleTester } = require('eslint')
  2. const noThrowInCallback = require('./no-throw-in-callback')
  3. const preferKebabUrl = require('./prefer-kebab-url')
  4. const noUnnecessaryTrans = require('./no-unnecessary-trans')
  5. const shouldUnescapeTrans = require('./should-unescape-trans')
  6. const noGeneratedEditorThemes = require('./no-generated-editor-themes')
  7. const viDoMockValidPath = require('./require-vi-doMock-valid-path')
  8. const requireCioSnakeCaseProperties = require('./require-cio-snake-case-properties')
  9. const ruleTester = new RuleTester({
  10. parser: require.resolve('@typescript-eslint/parser'),
  11. parserOptions: {
  12. ecmaVersion: 'latest',
  13. ecmaFeatures: { jsx: true },
  14. },
  15. })
  16. ruleTester.run('prefer-kebab-url', preferKebabUrl, {
  17. valid: [
  18. { code: `app.get('/foo-bar')` },
  19. { code: `app.get('/foo-bar/:id')` },
  20. { code: `router.post('/foo-bar')` },
  21. { code: `router.get('/foo-bar/:id/:name/:age')` },
  22. { code: `webRouter.get('/foo-bar/:user_id/(ProjectName)/get-info')` },
  23. { code: `webApp.post('/foo-bar/:user_id/(ProjectName)/get-info')` },
  24. {
  25. code: `router.get(/^\\/download\\/project\\/([^/]*)\\/output\\/output\\.pdf$/)`,
  26. },
  27. {
  28. code: `webRouter.get(/^\\/project\\/([^/]*)\\/user\\/([0-9a-f]+)\\/build\\/([0-9a-f-]+)\\/output\\/(.*)$/)`,
  29. },
  30. ],
  31. invalid: [
  32. {
  33. code: `app.get('/fooBar')`,
  34. errors: [{ message: 'Route path should be in kebab-case.' }],
  35. },
  36. {
  37. code: `app.get('/fooBar/:id')`,
  38. errors: [{ message: 'Route path should be in kebab-case.' }],
  39. },
  40. {
  41. code: `webRouter.get('/foo_bar/:id/FooBar/:name/fooBar')`,
  42. errors: [{ message: 'Route path should be in kebab-case.' }],
  43. },
  44. {
  45. code: `router.get(/^\\/downLoad\\/pro-ject\\/([^/]*)\\/OutPut\\/out-put\\.pdf$/)`,
  46. errors: [{ message: 'Route path should be in kebab-case.' }],
  47. },
  48. ],
  49. })
  50. ruleTester.run('no-unnecessary-trans', noUnnecessaryTrans, {
  51. valid: [
  52. { code: `<Trans i18nKey="test" components={{ strong: <strong/> }}/>` },
  53. ],
  54. invalid: [
  55. {
  56. code: `<Trans i18nKey="test" values={{ test: 'foo '}}/>`,
  57. errors: [{ message: `Use t('…') when there are no components` }],
  58. },
  59. {
  60. code: `<Trans i18nKey="test" />`,
  61. errors: [{ message: `Use t('…') when there are no components` }],
  62. output: `{t('test')}`,
  63. },
  64. ],
  65. })
  66. ruleTester.run('should-unescape-trans', shouldUnescapeTrans, {
  67. valid: [
  68. {
  69. code: `<Trans i18nKey="test" components={{ strong: <strong/> }}/>`,
  70. },
  71. {
  72. code: `<Trans i18nKey="test" values={{ foo: 'bar' }} components={{ strong: <strong/> }} shouldUnescape tOptions={{ interpolation: { escapeValue: true } }}/>`,
  73. },
  74. ],
  75. invalid: [
  76. {
  77. code: `<Trans i18nKey="test" values={{ foo: 'bar' }} components={{ strong: <strong/> }} />`,
  78. errors: [{ message: 'Trans with values must have shouldUnescape' }],
  79. output: `<Trans i18nKey="test" values={{ foo: 'bar' }}\nshouldUnescape components={{ strong: <strong/> }} />`,
  80. },
  81. {
  82. code: `<Trans i18nKey="test" values={{ foo: 'bar' }} components={{ strong: <strong/> }} shouldUnescape />`,
  83. errors: [
  84. {
  85. message:
  86. 'Trans with shouldUnescape must have tOptions.interpolation.escapeValue',
  87. },
  88. ],
  89. output: `<Trans i18nKey="test" values={{ foo: 'bar' }} components={{ strong: <strong/> }} shouldUnescape\ntOptions={{ interpolation: { escapeValue: true } }} />`,
  90. },
  91. ],
  92. })
  93. const noGeneratedEditorThemesError =
  94. 'EditorView.theme and EditorView.baseTheme each add CSS to the page for every instance of the theme. Store the theme in a variable and reuse it instead.'
  95. ruleTester.run('no-generated-editor-themes', noGeneratedEditorThemes, {
  96. valid: [
  97. {
  98. code: `EditorView.theme({ '.cm-editor': { color: 'black' } })`,
  99. },
  100. {
  101. code: `const theme = EditorView.theme({ '.cm-editor': { color: 'black' } })`,
  102. },
  103. ],
  104. invalid: [
  105. {
  106. code: `function createTheme() { return EditorView.theme({ '.cm-editor': { color: 'black' } }) }`,
  107. errors: [
  108. {
  109. message: noGeneratedEditorThemesError,
  110. },
  111. ],
  112. },
  113. {
  114. code: `() => EditorView.theme({ '.cm-editor': { color: 'black' } })`,
  115. errors: [
  116. {
  117. message: noGeneratedEditorThemesError,
  118. },
  119. ],
  120. },
  121. {
  122. code: `class Foo { createTheme() { return EditorView.theme({ '.cm-editor': { color: 'black' } }) } }`,
  123. errors: [
  124. {
  125. message: noGeneratedEditorThemesError,
  126. },
  127. ],
  128. },
  129. ],
  130. })
  131. ruleTester.run('domock-require-valid-path', viDoMockValidPath, {
  132. valid: [
  133. {
  134. code: 'vi.doMock("./require-vi-doMock-valid-path.js")',
  135. filename: __filename,
  136. },
  137. {
  138. code: 'const filename = "./require-vi-doMock-valid-path.js"; vi.doMock(filename);',
  139. filename: __filename,
  140. },
  141. ],
  142. invalid: [
  143. {
  144. code: "vi.doMock('./require-vi-doMock-valid-path2')",
  145. filename: __filename,
  146. errors: [
  147. {
  148. message:
  149. 'The path "./require-vi-doMock-valid-path2" in vi.doMock() cannot be resolved relative to the current file.',
  150. },
  151. ],
  152. },
  153. {
  154. code: 'const filename = "./require-vi-doMock-valid-path2.js"; vi.doMock(filename);',
  155. filename: __filename,
  156. errors: [
  157. {
  158. message:
  159. 'The first argument of vi.doMock() must be (or resolve to) a string literal representing a path.',
  160. },
  161. ],
  162. },
  163. ],
  164. })
  165. ruleTester.run(
  166. 'require-cio-snake-case-properties',
  167. requireCioSnakeCaseProperties,
  168. {
  169. valid: [
  170. // updateUserAttributes with snake_case keys
  171. {
  172. code: `CustomerIoHandler.updateUserAttributes(userId, { plan_type: 'free', group_size: 10 })`,
  173. },
  174. // Modules.promises.hooks.fire with snake_case keys
  175. {
  176. code: `Modules.promises.hooks.fire('setUserProperties', userId, { plan_type: 'free', last_active: 123 })`,
  177. },
  178. // Modules.hooks.fire with snake_case keys
  179. {
  180. code: `Modules.hooks.fire('setUserProperties', userId, { plan_type: 'free' })`,
  181. },
  182. // Single-word keys are valid snake_case
  183. {
  184. code: `CustomerIoHandler.updateUserAttributes(userId, { email: 'a@b.com', role: 'admin' })`,
  185. },
  186. // Computed/dynamic keys are skipped
  187. {
  188. code: `CustomerIoHandler.updateUserAttributes(userId, { [dynamicKey]: true })`,
  189. },
  190. // Spread elements are skipped
  191. {
  192. code: `CustomerIoHandler.updateUserAttributes(userId, { ...existingAttrs })`,
  193. },
  194. // Unrelated function calls are not checked
  195. {
  196. code: `SomeOtherHandler.updateUserAttributes(userId, { camelCase: true })`,
  197. },
  198. // fire() with a different event name is not checked
  199. {
  200. code: `Modules.promises.hooks.fire('someOtherEvent', userId, { camelCase: true })`,
  201. },
  202. ],
  203. invalid: [
  204. // camelCase key in updateUserAttributes
  205. {
  206. code: `CustomerIoHandler.updateUserAttributes(userId, { planType: 'free' })`,
  207. errors: [
  208. {
  209. message: `Customer.io attribute 'planType' must be in snake_case.`,
  210. },
  211. ],
  212. },
  213. // kebab-case string key
  214. {
  215. code: `CustomerIoHandler.updateUserAttributes(userId, { 'plan-type': 'free' })`,
  216. errors: [
  217. {
  218. message: `Customer.io attribute 'plan-type' must be in snake_case.`,
  219. },
  220. ],
  221. },
  222. // PascalCase key
  223. {
  224. code: `CustomerIoHandler.updateUserAttributes(userId, { PlanType: 'free' })`,
  225. errors: [
  226. {
  227. message: `Customer.io attribute 'PlanType' must be in snake_case.`,
  228. },
  229. ],
  230. },
  231. // camelCase in Modules.promises.hooks.fire
  232. {
  233. code: `Modules.promises.hooks.fire('setUserProperties', userId, { planType: 'free' })`,
  234. errors: [
  235. {
  236. message: `Customer.io attribute 'planType' must be in snake_case.`,
  237. },
  238. ],
  239. },
  240. // camelCase in Modules.hooks.fire
  241. {
  242. code: `Modules.hooks.fire('setUserProperties', userId, { planType: 'free' })`,
  243. errors: [
  244. {
  245. message: `Customer.io attribute 'planType' must be in snake_case.`,
  246. },
  247. ],
  248. },
  249. // Multiple invalid keys report multiple errors
  250. {
  251. code: `CustomerIoHandler.updateUserAttributes(userId, { planType: 'free', groupSize: 10, plan_term: 'annual' })`,
  252. errors: [
  253. {
  254. message: `Customer.io attribute 'planType' must be in snake_case.`,
  255. },
  256. {
  257. message: `Customer.io attribute 'groupSize' must be in snake_case.`,
  258. },
  259. ],
  260. },
  261. ],
  262. }
  263. )
  264. const noThrowInCallbackMessage =
  265. 'Pass the error to the callback instead of throwing in callback-based code.'
  266. ruleTester.run('no-throw-in-callback', noThrowInCallback, {
  267. valid: [
  268. // Calling the callback with an error is fine
  269. { code: `function foo(cb) { cb(new Error()) }` },
  270. // async functions may throw (they return a rejected promise)
  271. { code: `async function foo(cb) { throw new Error() }` },
  272. // Last param not a callback name — not a callback-style function
  273. { code: `function foo(data) { throw new Error() }` },
  274. // No params at all
  275. { code: `function foo() { throw new Error() }` },
  276. // throw inside a nested non-callback function is fine
  277. { code: `function foo(cb) { [1].map(function() { throw new Error() }) }` },
  278. // throw inside a nested async arrow is fine
  279. { code: `function foo(cb) { [1].map(async () => { throw new Error() }) }` },
  280. ],
  281. invalid: [
  282. {
  283. code: `function foo(cb) { throw new Error() }`,
  284. errors: [{ message: noThrowInCallbackMessage }],
  285. },
  286. {
  287. code: `function foo(callback) { throw new Error() }`,
  288. errors: [{ message: noThrowInCallbackMessage }],
  289. },
  290. {
  291. code: `function foo(done) { throw new Error() }`,
  292. errors: [{ message: noThrowInCallbackMessage }],
  293. },
  294. {
  295. code: `function foo(next) { throw new Error() }`,
  296. errors: [{ message: noThrowInCallbackMessage }],
  297. },
  298. {
  299. code: `function foo(data, cb) { throw new Error() }`,
  300. errors: [{ message: noThrowInCallbackMessage }],
  301. },
  302. {
  303. code: `const foo = (cb) => { throw new Error() }`,
  304. errors: [{ message: noThrowInCallbackMessage }],
  305. },
  306. // throw in a nested callback-style function inside another callback function
  307. {
  308. code: `function foo(cb) { bar(function(done) { throw new Error() }) }`,
  309. errors: [{ message: noThrowInCallbackMessage }],
  310. },
  311. ],
  312. })