Skip to content

Commit a47675e

Browse files
authored
Chore/circular import cleanup (#4957)
* fix bad imports in extension-shared.ts * fix bad types imports from src/command/render/filters.ts * fix bad imports from parse-shortcode.ts * fix bad imports from lib/errors.ts * fix bad imports from src/core/process.ts * fix bad imports from src/format/reveal/format-reveal.ts * fix bad imports from src/core/http.ts * fix bad imports from src/core/http.ts * fix bad imports from src/core/port.ts * fix bad imports from src/publish/provider.ts * fix bad imports from src/core/console-types.ts * fix import in test * fix circular imports * fix more circular imports * fix more cyclic imports * more cyclic imports cleanup * speed up reporting tool * remove print * more cyclic dependencies cleanup * move constants * split off render services * add missing change from prev commit * add missing files * invert dependency arrow for format-html and format-reveal * invert dependency arrow for format-asciidoc
1 parent 950768f commit a47675e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+1108
-873
lines changed
398 KB
Loading

package/src/common/import-report/deno-info.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ export function graphTranspose(graph: DependencyGraph): DependencyGraph {
117117

118118
export function reachability(
119119
graph: DependencyGraph,
120-
_source: string,
121120
): Record<string, Set<string>> {
122121
const result: Record<string, Set<string>> = Object.fromEntries(
123122
Object.keys(graph).map((k) => [k, new Set()]),

package/src/common/import-report/explain-all-cycles.ts

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,74 @@ import {
1010

1111
import { longestCommonDirPrefix } from "./utils.ts";
1212

13+
function dropTypesFiles(edges: Edge[])
14+
{
15+
return edges.filter(({
16+
"from": edgeFrom,
17+
to,
18+
}) => !(to.endsWith("types.ts") || edgeFrom.endsWith("types.ts")));
19+
}
20+
21+
function trimCommonPrefix(edges: Edge[])
22+
{
23+
// https://stackoverflow.com/a/68702966
24+
const strs: Set<string> = new Set();
25+
for (const { "from": edgeFrom, to } of edges) {
26+
strs.add(edgeFrom);
27+
strs.add(to);
28+
}
29+
const p = longestCommonDirPrefix(Array.from(strs)).length;
30+
return edges.map(({ "from": edgeFrom, to }) => ({
31+
"from": edgeFrom.slice(p),
32+
to: to.slice(p),
33+
}));
34+
}
35+
36+
function simplify(
37+
edges: Edge[],
38+
prefixes: string[],
39+
): Edge[]
40+
{
41+
edges = trimCommonPrefix(edges);
42+
43+
const result: Edge[] = [];
44+
const keepPrefix = (s: string) => {
45+
for (const prefix of prefixes) {
46+
if (s.startsWith(prefix)) {
47+
return prefix + "...";
48+
}
49+
}
50+
return s;
51+
}
52+
const edgeSet = new Set<string>();
53+
for (const edge of edges) {
54+
const from = keepPrefix(edge.from);
55+
const to = keepPrefix(edge.to);
56+
if (from === to) {
57+
continue;
58+
}
59+
const key = `${from} -> ${to}`;
60+
if (edgeSet.has(key)) {
61+
continue;
62+
}
63+
edgeSet.add(key);
64+
result.push({ from, to });
65+
}
66+
return result;
67+
}
68+
1369
function explain(
1470
graph: DependencyGraph,
1571
): Edge[] {
1672
const result: Edge[] = [];
1773
const transpose = graphTranspose(graph);
1874
const visited: Set<string> = new Set();
1975
let found = false;
76+
let count = 0;
77+
78+
const deps = reachability(graph);
2079

2180
for (const source of Object.keys(graph)) {
22-
const deps = reachability(graph, source);
2381
if (!deps[source]) {
2482
continue;
2583
}
@@ -46,14 +104,6 @@ function explain(
46104
}
47105

48106
function generateGraph(edges: Edge[]): string {
49-
// https://stackoverflow.com/a/68702966
50-
const strs: Set<string> = new Set();
51-
for (const { "from": edgeFrom, to } of edges) {
52-
strs.add(edgeFrom);
53-
strs.add(to);
54-
}
55-
const p = longestCommonDirPrefix(Array.from(strs)).length;
56-
57107
const qmdOut: string[] = [];
58108
qmdOut.push("digraph G {");
59109
const m: Record<string, number> = {};
@@ -68,7 +118,7 @@ function generateGraph(edges: Edge[]): string {
68118
qmdOut.push(` ${m[edgeFrom]} -> ${m[to]};`);
69119
}
70120
for (const [name, ix] of Object.entries(m)) {
71-
qmdOut.push(` ${ix} [ label = "${name.slice(p)}", shape = "none" ];`);
121+
qmdOut.push(` ${ix} [ label = "${name}", shape = "none" ];`);
72122
}
73123
qmdOut.push("}\n");
74124
return qmdOut.join("");
@@ -122,10 +172,24 @@ If the second parameter is "--graph", then this program outputs the .dot specifi
122172
const json = await getDenoInfo(Deno.args[0]);
123173
const { graph } = moduleGraph(json);
124174

125-
const result = explain(graph);
126-
if (Deno.args[1] === "--graph") {
175+
let args = Array.from(Deno.args);
176+
177+
let result = explain(graph);
178+
if (args[1] === "--simplify") {
179+
args.splice(1, 1);
180+
const prefixes = [];
181+
while (!args[1].startsWith("--")) {
182+
prefixes.push(args[1]);
183+
args.splice(1, 1);
184+
}
185+
result = simplify(result, prefixes);
186+
}
187+
188+
result = dropTypesFiles(result);
189+
190+
if (args[1] === "--graph") {
127191
Deno.writeTextFileSync(
128-
Deno.args[2],
192+
args[2] ?? "graph.dot",
129193
generateGraph(result),
130194
);
131195
} else {

package/src/common/import-report/package_report.qmd

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ def run(what):
1414
("quarto run %s ../../../../src/quarto.ts" % what).split(" "),
1515
capture_output = True
1616
).stdout.decode('utf-8').replace("Bad import from ", "").replace(" to ", " -> ")
17-
print(str)
1817
return ansi_escape.sub("", str)
1918
```
2019

@@ -29,6 +28,5 @@ Other bad imports:
2928

3029
```{python}
3130
#| echo: false
32-
3331
print(run("find-bad-imports.ts"))
3432
```

package/src/common/import-report/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// https://stackoverflow.com/a/68702966
22
export const longestCommonDirPrefix = (strs: string[]) => {
3+
if (strs.length === 0) return "";
34
let prefix = strs.reduce((acc, str) => str.length < acc.length ? str : acc);
45

56
for (const str of strs) {

src/command/check/check.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
/*
2-
* check.ts
3-
*
4-
* Copyright (C) 2021-2022 Posit Software, PBC
5-
*
6-
*/
2+
* check.ts
3+
*
4+
* Copyright (C) 2021-2022 Posit Software, PBC
5+
*/
76

87
import { info } from "log/mod.ts";
98

10-
import { render, renderServices } from "../render/render-shared.ts";
9+
import { render } from "../render/render-shared.ts";
10+
import { renderServices } from "../render/render-services.ts";
1111

1212
import { JupyterCapabilities } from "../../core/jupyter/types.ts";
1313
import { jupyterCapabilities } from "../../core/jupyter/capabilities.ts";

src/command/preview/cmd.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,15 @@ import {
3232
} from "../../core/lib/yaml-validation/state.ts";
3333
import { initYamlIntelligenceResourcesFromFilesystem } from "../../core/schema/utils.ts";
3434
import { kProjectWatchInputs, ProjectContext } from "../../project/types.ts";
35+
import { projectContext } from "../../project/project-context.ts";
3536
import {
36-
projectContext,
3737
projectIsServeable,
3838
projectPreviewServe,
39-
} from "../../project/project-context.ts";
39+
} from "../../project/project-shared.ts";
40+
4041
import { isHtmlOutput } from "../../config/format.ts";
4142
import { renderProject } from "../render/project.ts";
42-
import { renderServices } from "../render/render-shared.ts";
43+
import { renderServices } from "../render/render-services.ts";
4344
import { parseFormatString } from "../../core/pandoc/pandoc-formats.ts";
4445
import { normalizePath } from "../../core/path.ts";
4546
import { kCliffyImplicitCwd } from "../../config/constants.ts";

src/command/preview/preview.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,18 @@ import {
3737
import { PromiseQueue } from "../../core/promise.ts";
3838
import { inputFilesDir } from "../../core/render.ts";
3939

40+
import { kQuartoRenderCommand } from "../render/constants.ts";
41+
4042
import {
41-
kQuartoRenderCommand,
4243
previewUnableToRenderResponse,
4344
previewURL,
4445
printBrowsePreviewMessage,
4546
printWatchingForChangesMessage,
4647
render,
47-
renderServices,
4848
renderToken,
4949
rswURL,
5050
} from "../render/render-shared.ts";
51+
import { renderServices } from "../render/render-services.ts";
5152
import {
5253
RenderFlags,
5354
RenderResult,

src/command/publish/cmd.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ import {
2323
import {
2424
projectContext,
2525
projectInputFiles,
26-
projectIsWebsite,
2726
} from "../../project/project-context.ts";
2827

28+
import { projectIsWebsite } from "../../project/project-shared.ts";
29+
2930
import { PublishCommandOptions } from "./options.ts";
3031
import { resolveDeployment } from "./deployment.ts";
3132
import { AccountPrompt, manageAccounts, resolveAccount } from "./account.ts";

src/command/render/cmd.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
/*
2-
* cmd.ts
3-
*
4-
* Copyright (C) 2020-2022 Posit Software, PBC
5-
*
6-
*/
2+
* cmd.ts
3+
*
4+
* Copyright (C) 2020-2022 Posit Software, PBC
5+
*/
76

87
import { dirname, relative } from "path/mod.ts";
98
import { expandGlobSync } from "fs/expand_glob.ts";
@@ -13,7 +12,9 @@ import { info, warning } from "log/mod.ts";
1312
import { fixupPandocArgs, kStdOut, parseRenderFlags } from "./flags.ts";
1413

1514
import { renderResultFinalOutput } from "./render.ts";
16-
import { render, renderServices } from "./render-shared.ts";
15+
import { render } from "./render-shared.ts";
16+
import { renderServices } from "./render-services.ts";
17+
1718
import { RenderResult } from "./types.ts";
1819
import { kCliffyImplicitCwd } from "../../config/constants.ts";
1920

0 commit comments

Comments
 (0)