|
| 1 | +import { assertEquals } 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 | + assertEquals(d[0].id, "no-type-assertion/no-type-assertion"); |
| 12 | + assertEquals( |
| 13 | + d[0].message, |
| 14 | + "Do not use any type assertions. Use a type annotation or `satisfies` operator instead.", |
| 15 | + ); |
| 16 | +}); |
| 17 | + |
| 18 | +Deno.test("reports angle-bracket type assertion", () => { |
| 19 | + const d = Deno.lint.runPlugin( |
| 20 | + plugin, |
| 21 | + "test.ts", |
| 22 | + `const x = <string>"hello";`, |
| 23 | + ); |
| 24 | + assertEquals(d.length, 1); |
| 25 | + assertEquals(d[0].id, "no-type-assertion/no-type-assertion"); |
| 26 | +}); |
| 27 | + |
| 28 | +Deno.test("allows `as const`", () => { |
| 29 | + const d = Deno.lint.runPlugin( |
| 30 | + plugin, |
| 31 | + "test.ts", |
| 32 | + `const x = "hello" as const;`, |
| 33 | + ); |
| 34 | + assertEquals(d.length, 0); |
| 35 | +}); |
| 36 | + |
| 37 | +Deno.test("allows `<const>`", () => { |
| 38 | + const d = Deno.lint.runPlugin(plugin, "test.ts", `const x = <const>"hello";`); |
| 39 | + assertEquals(d.length, 0); |
| 40 | +}); |
| 41 | + |
| 42 | +Deno.test("allows type annotations", () => { |
| 43 | + const d = Deno.lint.runPlugin( |
| 44 | + plugin, |
| 45 | + "test.ts", |
| 46 | + `const x: string = "hello";`, |
| 47 | + ); |
| 48 | + assertEquals(d.length, 0); |
| 49 | +}); |
| 50 | + |
| 51 | +Deno.test("allows `satisfies`", () => { |
| 52 | + const d = Deno.lint.runPlugin( |
| 53 | + plugin, |
| 54 | + "test.ts", |
| 55 | + `const x = "hello" satisfies string;`, |
| 56 | + ); |
| 57 | + assertEquals(d.length, 0); |
| 58 | +}); |
| 59 | + |
| 60 | +Deno.test("reports nested `as` assertion", () => { |
| 61 | + const d = Deno.lint.runPlugin( |
| 62 | + plugin, |
| 63 | + "test.ts", |
| 64 | + `const x = (obj as any).foo;`, |
| 65 | + ); |
| 66 | + assertEquals(d.length, 1); |
| 67 | +}); |
0 commit comments