|
| 1 | +import { ESLintUtils } from "@typescript-eslint/experimental-utils" |
| 2 | +import rule from "../rules/type-properties" |
| 3 | + |
| 4 | +const ruleTester = new ESLintUtils.RuleTester({ |
| 5 | + parser: "@typescript-eslint/parser", |
| 6 | +}) |
| 7 | + |
| 8 | +ruleTester.run("sort/type-properties", rule, { |
| 9 | + valid: [ |
| 10 | + "interface A {a: string, b: number}", |
| 11 | + "interface A {a: string}", |
| 12 | + "interface A {}", |
| 13 | + "type A = {_:string, a:string, b:string}", |
| 14 | + |
| 15 | + // Case insensitive |
| 16 | + "type A = {a:string, B:string, c:string, D:string}", |
| 17 | + "type A = {_:string, A:string, b:string}", |
| 18 | + |
| 19 | + // Weights |
| 20 | + ` |
| 21 | + interface A { |
| 22 | + new(f: string): void |
| 23 | + (e: string): void |
| 24 | + b: boolean |
| 25 | + c: boolean |
| 26 | + d(): void |
| 27 | + [a: string]: unknown |
| 28 | + } |
| 29 | + `, |
| 30 | + |
| 31 | + // Comments |
| 32 | + ` |
| 33 | + interface A { |
| 34 | + // a |
| 35 | + a: string |
| 36 | + // b |
| 37 | + b: number |
| 38 | + } |
| 39 | + `.trim(), |
| 40 | + ], |
| 41 | + invalid: [ |
| 42 | + { |
| 43 | + code: "interface A {c:string, a:number, b:boolean}", |
| 44 | + output: "interface A {a:number, b:boolean, c:string}", |
| 45 | + errors: [{ messageId: "unsorted" }], |
| 46 | + }, |
| 47 | + { |
| 48 | + code: "interface A {b:boolean, a:string, _:number}", |
| 49 | + output: "interface A {_:number, a:string, b:boolean}", |
| 50 | + errors: [{ messageId: "unsorted" }], |
| 51 | + }, |
| 52 | + |
| 53 | + // Case insensitive |
| 54 | + { |
| 55 | + code: "type A = {b: symbol; A: number; _: string}", |
| 56 | + output: "type A = {_: string; A: number; b: symbol}", |
| 57 | + errors: [{ messageId: "unsorted" }], |
| 58 | + }, |
| 59 | + { |
| 60 | + code: "type A = {D:number, a:boolean, c:string, B:string}", |
| 61 | + output: "type A = {a:boolean, B:string, c:string, D:number}", |
| 62 | + errors: [{ messageId: "unsorted" }], |
| 63 | + }, |
| 64 | + |
| 65 | + // All properties are sorted with a single sort |
| 66 | + { |
| 67 | + code: "interface A {z:string,y:number,x:boolean,w:symbol,v:string}", |
| 68 | + output: "interface A {v:string,w:symbol,x:boolean,y:number,z:string}", |
| 69 | + errors: [{ messageId: "unsorted" }], |
| 70 | + }, |
| 71 | + |
| 72 | + // Weights |
| 73 | + { |
| 74 | + code: ` |
| 75 | + interface A { |
| 76 | + b: boolean |
| 77 | + (e: string): void |
| 78 | + [a: string]: unknown |
| 79 | + [b: string]: boolean |
| 80 | + d(): void |
| 81 | + c: boolean |
| 82 | + [\`c\${o}g\`]: boolean |
| 83 | + new(f: string): void |
| 84 | + } |
| 85 | + `, |
| 86 | + output: ` |
| 87 | + interface A { |
| 88 | + new(f: string): void |
| 89 | + (e: string): void |
| 90 | + b: boolean |
| 91 | + c: boolean |
| 92 | + [\`c\${o}g\`]: boolean |
| 93 | + d(): void |
| 94 | + [a: string]: unknown |
| 95 | + [b: string]: boolean |
| 96 | + } |
| 97 | + `, |
| 98 | + errors: [{ messageId: "unsorted" }], |
| 99 | + }, |
| 100 | + |
| 101 | + // Comments |
| 102 | + { |
| 103 | + code: ` |
| 104 | + interface A { |
| 105 | + // c |
| 106 | + c: boolean |
| 107 | + // b |
| 108 | + b: number |
| 109 | + a: string |
| 110 | + } |
| 111 | + `.trim(), |
| 112 | + output: ` |
| 113 | + interface A { |
| 114 | + a: string |
| 115 | + // b |
| 116 | + b: number |
| 117 | + // c |
| 118 | + c: boolean |
| 119 | + } |
| 120 | + `.trim(), |
| 121 | + errors: [{ messageId: "unsorted" }], |
| 122 | + }, |
| 123 | + ], |
| 124 | +}) |
0 commit comments