-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(prefer-immutable-types): allow overriding options based on where…
… the type is declared fix #800
- Loading branch information
1 parent
20f7137
commit 78839d6
Showing
7 changed files
with
308 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./ignore"; | ||
export * from "./overrides"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { type TSESTree } from "@typescript-eslint/utils"; | ||
import { type RuleContext } from "@typescript-eslint/utils/ts-eslint"; | ||
import typeMatchesSpecifier, { | ||
type TypeDeclarationSpecifier, | ||
} from "ts-declaration-location"; | ||
|
||
import { getTypeOfNode } from "../utils/rule"; | ||
|
||
/** | ||
* Options that can be overridden. | ||
*/ | ||
export type OverridableOptions<CoreOptions> = CoreOptions & { | ||
overrides?: Array< | ||
{ | ||
specifiers: TypeDeclarationSpecifier | TypeDeclarationSpecifier[]; | ||
} & ( | ||
| { | ||
options: CoreOptions; | ||
disable?: false; | ||
} | ||
| { | ||
disable: true; | ||
} | ||
) | ||
>; | ||
}; | ||
|
||
/** | ||
* Get the core options to use, taking into account overrides. | ||
* | ||
* @throws when there is a configuration error. | ||
*/ | ||
export function getCoreOptions< | ||
CoreOptions extends object, | ||
Options extends readonly [Readonly<OverridableOptions<CoreOptions>>], | ||
>( | ||
node: TSESTree.Node, | ||
context: Readonly<RuleContext<string, Options>>, | ||
options: Readonly<Options>, | ||
): CoreOptions | null { | ||
const [optionsObject] = options; | ||
|
||
const program = context.sourceCode.parserServices?.program ?? undefined; | ||
if (program === undefined) { | ||
return optionsObject; | ||
} | ||
|
||
const type = getTypeOfNode(node, context); | ||
const found = optionsObject.overrides?.find((override) => | ||
(Array.isArray(override.specifiers) | ||
? override.specifiers | ||
: [override.specifiers] | ||
).some((specifier) => typeMatchesSpecifier(program, specifier, type)), | ||
); | ||
|
||
if (found !== undefined) { | ||
if (found.disable === true) { | ||
return null; | ||
} | ||
if (found.options === undefined) { | ||
// eslint-disable-next-line functional/no-throw-statements | ||
throw new Error("Configuration error: No options found for override."); | ||
} | ||
return found.options; | ||
} | ||
|
||
return optionsObject; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.