@shivaedev/quality 0.48.1 → 0.49.0
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/CHANGELOG.md +37 -0
- package/README.md +58 -11
- package/biome/plugins/ambient-runtime.grit +67 -1
- package/biome/plugins/type-re-exports.grit +33 -4
- package/dist/adapters/biome/run.js +4 -4
- package/dist/adapters/biome/run.js.map +1 -1
- package/dist/adapters/fixJsxConditionals.d.ts.map +1 -1
- package/dist/adapters/fixJsxConditionals.js +2 -5
- package/dist/adapters/fixJsxConditionals.js.map +1 -1
- package/dist/adapters/knip/configuration.d.ts +5 -2
- package/dist/adapters/knip/configuration.d.ts.map +1 -1
- package/dist/adapters/knip/configuration.js +12 -5
- package/dist/adapters/knip/configuration.js.map +1 -1
- package/dist/adapters/knip/run.d.ts +2 -3
- package/dist/adapters/knip/run.d.ts.map +1 -1
- package/dist/adapters/knip/run.js +25 -7
- package/dist/adapters/knip/run.js.map +1 -1
- package/dist/adapters/knip/sourceConfig.d.ts.map +1 -1
- package/dist/adapters/knip/sourceConfig.js +3 -0
- package/dist/adapters/knip/sourceConfig.js.map +1 -1
- package/dist/baseline/looseStagedEntries.d.ts +4 -0
- package/dist/baseline/looseStagedEntries.d.ts.map +1 -0
- package/dist/baseline/looseStagedEntries.js +31 -0
- package/dist/baseline/looseStagedEntries.js.map +1 -0
- package/dist/baseline/update.d.ts +1 -1
- package/dist/baseline/update.d.ts.map +1 -1
- package/dist/baseline/update.js +3 -3
- package/dist/baseline/update.js.map +1 -1
- package/dist/cli/args.d.ts +1 -1
- package/dist/cli/args.d.ts.map +1 -1
- package/dist/cli/args.js +5 -5
- package/dist/cli/args.js.map +1 -1
- package/dist/cli/baseline.d.ts +1 -1
- package/dist/cli/baseline.d.ts.map +1 -1
- package/dist/cli/baseline.js +19 -2
- package/dist/cli/baseline.js.map +1 -1
- package/dist/cli/lint.d.ts +1 -1
- package/dist/cli/lint.d.ts.map +1 -1
- package/dist/cli/lint.js +11 -2
- package/dist/cli/lint.js.map +1 -1
- package/dist/cli/messages.d.ts +1 -1
- package/dist/cli/messages.d.ts.map +1 -1
- package/dist/cli/messages.js +3 -3
- package/dist/cli/program.d.ts.map +1 -1
- package/dist/cli/program.js +3 -2
- package/dist/cli/program.js.map +1 -1
- package/dist/hooks/preCommit.js +1 -1
- package/dist/hooks/preCommit.js.map +1 -1
- package/dist/inventory/collect.js +3 -3
- package/dist/inventory/collect.js.map +1 -1
- package/dist/inventory/shadcnFiles.d.ts +4 -0
- package/dist/inventory/shadcnFiles.d.ts.map +1 -0
- package/dist/inventory/shadcnFiles.js +83 -0
- package/dist/inventory/shadcnFiles.js.map +1 -0
- package/dist/report/render.d.ts.map +1 -1
- package/dist/report/render.js +3 -10
- package/dist/report/render.js.map +1 -1
- package/dist/report/staleSections.js +1 -1
- package/dist/report/staleSections.js.map +1 -1
- package/dist/rules/builtIn.d.ts +1 -1
- package/dist/rules/importOrigins.d.ts +4 -0
- package/dist/rules/importOrigins.d.ts.map +1 -0
- package/dist/rules/importOrigins.js +36 -0
- package/dist/rules/importOrigins.js.map +1 -0
- package/dist/rules/noBarrel.d.ts.map +1 -1
- package/dist/rules/noBarrel.js +19 -7
- package/dist/rules/noBarrel.js.map +1 -1
- package/dist/rules/react/jsxChildDecision.d.ts +5 -0
- package/dist/rules/react/jsxChildDecision.d.ts.map +1 -0
- package/dist/rules/react/jsxChildDecision.js +74 -0
- package/dist/rules/react/jsxChildDecision.js.map +1 -0
- package/dist/rules/react/jsxConditionals.d.ts.map +1 -1
- package/dist/rules/react/jsxConditionals.js +9 -33
- package/dist/rules/react/jsxConditionals.js.map +1 -1
- package/dist/rules/secondNames.d.ts +7 -0
- package/dist/rules/secondNames.d.ts.map +1 -0
- package/dist/rules/secondNames.js +98 -0
- package/dist/rules/secondNames.js.map +1 -0
- package/dist/rules/stories/importBindings.d.ts.map +1 -1
- package/dist/rules/stories/importBindings.js +3 -0
- package/dist/rules/stories/importBindings.js.map +1 -1
- package/dist/rules/unusedCode.d.ts +1 -1
- package/dist/rules/unusedCode.d.ts.map +1 -1
- package/docs/debt-and-hooks.md +5 -5
- package/package.json +4 -4
- package/src/adapters/biome/run.ts +4 -4
- package/src/adapters/fixJsxConditionals.ts +2 -8
- package/src/adapters/knip/configuration.ts +12 -5
- package/src/adapters/knip/run.ts +27 -7
- package/src/adapters/knip/sourceConfig.ts +3 -0
- package/src/baseline/looseStagedEntries.ts +32 -0
- package/src/baseline/update.ts +5 -2
- package/src/cli/args.ts +6 -7
- package/src/cli/baseline.ts +19 -1
- package/src/cli/lint.ts +11 -1
- package/src/cli/messages.ts +3 -3
- package/src/cli/program.ts +3 -2
- package/src/hooks/preCommit.ts +1 -1
- package/src/inventory/collect.ts +3 -3
- package/src/inventory/shadcnFiles.ts +87 -0
- package/src/report/render.ts +3 -10
- package/src/report/staleSections.ts +1 -1
- package/src/rules/importOrigins.ts +38 -0
- package/src/rules/noBarrel.ts +21 -8
- package/src/rules/react/jsxChildDecision.ts +84 -0
- package/src/rules/react/jsxConditionals.ts +9 -35
- package/src/rules/secondNames.ts +121 -0
- package/src/rules/stories/importBindings.ts +3 -0
package/src/report/render.ts
CHANGED
|
@@ -60,15 +60,8 @@ function summary(outcome: Outcome, context: ReportContext): string {
|
|
|
60
60
|
return `${verdict}. ${plural(context.checked, "source file")} checked${suppressed}.`;
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
function
|
|
64
|
-
|
|
65
|
-
if (rules.length === 0) {
|
|
66
|
-
return [];
|
|
67
|
-
}
|
|
68
|
-
const command = ["quality baseline write", ...rules.map((rule) => `--rule ${rule}`)].join(" ");
|
|
69
|
-
return [
|
|
70
|
-
`If the baseline should keep these findings, such as the debt of a moved or renamed file, record them again with \`${command}\` and call out the baseline growth in the pull request description.`,
|
|
71
|
-
];
|
|
63
|
+
function movedDebt(outcome: Outcome): readonly string[] {
|
|
64
|
+
return passes(outcome) ? [] : ["For moved files, carry their existing debt with `quality baseline prune --against <base>`."];
|
|
72
65
|
}
|
|
73
66
|
|
|
74
67
|
export function render(outcome: Outcome, context: ReportContext): string {
|
|
@@ -77,6 +70,6 @@ export function render(outcome: Outcome, context: ReportContext): string {
|
|
|
77
70
|
...ruleSections(outcome.errors, context, regressions),
|
|
78
71
|
...staleSections(outcome, context),
|
|
79
72
|
summary(outcome, context),
|
|
80
|
-
...
|
|
73
|
+
...movedDebt(outcome),
|
|
81
74
|
].join("\n\n");
|
|
82
75
|
}
|
|
@@ -27,7 +27,7 @@ export function staleSections(outcome: Outcome, files: StaleFiles): readonly str
|
|
|
27
27
|
...section(
|
|
28
28
|
"error",
|
|
29
29
|
files.baseline,
|
|
30
|
-
"
|
|
30
|
+
"Drop unknown rules with `quality baseline prune`. Lower staged counts with `quality baseline tighten --staged`, then stage the baseline.",
|
|
31
31
|
outcome.staleBaseline.map(baselineLine),
|
|
32
32
|
),
|
|
33
33
|
...section(
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { importBindings } from "#rules/stories/importBindings.ts";
|
|
3
|
+
|
|
4
|
+
function constantOrigins(source: ts.SourceFile, origins: Map<string, string>): void {
|
|
5
|
+
for (const statement of source.statements) {
|
|
6
|
+
if (!ts.isVariableStatement(statement) || (statement.declarationList.flags & ts.NodeFlags.Const) === 0) {
|
|
7
|
+
continue;
|
|
8
|
+
}
|
|
9
|
+
for (const declaration of statement.declarationList.declarations) {
|
|
10
|
+
if (!ts.isIdentifier(declaration.name) || declaration.initializer === undefined) {
|
|
11
|
+
continue;
|
|
12
|
+
}
|
|
13
|
+
const origin = originOf(declaration.initializer, origins);
|
|
14
|
+
if (origin !== undefined) {
|
|
15
|
+
origins.set(declaration.name.text, origin);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function importOrigins(source: ts.SourceFile): ReadonlyMap<string, string> {
|
|
22
|
+
const origins = new Map([...importBindings(source)].map(([name, binding]) => [name, binding.module]));
|
|
23
|
+
constantOrigins(source, origins);
|
|
24
|
+
return origins;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function originOf(node: ts.Node, origins: ReadonlyMap<string, string>): string | undefined {
|
|
28
|
+
if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isSatisfiesExpression(node)) {
|
|
29
|
+
return originOf(node.expression, origins);
|
|
30
|
+
}
|
|
31
|
+
if (ts.isIdentifier(node)) {
|
|
32
|
+
return origins.get(node.text);
|
|
33
|
+
}
|
|
34
|
+
if (ts.isPropertyAccessExpression(node)) {
|
|
35
|
+
return originOf(node.expression, origins);
|
|
36
|
+
}
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
package/src/rules/noBarrel.ts
CHANGED
|
@@ -1,16 +1,29 @@
|
|
|
1
1
|
import { posix } from "node:path";
|
|
2
|
-
import { defineRule } from "#rule.ts";
|
|
2
|
+
import { defineRule, type Finding } from "#rule.ts";
|
|
3
|
+
import { parse } from "./parse.ts";
|
|
4
|
+
import { secondNames } from "./secondNames.ts";
|
|
3
5
|
|
|
4
6
|
const BARREL_FILE = /^index\.tsx?$/u;
|
|
5
7
|
|
|
6
8
|
export const noBarrel = defineRule({
|
|
7
9
|
check: ({ sources }) =>
|
|
8
|
-
sources
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
10
|
+
sources.flatMap((file) => {
|
|
11
|
+
const findings: Finding[] = BARREL_FILE.test(posix.basename(file.path))
|
|
12
|
+
? [{ file: file.path, message: "index.ts and index.tsx modules are banned. Name the module after its purpose and import it directly." }]
|
|
13
|
+
: [];
|
|
14
|
+
const source = parse(file);
|
|
15
|
+
if (source !== undefined) {
|
|
16
|
+
for (const { modules, node } of secondNames(source)) {
|
|
17
|
+
findings.push({
|
|
18
|
+
file: file.path,
|
|
19
|
+
line: source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1,
|
|
20
|
+
message: `An exported alias, facade or forwarding function gives an import a second name.\nHelp: Import directly from ${modules.map((module) => `"${module}"`).join(", ")} and remove the second name.`,
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return findings;
|
|
25
|
+
}),
|
|
26
|
+
description:
|
|
27
|
+
"Name modules after their purpose; import values from their defining module instead of exporting aliases, facades or unchanged forwarding calls.",
|
|
15
28
|
id: "structure/no-barrel",
|
|
16
29
|
});
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
|
|
3
|
+
function mapCall(node: ts.Node): boolean {
|
|
4
|
+
return ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === "map";
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
function componentWrapper(node: ts.CallExpression): boolean {
|
|
8
|
+
const callee = node.expression;
|
|
9
|
+
const name = ts.isPropertyAccessExpression(callee) ? callee.name : callee;
|
|
10
|
+
return ts.isIdentifier(name) && ["memo", "forwardRef"].includes(name.text);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
function component(node: ts.Node): boolean {
|
|
14
|
+
if (ts.isFunctionDeclaration(node)) {
|
|
15
|
+
return node.name === undefined || /^[A-Z]/u.test(node.name.text);
|
|
16
|
+
}
|
|
17
|
+
if (ts.isMethodDeclaration(node)) {
|
|
18
|
+
return ts.isClassLike(node.parent) && ts.isIdentifier(node.name) && node.name.text === "render";
|
|
19
|
+
}
|
|
20
|
+
let parent = node.parent;
|
|
21
|
+
while (ts.isParenthesizedExpression(parent) || ts.isCallExpression(parent)) {
|
|
22
|
+
if (ts.isCallExpression(parent) && !componentWrapper(parent)) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
parent = parent.parent;
|
|
26
|
+
}
|
|
27
|
+
return ts.isExportAssignment(parent) || (ts.isVariableDeclaration(parent) && ts.isIdentifier(parent.name) && /^[A-Z]/u.test(parent.name.text));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function literal(node: ts.Expression): boolean {
|
|
31
|
+
if (ts.isParenthesizedExpression(node)) {
|
|
32
|
+
return literal(node.expression);
|
|
33
|
+
}
|
|
34
|
+
return (
|
|
35
|
+
ts.isStringLiteral(node)
|
|
36
|
+
|| ts.isNumericLiteral(node)
|
|
37
|
+
|| (ts.isPrefixUnaryExpression(node)
|
|
38
|
+
&& (node.operator === ts.SyntaxKind.MinusToken || node.operator === ts.SyntaxKind.PlusToken)
|
|
39
|
+
&& ts.isNumericLiteral(node.operand))
|
|
40
|
+
);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function boundary(node: ts.Node): boolean {
|
|
44
|
+
return ts.isJsxAttribute(node) || ts.isJsxSpreadAttribute(node) || ts.isVariableDeclaration(node);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function jsxChild(node: ts.Node): boolean {
|
|
48
|
+
return ts.isJsxExpression(node) && (ts.isJsxElement(node.parent) || ts.isJsxFragment(node.parent));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function returnedComponent(node: ts.Node, returned: boolean): boolean {
|
|
52
|
+
return (returned || (ts.isArrowFunction(node) && !ts.isBlock(node.body))) && component(node);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function childContext(node: ts.Node): { readonly mapped: boolean } | undefined {
|
|
56
|
+
let mapped = false;
|
|
57
|
+
let child = false;
|
|
58
|
+
let returned = false;
|
|
59
|
+
for (let parent = node.parent; parent !== undefined; parent = parent.parent) {
|
|
60
|
+
if (boundary(parent)) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
child ||= jsxChild(parent);
|
|
64
|
+
if (ts.isFunctionLike(parent) && !mapCall(parent.parent)) {
|
|
65
|
+
return child && returnedComponent(parent, returned) ? { mapped } : undefined;
|
|
66
|
+
}
|
|
67
|
+
if (mapCall(parent)) {
|
|
68
|
+
mapped = true;
|
|
69
|
+
returned = false;
|
|
70
|
+
}
|
|
71
|
+
returned ||= ts.isReturnStatement(parent);
|
|
72
|
+
if (!returned && ts.isStatement(parent) && !ts.isBlock(parent)) {
|
|
73
|
+
return undefined;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
return undefined;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function jsxChildDecision(node: ts.Node): { readonly mapped: boolean } | undefined {
|
|
80
|
+
if (ts.isConditionalExpression(node)) {
|
|
81
|
+
return literal(node.whenTrue) && literal(node.whenFalse) ? undefined : childContext(node);
|
|
82
|
+
}
|
|
83
|
+
return ts.isBinaryExpression(node) && node.operatorToken.kind === ts.SyntaxKind.AmpersandAmpersandToken ? childContext(node) : undefined;
|
|
84
|
+
}
|
|
@@ -1,50 +1,24 @@
|
|
|
1
1
|
import ts from "typescript";
|
|
2
2
|
import { defineRule, type Finding } from "#rule.ts";
|
|
3
3
|
import { parse } from "#rules/parse.ts";
|
|
4
|
+
import { jsxChildDecision } from "./jsxChildDecision.ts";
|
|
4
5
|
|
|
5
6
|
const REPAIR =
|
|
6
7
|
"Decide before the JSX with an if and early return, or a variable computed above the return; or extract a component that receives the data and returns null when it has nothing to show.";
|
|
7
8
|
|
|
8
|
-
function mapCall(node: ts.Node): boolean {
|
|
9
|
-
return ts.isCallExpression(node) && ts.isPropertyAccessExpression(node.expression) && node.expression.name.text === "map";
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
function childContext(node: ts.Node): { readonly mapped: boolean } | undefined {
|
|
13
|
-
let mapped = false;
|
|
14
|
-
let child = false;
|
|
15
|
-
for (let parent = node.parent; parent !== undefined; parent = parent.parent) {
|
|
16
|
-
if (!child && ts.isJsxSpreadAttribute(parent)) {
|
|
17
|
-
return undefined;
|
|
18
|
-
}
|
|
19
|
-
if (ts.isJsxExpression(parent)) {
|
|
20
|
-
if (!(child || ts.isJsxElement(parent.parent) || ts.isJsxFragment(parent.parent))) {
|
|
21
|
-
return undefined;
|
|
22
|
-
}
|
|
23
|
-
child = true;
|
|
24
|
-
}
|
|
25
|
-
if (ts.isVariableDeclaration(parent) || (ts.isStatement(parent) && !ts.isReturnStatement(parent) && !ts.isBlock(parent))) {
|
|
26
|
-
break;
|
|
27
|
-
}
|
|
28
|
-
mapped ||= mapCall(parent);
|
|
29
|
-
}
|
|
30
|
-
return child ? { mapped } : undefined;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
9
|
export const jsxConditionals = defineRule({
|
|
34
10
|
check: ({ sources }) =>
|
|
35
11
|
sources.flatMap((file) => {
|
|
36
12
|
const source = parse(file);
|
|
37
13
|
const findings: Finding[] = [];
|
|
38
14
|
function visit(node: ts.Node): void {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
});
|
|
47
|
-
}
|
|
15
|
+
const context = jsxChildDecision(node);
|
|
16
|
+
if (context !== undefined && source !== undefined) {
|
|
17
|
+
findings.push({
|
|
18
|
+
file: file.path,
|
|
19
|
+
line: source.getLineAndCharacterOfPosition(node.getStart(source)).line + 1,
|
|
20
|
+
message: `Conditional rendering inside a JSX child. ${REPAIR}${context.mapped ? " Inside .map, extract the item into its own component that makes its own decision." : ""}`,
|
|
21
|
+
});
|
|
48
22
|
}
|
|
49
23
|
ts.forEachChild(node, visit);
|
|
50
24
|
}
|
|
@@ -53,7 +27,7 @@ export const jsxConditionals = defineRule({
|
|
|
53
27
|
}
|
|
54
28
|
return findings;
|
|
55
29
|
}),
|
|
56
|
-
description: `Components decide what to render outside their JSX. ${REPAIR} Inside .map, extract an item component that makes its own decision. Attribute values are accepted.`,
|
|
30
|
+
description: `Components decide what to render outside their returned JSX. ${REPAIR} Inside .map, extract an item component that makes its own decision. Attribute values, library callbacks and ternaries between plain string or number literals are accepted.`,
|
|
57
31
|
id: "react/jsx-conditionals",
|
|
58
32
|
registrable: false,
|
|
59
33
|
});
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import ts from "typescript";
|
|
2
|
+
import { importOrigins, originOf } from "./importOrigins.ts";
|
|
3
|
+
|
|
4
|
+
function returned(body: ts.ConciseBody): ts.Expression | undefined {
|
|
5
|
+
if (!ts.isBlock(body)) {
|
|
6
|
+
return body;
|
|
7
|
+
}
|
|
8
|
+
const statement = body.statements.length === 1 ? body.statements[0] : undefined;
|
|
9
|
+
return statement !== undefined && ts.isReturnStatement(statement) ? statement.expression : undefined;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
function forwarding(node: ts.FunctionLikeDeclaration, origins: ReadonlyMap<string, string>): string | undefined {
|
|
13
|
+
if (
|
|
14
|
+
node.body === undefined
|
|
15
|
+
|| node.asteriskToken !== undefined
|
|
16
|
+
|| node.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.AsyncKeyword)
|
|
17
|
+
) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
const call = returned(node.body);
|
|
21
|
+
if (call === undefined || !ts.isCallExpression(call) || call.questionDotToken !== undefined || call.arguments.length !== node.parameters.length) {
|
|
22
|
+
return undefined;
|
|
23
|
+
}
|
|
24
|
+
const visible = new Map(origins);
|
|
25
|
+
for (const [index, parameter] of node.parameters.entries()) {
|
|
26
|
+
const argument = call.arguments[index];
|
|
27
|
+
if (!ts.isIdentifier(parameter.name) || parameter.initializer !== undefined || argument === undefined) {
|
|
28
|
+
return undefined;
|
|
29
|
+
}
|
|
30
|
+
const value = ts.isSpreadElement(argument) ? argument.expression : argument;
|
|
31
|
+
if (!ts.isIdentifier(value) || value.text !== parameter.name.text || Boolean(parameter.dotDotDotToken) !== ts.isSpreadElement(argument)) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
visible.delete(parameter.name.text);
|
|
35
|
+
}
|
|
36
|
+
return originOf(call.expression, visible);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function valueOrigins(node: ts.Expression, origins: ReadonlyMap<string, string>): readonly string[] {
|
|
40
|
+
if (ts.isParenthesizedExpression(node) || ts.isAsExpression(node) || ts.isSatisfiesExpression(node)) {
|
|
41
|
+
return valueOrigins(node.expression, origins);
|
|
42
|
+
}
|
|
43
|
+
const origin = originOf(node, origins);
|
|
44
|
+
if (origin !== undefined) {
|
|
45
|
+
return [origin];
|
|
46
|
+
}
|
|
47
|
+
if (ts.isArrowFunction(node) || ts.isFunctionExpression(node)) {
|
|
48
|
+
const module = forwarding(node, origins);
|
|
49
|
+
return module === undefined ? [] : [module];
|
|
50
|
+
}
|
|
51
|
+
if (ts.isObjectLiteralExpression(node)) {
|
|
52
|
+
const modules = node.properties.map((property) => {
|
|
53
|
+
if (ts.isShorthandPropertyAssignment(property)) {
|
|
54
|
+
return valueOrigins(property.name, origins);
|
|
55
|
+
}
|
|
56
|
+
if (ts.isPropertyAssignment(property)) {
|
|
57
|
+
return valueOrigins(property.initializer, origins);
|
|
58
|
+
}
|
|
59
|
+
if (ts.isSpreadAssignment(property)) {
|
|
60
|
+
return valueOrigins(property.expression, origins);
|
|
61
|
+
}
|
|
62
|
+
return [];
|
|
63
|
+
});
|
|
64
|
+
return modules.length > 0 && modules.every((module) => module.length > 0) ? modules.flat() : [];
|
|
65
|
+
}
|
|
66
|
+
return [];
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export interface SecondName {
|
|
70
|
+
readonly modules: readonly string[];
|
|
71
|
+
readonly node: ts.Node;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function exportedNames(source: ts.SourceFile): ReadonlySet<string> {
|
|
75
|
+
return new Set(
|
|
76
|
+
source.statements.flatMap((statement) =>
|
|
77
|
+
ts.isExportDeclaration(statement)
|
|
78
|
+
&& statement.moduleSpecifier === undefined
|
|
79
|
+
&& statement.exportClause !== undefined
|
|
80
|
+
&& ts.isNamedExports(statement.exportClause)
|
|
81
|
+
? statement.exportClause.elements.map((element) => (element.propertyName ?? element.name).text)
|
|
82
|
+
: [],
|
|
83
|
+
),
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function declarations(statement: ts.Statement, exported: ReadonlySet<string>): readonly (ts.VariableDeclaration | ts.FunctionDeclaration)[] {
|
|
88
|
+
const publicStatement =
|
|
89
|
+
ts.canHaveModifiers(statement) && ts.getModifiers(statement)?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword);
|
|
90
|
+
let nodes: readonly (ts.VariableDeclaration | ts.FunctionDeclaration)[] = [];
|
|
91
|
+
if (ts.isVariableStatement(statement)) {
|
|
92
|
+
nodes = statement.declarationList.declarations;
|
|
93
|
+
} else if (ts.isFunctionDeclaration(statement)) {
|
|
94
|
+
nodes = [statement];
|
|
95
|
+
}
|
|
96
|
+
return nodes.filter((node) => node.name !== undefined && ts.isIdentifier(node.name) && (publicStatement || exported.has(node.name.text)));
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function declarationOrigins(
|
|
100
|
+
node: ts.VariableDeclaration | ts.FunctionDeclaration,
|
|
101
|
+
origins: ReadonlyMap<string, string>,
|
|
102
|
+
source: ts.SourceFile,
|
|
103
|
+
): readonly string[] {
|
|
104
|
+
if (ts.isVariableDeclaration(node)) {
|
|
105
|
+
return node.initializer === undefined ? [] : valueOrigins(node.initializer, origins);
|
|
106
|
+
}
|
|
107
|
+
const overloaded = source.statements.some((other) => ts.isFunctionDeclaration(other) && other !== node && other.name?.text === node.name?.text);
|
|
108
|
+
const module = overloaded ? undefined : forwarding(node, origins);
|
|
109
|
+
return module === undefined ? [] : [module];
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function secondNames(source: ts.SourceFile): readonly SecondName[] {
|
|
113
|
+
const origins = importOrigins(source);
|
|
114
|
+
const exported = exportedNames(source);
|
|
115
|
+
return source.statements.flatMap((statement) =>
|
|
116
|
+
declarations(statement, exported).flatMap((node) => {
|
|
117
|
+
const modules = declarationOrigins(node, origins, source);
|
|
118
|
+
return modules.length === 0 ? [] : [{ modules: [...new Set(modules)], node }];
|
|
119
|
+
}),
|
|
120
|
+
);
|
|
121
|
+
}
|
|
@@ -15,6 +15,9 @@ export function importBindings(source: ts.SourceFile): ReadonlyMap<string, Impor
|
|
|
15
15
|
}
|
|
16
16
|
const module = statement.moduleSpecifier.text;
|
|
17
17
|
const clause = statement.importClause;
|
|
18
|
+
if (clause?.name !== undefined) {
|
|
19
|
+
bindings.set(clause.name.text, { imported: "default", module });
|
|
20
|
+
}
|
|
18
21
|
const named = clause?.namedBindings;
|
|
19
22
|
if (named !== undefined && ts.isNamespaceImport(named)) {
|
|
20
23
|
bindings.set(named.name.text, { imported: NAMESPACE, module });
|