|
| 1 | +package no_non_null_assertion |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/web-infra-dev/rslint/internal/rule_tester" |
| 7 | + "github.com/web-infra-dev/rslint/internal/rules/fixtures" |
| 8 | +) |
| 9 | + |
| 10 | +func TestNoNonNullAssertionRule(t *testing.T) { |
| 11 | + validTestCases := []rule_tester.ValidTestCase{ |
| 12 | + // Basic valid cases - no non-null assertions |
| 13 | + {Code: `const foo = "hello"; console.log(foo);`}, |
| 14 | + {Code: `function foo(bar: string) { console.log(bar); }`}, |
| 15 | + {Code: `const foo: string | null = "hello"; if (foo) { console.log(foo); }`}, |
| 16 | + {Code: `const foo: string | undefined = "hello"; if (foo !== undefined) { console.log(foo); }`}, |
| 17 | + {Code: `const foo: string | null = "hello"; const bar = foo || "default";`}, |
| 18 | + {Code: `const foo: string | null = "hello"; const bar = foo ?? "default";`}, |
| 19 | + {Code: `const foo: string | null = "hello"; const bar = foo?.length || 0;`}, |
| 20 | + {Code: `const foo: string | null = "hello"; if (foo !== null) { console.log(foo); }`}, |
| 21 | + } |
| 22 | + |
| 23 | + invalidTestCases := []rule_tester.InvalidTestCase{ |
| 24 | + // Basic non-null assertion - should report error |
| 25 | + { |
| 26 | + Code: `const foo: string | null = "hello"; const bar = foo!;`, |
| 27 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 28 | + { |
| 29 | + MessageId: "noNonNull", |
| 30 | + }, |
| 31 | + }, |
| 32 | + }, |
| 33 | + // Non-null assertion in property access |
| 34 | + { |
| 35 | + Code: `const foo: string | null = "hello"; const bar = foo!.length;`, |
| 36 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 37 | + { |
| 38 | + MessageId: "noNonNull", |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + // Non-null assertion in function call |
| 43 | + { |
| 44 | + Code: `const foo: string | null = "hello"; const bar = foo!.toUpperCase();`, |
| 45 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 46 | + { |
| 47 | + MessageId: "noNonNull", |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + // Non-null assertion in array access |
| 52 | + { |
| 53 | + Code: `const foo: string[] | null = ["hello"]; const bar = foo![0];`, |
| 54 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 55 | + { |
| 56 | + MessageId: "noNonNull", |
| 57 | + }, |
| 58 | + }, |
| 59 | + }, |
| 60 | + // Chained non-null assertions - should report 2 errors |
| 61 | + { |
| 62 | + Code: `const foo: string | null = "hello"; const bar = foo!!;`, |
| 63 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 64 | + { |
| 65 | + MessageId: "noNonNull", |
| 66 | + }, |
| 67 | + { |
| 68 | + MessageId: "noNonNull", |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + // Non-null assertion in conditional expression |
| 73 | + { |
| 74 | + Code: `const foo: string | null = "hello"; const bar = foo! ? "yes" : "no";`, |
| 75 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 76 | + { |
| 77 | + MessageId: "noNonNull", |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + // Non-null assertion in logical expression |
| 82 | + { |
| 83 | + Code: `const foo: string | null = "hello"; const bar = foo! && "yes";`, |
| 84 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 85 | + { |
| 86 | + MessageId: "noNonNull", |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + // Non-null assertion in return statement |
| 91 | + { |
| 92 | + Code: `function test(): string { const foo: string | null = "hello"; return foo!; }`, |
| 93 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 94 | + { |
| 95 | + MessageId: "noNonNull", |
| 96 | + }, |
| 97 | + }, |
| 98 | + }, |
| 99 | + // Non-null assertion in variable declaration |
| 100 | + { |
| 101 | + Code: `let foo: string | null = "hello"; foo = foo!;`, |
| 102 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 103 | + { |
| 104 | + MessageId: "noNonNull", |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, |
| 108 | + // Non-null assertion in parameters |
| 109 | + { |
| 110 | + Code: `function test(foo: string | null) { const bar = foo!; }`, |
| 111 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 112 | + { |
| 113 | + MessageId: "noNonNull", |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + // Non-null assertion in object properties |
| 118 | + { |
| 119 | + Code: `const obj = { foo: "hello" as string | null }; const bar = obj.foo!;`, |
| 120 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 121 | + { |
| 122 | + MessageId: "noNonNull", |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + // Non-null assertion in template strings |
| 127 | + { |
| 128 | + Code: "const foo: string | null = \"hello\"; const bar = `Value: ${foo!}`;", |
| 129 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 130 | + { |
| 131 | + MessageId: "noNonNull", |
| 132 | + }, |
| 133 | + }, |
| 134 | + }, |
| 135 | + // Non-null assertion in type assertion |
| 136 | + { |
| 137 | + Code: `const foo: string | null = "hello"; const bar = (foo! as string).length;`, |
| 138 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 139 | + { |
| 140 | + MessageId: "noNonNull", |
| 141 | + }, |
| 142 | + }, |
| 143 | + }, |
| 144 | + // Non-null assertion in generics |
| 145 | + { |
| 146 | + Code: `function test<T extends string | null>(foo: T): T { return foo!; }`, |
| 147 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 148 | + { |
| 149 | + MessageId: "noNonNull", |
| 150 | + }, |
| 151 | + }, |
| 152 | + }, |
| 153 | + // Non-null assertion in union types |
| 154 | + { |
| 155 | + Code: `const foo: (string | null)[] = ["hello"]; const bar = foo[0]!;`, |
| 156 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 157 | + { |
| 158 | + MessageId: "noNonNull", |
| 159 | + }, |
| 160 | + }, |
| 161 | + }, |
| 162 | + // Non-null assertion in nested expressions |
| 163 | + { |
| 164 | + Code: `const foo: string | null = "hello"; const bar = (foo! + "world").length;`, |
| 165 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 166 | + { |
| 167 | + MessageId: "noNonNull", |
| 168 | + }, |
| 169 | + }, |
| 170 | + }, |
| 171 | + // Non-null assertion in ternary expressions |
| 172 | + { |
| 173 | + Code: `const foo: string | null = "hello"; const bar = foo! ? foo!.length : 0;`, |
| 174 | + Errors: []rule_tester.InvalidTestCaseError{ |
| 175 | + { |
| 176 | + MessageId: "noNonNull", |
| 177 | + }, |
| 178 | + { |
| 179 | + MessageId: "noNonNull", |
| 180 | + }, |
| 181 | + }, |
| 182 | + }, |
| 183 | + } |
| 184 | + |
| 185 | + rule_tester.RunRuleTester(fixtures.GetRootDir(), "tsconfig.json", t, &NoNonNullAssertionRule, validTestCases, invalidTestCases) |
| 186 | +} |
| 187 | + |
| 188 | +func TestNoNonNullAssertionOptionsParsing(t *testing.T) { |
| 189 | + // Test basic rule information |
| 190 | + rule := NoNonNullAssertionRule |
| 191 | + if rule.Name != "@typescript-eslint/no-non-null-assertion" { |
| 192 | + t.Errorf("Expected rule name to be '@typescript-eslint/no-non-null-assertion', got %s", rule.Name) |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +func TestNoNonNullAssertionMessage(t *testing.T) { |
| 197 | + msg := buildNoNonNullAssertionMessage() |
| 198 | + if msg.Id != "noNonNull" { |
| 199 | + t.Errorf("Expected message ID to be 'noNonNull', got %s", msg.Id) |
| 200 | + } |
| 201 | + if msg.Description != "Non-null assertion operator (!) is not allowed." { |
| 202 | + t.Errorf("Expected description to be 'Non-null assertion operator (!) is not allowed.', got %s", msg.Description) |
| 203 | + } |
| 204 | +} |
| 205 | + |
| 206 | +// Test edge cases |
| 207 | +func TestNoNonNullAssertionEdgeCases(t *testing.T) { |
| 208 | + validTestCases := []rule_tester.ValidTestCase{ |
| 209 | + // Nested assignment expressions |
| 210 | + {Code: `let obj: { prop?: string } = {}; obj.prop! = "value";`}, |
| 211 | + |
| 212 | + // Non-null assertion in destructuring assignment |
| 213 | + {Code: `let arr: (string | null)[] = ["hello"]; [arr[0]!] = ["world"];`}, |
| 214 | + |
| 215 | + // Complex assignment expressions |
| 216 | + {Code: `let foo: string | null = "hello"; (foo! as any) = "world";`}, |
| 217 | + } |
| 218 | + |
| 219 | + invalidTestCases := []rule_tester.InvalidTestCase{ |
| 220 | + // These test cases are already included in the main test function |
| 221 | + } |
| 222 | + |
| 223 | + rule_tester.RunRuleTester(fixtures.GetRootDir(), "tsconfig.json", t, &NoNonNullAssertionRule, validTestCases, invalidTestCases) |
| 224 | +} |
0 commit comments