Skip to content

Commit

Permalink
fix: accept readonly arrays in fromFixture's parameters (#6)
Browse files Browse the repository at this point in the history
* fix: accept readonly arrays in `fromFixture`'s parameters

* refactor: remove redundant `undefined`
  • Loading branch information
rafaelss95 committed Oct 17, 2021
1 parent 397a0e9 commit 11a0792
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
2 changes: 1 addition & 1 deletion source/from-fixture-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ describe("fromFixture", () => {
`,
},
],
}
} as const
);
expect(test).to.have.property("code", `const name = "alice";`);
expect(test).to.have.property("errors");
Expand Down
29 changes: 18 additions & 11 deletions source/from-fixture.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,33 +9,33 @@ export function fromFixture<TMessageIds extends string>(
fixture: string,
invalidTestCase?: {
output?: string;
suggestions?: eslint.SuggestionOutput<TMessageIds>[] | null | undefined;
suggestions?: readonly eslint.SuggestionOutput<TMessageIds>[] | null;
}
): eslint.InvalidTestCase<TMessageIds, never>;

export function fromFixture<
TMessageIds extends string,
TOptions extends unknown[]
TOptions extends readonly unknown[]
>(
fixture: string,
invalidTestCase: Omit<
eslint.InvalidTestCase<TMessageIds, TOptions>,
"code" | "errors"
> & {
suggestions?: eslint.SuggestionOutput<TMessageIds>[] | null | undefined;
suggestions?: readonly eslint.SuggestionOutput<TMessageIds>[] | null;
}
): eslint.InvalidTestCase<TMessageIds, TOptions>;

export function fromFixture<
TMessageIds extends string,
TOptions extends unknown[]
TOptions extends readonly unknown[]
>(
fixture: string,
invalidTestCase: Omit<
eslint.InvalidTestCase<TMessageIds, TOptions>,
"code" | "errors"
> & {
suggestions?: eslint.SuggestionOutput<TMessageIds>[] | null | undefined;
suggestions?: readonly eslint.SuggestionOutput<TMessageIds>[] | null;
} = {}
): eslint.InvalidTestCase<TMessageIds, TOptions> {
const { suggestions, ...rest } = invalidTestCase;
Expand All @@ -46,25 +46,28 @@ export function fromFixture<
}

function getSuggestions<TMessageIds extends string>(
suggestions: eslint.SuggestionOutput<TMessageIds>[] | null | undefined,
suggestions:
| readonly eslint.SuggestionOutput<TMessageIds>[]
| null
| undefined,
indices: string | undefined
): { suggestions?: eslint.SuggestionOutput<TMessageIds>[] } {
) {
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<TMessageIds extends string>(
fixture: string,
suggestions: eslint.SuggestionOutput<TMessageIds>[] | null | undefined
suggestions?: readonly eslint.SuggestionOutput<TMessageIds>[] | null
) {
const errorRegExp =
/^(?<indent>\s*)(?<error>~+)\s*\[(?<id>\w+)\s*(?<data>.*?)(?:\s*suggest\s*(?<indices>[\d\s]*))?\]\s*$/;
Expand All @@ -83,7 +86,11 @@ function parseFixture<TMessageIds extends string>(
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<TMessageIds>["suggestions"]),
});
} else {
lines.push(line);
Expand Down

0 comments on commit 11a0792

Please sign in to comment.