Skip to content

Commit

Permalink
fix(cli): info now displays type reference deps (denoland#11478)
Browse files Browse the repository at this point in the history
  • Loading branch information
kitsonk committed Jul 22, 2021
1 parent bdc53b4 commit 7d151ef
Show file tree
Hide file tree
Showing 7 changed files with 244 additions and 29 deletions.
100 changes: 92 additions & 8 deletions cli/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ const EMPTY_CONNECTOR: char = ' ';
#[serde(rename_all = "camelCase")]
pub struct ModuleGraphInfoDep {
pub specifier: String,
#[serde(skip_serializing_if = "is_false")]
pub is_dynamic: bool,
#[serde(rename = "code", skip_serializing_if = "Option::is_none")]
pub maybe_code: Option<ModuleSpecifier>,
#[serde(rename = "type", skip_serializing_if = "Option::is_none")]
pub maybe_type: Option<ModuleSpecifier>,
}

fn is_false(b: &bool) -> bool {
!b
}

impl ModuleGraphInfoDep {
fn write_info<S: AsRef<str> + fmt::Display + Clone>(
&self,
Expand Down Expand Up @@ -68,6 +73,8 @@ impl ModuleGraphInfoDep {
pub struct ModuleGraphInfoMod {
pub specifier: ModuleSpecifier,
pub dependencies: Vec<ModuleGraphInfoDep>,
#[serde(rename = "typeDependency", skip_serializing_if = "Option::is_none")]
pub maybe_type_dependency: Option<ModuleGraphInfoDep>,
#[serde(skip_serializing_if = "Option::is_none")]
pub size: Option<usize>,
#[serde(
Expand All @@ -92,6 +99,7 @@ impl Default for ModuleGraphInfoMod {
ModuleGraphInfoMod {
specifier: resolve_url("https://deno.land/x/mod.ts").unwrap(),
dependencies: Vec::new(),
maybe_type_dependency: None,
size: None,
media_type: None,
local: None,
Expand Down Expand Up @@ -119,7 +127,10 @@ impl ModuleGraphInfoMod {
} else {
SIBLING_CONNECTOR
};
let child_connector = if self.dependencies.is_empty() || was_seen {
let child_connector = if (self.dependencies.is_empty()
&& self.maybe_type_dependency.is_none())
|| was_seen
{
CHILD_NO_DEPS_CONNECTOR
} else {
CHILD_DEPS_CONNECTOR
Expand Down Expand Up @@ -175,7 +186,16 @@ impl ModuleGraphInfoMod {
prefix.push(EMPTY_CONNECTOR);
let dep_count = self.dependencies.len();
for (idx, dep) in self.dependencies.iter().enumerate() {
dep.write_info(f, &prefix, idx == dep_count - 1, modules, seen)?;
dep.write_info(
f,
&prefix,
idx == dep_count - 1 && self.maybe_type_dependency.is_none(),
modules,
seen,
)?;
}
if let Some(dep) = &self.maybe_type_dependency {
dep.write_info(f, &prefix, true, modules, seen)?;
}
}

Expand Down Expand Up @@ -233,7 +253,16 @@ impl fmt::Display for ModuleGraphInfo {
let mut seen = HashSet::new();
let dep_len = root.dependencies.len();
for (idx, dep) in root.dependencies.iter().enumerate() {
dep.write_info(f, "", idx == dep_len - 1, &self.modules, &mut seen)?;
dep.write_info(
f,
"",
idx == dep_len - 1 && root.maybe_type_dependency.is_none(),
&self.modules,
&mut seen,
)?;
}
if let Some(dep) = &root.maybe_type_dependency {
dep.write_info(f, "", true, &self.modules, &mut seen)?;
}
Ok(())
}
Expand Down Expand Up @@ -296,6 +325,8 @@ mod test {
let specifier_b = resolve_url("https://deno.land/x/b.ts").unwrap();
let specifier_c_js = resolve_url("https://deno.land/x/c.js").unwrap();
let specifier_c_dts = resolve_url("https://deno.land/x/c.d.ts").unwrap();
let specifier_d_js = resolve_url("https://deno.land/x/d.js").unwrap();
let specifier_d_dts = resolve_url("https://deno.land/x/d.d.ts").unwrap();
let modules = vec![
ModuleGraphInfoMod {
specifier: specifier_a.clone(),
Expand Down Expand Up @@ -329,6 +360,12 @@ mod test {
},
ModuleGraphInfoMod {
specifier: specifier_c_js,
dependencies: vec![ModuleGraphInfoDep {
specifier: "./d.js".to_string(),
is_dynamic: false,
maybe_code: Some(specifier_d_js.clone()),
maybe_type: None,
}],
size: Some(789),
media_type: Some(MediaType::JavaScript),
local: Some(PathBuf::from("/cache/deps/https/deno.land/x/c.js")),
Expand All @@ -343,6 +380,28 @@ mod test {
checksum: Some("a2b3c4d5".to_string()),
..Default::default()
},
ModuleGraphInfoMod {
specifier: specifier_d_js,
size: Some(987),
maybe_type_dependency: Some(ModuleGraphInfoDep {
specifier: "/x/d.d.ts".to_string(),
is_dynamic: false,
maybe_code: None,
maybe_type: Some(specifier_d_dts.clone()),
}),
media_type: Some(MediaType::JavaScript),
local: Some(PathBuf::from("/cache/deps/https/deno.land/x/d.js")),
checksum: Some("5k6j7h8g".to_string()),
..Default::default()
},
ModuleGraphInfoMod {
specifier: specifier_d_dts,
size: Some(67),
media_type: Some(MediaType::Dts),
local: Some(PathBuf::from("/cache/deps/https/deno.land/x/d.d.ts")),
checksum: Some("0h0h0h0h0h".to_string()),
..Default::default()
},
];
ModuleGraphInfo {
root: specifier_a,
Expand All @@ -359,11 +418,13 @@ mod test {
let expected = r#"local: /cache/deps/https/deno.land/x/a.ts
type: TypeScript
emit: /cache/emit/https/deno.land/x/a.js
dependencies: 3 unique (total 97.66KB)
dependencies: 5 unique (total 97.66KB)
https://deno.land/x/a.ts (123B)
└─┬ https://deno.land/x/b.ts (456B)
├── https://deno.land/x/c.js (789B)
├─┬ https://deno.land/x/c.js (789B)
│ └─┬ https://deno.land/x/d.js (987B)
│ └── https://deno.land/x/d.d.ts (67B)
└── https://deno.land/x/c.d.ts (999B)
"#;
assert_eq!(actual, expected);
Expand All @@ -383,7 +444,6 @@ https://deno.land/x/a.ts (123B)
"dependencies": [
{
"specifier": "./b.ts",
"isDynamic": false,
"code": "https://deno.land/x/b.ts"
}
],
Expand All @@ -398,7 +458,6 @@ https://deno.land/x/a.ts (123B)
"dependencies": [
{
"specifier": "./c.js",
"isDynamic": false,
"code": "https://deno.land/x/c.js",
"type": "https://deno.land/x/c.d.ts"
}
Expand All @@ -411,7 +470,12 @@ https://deno.land/x/a.ts (123B)
},
{
"specifier": "https://deno.land/x/c.js",
"dependencies": [],
"dependencies": [
{
"specifier": "./d.js",
"code": "https://deno.land/x/d.js"
}
],
"size": 789,
"mediaType": "JavaScript",
"local": "/cache/deps/https/deno.land/x/c.js",
Expand All @@ -424,6 +488,26 @@ https://deno.land/x/a.ts (123B)
"mediaType": "Dts",
"local": "/cache/deps/https/deno.land/x/c.d.ts",
"checksum": "a2b3c4d5"
},
{
"specifier": "https://deno.land/x/d.js",
"dependencies": [],
"typeDependency": {
"specifier": "/x/d.d.ts",
"type": "https://deno.land/x/d.d.ts"
},
"size": 987,
"mediaType": "JavaScript",
"local": "/cache/deps/https/deno.land/x/d.js",
"checksum": "5k6j7h8g"
},
{
"specifier": "https://deno.land/x/d.d.ts",
"dependencies": [],
"size": 67,
"mediaType": "Dts",
"local": "/cache/deps/https/deno.land/x/d.d.ts",
"checksum": "0h0h0h0h0h"
}
],
"size": 99999
Expand Down
24 changes: 13 additions & 11 deletions cli/module_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,17 +435,9 @@ impl Module {
.entry(desc.specifier.to_string())
.or_insert_with(|| Dependency::new(location));
dep.is_dynamic = desc.is_dynamic;
if let Some(specifier) = maybe_specifier {
if desc.kind == swc_ecmascript::dep_graph::DependencyKind::ExportType
|| desc.kind == swc_ecmascript::dep_graph::DependencyKind::ImportType
{
dep.maybe_type = Some(specifier);
} else {
dep.maybe_code = Some(specifier);
}
}
// If the dependency wasn't a type only dependency already, and there is
// a `@deno-types` comment, then we will set the `maybe_type` dependency.
dep.maybe_code = maybe_specifier;
// If there is a `@deno-types` pragma, we will add it to the dependency
// if one doesn't already exist.
if maybe_type.is_some() && dep.maybe_type.is_none() {
dep.maybe_type = maybe_type;
}
Expand Down Expand Up @@ -1475,9 +1467,19 @@ impl Graph {
} else {
(None, None)
};
let maybe_type_dependency =
module.maybe_types.clone().map(|(specifier, _type)| {
info::ModuleGraphInfoDep {
specifier,
is_dynamic: false,
maybe_code: None,
maybe_type: Some(_type),
}
});
Some(info::ModuleGraphInfoMod {
specifier: sp.clone(),
dependencies,
maybe_type_dependency,
size: Some(module.size()),
media_type: Some(module.media_type),
local: Some(module.source_path.clone()),
Expand Down
125 changes: 125 additions & 0 deletions cli/schemas/module-graph.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
{
"$id": "https://deno.land/schemas/module-graph.json",
"$schema": "http:https://json-schema.org/draft-07/schema",
"description": "A JSON representation of a Deno module dependency graph.",
"required": [
"root",
"modules",
"size"
],
"title": "Deno Dependency Graph Schema",
"type": "object",
"properties": {
"root": {
"default": "",
"description": "The root specifier for the graph.",
"examples": [
"https://deno.land/x/mod.ts"
],
"type": "string"
},
"modules": {
"default": [],
"description": "The modules that are part of the graph.",
"type": "array",
"items": {
"$ref": "#/definitions/module"
}
},
"size": {
"type": "integer",
"description": "The total size of all the unique dependencies in the graph in bytes.",
"default": 0
}
},
"definitions": {
"module": {
"type": "object",
"required": [
"specifier",
"dependencies"
],
"properties": {
"specifier": {
"type": "string",
"description": "The fully qualified module specifier (URL) for the module."
},
"dependencies": {
"type": "array",
"description": "An array of dependencies of the module.",
"items": {
"$ref": "#/definitions/dependency"
}
},
"typeDependency": {
"$ref": "#/definitions/dependency",
"description": "The type dependency for the module. This is set when the file contains a reference to its types or the module was supplied with a types header."
},
"size": {
"type": "integer",
"description": "The size of the module on disk in bytes."
},
"mediaType": {
"type": "string",
"description": "How the file is treated within Deno. All the possible media types that Deno considers are listed here, but in practice, several of them would never appear in a module graph.",
"enum": [
"JavaScript",
"TypeScript",
"JSX",
"TSX",
"Dts",
"Json",
"Wasm",
"TsBuildInfo",
"SourceMap",
"Unknown"
]
},
"local": {
"type": "string",
"description": "The path to the local file. For local modules this will be the local file path, for remote modules and data URLs, this would be the path to the file in the Deno cache."
},
"checksum": {
"type": "string",
"description": "The checksum of the local source file. This can be used to validate if the current on disk version matches the version described here."
},
"emit": {
"type": "string",
"description": "The path to an emitted version of the module, if the module requires transpilation to be loaded into the Deno runtime."
},
"map": {
"type": "string",
"description": "The path to an optionally emitted source map between the original and emitted version of the file."
},
"error": {
"type": "string",
"description": "If when resolving the module, Deno encountered an error and the module is unavailable, the text of that error will be indicated here."
}
}
},
"dependency": {
"type": "object",
"required": [
"specifier"
],
"properties": {
"specifier": {
"type": "string",
"description": "The specifier provided from within the module."
},
"isDynamic": {
"type": "boolean",
"description": "A flag that indicates if the import is dynamically imported. A dynamic import may not be fully resolved until it is accessed at runtime."
},
"code": {
"type": "string",
"description": "The fully qualified module specifier (URL) for the code dependency."
},
"type": {
"type": "string",
"description": "The fully qualified module specifier (URL) for the type only dependency."
}
}
}
}
}
3 changes: 0 additions & 3 deletions cli/tests/055_info_file_json.out
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"dependencies": [
{
"specifier": "./subdir/mod1.ts",
"isDynamic": false,
"code": "file:https://[WILDCARD]/cli/tests/subdir/mod1.ts"
}
],
Expand All @@ -20,7 +19,6 @@
"dependencies": [
{
"specifier": "./subdir2/mod2.ts",
"isDynamic": false,
"code": "file:https://[WILDCARD]/cli/tests/subdir/subdir2/mod2.ts"
}
],
Expand All @@ -42,7 +40,6 @@
"dependencies": [
{
"specifier": "../print_hello.ts",
"isDynamic": false,
"code": "file:https://[WILDCARD]/cli/tests/subdir/print_hello.ts"
}
],
Expand Down
Loading

0 comments on commit 7d151ef

Please sign in to comment.