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

feat(experimental-utils): allow rule options to be a readonly tuple #1924

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -17,7 +17,7 @@ function RuleCreator(urlCreator: (ruleName: string) => string) {
// This function will get much easier to call when this is merged https://github.com/Microsoft/TypeScript/pull/26349
// TODO - when the above PR lands; add type checking for the context.report `data` property
return function createRule<
TOptions extends unknown[],
TOptions extends readonly unknown[],
TMessageIds extends string,
TRuleListener extends RuleListener = RuleListener
>({
Expand Down
8 changes: 6 additions & 2 deletions packages/experimental-utils/src/eslint-utils/applyDefault.ts
Expand Up @@ -7,12 +7,14 @@ import { deepMerge, isObjectNotArray } from './deepMerge';
* @param userOptions the user opts
* @returns the options with defaults
*/
function applyDefault<TUser extends unknown[], TDefault extends TUser>(
function applyDefault<TUser extends readonly unknown[], TDefault extends TUser>(
defaultOptions: TDefault,
userOptions: TUser | null,
): TDefault {
// clone defaults
const options: TDefault = JSON.parse(JSON.stringify(defaultOptions));
const options: AsMutable<TDefault> = JSON.parse(
JSON.stringify(defaultOptions),
);

if (userOptions === null || userOptions === undefined) {
return options;
Expand All @@ -33,4 +35,6 @@ function applyDefault<TUser extends unknown[], TDefault extends TUser>(
return options;
}

type AsMutable<T extends {}> = { -readonly [TKey in keyof T]: T[TKey] };

export { applyDefault };