-
-
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.
- Loading branch information
1 parent
0dc490d
commit ffaaa6b
Showing
7 changed files
with
373 additions
and
8 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,81 @@ | ||
import { type TSESTree } from "@typescript-eslint/utils"; | ||
import { type RuleContext } from "@typescript-eslint/utils/ts-eslint"; | ||
import { deepmerge } from "deepmerge-ts"; | ||
import typeMatchesSpecifier from "ts-declaration-location"; | ||
|
||
import { getTypeDataOfNode } from "#eslint-plugin-functional/utils/rule"; | ||
import { | ||
type TypeSpecifier, | ||
typeMatchesPattern, | ||
} from "#eslint-plugin-functional/utils/type-specifier"; | ||
|
||
/** | ||
* Options that can be overridden. | ||
*/ | ||
export type OverridableOptions<CoreOptions> = CoreOptions & { | ||
overrides?: Array< | ||
{ | ||
specifiers: TypeSpecifier | TypeSpecifier[]; | ||
} & ( | ||
| { | ||
options: CoreOptions; | ||
inherit?: boolean; | ||
disable?: false; | ||
} | ||
| { | ||
disable: true; | ||
} | ||
) | ||
>; | ||
}; | ||
|
||
/** | ||
* Get the core options to use, taking into account overrides. | ||
*/ | ||
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, typeNode] = getTypeDataOfNode(node, context); | ||
const found = optionsObject.overrides?.find((override) => | ||
(Array.isArray(override.specifiers) | ||
? override.specifiers | ||
: [override.specifiers] | ||
).some( | ||
(specifier) => | ||
typeMatchesSpecifier(program, specifier, type) && | ||
(specifier.include === undefined || | ||
specifier.include.length === 0 || | ||
typeMatchesPattern( | ||
program, | ||
type, | ||
typeNode, | ||
specifier.include, | ||
specifier.exclude, | ||
)), | ||
), | ||
); | ||
|
||
if (found !== undefined) { | ||
if (found.disable === true) { | ||
return null; | ||
} | ||
if (found.inherit !== false) { | ||
return deepmerge(optionsObject, found.options) as CoreOptions; | ||
} | ||
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
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,81 @@ | ||
import { | ||
type JSONSchema4, | ||
type JSONSchema4ObjectSchema, | ||
} from "@typescript-eslint/utils/json-schema"; | ||
|
||
const typeSpecifierPatternSchemaProperties: JSONSchema4ObjectSchema["properties"] = | ||
{ | ||
name: schemaInstanceOrInstanceArray({ | ||
type: "string", | ||
}), | ||
pattern: schemaInstanceOrInstanceArray({ | ||
type: "string", | ||
}), | ||
ignoreName: schemaInstanceOrInstanceArray({ | ||
type: "string", | ||
}), | ||
ignorePattern: schemaInstanceOrInstanceArray({ | ||
type: "string", | ||
}), | ||
}; | ||
|
||
const typeSpecifierSchema: JSONSchema4 = { | ||
oneOf: [ | ||
{ | ||
type: "object", | ||
properties: { | ||
...typeSpecifierPatternSchemaProperties, | ||
from: { | ||
type: "string", | ||
enum: ["file"], | ||
}, | ||
path: { | ||
type: "string", | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
{ | ||
type: "object", | ||
properties: { | ||
...typeSpecifierPatternSchemaProperties, | ||
from: { | ||
type: "string", | ||
enum: ["lib"], | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
{ | ||
type: "object", | ||
properties: { | ||
...typeSpecifierPatternSchemaProperties, | ||
from: { | ||
type: "string", | ||
enum: ["package"], | ||
}, | ||
package: { | ||
type: "string", | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
], | ||
}; | ||
|
||
export const typeSpecifiersSchema: JSONSchema4 = | ||
schemaInstanceOrInstanceArray(typeSpecifierSchema); | ||
|
||
export function schemaInstanceOrInstanceArray( | ||
items: JSONSchema4, | ||
): NonNullable<JSONSchema4ObjectSchema["properties"]>[string] { | ||
return { | ||
oneOf: [ | ||
items, | ||
{ | ||
type: "array", | ||
items, | ||
}, | ||
], | ||
}; | ||
} |
Oops, something went wrong.