|
| 1 | +/** |
| 2 | + * @fileoverview Type tests for ESLint Plugin Kit. |
| 3 | + * @author Francesco Trotta |
| 4 | + */ |
| 5 | + |
| 6 | +//----------------------------------------------------------------------------- |
| 7 | +// Imports |
| 8 | +//----------------------------------------------------------------------------- |
| 9 | + |
| 10 | +import { |
| 11 | + BooleanConfig, |
| 12 | + CallMethodStep, |
| 13 | + ConfigCommentParser, |
| 14 | + Directive, |
| 15 | + DirectiveType, |
| 16 | + RulesConfig, |
| 17 | + SourceLocation, |
| 18 | + SourceRange, |
| 19 | + StringConfig, |
| 20 | + TextSourceCodeBase, |
| 21 | + VisitNodeStep, |
| 22 | +} from "@eslint/plugin-kit"; |
| 23 | + |
| 24 | +//----------------------------------------------------------------------------- |
| 25 | +// Tests |
| 26 | +//----------------------------------------------------------------------------- |
| 27 | + |
| 28 | +// CallMethodStep |
| 29 | +class TestCallMethodStep extends CallMethodStep { |
| 30 | + constructor({ target, args }: { target: string; args: [string, number] }) { |
| 31 | + super({ target, args }); |
| 32 | + } |
| 33 | +} |
| 34 | +const step2 = new TestCallMethodStep({ target: "foo", args: ["foo", 42] }); |
| 35 | +step2.args satisfies unknown[]; |
| 36 | +step2.kind satisfies 2; |
| 37 | +step2.target satisfies string; |
| 38 | +step2.type satisfies "call"; |
| 39 | + |
| 40 | +// ConfigCommentParser |
| 41 | +const configCommentParser = new ConfigCommentParser(); |
| 42 | +configCommentParser.parseDirective("foo") satisfies |
| 43 | + | { label: string; value: string; justification: string } |
| 44 | + | undefined; |
| 45 | +const jsonLikeConfig = configCommentParser.parseJSONLikeConfig("bar"); |
| 46 | +if (jsonLikeConfig.ok) { |
| 47 | + jsonLikeConfig.config satisfies RulesConfig; |
| 48 | +} else { |
| 49 | + jsonLikeConfig.error.message satisfies string; |
| 50 | +} |
| 51 | +configCommentParser.parseListConfig("baz") satisfies BooleanConfig; |
| 52 | +configCommentParser.parseStringConfig("qux") satisfies StringConfig; |
| 53 | + |
| 54 | +// Directive |
| 55 | +void ((type: "disable" | "enable" | "disable-next-line" | "disable-line") => { |
| 56 | + const directive = new Directive({ |
| 57 | + type, |
| 58 | + node: {}, |
| 59 | + value: "foo", |
| 60 | + justification: "bar", |
| 61 | + }); |
| 62 | + directive.justification satisfies string; |
| 63 | + directive.node satisfies unknown; |
| 64 | + directive.type satisfies DirectiveType; |
| 65 | + directive.value satisfies string; |
| 66 | +}); |
| 67 | + |
| 68 | +// TextSourceCodeBase |
| 69 | +class TestTextSourceCode extends TextSourceCodeBase { |
| 70 | + declare ast: { foo: string; bar: number }; |
| 71 | + constructor({ |
| 72 | + text, |
| 73 | + ast, |
| 74 | + }: { |
| 75 | + text: string; |
| 76 | + ast: { foo: string; bar: number }; |
| 77 | + }) { |
| 78 | + super({ text, ast, lineEndingPattern: /\r\n|[\r\n\u2028\u2029]/u }); |
| 79 | + } |
| 80 | +} |
| 81 | +const sourceCode = new TestTextSourceCode({ |
| 82 | + text: "text", |
| 83 | + ast: { foo: "ABC", bar: 123 }, |
| 84 | +}); |
| 85 | +sourceCode.ast satisfies { foo: string; bar: number }; |
| 86 | +sourceCode.getAncestors({}) satisfies object[]; |
| 87 | +sourceCode.getLoc({}) satisfies SourceLocation; |
| 88 | +sourceCode.getParent({}) satisfies object | undefined; |
| 89 | +sourceCode.getRange({}) satisfies SourceRange; |
| 90 | +sourceCode.getText() satisfies string; |
| 91 | +sourceCode.getText({}, 0, 1) satisfies string; |
| 92 | + |
| 93 | +// VisitNodeStep |
| 94 | +class TestVisitNodeStep extends VisitNodeStep { |
| 95 | + constructor({ target, phase }: { target: object; phase: 1 | 2 }) { |
| 96 | + super({ target, phase, args: ["foo", 42] }); |
| 97 | + } |
| 98 | +} |
| 99 | +const step1 = new TestVisitNodeStep({ target: { foo: "bar" }, phase: 2 }); |
| 100 | +step1.args satisfies unknown[]; |
| 101 | +step1.kind satisfies 1; |
| 102 | +step1.phase satisfies 1 | 2; |
| 103 | +step1.target satisfies object; |
| 104 | +step1.type satisfies "visit"; |
0 commit comments