-
-
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 status checks…
part: set up overridable options
1 parent
39e1740
commit 7ff9d4d
Showing
7 changed files
with
479 additions
and
7 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,185 @@ | ||
import assert from "node:assert/strict"; | ||
|
||
import { type TSESTree } from "@typescript-eslint/utils"; | ||
import { type RuleContext } from "@typescript-eslint/utils/ts-eslint"; | ||
import { deepmerge } from "deepmerge-ts"; | ||
import typeMatchesSpecifier, { | ||
type TypeDeclarationSpecifier, | ||
} from "ts-declaration-location"; | ||
import { type Program, type Type, type TypeNode } from "typescript"; | ||
|
||
import { getTypeDataOfNode } from "#eslint-plugin-functional/utils/rule"; | ||
import { | ||
type RawTypeSpecifier, | ||
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; | ||
} | ||
) | ||
>; | ||
}; | ||
|
||
export type RawOverridableOptions<CoreOptions> = CoreOptions & { | ||
overrides?: Array<{ | ||
specifiers?: RawTypeSpecifier | RawTypeSpecifier[]; | ||
options?: CoreOptions; | ||
inherit?: boolean; | ||
disable?: boolean; | ||
}>; | ||
}; | ||
|
||
export function upgradeRawOverridableOptions<CoreOptions>( | ||
raw: Readonly<RawOverridableOptions<CoreOptions>>, | ||
): OverridableOptions<CoreOptions> { | ||
return { | ||
...raw, | ||
overrides: | ||
raw.overrides?.map((override) => ({ | ||
...override, | ||
specifiers: | ||
override.specifiers === undefined | ||
? [] | ||
: Array.isArray(override.specifiers) | ||
? override.specifiers.map(upgradeRawTypeSpecifier) | ||
: [upgradeRawTypeSpecifier(override.specifiers)], | ||
})) ?? [], | ||
} as OverridableOptions<CoreOptions>; | ||
} | ||
|
||
function upgradeRawTypeSpecifier(raw: RawTypeSpecifier): TypeSpecifier { | ||
const { ignoreName, ignorePattern, name, pattern, ...rest } = raw; | ||
|
||
const names = name === undefined ? [] : Array.isArray(name) ? name : [name]; | ||
|
||
const patterns = ( | ||
pattern === undefined ? [] : Array.isArray(pattern) ? pattern : [pattern] | ||
).map((p) => new RegExp(p, "u")); | ||
|
||
const ignoreNames = | ||
ignoreName === undefined | ||
? [] | ||
: Array.isArray(ignoreName) | ||
? ignoreName | ||
: [ignoreName]; | ||
|
||
const ignorePatterns = ( | ||
ignorePattern === undefined | ||
? [] | ||
: Array.isArray(ignorePattern) | ||
? ignorePattern | ||
: [ignorePattern] | ||
).map((p) => new RegExp(p, "u")); | ||
|
||
const include = [...names, ...patterns]; | ||
const exclude = [...ignoreNames, ...ignorePatterns]; | ||
|
||
return { | ||
...rest, | ||
include, | ||
exclude, | ||
}; | ||
} | ||
|
||
/** | ||
* Get the core options to use, taking into account overrides. | ||
*/ | ||
export function getCoreOptions< | ||
CoreOptions extends object, | ||
Options extends Readonly<OverridableOptions<CoreOptions>>, | ||
>( | ||
node: TSESTree.Node, | ||
context: Readonly<RuleContext<string, unknown[]>>, | ||
options: Readonly<Options>, | ||
): CoreOptions | null { | ||
const program = context.sourceCode.parserServices?.program ?? undefined; | ||
if (program === undefined) { | ||
return options; | ||
} | ||
|
||
const [type, typeNode] = getTypeDataOfNode(node, context); | ||
return getCoreOptionsForType(type, typeNode, context, options); | ||
} | ||
|
||
export function getCoreOptionsForType< | ||
CoreOptions extends object, | ||
Options extends Readonly<OverridableOptions<CoreOptions>>, | ||
>( | ||
type: Type, | ||
typeNode: TypeNode | null, | ||
context: Readonly<RuleContext<string, unknown[]>>, | ||
options: Readonly<Options>, | ||
): CoreOptions | null { | ||
const program = context.sourceCode.parserServices?.program ?? undefined; | ||
if (program === undefined) { | ||
return options; | ||
} | ||
|
||
const found = options.overrides?.find((override) => | ||
(Array.isArray(override.specifiers) | ||
? override.specifiers | ||
: [override.specifiers] | ||
).some( | ||
(specifier) => | ||
typeMatchesSpecifierDeep(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(options, found.options) as CoreOptions; | ||
} | ||
return found.options; | ||
} | ||
|
||
return options; | ||
} | ||
|
||
function typeMatchesSpecifierDeep( | ||
program: Program, | ||
specifier: TypeDeclarationSpecifier, | ||
type: Type, | ||
) { | ||
const stack = [type]; | ||
// eslint-disable-next-line functional/no-loop-statements -- best to do this iteratively. | ||
while (stack.length > 0) { | ||
const t = stack.pop() ?? assert.fail(); | ||
|
||
if (typeMatchesSpecifier(program, specifier, t)) { | ||
return true; | ||
} | ||
|
||
if (t.aliasTypeArguments !== undefined) { | ||
stack.push(...t.aliasTypeArguments); | ||
} | ||
} | ||
|
||
return false; | ||
} |
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, | ||
}, | ||
], | ||
}; | ||
} |
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,161 @@ | ||
import assert from "node:assert/strict"; | ||
|
||
import { type TypeDeclarationSpecifier } from "ts-declaration-location"; | ||
import { type Program, type Type, type TypeNode } from "typescript"; | ||
|
||
import ts from "#eslint-plugin-functional/conditional-imports/typescript"; | ||
|
||
export type TypePattern = string | RegExp; | ||
|
||
type TypeSpecifierPattern = { | ||
include?: TypePattern[]; | ||
exclude?: TypePattern[]; | ||
}; | ||
|
||
/** | ||
* How a type can be specified. | ||
*/ | ||
export type TypeSpecifier = TypeSpecifierPattern & TypeDeclarationSpecifier; | ||
|
||
export type RawTypeSpecifier = { | ||
name?: string | string[]; | ||
pattern?: string | string[]; | ||
ignoreName?: string | string[]; | ||
ignorePattern?: string | string[]; | ||
} & TypeDeclarationSpecifier; | ||
|
||
export function typeMatchesPattern( | ||
program: Program, | ||
type: Type, | ||
typeNode: TypeNode | null, | ||
include: ReadonlyArray<TypePattern>, | ||
exclude: ReadonlyArray<TypePattern> = [], | ||
) { | ||
assert(ts !== undefined); | ||
|
||
if (include.length === 0) { | ||
return false; | ||
} | ||
|
||
let m_shouldInclude = false; | ||
|
||
const typeNameAlias = getTypeAliasName(type, typeNode); | ||
if (typeNameAlias !== null) { | ||
const testTypeNameAlias = (pattern: TypePattern) => | ||
typeof pattern === "string" | ||
? pattern === typeNameAlias | ||
: pattern.test(typeNameAlias); | ||
|
||
if (exclude.some(testTypeNameAlias)) { | ||
return false; | ||
} | ||
m_shouldInclude ||= include.some(testTypeNameAlias); | ||
} | ||
|
||
const typeValue = getTypeAsString(program, type, typeNode); | ||
const testTypeValue = (pattern: TypePattern) => | ||
typeof pattern === "string" | ||
? pattern === typeValue | ||
: pattern.test(typeValue); | ||
|
||
if (exclude.some(testTypeValue)) { | ||
return false; | ||
} | ||
m_shouldInclude ||= include.some(testTypeValue); | ||
|
||
const typeNameName = extractTypeName(typeValue); | ||
if (typeNameName !== null) { | ||
const testTypeNameName = (pattern: TypePattern) => | ||
typeof pattern === "string" | ||
? pattern === typeNameName | ||
: pattern.test(typeNameName); | ||
|
||
if (exclude.some(testTypeNameName)) { | ||
return false; | ||
} | ||
m_shouldInclude ||= include.some(testTypeNameName); | ||
} | ||
|
||
// Special handling for arrays not written in generic syntax. | ||
if (program.getTypeChecker().isArrayType(type) && typeNode !== null) { | ||
if ( | ||
(ts.isTypeOperatorNode(typeNode) && | ||
typeNode.operator === ts.SyntaxKind.ReadonlyKeyword) || | ||
(ts.isTypeOperatorNode(typeNode.parent) && | ||
typeNode.parent.operator === ts.SyntaxKind.ReadonlyKeyword) | ||
) { | ||
const testIsReadonlyArray = (pattern: TypePattern) => | ||
typeof pattern === "string" && pattern === "ReadonlyArray"; | ||
|
||
if (exclude.some(testIsReadonlyArray)) { | ||
return false; | ||
} | ||
m_shouldInclude ||= include.some(testIsReadonlyArray); | ||
} else { | ||
const testIsArray = (pattern: TypePattern) => | ||
typeof pattern === "string" && pattern === "Array"; | ||
|
||
if (exclude.some(testIsArray)) { | ||
return false; | ||
} | ||
m_shouldInclude ||= include.some(testIsArray); | ||
} | ||
} | ||
|
||
return m_shouldInclude; | ||
} | ||
|
||
/** | ||
* Get the type alias name from the given type data. | ||
* | ||
* Null will be returned if the type is not a type alias. | ||
*/ | ||
function getTypeAliasName(type: Type, typeNode: TypeNode | null) { | ||
assert(ts !== undefined); | ||
|
||
if (typeNode === null) { | ||
const t = "target" in type ? (type.target as Type) : type; | ||
return t.aliasSymbol?.getName() ?? null; | ||
} | ||
|
||
return ts.isTypeAliasDeclaration(typeNode.parent) | ||
? typeNode.parent.name.getText() | ||
: null; | ||
} | ||
|
||
/** | ||
* Get the type as a string. | ||
*/ | ||
function getTypeAsString( | ||
program: Program, | ||
type: Type, | ||
typeNode: TypeNode | null, | ||
) { | ||
assert(ts !== undefined); | ||
|
||
return typeNode === null | ||
? program | ||
.getTypeChecker() | ||
.typeToString( | ||
type, | ||
undefined, | ||
ts.TypeFormatFlags.AddUndefined | | ||
ts.TypeFormatFlags.NoTruncation | | ||
ts.TypeFormatFlags.OmitParameterModifiers | | ||
ts.TypeFormatFlags.UseFullyQualifiedType | | ||
ts.TypeFormatFlags.WriteArrayAsGenericType | | ||
ts.TypeFormatFlags.WriteArrowStyleSignature | | ||
ts.TypeFormatFlags.WriteTypeArgumentsOfSignature, | ||
) | ||
: typeNode.getText(); | ||
} | ||
|
||
/** | ||
* Get the type name extracted from the the type's string. | ||
* | ||
* This only work if the type is a type reference. | ||
*/ | ||
function extractTypeName(typeValue: string) { | ||
const match = /^([^<]+)<.+>$/u.exec(typeValue); | ||
return match?.[1] ?? null; | ||
} |