From a6f4f569eb9f2ce625031b8566b855c39dac0474 Mon Sep 17 00:00:00 2001 From: Rafael Santana Date: Wed, 13 Oct 2021 08:09:08 -0300 Subject: [PATCH 1/2] fix: accept readonly arrays in `fromFixture`'s parameters --- source/from-fixture-spec.ts | 2 +- source/from-fixture.ts | 41 +++++++++++++++++++++++++++---------- 2 files changed, 31 insertions(+), 12 deletions(-) diff --git a/source/from-fixture-spec.ts b/source/from-fixture-spec.ts index d48d804..89ec3d1 100644 --- a/source/from-fixture-spec.ts +++ b/source/from-fixture-spec.ts @@ -272,7 +272,7 @@ describe("fromFixture", () => { `, }, ], - } + } as const ); expect(test).to.have.property("code", `const name = "alice";`); expect(test).to.have.property("errors"); diff --git a/source/from-fixture.ts b/source/from-fixture.ts index e20545d..c242fe8 100644 --- a/source/from-fixture.ts +++ b/source/from-fixture.ts @@ -9,33 +9,42 @@ export function fromFixture( fixture: string, invalidTestCase?: { output?: string; - suggestions?: eslint.SuggestionOutput[] | null | undefined; + suggestions?: + | readonly eslint.SuggestionOutput[] + | null + | undefined; } ): eslint.InvalidTestCase; export function fromFixture< TMessageIds extends string, - TOptions extends unknown[] + TOptions extends readonly unknown[] >( fixture: string, invalidTestCase: Omit< eslint.InvalidTestCase, "code" | "errors" > & { - suggestions?: eslint.SuggestionOutput[] | null | undefined; + suggestions?: + | readonly eslint.SuggestionOutput[] + | null + | undefined; } ): eslint.InvalidTestCase; export function fromFixture< TMessageIds extends string, - TOptions extends unknown[] + TOptions extends readonly unknown[] >( fixture: string, invalidTestCase: Omit< eslint.InvalidTestCase, "code" | "errors" > & { - suggestions?: eslint.SuggestionOutput[] | null | undefined; + suggestions?: + | readonly eslint.SuggestionOutput[] + | null + | undefined; } = {} ): eslint.InvalidTestCase { const { suggestions, ...rest } = invalidTestCase; @@ -46,25 +55,31 @@ export function fromFixture< } function getSuggestions( - suggestions: eslint.SuggestionOutput[] | null | undefined, + suggestions: + | readonly eslint.SuggestionOutput[] + | null + | undefined, indices: string | undefined -): { suggestions?: eslint.SuggestionOutput[] } { +) { if (!suggestions || indices === "") { return {}; } if (indices === undefined) { - return { suggestions }; + return { suggestions } as const; } return { suggestions: indices .split(/\s+/) .map((index) => suggestions[Number.parseInt(index, 10)]), - }; + } as const; } function parseFixture( fixture: string, - suggestions: eslint.SuggestionOutput[] | null | undefined + suggestions: + | readonly eslint.SuggestionOutput[] + | null + | undefined ) { const errorRegExp = /^(?\s*)(?~+)\s*\[(?\w+)\s*(?.*?)(?:\s*suggest\s*(?[\d\s]*))?\]\s*$/; @@ -83,7 +98,11 @@ function parseFixture( endLine: length, line: length, messageId: match.groups.id as TMessageIds, - ...getSuggestions(suggestions, match.groups.indices?.trim()), + // TODO: Remove typecast once https://github.com/typescript-eslint/typescript-eslint/pull/3844 is available. + ...(getSuggestions( + suggestions, + match.groups.indices?.trim() + ) as eslint.TestCaseError["suggestions"]), }); } else { lines.push(line); From 5605aed746722ee1ac537539abd5e8928287949f Mon Sep 17 00:00:00 2001 From: Rafael Santana Date: Wed, 13 Oct 2021 08:15:30 -0300 Subject: [PATCH 2/2] refactor: remove redundant `undefined` --- source/from-fixture.ts | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) diff --git a/source/from-fixture.ts b/source/from-fixture.ts index c242fe8..46824ea 100644 --- a/source/from-fixture.ts +++ b/source/from-fixture.ts @@ -9,10 +9,7 @@ export function fromFixture( fixture: string, invalidTestCase?: { output?: string; - suggestions?: - | readonly eslint.SuggestionOutput[] - | null - | undefined; + suggestions?: readonly eslint.SuggestionOutput[] | null; } ): eslint.InvalidTestCase; @@ -25,10 +22,7 @@ export function fromFixture< eslint.InvalidTestCase, "code" | "errors" > & { - suggestions?: - | readonly eslint.SuggestionOutput[] - | null - | undefined; + suggestions?: readonly eslint.SuggestionOutput[] | null; } ): eslint.InvalidTestCase; @@ -41,10 +35,7 @@ export function fromFixture< eslint.InvalidTestCase, "code" | "errors" > & { - suggestions?: - | readonly eslint.SuggestionOutput[] - | null - | undefined; + suggestions?: readonly eslint.SuggestionOutput[] | null; } = {} ): eslint.InvalidTestCase { const { suggestions, ...rest } = invalidTestCase; @@ -76,10 +67,7 @@ function getSuggestions( function parseFixture( fixture: string, - suggestions: - | readonly eslint.SuggestionOutput[] - | null - | undefined + suggestions?: readonly eslint.SuggestionOutput[] | null ) { const errorRegExp = /^(?\s*)(?~+)\s*\[(?\w+)\s*(?.*?)(?:\s*suggest\s*(?[\d\s]*))?\]\s*$/;