Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: accept readonly arrays in fromFixture's parameters #6

Merged
merged 2 commits into from Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/from-fixture-spec.ts
Expand Up @@ -272,7 +272,7 @@ describe("fromFixture", () => {
`,
},
],
}
} as const
);
expect(test).to.have.property("code", `const name = "alice";`);
expect(test).to.have.property("errors");
Expand Down
41 changes: 30 additions & 11 deletions source/from-fixture.ts
Expand Up @@ -9,33 +9,42 @@ export function fromFixture<TMessageIds extends string>(
fixture: string,
invalidTestCase?: {
output?: string;
suggestions?: eslint.SuggestionOutput<TMessageIds>[] | null | undefined;
suggestions?:
| readonly eslint.SuggestionOutput<TMessageIds>[]
| null
| undefined;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When typescript-eslint/typescript-eslint#3844 gets in, it could be simply:

Suggested change
suggestions?:
| readonly eslint.SuggestionOutput<TMessageIds>[]
| null
| undefined;
suggestions: eslint.TestCaseError<TMessageIds>["suggestions"];

}
): 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
| undefined;
}
): 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
| undefined;
} = {}
): eslint.InvalidTestCase<TMessageIds, TOptions> {
const { suggestions, ...rest } = invalidTestCase;
Expand All @@ -46,25 +55,31 @@ 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
| undefined
) {
const errorRegExp =
/^(?<indent>\s*)(?<error>~+)\s*\[(?<id>\w+)\s*(?<data>.*?)(?:\s*suggest\s*(?<indices>[\d\s]*))?\]\s*$/;
Expand All @@ -83,7 +98,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