@rc312/oxlint-rules 0.0.0-stage → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +43 -2
- package/dist/anti-slop/effect/index.d.ts +3 -0
- package/dist/anti-slop/effect/index.js +18 -0
- package/dist/anti-slop/effect/rules/no-manual-effect-error-tag.d.ts +1 -0
- package/dist/anti-slop/effect/rules/no-manual-effect-error-tag.js +39 -0
- package/dist/anti-slop/effect/rules/no-manual-tag-comparison.d.ts +1 -0
- package/dist/anti-slop/effect/rules/no-manual-tag-comparison.js +32 -0
- package/dist/anti-slop/effect/rules/no-manual-tagged-construction.d.ts +1 -0
- package/dist/anti-slop/effect/rules/no-manual-tagged-construction.js +27 -0
- package/dist/anti-slop/effect/rules/no-service-constructor-imports.d.ts +2 -0
- package/dist/anti-slop/effect/rules/no-service-constructor-imports.js +44 -0
- package/dist/anti-slop/effect/rules/prefer-effect-match.d.ts +1 -0
- package/dist/anti-slop/effect/rules/prefer-effect-match.js +48 -0
- package/dist/anti-slop/effect/shared/tagged-values.d.ts +8 -0
- package/dist/anti-slop/effect/shared/tagged-values.js +68 -0
- package/dist/anti-slop/index.d.ts +3 -0
- package/dist/anti-slop/index.js +44 -0
- package/dist/anti-slop/rules/no-array-filter-map.d.ts +2 -0
- package/dist/anti-slop/rules/no-array-filter-map.js +30 -0
- package/dist/anti-slop/rules/no-chained-type-assertions.d.ts +2 -0
- package/dist/anti-slop/rules/no-chained-type-assertions.js +60 -0
- package/dist/anti-slop/rules/no-conditional-empty-object-spread.d.ts +2 -0
- package/dist/anti-slop/rules/no-conditional-empty-object-spread.js +40 -0
- package/dist/anti-slop/rules/no-known-value-widening.d.ts +2 -0
- package/dist/anti-slop/rules/no-known-value-widening.js +300 -0
- package/dist/anti-slop/rules/no-module-mocking.d.ts +2 -0
- package/dist/anti-slop/rules/no-module-mocking.js +69 -0
- package/dist/anti-slop/rules/no-object-parameters.d.ts +2 -0
- package/dist/anti-slop/rules/no-object-parameters.js +56 -0
- package/dist/anti-slop/rules/no-reduce-accumulator-copy.d.ts +2 -0
- package/dist/anti-slop/rules/no-reduce-accumulator-copy.js +99 -0
- package/dist/anti-slop/rules/no-reflect-apply.d.ts +2 -0
- package/dist/anti-slop/rules/no-reflect-apply.js +25 -0
- package/dist/anti-slop/rules/no-reflect-get.d.ts +2 -0
- package/dist/anti-slop/rules/no-reflect-get.js +25 -0
- package/dist/anti-slop/rules/no-runtime-typeof.d.ts +2 -0
- package/dist/anti-slop/rules/no-runtime-typeof.js +64 -0
- package/dist/anti-slop/rules/no-shape-in-symbol-names.d.ts +2 -0
- package/dist/anti-slop/rules/no-shape-in-symbol-names.js +40 -0
- package/dist/anti-slop/rules/no-unknown-parameters.d.ts +2 -0
- package/dist/anti-slop/rules/no-unknown-parameters.js +51 -0
- package/dist/anti-slop/rules/no-unknown-returns.d.ts +2 -0
- package/dist/anti-slop/rules/no-unknown-returns.js +58 -0
- package/dist/anti-slop/rules/no-unknown-type-aliases.d.ts +2 -0
- package/dist/anti-slop/rules/no-unknown-type-aliases.js +40 -0
- package/dist/anti-slop/rules/no-unsafe-dictionary-type.d.ts +2 -0
- package/dist/anti-slop/rules/no-unsafe-dictionary-type.js +134 -0
- package/dist/anti-slop/rules/no-widen-then-assert.d.ts +2 -0
- package/dist/anti-slop/rules/no-widen-then-assert.js +270 -0
- package/dist/anti-slop/rules/require-readable-spacing.d.ts +3 -0
- package/dist/anti-slop/rules/require-readable-spacing.js +65 -0
- package/dist/anti-slop/rules/require-safety-comment-for-type-assertion.d.ts +2 -0
- package/dist/anti-slop/rules/require-safety-comment-for-type-assertion.js +100 -0
- package/dist/anti-slop/shared/array-method.d.ts +12 -0
- package/dist/anti-slop/shared/array-method.js +84 -0
- package/dist/anti-slop/shared/dictionary-types.d.ts +20 -0
- package/dist/anti-slop/shared/dictionary-types.js +377 -0
- package/dist/anti-slop/shared/function-parameters.d.ts +8 -0
- package/dist/anti-slop/shared/function-parameters.js +40 -0
- package/dist/anti-slop/shared/lexical-type-parameters.d.ts +5 -0
- package/dist/anti-slop/shared/lexical-type-parameters.js +47 -0
- package/dist/anti-slop/shared/reflect-method.d.ts +3 -0
- package/dist/anti-slop/shared/reflect-method.js +20 -0
- package/dist/anti-slop/shared/scope.d.ts +3 -0
- package/dist/anti-slop/shared/scope.js +11 -0
- package/dist/anti-slop/shared/type-alias-resolution.d.ts +23 -0
- package/dist/anti-slop/shared/type-alias-resolution.js +162 -0
- package/dist/anti-slop/vendor/eslint-stylistic/padding-line-ast.d.ts +27 -0
- package/dist/anti-slop/vendor/eslint-stylistic/padding-line-ast.js +28 -0
- package/dist/anti-slop/vendor/eslint-stylistic/padding-line-between-statements.d.ts +37 -0
- package/dist/anti-slop/vendor/eslint-stylistic/padding-line-between-statements.js +703 -0
- package/dist/anti-slop/vendor/eslint-stylistic/padding-line-options.d.ts +16 -0
- package/dist/anti-slop/vendor/eslint-stylistic/padding-line-options.js +2 -0
- package/dist/configs/base.d.ts +28 -0
- package/dist/configs/base.js +27 -0
- package/dist/configs/effect-starter.d.ts +10 -0
- package/dist/configs/effect-starter.js +10 -0
- package/dist/configs/effect.d.ts +14 -0
- package/dist/configs/effect.js +16 -0
- package/dist/effect-starter.d.ts +3 -0
- package/dist/effect-starter.js +40 -0
- package/package.json +46 -4
- package/src/anti-slop/LICENSE +21 -0
- package/src/anti-slop/UPSTREAM.md +14 -0
- package/src/anti-slop/vendor/eslint-stylistic/LICENSE +22 -0
- package/src/anti-slop/vendor/eslint-stylistic/UPSTREAM.md +28 -0
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/** Return whether a type is or contains TypeScript's absorbing unknown top type. */
|
|
2
|
+
export function containsUnknownType(type) {
|
|
3
|
+
if (type.type === "TSUnknownKeyword")
|
|
4
|
+
return true;
|
|
5
|
+
if (type.type === "TSParenthesizedType")
|
|
6
|
+
return containsUnknownType(type.typeAnnotation);
|
|
7
|
+
return type.type === "TSUnionType" && type.types.some(containsUnknownType);
|
|
8
|
+
}
|
|
9
|
+
/** Return the TypeScript annotation attached to a function parameter or its wrapped binding. */
|
|
10
|
+
export function functionParameterTypeAnnotation(parameter) {
|
|
11
|
+
if (parameter.type === "TSParameterProperty") {
|
|
12
|
+
return functionParameterTypeAnnotation(parameter.parameter);
|
|
13
|
+
}
|
|
14
|
+
if (parameter.type === "RestElement") {
|
|
15
|
+
return parameter.typeAnnotation ?? functionParameterTypeAnnotation(parameter.argument);
|
|
16
|
+
}
|
|
17
|
+
if (parameter.type === "AssignmentPattern") {
|
|
18
|
+
return parameter.typeAnnotation ?? functionParameterTypeAnnotation(parameter.left);
|
|
19
|
+
}
|
|
20
|
+
return parameter.typeAnnotation;
|
|
21
|
+
}
|
|
22
|
+
/** Return only a function parameter's local binding, excluding its annotation and default value. */
|
|
23
|
+
export function functionParameterBindingName(parameter, sourceCode) {
|
|
24
|
+
if (parameter.type === "TSParameterProperty") {
|
|
25
|
+
return functionParameterBindingName(parameter.parameter, sourceCode);
|
|
26
|
+
}
|
|
27
|
+
if (parameter.type === "AssignmentPattern") {
|
|
28
|
+
return functionParameterBindingName(parameter.left, sourceCode);
|
|
29
|
+
}
|
|
30
|
+
if (parameter.type === "RestElement") {
|
|
31
|
+
return functionParameterBindingName(parameter.argument, sourceCode);
|
|
32
|
+
}
|
|
33
|
+
if (parameter.type === "Identifier")
|
|
34
|
+
return parameter.name;
|
|
35
|
+
const sourceText = sourceCode.getText(parameter);
|
|
36
|
+
const annotationStart = parameter.typeAnnotation?.start;
|
|
37
|
+
return annotationStart === undefined
|
|
38
|
+
? sourceText
|
|
39
|
+
: sourceText.slice(0, annotationStart - parameter.start).trimEnd();
|
|
40
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
2
|
+
type VisitorKeys = Readonly<Record<string, readonly string[]>>;
|
|
3
|
+
/** Collect type binders that are in scope at a node and can shadow module aliases. */
|
|
4
|
+
export declare function lexicalTypeParameterNames(node: ESTree.Node, visitorKeys: VisitorKeys): ReadonlySet<string>;
|
|
5
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
function isNode(value) {
|
|
2
|
+
return (typeof value === "object" &&
|
|
3
|
+
value !== null &&
|
|
4
|
+
"type" in value &&
|
|
5
|
+
typeof value.type === "string");
|
|
6
|
+
}
|
|
7
|
+
function collectInferTypeParameterNames(node, visitorKeys, names) {
|
|
8
|
+
if (node.type === "TSInferType")
|
|
9
|
+
names.add(node.typeParameter.name.name);
|
|
10
|
+
const record = node;
|
|
11
|
+
for (const key of visitorKeys[node.type] ?? []) {
|
|
12
|
+
const value = record[key];
|
|
13
|
+
if (isNode(value)) {
|
|
14
|
+
collectInferTypeParameterNames(value, visitorKeys, names);
|
|
15
|
+
continue;
|
|
16
|
+
}
|
|
17
|
+
if (!Array.isArray(value))
|
|
18
|
+
continue;
|
|
19
|
+
for (const child of value) {
|
|
20
|
+
if (isNode(child))
|
|
21
|
+
collectInferTypeParameterNames(child, visitorKeys, names);
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
/** Collect type binders that are in scope at a node and can shadow module aliases. */
|
|
26
|
+
export function lexicalTypeParameterNames(node, visitorKeys) {
|
|
27
|
+
const names = new Set();
|
|
28
|
+
let descendant = node;
|
|
29
|
+
let current = node;
|
|
30
|
+
while (current !== null && current.type !== "Program") {
|
|
31
|
+
if ("typeParameters" in current) {
|
|
32
|
+
for (const parameter of current.typeParameters?.params ?? []) {
|
|
33
|
+
names.add(parameter.name.name);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (current.type === "TSMappedType" &&
|
|
37
|
+
(descendant === current.nameType || descendant === current.typeAnnotation)) {
|
|
38
|
+
names.add(current.key.name);
|
|
39
|
+
}
|
|
40
|
+
if (current.type === "TSConditionalType" && descendant === current.trueType) {
|
|
41
|
+
collectInferTypeParameterNames(current.extendsType, visitorKeys, names);
|
|
42
|
+
}
|
|
43
|
+
descendant = current;
|
|
44
|
+
current = current.parent;
|
|
45
|
+
}
|
|
46
|
+
return names;
|
|
47
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
2
|
+
/** Reports whether a call target names one method on the global Reflect object. */
|
|
3
|
+
export declare function isGlobalReflectMethodCall(sourceCode: SourceCode, callee: ESTree.Expression, methodName: string): boolean;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { resolveVariable } from "./scope.js";
|
|
2
|
+
function isGlobalReflect(sourceCode, expression) {
|
|
3
|
+
if (expression.type !== "Identifier" || expression.name !== "Reflect")
|
|
4
|
+
return false;
|
|
5
|
+
if (sourceCode.isGlobalReference(expression))
|
|
6
|
+
return true;
|
|
7
|
+
const variable = resolveVariable(sourceCode, expression);
|
|
8
|
+
return variable === null || variable.defs.length === 0;
|
|
9
|
+
}
|
|
10
|
+
/** Reports whether a call target names one method on the global Reflect object. */
|
|
11
|
+
export function isGlobalReflectMethodCall(sourceCode, callee, methodName) {
|
|
12
|
+
if (!("property" in callee) || !("object" in callee) || !("computed" in callee))
|
|
13
|
+
return false;
|
|
14
|
+
if (!isGlobalReflect(sourceCode, callee.object))
|
|
15
|
+
return false;
|
|
16
|
+
const property = callee.property;
|
|
17
|
+
return callee.computed
|
|
18
|
+
? property.type === "Literal" && property.value === methodName
|
|
19
|
+
: property.type === "Identifier" && property.name === methodName;
|
|
20
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Resolve an identifier to its binding by walking lexical scopes upward. */
|
|
2
|
+
export function resolveVariable(sourceCode, identifier) {
|
|
3
|
+
let scope = sourceCode.getScope(identifier);
|
|
4
|
+
while (scope !== null) {
|
|
5
|
+
const variable = scope.set.get(identifier.name);
|
|
6
|
+
if (variable !== undefined)
|
|
7
|
+
return variable;
|
|
8
|
+
scope = scope.upper;
|
|
9
|
+
}
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ESTree } from "@oxlint/plugins";
|
|
2
|
+
type VisitorKeys = Readonly<Record<string, readonly string[]>>;
|
|
3
|
+
type TypeScope = ESTree.Node;
|
|
4
|
+
type TypeBinding = {
|
|
5
|
+
readonly alias: ESTree.TSTypeAliasDeclaration | null;
|
|
6
|
+
readonly name: string;
|
|
7
|
+
readonly scope: TypeScope;
|
|
8
|
+
};
|
|
9
|
+
export type TypeAliasEnvironment = {
|
|
10
|
+
readonly aliases: readonly ESTree.TSTypeAliasDeclaration[];
|
|
11
|
+
readonly bindingsByName: ReadonlyMap<string, readonly TypeBinding[]>;
|
|
12
|
+
readonly visitorKeys: VisitorKeys;
|
|
13
|
+
};
|
|
14
|
+
export type ResolvedTypeMatcher = (type: ESTree.TSType, matches: (child: ESTree.TSType) => boolean) => boolean;
|
|
15
|
+
/** Collect every lexical type alias and competing type binding in a program. */
|
|
16
|
+
export declare function createTypeAliasEnvironment(program: ESTree.Program, visitorKeys: VisitorKeys): TypeAliasEnvironment;
|
|
17
|
+
/** Resolve the nearest visible alias with this name, respecting lexical shadowing. */
|
|
18
|
+
export declare function visibleTypeAlias(name: string, use: ESTree.Node, environment: TypeAliasEnvironment): ESTree.TSTypeAliasDeclaration | null;
|
|
19
|
+
/** Return whether a local declaration shadows a built-in type at this use. */
|
|
20
|
+
export declare function hasVisibleTypeBinding(name: string, use: ESTree.Node, environment: TypeAliasEnvironment): boolean;
|
|
21
|
+
/** Match a type after resolving visible aliases and substituting their type parameters. */
|
|
22
|
+
export declare function resolvedTypeMatches(type: ESTree.TSType, environment: TypeAliasEnvironment, matcher: ResolvedTypeMatcher): boolean;
|
|
23
|
+
export {};
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { lexicalTypeParameterNames } from "./lexical-type-parameters.js";
|
|
2
|
+
const environmentsByProgram = new WeakMap();
|
|
3
|
+
function isNode(value) {
|
|
4
|
+
return (typeof value === "object" &&
|
|
5
|
+
value !== null &&
|
|
6
|
+
"type" in value &&
|
|
7
|
+
typeof value.type === "string");
|
|
8
|
+
}
|
|
9
|
+
function enclosingTypeScope(node) {
|
|
10
|
+
let current = node.parent;
|
|
11
|
+
while (current !== null) {
|
|
12
|
+
if (current.type === "Program" ||
|
|
13
|
+
current.type === "BlockStatement" ||
|
|
14
|
+
current.type === "TSModuleBlock" ||
|
|
15
|
+
current.type === "StaticBlock" ||
|
|
16
|
+
current.type === "SwitchStatement") {
|
|
17
|
+
return current;
|
|
18
|
+
}
|
|
19
|
+
current = current.parent;
|
|
20
|
+
}
|
|
21
|
+
return node;
|
|
22
|
+
}
|
|
23
|
+
function declaredTypeBinding(node) {
|
|
24
|
+
if (node.type === "TSTypeAliasDeclaration") {
|
|
25
|
+
return { alias: node, name: node.id.name };
|
|
26
|
+
}
|
|
27
|
+
if (node.type === "TSInterfaceDeclaration" ||
|
|
28
|
+
node.type === "TSEnumDeclaration" ||
|
|
29
|
+
node.type === "ClassDeclaration" ||
|
|
30
|
+
node.type === "ClassExpression") {
|
|
31
|
+
return node.id === null ? null : { alias: null, name: node.id.name };
|
|
32
|
+
}
|
|
33
|
+
if (node.type === "ImportSpecifier" ||
|
|
34
|
+
node.type === "ImportDefaultSpecifier" ||
|
|
35
|
+
node.type === "ImportNamespaceSpecifier") {
|
|
36
|
+
return { alias: null, name: node.local.name };
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
function collectTypeBindings(node, visitorKeys, bindingsByName, aliases) {
|
|
41
|
+
const declared = declaredTypeBinding(node);
|
|
42
|
+
if (declared !== null) {
|
|
43
|
+
const bindings = bindingsByName.get(declared.name) ?? [];
|
|
44
|
+
bindings.push({ ...declared, scope: enclosingTypeScope(node) });
|
|
45
|
+
bindingsByName.set(declared.name, bindings);
|
|
46
|
+
if (declared.alias !== null)
|
|
47
|
+
aliases.push(declared.alias);
|
|
48
|
+
}
|
|
49
|
+
// SAFETY: Oxlint's visitor keys identify only ESTree child-node properties.
|
|
50
|
+
const fields = node;
|
|
51
|
+
for (const key of visitorKeys[node.type] ?? []) {
|
|
52
|
+
const value = fields[key];
|
|
53
|
+
if (isNode(value)) {
|
|
54
|
+
collectTypeBindings(value, visitorKeys, bindingsByName, aliases);
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (!Array.isArray(value))
|
|
58
|
+
continue;
|
|
59
|
+
for (const child of value) {
|
|
60
|
+
if (isNode(child)) {
|
|
61
|
+
collectTypeBindings(child, visitorKeys, bindingsByName, aliases);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
/** Collect every lexical type alias and competing type binding in a program. */
|
|
67
|
+
export function createTypeAliasEnvironment(program, visitorKeys) {
|
|
68
|
+
const cached = environmentsByProgram.get(program);
|
|
69
|
+
if (cached !== undefined)
|
|
70
|
+
return cached;
|
|
71
|
+
const bindingsByName = new Map();
|
|
72
|
+
const aliases = [];
|
|
73
|
+
collectTypeBindings(program, visitorKeys, bindingsByName, aliases);
|
|
74
|
+
const environment = { aliases, bindingsByName, visitorKeys };
|
|
75
|
+
environmentsByProgram.set(program, environment);
|
|
76
|
+
return environment;
|
|
77
|
+
}
|
|
78
|
+
function ancestorDistance(ancestor, node) {
|
|
79
|
+
let current = node;
|
|
80
|
+
let distance = 0;
|
|
81
|
+
while (current !== null) {
|
|
82
|
+
if (current === ancestor)
|
|
83
|
+
return distance;
|
|
84
|
+
current = current.parent;
|
|
85
|
+
distance += 1;
|
|
86
|
+
}
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
function nearestTypeBindings(name, use, environment) {
|
|
90
|
+
const candidates = environment.bindingsByName.get(name) ?? [];
|
|
91
|
+
let nearestDistance = Number.POSITIVE_INFINITY;
|
|
92
|
+
let nearest = [];
|
|
93
|
+
for (const candidate of candidates) {
|
|
94
|
+
const distance = ancestorDistance(candidate.scope, use);
|
|
95
|
+
if (distance === null || distance > nearestDistance)
|
|
96
|
+
continue;
|
|
97
|
+
if (distance === nearestDistance) {
|
|
98
|
+
nearest.push(candidate);
|
|
99
|
+
continue;
|
|
100
|
+
}
|
|
101
|
+
nearestDistance = distance;
|
|
102
|
+
nearest = [candidate];
|
|
103
|
+
}
|
|
104
|
+
return nearest;
|
|
105
|
+
}
|
|
106
|
+
/** Resolve the nearest visible alias with this name, respecting lexical shadowing. */
|
|
107
|
+
export function visibleTypeAlias(name, use, environment) {
|
|
108
|
+
if (lexicalTypeParameterNames(use, environment.visitorKeys).has(name))
|
|
109
|
+
return null;
|
|
110
|
+
const bindings = nearestTypeBindings(name, use, environment);
|
|
111
|
+
return bindings.length === 1 ? (bindings[0]?.alias ?? null) : null;
|
|
112
|
+
}
|
|
113
|
+
/** Return whether a local declaration shadows a built-in type at this use. */
|
|
114
|
+
export function hasVisibleTypeBinding(name, use, environment) {
|
|
115
|
+
return (lexicalTypeParameterNames(use, environment.visitorKeys).has(name) ||
|
|
116
|
+
nearestTypeBindings(name, use, environment).length > 0);
|
|
117
|
+
}
|
|
118
|
+
function typeReferenceName(type) {
|
|
119
|
+
return type.typeName.type === "Identifier" ? type.typeName.name : null;
|
|
120
|
+
}
|
|
121
|
+
function aliasSubstitutions(alias, reference, base) {
|
|
122
|
+
const parameters = alias.typeParameters?.params ?? [];
|
|
123
|
+
const arguments_ = reference.typeArguments?.params ?? [];
|
|
124
|
+
const next = new Map(base);
|
|
125
|
+
for (const [index, parameter] of parameters.entries()) {
|
|
126
|
+
const explicitArgument = arguments_[index];
|
|
127
|
+
const argument = explicitArgument ?? parameter.default;
|
|
128
|
+
if (argument === null || argument === undefined)
|
|
129
|
+
return null;
|
|
130
|
+
const argumentSubstitutions = explicitArgument === undefined ? next : base;
|
|
131
|
+
next.set(parameter.name.name, {
|
|
132
|
+
type: argument,
|
|
133
|
+
substitutions: new Map(argumentSubstitutions),
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
return next;
|
|
137
|
+
}
|
|
138
|
+
/** Match a type after resolving visible aliases and substituting their type parameters. */
|
|
139
|
+
export function resolvedTypeMatches(type, environment, matcher) {
|
|
140
|
+
const evaluate = (current, substitutions, resolvingAliases) => {
|
|
141
|
+
if (current.type === "TSTypeReference") {
|
|
142
|
+
const name = typeReferenceName(current);
|
|
143
|
+
if (name !== null) {
|
|
144
|
+
const substitution = substitutions.get(name);
|
|
145
|
+
if (substitution !== undefined && !current.typeArguments?.params.length) {
|
|
146
|
+
return evaluate(substitution.type, substitution.substitutions, resolvingAliases);
|
|
147
|
+
}
|
|
148
|
+
const alias = visibleTypeAlias(name, current, environment);
|
|
149
|
+
if (alias !== null && !resolvingAliases.has(alias)) {
|
|
150
|
+
const nextSubstitutions = aliasSubstitutions(alias, current, substitutions);
|
|
151
|
+
if (nextSubstitutions !== null) {
|
|
152
|
+
const nextResolving = new Set(resolvingAliases);
|
|
153
|
+
nextResolving.add(alias);
|
|
154
|
+
return evaluate(alias.typeAnnotation, nextSubstitutions, nextResolving);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return matcher(current, (child) => evaluate(child, substitutions, resolvingAliases));
|
|
160
|
+
};
|
|
161
|
+
return evaluate(type, new Map(), new Set());
|
|
162
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import type { ESTree, SourceCode, Token as SyntaxToken, Comment, Location } from "@oxlint/plugins";
|
|
2
|
+
type Token = SyntaxToken | Comment;
|
|
3
|
+
/** Line terminators recognized by the upstream padding matcher. */
|
|
4
|
+
export declare const LINEBREAKS: Set<string>;
|
|
5
|
+
/** Test a closing brace without treating comment text as punctuation. */
|
|
6
|
+
export declare const isClosingBraceToken: (token: Token) => boolean;
|
|
7
|
+
/** Test a semicolon without treating comment text as punctuation. */
|
|
8
|
+
export declare const isSemicolonToken: (token: Token) => boolean;
|
|
9
|
+
/** Filter the optional final semicolon when identifying block-like statements. */
|
|
10
|
+
export declare const isNotSemicolonToken: (token: Token) => boolean;
|
|
11
|
+
/** Compare token/node boundaries, including attached comments. */
|
|
12
|
+
export declare const isTokenOnSameLine: (left: {
|
|
13
|
+
loc: Location;
|
|
14
|
+
}, right: {
|
|
15
|
+
loc: Location;
|
|
16
|
+
}) => boolean;
|
|
17
|
+
/** Recognize declarations and expressions used by the upstream IIFE matcher. */
|
|
18
|
+
export declare const isFunction: (node: ESTree.Node) => boolean;
|
|
19
|
+
/** Preserve the upstream multiline statement heuristic. */
|
|
20
|
+
export declare const isSingleLine: (node: ESTree.Node) => boolean;
|
|
21
|
+
/** Unwrap optional chaining before checking IIFE syntax. */
|
|
22
|
+
export declare const skipChainExpression: (node: ESTree.Node) => ESTree.Node;
|
|
23
|
+
/** Only a program or function-body expression can begin a directive prologue. */
|
|
24
|
+
export declare const isTopLevelExpressionStatement: (node: ESTree.Node) => node is ESTree.ExpressionStatement;
|
|
25
|
+
/** A single wrapping pair suffices to exclude a string from directive syntax. */
|
|
26
|
+
export declare function isParenthesized(node: ESTree.Node, sourceCode: SourceCode): boolean;
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Line terminators recognized by the upstream padding matcher. */
|
|
2
|
+
export const LINEBREAKS = new Set(["\r\n", "\r", "\n", "\u2028", "\u2029"]);
|
|
3
|
+
/** Test a closing brace without treating comment text as punctuation. */
|
|
4
|
+
export const isClosingBraceToken = (token) => token.type === "Punctuator" && token.value === "}";
|
|
5
|
+
/** Test a semicolon without treating comment text as punctuation. */
|
|
6
|
+
export const isSemicolonToken = (token) => token.type === "Punctuator" && token.value === ";";
|
|
7
|
+
/** Filter the optional final semicolon when identifying block-like statements. */
|
|
8
|
+
export const isNotSemicolonToken = (token) => !isSemicolonToken(token);
|
|
9
|
+
/** Compare token/node boundaries, including attached comments. */
|
|
10
|
+
export const isTokenOnSameLine = (left, right) => left.loc.end.line === right.loc.start.line;
|
|
11
|
+
/** Recognize declarations and expressions used by the upstream IIFE matcher. */
|
|
12
|
+
export const isFunction = (node) => node.type === "FunctionDeclaration" ||
|
|
13
|
+
node.type === "FunctionExpression" ||
|
|
14
|
+
node.type === "ArrowFunctionExpression";
|
|
15
|
+
/** Preserve the upstream multiline statement heuristic. */
|
|
16
|
+
export const isSingleLine = (node) => node.loc.start.line === node.loc.end.line;
|
|
17
|
+
/** Unwrap optional chaining before checking IIFE syntax. */
|
|
18
|
+
export const skipChainExpression = (node) => node.type === "ChainExpression" ? node.expression : node;
|
|
19
|
+
/** Only a program or function-body expression can begin a directive prologue. */
|
|
20
|
+
export const isTopLevelExpressionStatement = (node) => node.type === "ExpressionStatement" &&
|
|
21
|
+
(node.parent.type === "Program" ||
|
|
22
|
+
(node.parent.type === "BlockStatement" && isFunction(node.parent.parent)));
|
|
23
|
+
/** A single wrapping pair suffices to exclude a string from directive syntax. */
|
|
24
|
+
export function isParenthesized(node, sourceCode) {
|
|
25
|
+
const before = sourceCode.getTokenBefore(node);
|
|
26
|
+
const after = sourceCode.getTokenAfter(node);
|
|
27
|
+
return before?.value === "(" && after?.value === ")";
|
|
28
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ESTree, SourceCode, CreateRule } from '@oxlint/plugins';
|
|
2
|
+
type ASTNode = ESTree.Node;
|
|
3
|
+
import type { PaddingType, StatementMatcher } from './padding-line-options.ts';
|
|
4
|
+
/**
|
|
5
|
+
* This rule is a replica of padding-line-between-statements.
|
|
6
|
+
*
|
|
7
|
+
* Ideally we would want to extend the rule support typescript specific support.
|
|
8
|
+
* But since not all the state is exposed by the eslint and eslint has frozen stylistic rules,
|
|
9
|
+
* (see - https://eslint.org/blog/2020/05/changes-to-rules-policies for details.)
|
|
10
|
+
* we are forced to re-implement the rule here.
|
|
11
|
+
*
|
|
12
|
+
* We have tried to keep the implementation as close as possible to the eslint implementation, to make
|
|
13
|
+
* patching easier for future contributors.
|
|
14
|
+
*
|
|
15
|
+
* Reference rule - https://github.com/eslint/eslint/blob/main/lib/rules/padding-line-between-statements.js
|
|
16
|
+
*/
|
|
17
|
+
type NodeTest = (node: ASTNode, sourceCode: SourceCode) => boolean;
|
|
18
|
+
export interface NodeTestObject {
|
|
19
|
+
test: NodeTest;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* A policy entry may name a caller-defined statement type (registered through
|
|
23
|
+
* `createPaddingLineRule`) anywhere the upstream statement types are accepted.
|
|
24
|
+
*/
|
|
25
|
+
export type PolicyStatementOption<Extra extends string = never> = StatementMatcher | Extra | [StatementMatcher | Extra, ...(StatementMatcher | Extra)[]];
|
|
26
|
+
export type PolicyOptions<Extra extends string = never> = {
|
|
27
|
+
blankLine: PaddingType;
|
|
28
|
+
prev: PolicyStatementOption<Extra>;
|
|
29
|
+
next: PolicyStatementOption<Extra>;
|
|
30
|
+
}[];
|
|
31
|
+
/**
|
|
32
|
+
* Build the vendored padding rule with caller-owned, typed policy options.
|
|
33
|
+
* `statementTypes` registers caller-defined statement types, looked up before
|
|
34
|
+
* the upstream table, for syntax the upstream matchers do not distinguish.
|
|
35
|
+
*/
|
|
36
|
+
export default function createPaddingLineRule<Extra extends string = never>(options: PolicyOptions<NoInfer<Extra>>, statementTypes?: Record<Extra, NodeTestObject>): CreateRule;
|
|
37
|
+
export {};
|