Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { runTests } from "@root/test-utils/eslint-rule-tester";
- import noStringLengthCheck from "@root/rules/no-string-length-check";
- const validCases = {
- valid: [
- {
- code: "const foo = bar ? bar : baz;",
- },
- {
- code: "const foo = bar ?? baz;",
- },
- {
- code: "if (str) { console.log('not empty'); }",
- },
- {
- code: "const result = str ? 'has value' : 'empty';",
- },
- {
- code: "const hasValue = !!str;",
- },
- {
- code: "const len = str.length;",
- },
- {
- code: "console.log(str.length);",
- },
- {
- code: "const result = str.length + 5;",
- },
- {
- code: "for (let i = 0; i < str.length; i++) { }",
- },
- {
- code: "const arr = [1, 2, 3]; if (arr.length) { console.log('has elements'); }",
- },
- {
- code: "const arr = new Array(3); if (arr.length) { console.log('has elements'); }",
- },
- ],
- };
- const invalidCases = {
- invalid: [
- {
- code: "if (str.length) { console.log('not empty'); }",
- errors: [{ messageId: "noStringLength" }],
- },
- {
- code: "if (str.length === 0) { console.log('empty'); }",
- errors: [{ messageId: "noStringLength" }],
- },
- {
- code: "if (str.length !== 0) { console.log('not empty'); }",
- errors: [{ messageId: "noStringLength" }],
- },
- {
- code: "if (str.length == 0) { console.log('empty'); }",
- errors: [{ messageId: "noStringLength" }],
- },
- {
- code: "if (str.length != 0) { console.log('not empty'); }",
- errors: [{ messageId: "noStringLength" }],
- },
- {
- code: "if (str.length > 0) { console.log('not empty'); }",
- errors: [{ messageId: "noStringLength" }],
- },
- {
- code: "if (str.length < 1) { console.log('empty'); }",
- errors: [{ messageId: "noStringLength" }],
- },
- {
- code: "if (str.length >= 1) { console.log('not empty'); }",
- errors: [{ messageId: "noStringLength" }],
- },
- {
- code: "const result = str.length ? 'has value' : 'empty';",
- errors: [{ messageId: "noStringLength" }],
- },
- {
- code: "const hasValue = !!str.length;",
- errors: [{ messageId: "noStringLength" }],
- },
- {
- code: "return str.length;",
- errors: [{ messageId: "noStringLength" }],
- },
- {
- code: "while (str.length) { break; }",
- errors: [{ messageId: "noStringLength" }],
- },
- {
- code: "do { break; } while (str.length);",
- errors: [{ messageId: "noStringLength" }],
- },
- ],
- };
- const testCases = [validCases, invalidCases];
- runTests("no-string-length-check", noStringLengthCheck, testCases);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement