djbob2000

Untitled

Aug 25th, 2025
16
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.70 KB | None | 0 0
  1. import { runTests } from "@root/test-utils/eslint-rule-tester";
  2. import mustUsePluralInFunctionNamesIfTheFunctionReturnsAnArrayOfAnything from "@root/rules/must-use-plural-in-function-names-if-the-function-returns-an-array-of-anything";
  3.  
  4. const validCases = {
  5. valid: [
  6. {
  7. code: `
  8. function getItems() {
  9. return [];
  10. }
  11. `,
  12. },
  13. {
  14. code: `
  15. function getItem() {
  16. return { id: 1 };
  17. }
  18. `,
  19. },
  20. {
  21. code: `
  22. function getItem() {
  23. return "hello";
  24. }
  25. `,
  26. },
  27. {
  28. code: `
  29. const getItems = () => [];
  30. `,
  31. },
  32. {
  33. code: `
  34. const getItems = function() {
  35. return [];
  36. };
  37. `,
  38. },
  39. {
  40. code: `
  41. class MyClass {
  42. getItems() {
  43. return [];
  44. }
  45. }
  46. `,
  47. },
  48. {
  49. code: `
  50. function getData() {
  51. return [];
  52. }
  53. `,
  54. options: [{ exceptions: ["getData"] }],
  55. },
  56. {
  57. code: `
  58. function getItems() {
  59. if (true) {
  60. return [];
  61. }
  62. return null;
  63. }
  64. `,
  65. },
  66. {
  67. code: `
  68. function getChildren() {
  69. return [];
  70. }
  71. `,
  72. },
  73. {
  74. code: `
  75. function processData() {
  76. return [];
  77. }
  78. `,
  79. options: [{ exceptions: ["processData"] }],
  80. },
  81. {
  82. code: `
  83. async function getItems() {
  84. return [];
  85. }
  86. `,
  87. },
  88. {
  89. code: `
  90. function getItems(): string[] {
  91. return [];
  92. }
  93. `,
  94. },
  95. {
  96. code: `
  97. function getItems(): Promise<string[]> {
  98. return Promise.resolve([]);
  99. }
  100. `,
  101. },
  102. {
  103. code: `
  104. function getItems(): Array<string> {
  105. return [];
  106. }
  107. `,
  108. },
  109. {
  110. code: `
  111. function getItems(): ReadonlyArray<string> {
  112. return [];
  113. }
  114. `,
  115. },
  116. {
  117. code: `
  118. function getItem(): string | undefined {
  119. return "hello";
  120. }
  121. `,
  122. },
  123. {
  124. code: `
  125. function getData(): string {
  126. return "data";
  127. }
  128. `,
  129. },
  130. {
  131. code: `
  132. function getAnalytics() {
  133. return [];
  134. }
  135. `,
  136. },
  137. {
  138. code: `
  139. async function getItem() {
  140. return [];
  141. }
  142. `,
  143. },
  144. {
  145. code: `
  146. function getItem(): Promise<string[]> {
  147. return Promise.resolve([]);
  148. }
  149. `,
  150. },
  151. {
  152. code: `
  153. function getMetadata() {
  154. return [];
  155. }
  156. `,
  157. },
  158. {
  159. code: `
  160. const obj = {
  161. ["getItem"]() {
  162. return [];
  163. }
  164. };
  165. `,
  166. },
  167. ],
  168. };
  169.  
  170. const invalidCases = {
  171. invalid: [
  172. {
  173. code: `
  174. function getItem() {
  175. return [];
  176. }
  177. `,
  178. errors: [
  179. {
  180. messageId: "usePluralInFunctionName",
  181. data: {
  182. functionName: "getItem",
  183. suggestedName: "getItems",
  184. },
  185. },
  186. ],
  187. },
  188. {
  189. code: `
  190. const getItem = () => [];
  191. `,
  192. errors: [
  193. {
  194. messageId: "usePluralInFunctionName",
  195. data: {
  196. functionName: "getItem",
  197. suggestedName: "getItems",
  198. },
  199. },
  200. ],
  201. },
  202. {
  203. code: `
  204. const getItem = function() {
  205. return [];
  206. };
  207. `,
  208. errors: [
  209. {
  210. messageId: "usePluralInFunctionName",
  211. data: {
  212. functionName: "getItem",
  213. suggestedName: "getItems",
  214. },
  215. },
  216. ],
  217. },
  218. {
  219. code: `
  220. class MyClass {
  221. getItem() {
  222. return [];
  223. }
  224. }
  225. `,
  226. errors: [
  227. {
  228. messageId: "usePluralInFunctionName",
  229. data: {
  230. functionName: "getItem",
  231. suggestedName: "getItems",
  232. },
  233. },
  234. ],
  235. },
  236. {
  237. code: `
  238. function getItem() {
  239. if (true) {
  240. return [];
  241. }
  242. return null;
  243. }
  244. `,
  245. errors: [
  246. {
  247. messageId: "usePluralInFunctionName",
  248. data: {
  249. functionName: "getItem",
  250. suggestedName: "getItems",
  251. },
  252. },
  253. ],
  254. },
  255. {
  256. code: `
  257. function getItem() {
  258. if (false) {
  259. return null;
  260. } else {
  261. return [];
  262. }
  263. }
  264. `,
  265. errors: [
  266. {
  267. messageId: "usePluralInFunctionName",
  268. data: {
  269. functionName: "getItem",
  270. suggestedName: "getItems",
  271. },
  272. },
  273. ],
  274. },
  275. {
  276. code: `
  277. function getItem() {
  278. try {
  279. return [];
  280. } catch (error) {
  281. return null;
  282. }
  283. }
  284. `,
  285. errors: [
  286. {
  287. messageId: "usePluralInFunctionName",
  288. data: {
  289. functionName: "getItem",
  290. suggestedName: "getItems",
  291. },
  292. },
  293. ],
  294. },
  295. {
  296. code: `
  297. const obj = {
  298. getItem() {
  299. return [];
  300. }
  301. };
  302. `,
  303. errors: [
  304. {
  305. messageId: "usePluralInFunctionName",
  306. data: {
  307. functionName: "getItem",
  308. suggestedName: "getItems",
  309. },
  310. },
  311. ],
  312. },
  313. {
  314. code: `
  315. function getItem() {
  316. if (Math.random() > 0.5) {
  317. return [];
  318. }
  319. return "string";
  320. }
  321. `,
  322. errors: [
  323. {
  324. messageId: "usePluralInFunctionName",
  325. data: {
  326. functionName: "getItem",
  327. suggestedName: "getItems",
  328. },
  329. },
  330. ],
  331. },
  332. {
  333. code: `
  334. function getItem() {
  335. let someArray = [];
  336. return someArray;
  337. }
  338. `,
  339. errors: [
  340. {
  341. messageId: "usePluralInFunctionName",
  342. data: {
  343. functionName: "getItem",
  344. suggestedName: "getItems",
  345. },
  346. },
  347. ],
  348. },
  349. {
  350. code: `
  351. function getItem() {
  352. return items.filter(Boolean);
  353. }
  354. `,
  355. errors: [
  356. {
  357. messageId: "usePluralInFunctionName",
  358. data: {
  359. functionName: "getItem",
  360. suggestedName: "getItems",
  361. },
  362. },
  363. ],
  364. },
  365. {
  366. code: `
  367. function getItem() {
  368. return new Array(5);
  369. }
  370. `,
  371. errors: [
  372. {
  373. messageId: "usePluralInFunctionName",
  374. data: {
  375. functionName: "getItem",
  376. suggestedName: "getItems",
  377. },
  378. },
  379. ],
  380. },
  381.  
  382. {
  383. code: `
  384. function getItem(): string[] {
  385. return [];
  386. }
  387. `,
  388. errors: [
  389. {
  390. messageId: "usePluralInFunctionName",
  391. data: {
  392. functionName: "getItem",
  393. suggestedName: "getItems",
  394. },
  395. },
  396. ],
  397. },
  398. {
  399. code: `
  400. function getItem(): Array<string> {
  401. return [];
  402. }
  403. `,
  404. errors: [
  405. {
  406. messageId: "usePluralInFunctionName",
  407. data: {
  408. functionName: "getItem",
  409. suggestedName: "getItems",
  410. },
  411. },
  412. ],
  413. },
  414. {
  415. code: `
  416. function getItem(): ReadonlyArray<string> {
  417. return [];
  418. }
  419. `,
  420. errors: [
  421. {
  422. messageId: "usePluralInFunctionName",
  423. data: {
  424. functionName: "getItem",
  425. suggestedName: "getItems",
  426. },
  427. },
  428. ],
  429. },
  430. {
  431. code: `
  432. function getAnalytic() {
  433. return [];
  434. }
  435. `,
  436. errors: [
  437. {
  438. messageId: "usePluralInFunctionName",
  439. data: {
  440. functionName: "getAnalytic",
  441. suggestedName: "getAnalytics",
  442. },
  443. },
  444. ],
  445. },
  446. {
  447. code: `
  448. function getItem(): string[] | null {
  449. return [];
  450. }
  451. `,
  452. errors: [
  453. {
  454. messageId: "usePluralInFunctionName",
  455. data: {
  456. functionName: "getItem",
  457. suggestedName: "getItems",
  458. },
  459. },
  460. ],
  461. },
  462. {
  463. code: `
  464. function getItem(): (string | number)[] {
  465. return [];
  466. }
  467. `,
  468. errors: [
  469. {
  470. messageId: "usePluralInFunctionName",
  471. data: {
  472. functionName: "getItem",
  473. suggestedName: "getItems",
  474. },
  475. },
  476. ],
  477. },
  478. ],
  479. };
  480.  
  481. const testCases = [validCases, invalidCases];
  482. runTests(
  483. "must-use-plural-in-function-names-if-the-function-returns-an-array-of-anything",
  484. mustUsePluralInFunctionNamesIfTheFunctionReturnsAnArrayOfAnything,
  485. testCases,
  486. );
  487.  
Advertisement
Add Comment
Please, Sign In to add comment