@jfalava/ts-standard 0.2.3 → 0.3.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/dist/oxlint/effect/index.js +254 -0
- package/dist/oxlint/index.js +183 -0
- package/oxlint/LICENSE +21 -0
- package/oxlint/UPSTREAM.md +9 -0
- package/package.json +13 -4
- package/src/lint.ts +181 -43
- package/reference/anti-slop/UPSTREAM.md +0 -8
- package/reference/anti-slop/effect/index.ts +0 -21
- package/reference/anti-slop/effect/rules/no-manual-effect-error-tag.ts +0 -52
- package/reference/anti-slop/effect/rules/no-manual-tag-comparison.ts +0 -45
- package/reference/anti-slop/effect/rules/no-manual-tagged-construction.ts +0 -37
- package/reference/anti-slop/effect/rules/no-service-constructor-imports.ts +0 -52
- package/reference/anti-slop/effect/rules/prefer-effect-match.ts +0 -54
- package/reference/anti-slop/effect/shared/tagged-values.ts +0 -97
- package/reference/anti-slop/index.ts +0 -24
- package/reference/anti-slop/rules/no-module-mocking.ts +0 -80
- package/reference/anti-slop/rules/no-runtime-typeof.ts +0 -77
- package/reference/anti-slop/rules/no-shape-in-symbol-names.ts +0 -46
- package/reference/anti-slop/rules/no-unknown-parameters.ts +0 -69
- package/reference/anti-slop/rules/require-safety-comment-for-type-assertion.ts +0 -131
- package/reference/anti-slop/shared/function-parameters.ts +0 -49
- package/reference/anti-slop/shared/scope.ts +0 -15
|
@@ -1,80 +0,0 @@
|
|
|
1
|
-
import { defineRule } from "@oxlint/plugins";
|
|
2
|
-
|
|
3
|
-
import { resolveVariable } from "../shared/scope.ts";
|
|
4
|
-
|
|
5
|
-
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
6
|
-
|
|
7
|
-
const moduleMockMethods = new Set(["doMock", "mock", "unstable_mockModule"]);
|
|
8
|
-
|
|
9
|
-
function importedName(node: ESTree.Node): string | null {
|
|
10
|
-
if (node.type !== "ImportSpecifier") return null;
|
|
11
|
-
return node.imported.type === "Identifier" ? node.imported.name : node.imported.value;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function isTestFrameworkObject(
|
|
15
|
-
sourceCode: SourceCode,
|
|
16
|
-
expression: ESTree.Expression,
|
|
17
|
-
): expression is ESTree.IdentifierReference {
|
|
18
|
-
if (expression.type !== "Identifier") return false;
|
|
19
|
-
if (
|
|
20
|
-
(expression.name === "vi" || expression.name === "jest") &&
|
|
21
|
-
sourceCode.isGlobalReference(expression)
|
|
22
|
-
) {
|
|
23
|
-
return true;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
const variable = resolveVariable(sourceCode, expression);
|
|
27
|
-
if (variable === null || variable.defs.length === 0) {
|
|
28
|
-
return expression.name === "vi" || expression.name === "jest";
|
|
29
|
-
}
|
|
30
|
-
return variable.defs.some((definition) => {
|
|
31
|
-
if (definition.type !== "ImportBinding" || definition.parent?.type !== "ImportDeclaration") {
|
|
32
|
-
return false;
|
|
33
|
-
}
|
|
34
|
-
const source = definition.parent.source.value;
|
|
35
|
-
const name = importedName(definition.node);
|
|
36
|
-
return (source === "vitest" && name === "vi") || (source === "@jest/globals" && name === "jest");
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function moduleMockCall(sourceCode: SourceCode, callee: ESTree.Expression): boolean {
|
|
41
|
-
if (!("property" in callee) || !("object" in callee) || !("computed" in callee)) return false;
|
|
42
|
-
if (!isTestFrameworkObject(sourceCode, callee.object)) return false;
|
|
43
|
-
const property = callee.property;
|
|
44
|
-
const method = callee.computed
|
|
45
|
-
? property.type === "Literal" &&
|
|
46
|
-
(property.value === "doMock" ||
|
|
47
|
-
property.value === "mock" ||
|
|
48
|
-
property.value === "unstable_mockModule")
|
|
49
|
-
? property.value
|
|
50
|
-
: null
|
|
51
|
-
: property.type === "Identifier"
|
|
52
|
-
? property.name
|
|
53
|
-
: null;
|
|
54
|
-
return method !== null && moduleMockMethods.has(method);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/** Ban test framework module mocking in favor of real dependency seams. */
|
|
58
|
-
export const noModuleMockingRule = defineRule({
|
|
59
|
-
meta: {
|
|
60
|
-
type: "problem",
|
|
61
|
-
docs: {
|
|
62
|
-
description:
|
|
63
|
-
"Disallow Vitest and Jest module mocking; tests must replace dependencies through real interfaces.",
|
|
64
|
-
},
|
|
65
|
-
messages: {
|
|
66
|
-
moduleMock:
|
|
67
|
-
"Replace module mocking with dependency injection through a real interface, service layer, or faithful test implementation.",
|
|
68
|
-
},
|
|
69
|
-
},
|
|
70
|
-
createOnce(context) {
|
|
71
|
-
return {
|
|
72
|
-
CallExpression(node) {
|
|
73
|
-
if (node.callee.type === "Super" || node.callee.type === "V8IntrinsicExpression") return;
|
|
74
|
-
if (moduleMockCall(context.sourceCode, node.callee)) {
|
|
75
|
-
context.report({ node, messageId: "moduleMock" });
|
|
76
|
-
}
|
|
77
|
-
},
|
|
78
|
-
};
|
|
79
|
-
},
|
|
80
|
-
});
|
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
import { defineRule } from "@oxlint/plugins";
|
|
2
|
-
|
|
3
|
-
import type { ESTree } from "@oxlint/plugins";
|
|
4
|
-
|
|
5
|
-
type RuntimeFunction = ESTree.ArrowFunctionExpression | ESTree.Function;
|
|
6
|
-
|
|
7
|
-
function isRuntimeFunction(node: ESTree.Node): node is RuntimeFunction {
|
|
8
|
-
return (
|
|
9
|
-
node.type === "ArrowFunctionExpression" ||
|
|
10
|
-
node.type === "FunctionDeclaration" ||
|
|
11
|
-
node.type === "FunctionExpression"
|
|
12
|
-
);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function isInsideTypeGuard(node: ESTree.Node): boolean {
|
|
16
|
-
let current: ESTree.Node | null = node.parent;
|
|
17
|
-
while (current !== null && current.type !== "Program") {
|
|
18
|
-
if (isRuntimeFunction(current)) {
|
|
19
|
-
return current.returnType?.typeAnnotation.type === "TSTypePredicate";
|
|
20
|
-
}
|
|
21
|
-
current = current.parent;
|
|
22
|
-
}
|
|
23
|
-
return false;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
/** Return whether typeof safely probes for the existence of a possibly absent binding. */
|
|
27
|
-
function isExistenceProbe(node: ESTree.UnaryExpression): boolean {
|
|
28
|
-
const parent = node.parent;
|
|
29
|
-
if (parent.type !== "BinaryExpression") return false;
|
|
30
|
-
if (!["===", "!==", "==", "!="].includes(parent.operator)) return false;
|
|
31
|
-
const other = parent.left === node ? parent.right : parent.left;
|
|
32
|
-
return other.type === "Literal" && other.value === "undefined";
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/** Disallow runtime typeof checks that narrow unparsed values instead of decoding them. */
|
|
36
|
-
export const noRuntimeTypeofRule = defineRule({
|
|
37
|
-
meta: {
|
|
38
|
-
type: "problem",
|
|
39
|
-
docs: {
|
|
40
|
-
description:
|
|
41
|
-
"Disallow runtime typeof checks; external values must be decoded into meaningful types at their I/O boundary.",
|
|
42
|
-
},
|
|
43
|
-
messages: {
|
|
44
|
-
runtimeTypeof:
|
|
45
|
-
"A `typeof` check narrows a representation without establishing its contract. Parse input at its I/O boundary, then branch on the domain value.",
|
|
46
|
-
},
|
|
47
|
-
schema: [
|
|
48
|
-
{
|
|
49
|
-
type: "object",
|
|
50
|
-
properties: {
|
|
51
|
-
allowInTypeGuards: { type: "boolean" },
|
|
52
|
-
},
|
|
53
|
-
additionalProperties: false,
|
|
54
|
-
},
|
|
55
|
-
],
|
|
56
|
-
defaultOptions: [{ allowInTypeGuards: false }],
|
|
57
|
-
},
|
|
58
|
-
createOnce(context) {
|
|
59
|
-
return {
|
|
60
|
-
UnaryExpression(node) {
|
|
61
|
-
const option = context.options?.[0];
|
|
62
|
-
const allowInTypeGuards =
|
|
63
|
-
typeof option === "object" &&
|
|
64
|
-
option !== null &&
|
|
65
|
-
!Array.isArray(option) &&
|
|
66
|
-
option.allowInTypeGuards === true;
|
|
67
|
-
if (
|
|
68
|
-
node.operator === "typeof" &&
|
|
69
|
-
!isExistenceProbe(node) &&
|
|
70
|
-
(!allowInTypeGuards || !isInsideTypeGuard(node))
|
|
71
|
-
) {
|
|
72
|
-
context.report({ node, messageId: "runtimeTypeof" });
|
|
73
|
-
}
|
|
74
|
-
},
|
|
75
|
-
};
|
|
76
|
-
},
|
|
77
|
-
});
|
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { defineRule } from "@oxlint/plugins";
|
|
2
|
-
import type { ESTree } from "@oxlint/plugins";
|
|
3
|
-
|
|
4
|
-
const FORBIDDEN_SYMBOL_NAME = "shape";
|
|
5
|
-
|
|
6
|
-
function containsForbiddenSymbolName(name: string): boolean {
|
|
7
|
-
return name.toLowerCase().includes(FORBIDDEN_SYMBOL_NAME);
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
/** Return whether an identifier names a statically accessed member owned by another value. */
|
|
11
|
-
function isBorrowedMemberName(node: ESTree.Node): boolean {
|
|
12
|
-
const parent = node.parent;
|
|
13
|
-
if (parent === null || parent.type !== "MemberExpression") return false;
|
|
14
|
-
return parent.property === node && parent.computed === false;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/** Ban the case-insensitive substring "shape" in every JavaScript and TypeScript symbol name. */
|
|
18
|
-
export const noForbiddenTermInSymbolNamesRule = defineRule({
|
|
19
|
-
meta: {
|
|
20
|
-
type: "problem",
|
|
21
|
-
docs: {
|
|
22
|
-
description:
|
|
23
|
-
'Disallow the case-insensitive substring "shape" in JavaScript, TypeScript, private, and JSX symbol names.',
|
|
24
|
-
},
|
|
25
|
-
messages: {
|
|
26
|
-
forbiddenSymbolName:
|
|
27
|
-
'Rename symbol "{{name}}" for its domain role; "shape" describes structure rather than ownership.',
|
|
28
|
-
},
|
|
29
|
-
},
|
|
30
|
-
createOnce(context) {
|
|
31
|
-
const reportForbiddenSymbolName = (node: ESTree.Node & { name: string }) => {
|
|
32
|
-
if (!containsForbiddenSymbolName(node.name) || isBorrowedMemberName(node)) return;
|
|
33
|
-
context.report({
|
|
34
|
-
node,
|
|
35
|
-
messageId: "forbiddenSymbolName",
|
|
36
|
-
data: { name: node.name },
|
|
37
|
-
});
|
|
38
|
-
};
|
|
39
|
-
|
|
40
|
-
return {
|
|
41
|
-
Identifier: reportForbiddenSymbolName,
|
|
42
|
-
PrivateIdentifier: reportForbiddenSymbolName,
|
|
43
|
-
JSXIdentifier: reportForbiddenSymbolName,
|
|
44
|
-
};
|
|
45
|
-
},
|
|
46
|
-
});
|
|
@@ -1,69 +0,0 @@
|
|
|
1
|
-
import { defineRule } from "@oxlint/plugins";
|
|
2
|
-
import type { ESTree } from "@oxlint/plugins";
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
containsUnknownType,
|
|
6
|
-
functionParameterBindingName,
|
|
7
|
-
functionParameterTypeAnnotation,
|
|
8
|
-
} from "../shared/function-parameters.ts";
|
|
9
|
-
type ParameterOwner =
|
|
10
|
-
| ESTree.ArrowFunctionExpression
|
|
11
|
-
| ESTree.Function
|
|
12
|
-
| ESTree.TSCallSignatureDeclaration
|
|
13
|
-
| ESTree.TSConstructSignatureDeclaration
|
|
14
|
-
| ESTree.TSConstructorType
|
|
15
|
-
| ESTree.TSFunctionType
|
|
16
|
-
| ESTree.TSMethodSignature;
|
|
17
|
-
|
|
18
|
-
function isTypePredicateSubject(owner: ParameterOwner, parameterName: string): boolean {
|
|
19
|
-
const predicate = owner.returnType?.typeAnnotation;
|
|
20
|
-
return (
|
|
21
|
-
predicate?.type === "TSTypePredicate" &&
|
|
22
|
-
predicate.parameterName.type === "Identifier" &&
|
|
23
|
-
predicate.parameterName.name === parameterName
|
|
24
|
-
);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
/** Disallow unknown inputs except explicitly named error-cause enrichment. */
|
|
28
|
-
export const noUnknownParametersRule = defineRule({
|
|
29
|
-
meta: {
|
|
30
|
-
type: "problem",
|
|
31
|
-
docs: {
|
|
32
|
-
description:
|
|
33
|
-
"Disallow explicitly unknown function parameters except `cause` and type-predicate subjects; decode unknown input at its I/O boundary instead.",
|
|
34
|
-
},
|
|
35
|
-
messages: {
|
|
36
|
-
unknownParameter:
|
|
37
|
-
"Parameter `{{parameter}}` leaves input unparsed. Accept a named domain type; run the expected schema or parser at the I/O boundary before calling this function.",
|
|
38
|
-
},
|
|
39
|
-
},
|
|
40
|
-
createOnce(context) {
|
|
41
|
-
const checkParameters = (node: ParameterOwner) => {
|
|
42
|
-
for (const parameter of node.params) {
|
|
43
|
-
const annotation = functionParameterTypeAnnotation(parameter);
|
|
44
|
-
if (annotation === null || annotation === undefined) continue;
|
|
45
|
-
if (!containsUnknownType(annotation.typeAnnotation)) continue;
|
|
46
|
-
const name = functionParameterBindingName(parameter, context.sourceCode);
|
|
47
|
-
if (name === "cause" || isTypePredicateSubject(node, name)) continue;
|
|
48
|
-
context.report({
|
|
49
|
-
node: annotation.typeAnnotation,
|
|
50
|
-
messageId: "unknownParameter",
|
|
51
|
-
data: { parameter: name },
|
|
52
|
-
});
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
return {
|
|
57
|
-
ArrowFunctionExpression: checkParameters,
|
|
58
|
-
FunctionDeclaration: checkParameters,
|
|
59
|
-
FunctionExpression: checkParameters,
|
|
60
|
-
TSCallSignatureDeclaration: checkParameters,
|
|
61
|
-
TSConstructSignatureDeclaration: checkParameters,
|
|
62
|
-
TSConstructorType: checkParameters,
|
|
63
|
-
TSDeclareFunction: checkParameters,
|
|
64
|
-
TSEmptyBodyFunctionExpression: checkParameters,
|
|
65
|
-
TSFunctionType: checkParameters,
|
|
66
|
-
TSMethodSignature: checkParameters,
|
|
67
|
-
};
|
|
68
|
-
},
|
|
69
|
-
});
|
|
@@ -1,131 +0,0 @@
|
|
|
1
|
-
import { defineRule } from "@oxlint/plugins";
|
|
2
|
-
|
|
3
|
-
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
4
|
-
|
|
5
|
-
type TypeAssertion = ESTree.TSAsExpression | ESTree.TSTypeAssertion;
|
|
6
|
-
|
|
7
|
-
const DEFAULT_SAFETY_MARKERS = ["SAFETY"] as const;
|
|
8
|
-
|
|
9
|
-
const commentOwnerKinds = new Set([
|
|
10
|
-
"ExpressionStatement",
|
|
11
|
-
"PropertyDefinition",
|
|
12
|
-
"ReturnStatement",
|
|
13
|
-
"ThrowStatement",
|
|
14
|
-
"VariableDeclaration",
|
|
15
|
-
]);
|
|
16
|
-
|
|
17
|
-
function isConstAssertion(node: TypeAssertion): boolean {
|
|
18
|
-
return (
|
|
19
|
-
node.typeAnnotation.type === "TSTypeReference" &&
|
|
20
|
-
node.typeAnnotation.typeName.type === "Identifier" &&
|
|
21
|
-
node.typeAnnotation.typeName.name === "const"
|
|
22
|
-
);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function configuredSafetyMarkers(option: unknown): readonly string[] {
|
|
26
|
-
if (typeof option !== "object" || option === null || !("markers" in option)) {
|
|
27
|
-
return DEFAULT_SAFETY_MARKERS;
|
|
28
|
-
}
|
|
29
|
-
const configured = option.markers;
|
|
30
|
-
if (!Array.isArray(configured)) return DEFAULT_SAFETY_MARKERS;
|
|
31
|
-
const markers = configured.flatMap((marker) =>
|
|
32
|
-
typeof marker === "string" && marker.trim().length > 0 ? [marker.trim()] : [],
|
|
33
|
-
);
|
|
34
|
-
return markers.length > 0 ? markers : DEFAULT_SAFETY_MARKERS;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function markerPattern(markers: readonly string[]): RegExp {
|
|
38
|
-
const alternation = markers
|
|
39
|
-
.map((marker) => marker.replaceAll(/[.*+?^${}()|[\]\\]/gu, String.raw`\$&`))
|
|
40
|
-
.join("|");
|
|
41
|
-
return new RegExp(
|
|
42
|
-
String.raw`(?:^|[^\p{L}\p{N}_])(?:${alternation})\s*:\s*\S`,
|
|
43
|
-
"u",
|
|
44
|
-
);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
function hasSafetyJustificationBefore(
|
|
48
|
-
sourceCode: SourceCode,
|
|
49
|
-
owner: ESTree.Node,
|
|
50
|
-
assertion: TypeAssertion,
|
|
51
|
-
pattern: RegExp,
|
|
52
|
-
): boolean {
|
|
53
|
-
return sourceCode
|
|
54
|
-
.getCommentsBefore(owner)
|
|
55
|
-
.some(
|
|
56
|
-
(comment) => comment.end <= assertion.start && pattern.test(comment.value),
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
function hasSafetyComment(
|
|
61
|
-
sourceCode: SourceCode,
|
|
62
|
-
node: TypeAssertion,
|
|
63
|
-
pattern: RegExp,
|
|
64
|
-
): boolean {
|
|
65
|
-
let current: ESTree.Node = node;
|
|
66
|
-
while (true) {
|
|
67
|
-
if (hasSafetyJustificationBefore(sourceCode, current, node, pattern)) return true;
|
|
68
|
-
if (commentOwnerKinds.has(current.type)) {
|
|
69
|
-
const exportDeclaration = current.parent;
|
|
70
|
-
return (
|
|
71
|
-
exportDeclaration.type === "ExportNamedDeclaration" &&
|
|
72
|
-
exportDeclaration.declaration === current &&
|
|
73
|
-
hasSafetyJustificationBefore(sourceCode, exportDeclaration, node, pattern)
|
|
74
|
-
);
|
|
75
|
-
}
|
|
76
|
-
if (current.parent.type === "Program") return false;
|
|
77
|
-
current = current.parent;
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
/** Require every non-const type assertion to state the invariant TypeScript cannot express. */
|
|
82
|
-
export const requireSafetyCommentForTypeAssertionRule = defineRule({
|
|
83
|
-
meta: {
|
|
84
|
-
type: "problem",
|
|
85
|
-
docs: {
|
|
86
|
-
description:
|
|
87
|
-
"Require a nearby SAFETY comment for every TypeScript type assertion except const assertions.",
|
|
88
|
-
},
|
|
89
|
-
messages: {
|
|
90
|
-
missingSafetyComment:
|
|
91
|
-
"This type assertion has no `{{marker}}:` justification. State the checked invariant immediately before the assertion or its containing statement.",
|
|
92
|
-
},
|
|
93
|
-
schema: [
|
|
94
|
-
{
|
|
95
|
-
type: "object",
|
|
96
|
-
properties: {
|
|
97
|
-
markers: {
|
|
98
|
-
type: "array",
|
|
99
|
-
items: { type: "string", minLength: 1 },
|
|
100
|
-
minItems: 1,
|
|
101
|
-
uniqueItems: true,
|
|
102
|
-
},
|
|
103
|
-
},
|
|
104
|
-
additionalProperties: false,
|
|
105
|
-
},
|
|
106
|
-
],
|
|
107
|
-
defaultOptions: [{ markers: ["SAFETY"] }],
|
|
108
|
-
},
|
|
109
|
-
createOnce(context) {
|
|
110
|
-
const patterns = new Map<string, RegExp>();
|
|
111
|
-
|
|
112
|
-
const checkAssertion = (node: TypeAssertion) => {
|
|
113
|
-
if (isConstAssertion(node)) return;
|
|
114
|
-
const markers = configuredSafetyMarkers(context.options?.[0]);
|
|
115
|
-
const patternKey = markers.join("\u0000");
|
|
116
|
-
const pattern = patterns.get(patternKey) ?? markerPattern(markers);
|
|
117
|
-
patterns.set(patternKey, pattern);
|
|
118
|
-
if (hasSafetyComment(context.sourceCode, node, pattern)) return;
|
|
119
|
-
context.report({
|
|
120
|
-
node,
|
|
121
|
-
messageId: "missingSafetyComment",
|
|
122
|
-
data: { marker: markers[0] ?? DEFAULT_SAFETY_MARKERS[0] },
|
|
123
|
-
});
|
|
124
|
-
};
|
|
125
|
-
|
|
126
|
-
return {
|
|
127
|
-
TSAsExpression: checkAssertion,
|
|
128
|
-
TSTypeAssertion: checkAssertion,
|
|
129
|
-
};
|
|
130
|
-
},
|
|
131
|
-
});
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import type { ESTree, SourceCode } from "@oxlint/plugins";
|
|
2
|
-
|
|
3
|
-
export type FunctionParameter = ESTree.ParamPattern;
|
|
4
|
-
|
|
5
|
-
/** Return whether a type is or contains TypeScript's absorbing unknown top type. */
|
|
6
|
-
export function containsUnknownType(type: ESTree.TSType): boolean {
|
|
7
|
-
if (type.type === "TSUnknownKeyword") return true;
|
|
8
|
-
if (type.type === "TSParenthesizedType") return containsUnknownType(type.typeAnnotation);
|
|
9
|
-
return type.type === "TSUnionType" && type.types.some(containsUnknownType);
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/** Return the TypeScript annotation attached to a function parameter or its wrapped binding. */
|
|
13
|
-
export function functionParameterTypeAnnotation(
|
|
14
|
-
parameter: FunctionParameter,
|
|
15
|
-
): ESTree.TSTypeAnnotation | null | undefined {
|
|
16
|
-
if (parameter.type === "TSParameterProperty") {
|
|
17
|
-
return functionParameterTypeAnnotation(parameter.parameter);
|
|
18
|
-
}
|
|
19
|
-
if (parameter.type === "RestElement") {
|
|
20
|
-
return parameter.typeAnnotation ?? functionParameterTypeAnnotation(parameter.argument);
|
|
21
|
-
}
|
|
22
|
-
if (parameter.type === "AssignmentPattern") {
|
|
23
|
-
return parameter.typeAnnotation ?? functionParameterTypeAnnotation(parameter.left);
|
|
24
|
-
}
|
|
25
|
-
return parameter.typeAnnotation;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
/** Return only a function parameter's local binding, excluding its annotation and default value. */
|
|
29
|
-
export function functionParameterBindingName(
|
|
30
|
-
parameter: FunctionParameter,
|
|
31
|
-
sourceCode: SourceCode,
|
|
32
|
-
): string {
|
|
33
|
-
if (parameter.type === "TSParameterProperty") {
|
|
34
|
-
return functionParameterBindingName(parameter.parameter, sourceCode);
|
|
35
|
-
}
|
|
36
|
-
if (parameter.type === "AssignmentPattern") {
|
|
37
|
-
return functionParameterBindingName(parameter.left, sourceCode);
|
|
38
|
-
}
|
|
39
|
-
if (parameter.type === "RestElement") {
|
|
40
|
-
return functionParameterBindingName(parameter.argument, sourceCode);
|
|
41
|
-
}
|
|
42
|
-
if (parameter.type === "Identifier") return parameter.name;
|
|
43
|
-
|
|
44
|
-
const sourceText = sourceCode.getText(parameter);
|
|
45
|
-
const annotationStart = parameter.typeAnnotation?.start;
|
|
46
|
-
return annotationStart === undefined
|
|
47
|
-
? sourceText
|
|
48
|
-
: sourceText.slice(0, annotationStart - parameter.start).trimEnd();
|
|
49
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import type { ESTree, Scope, SourceCode, Variable } from "@oxlint/plugins";
|
|
2
|
-
|
|
3
|
-
/** Resolve an identifier to its binding by walking lexical scopes upward. */
|
|
4
|
-
export function resolveVariable(
|
|
5
|
-
sourceCode: SourceCode,
|
|
6
|
-
identifier: ESTree.IdentifierReference,
|
|
7
|
-
): Variable | null {
|
|
8
|
-
let scope: Scope | null = sourceCode.getScope(identifier);
|
|
9
|
-
while (scope !== null) {
|
|
10
|
-
const variable = scope.set.get(identifier.name);
|
|
11
|
-
if (variable !== undefined) return variable;
|
|
12
|
-
scope = scope.upper;
|
|
13
|
-
}
|
|
14
|
-
return null;
|
|
15
|
-
}
|