|
| 1 | +import { assertEquals, assertExists } from "@std/assert"; |
| 2 | +import plugin from "./mod.ts"; |
| 3 | + |
| 4 | +Deno.test("reports `as` type assertion", () => { |
| 5 | + const d = Deno.lint.runPlugin( |
| 6 | + plugin, |
| 7 | + "test.ts", |
| 8 | + `const x = "hello" as unknown;`, |
| 9 | + ); |
| 10 | + assertEquals(d.length, 1); |
| 11 | + const diag = d[0]; |
| 12 | + assertExists(diag); |
| 13 | + assertEquals(diag.id, "no-type-assertion/no-type-assertion"); |
| 14 | + assertEquals( |
| 15 | + diag.message, |
| 16 | + "Do not use any type assertions. Use a type annotation or `satisfies` operator instead.", |
| 17 | + ); |
| 18 | +}); |
| 19 | + |
| 20 | +Deno.test("reports angle-bracket type assertion", () => { |
| 21 | + const d = Deno.lint.runPlugin( |
| 22 | + plugin, |
| 23 | + "test.ts", |
| 24 | + `const x = <string>"hello";`, |
| 25 | + ); |
| 26 | + assertEquals(d.length, 1); |
| 27 | + const diag = d[0]; |
| 28 | + assertExists(diag); |
| 29 | + assertEquals(diag.id, "no-type-assertion/no-type-assertion"); |
| 30 | +}); |
| 31 | + |
| 32 | +Deno.test("allows `as const`", () => { |
| 33 | + const d = Deno.lint.runPlugin( |
| 34 | + plugin, |
| 35 | + "test.ts", |
| 36 | + `const x = "hello" as const;`, |
| 37 | + ); |
| 38 | + assertEquals(d.length, 0); |
| 39 | +}); |
| 40 | + |
| 41 | +Deno.test("allows `<const>`", () => { |
| 42 | + const d = Deno.lint.runPlugin(plugin, "test.ts", `const x = <const>"hello";`); |
| 43 | + assertEquals(d.length, 0); |
| 44 | +}); |
| 45 | + |
| 46 | +Deno.test("allows type annotations", () => { |
| 47 | + const d = Deno.lint.runPlugin( |
| 48 | + plugin, |
| 49 | + "test.ts", |
| 50 | + `const x: string = "hello";`, |
| 51 | + ); |
| 52 | + assertEquals(d.length, 0); |
| 53 | +}); |
| 54 | + |
| 55 | +Deno.test("allows `satisfies`", () => { |
| 56 | + const d = Deno.lint.runPlugin( |
| 57 | + plugin, |
| 58 | + "test.ts", |
| 59 | + `const x = "hello" satisfies string;`, |
| 60 | + ); |
| 61 | + assertEquals(d.length, 0); |
| 62 | +}); |
| 63 | + |
| 64 | +Deno.test("reports nested `as` assertion", () => { |
| 65 | + const d = Deno.lint.runPlugin( |
| 66 | + plugin, |
| 67 | + "test.ts", |
| 68 | + `const x = (obj as any).foo;`, |
| 69 | + ); |
| 70 | + assertEquals(d.length, 1); |
| 71 | +}); |
| 72 | + |
| 73 | +Deno.test("reports double assertion (as unknown as T)", () => { |
| 74 | + const d = Deno.lint.runPlugin( |
| 75 | + plugin, |
| 76 | + "test.ts", |
| 77 | + `const x = value as unknown as string;`, |
| 78 | + ); |
| 79 | + assertEquals(d.length, 2); |
| 80 | +}); |
| 81 | + |
| 82 | +Deno.test("reports generic angle-bracket assertion", () => { |
| 83 | + const d = Deno.lint.runPlugin( |
| 84 | + plugin, |
| 85 | + "test.ts", |
| 86 | + `const x = <Array<string>>value;`, |
| 87 | + ); |
| 88 | + assertEquals(d.length, 1); |
| 89 | +}); |
| 90 | + |
| 91 | +Deno.test("allows `as const satisfies`", () => { |
| 92 | + const d = Deno.lint.runPlugin( |
| 93 | + plugin, |
| 94 | + "test.ts", |
| 95 | + `const x = { a: 1 } as const satisfies { a: number };`, |
| 96 | + ); |
| 97 | + assertEquals(d.length, 0); |
| 98 | +}); |
0 commit comments