Advertisement
djbob2000

Untitled

Jun 23rd, 2025
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { runTests } from "@root/test-utils/eslint-rule-tester";
  2. import noStringLengthCheck from "@root/rules/no-string-length-check";
  3.  
  4. const validCases = {
  5.     valid: [
  6.         {
  7.             code: "const foo = bar ? bar : baz;",
  8.         },
  9.         {
  10.             code: "const foo = bar ?? baz;",
  11.         },
  12.         {
  13.             code: "if (str) { console.log('not empty'); }",
  14.         },
  15.         {
  16.             code: "const result = str ? 'has value' : 'empty';",
  17.         },
  18.         {
  19.             code: "const hasValue = !!str;",
  20.         },
  21.         {
  22.             code: "const len = str.length;",
  23.         },
  24.         {
  25.             code: "console.log(str.length);",
  26.         },
  27.         {
  28.             code: "const result = str.length + 5;",
  29.         },
  30.         {
  31.             code: "for (let i = 0; i < str.length; i++) { }",
  32.         },
  33.         {
  34.             code: "const arr = [1, 2, 3]; if (arr.length) { console.log('has elements'); }",
  35.         },
  36.         {
  37.             code: "const arr = new Array(3); if (arr.length) { console.log('has elements'); }",
  38.         },
  39.     ],
  40. };
  41.  
  42. const invalidCases = {
  43.     invalid: [
  44.         {
  45.             code: "if (str.length) { console.log('not empty'); }",
  46.             errors: [{ messageId: "noStringLength" }],
  47.         },
  48.         {
  49.             code: "if (str.length === 0) { console.log('empty'); }",
  50.             errors: [{ messageId: "noStringLength" }],
  51.         },
  52.         {
  53.             code: "if (str.length !== 0) { console.log('not empty'); }",
  54.             errors: [{ messageId: "noStringLength" }],
  55.         },
  56.         {
  57.             code: "if (str.length == 0) { console.log('empty'); }",
  58.             errors: [{ messageId: "noStringLength" }],
  59.         },
  60.         {
  61.             code: "if (str.length != 0) { console.log('not empty'); }",
  62.             errors: [{ messageId: "noStringLength" }],
  63.         },
  64.         {
  65.             code: "if (str.length > 0) { console.log('not empty'); }",
  66.             errors: [{ messageId: "noStringLength" }],
  67.         },
  68.         {
  69.             code: "if (str.length < 1) { console.log('empty'); }",
  70.             errors: [{ messageId: "noStringLength" }],
  71.         },
  72.         {
  73.             code: "if (str.length >= 1) { console.log('not empty'); }",
  74.             errors: [{ messageId: "noStringLength" }],
  75.         },
  76.         {
  77.             code: "const result = str.length ? 'has value' : 'empty';",
  78.             errors: [{ messageId: "noStringLength" }],
  79.         },
  80.         {
  81.             code: "const hasValue = !!str.length;",
  82.             errors: [{ messageId: "noStringLength" }],
  83.         },
  84.         {
  85.             code: "return str.length;",
  86.             errors: [{ messageId: "noStringLength" }],
  87.         },
  88.         {
  89.             code: "while (str.length) { break; }",
  90.             errors: [{ messageId: "noStringLength" }],
  91.         },
  92.         {
  93.             code: "do { break; } while (str.length);",
  94.             errors: [{ messageId: "noStringLength" }],
  95.         },
  96.     ],
  97. };
  98.  
  99. const testCases = [validCases, invalidCases];
  100. runTests("no-string-length-check", noStringLengthCheck, testCases);
  101.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement