about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-04-14 00:39:48 +0200
committerGitHub <noreply@github.com>2019-04-14 00:39:48 +0200
commit426ab8e018f2ce2559c1898cb53853b1b00c7cc9 (patch)
tree5311f55df8b1d199683b1208896335b4148281b3
parent021a8eca475af9dc0b4adc234c2c581e8e1b2f92 (diff)
parent796e6e37d6fa76b719dbf9b38872daffdc9caa70 (diff)
downloadrust-426ab8e018f2ce2559c1898cb53853b1b00c7cc9.tar.gz
rust-426ab8e018f2ce2559c1898cb53853b1b00c7cc9.zip
Rollup merge of #59890 - GuillaumeGomez:empty-json-variables, r=QuietMisdreavus
Don't generate empty json variables

r? @rust-lang/rustdoc
-rw-r--r--src/librustdoc/html/render.rs19
-rw-r--r--src/librustdoc/html/static/source-script.js34
2 files changed, 34 insertions, 19 deletions
diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs
index 98cf2f38108..0b463b08233 100644
--- a/src/librustdoc/html/render.rs
+++ b/src/librustdoc/html/render.rs
@@ -1064,11 +1064,22 @@ themePicker.onblur = handleThemeButtonsBlur;
                                                         .expect("invalid osstring conversion")))
                                       .collect::<Vec<_>>();
             files.sort_unstable_by(|a, b| a.cmp(b));
-            // FIXME(imperio): we could avoid to generate "dirs" and "files" if they're empty.
-            format!("{{\"name\":\"{name}\",\"dirs\":[{subs}],\"files\":[{files}]}}",
+            let subs = subs.iter().map(|s| s.to_json_string()).collect::<Vec<_>>().join(",");
+            let dirs = if subs.is_empty() {
+                String::new()
+            } else {
+                format!(",\"dirs\":[{}]", subs)
+            };
+            let files = files.join(",");
+            let files = if files.is_empty() {
+                String::new()
+            } else {
+                format!(",\"files\":[{}]", files)
+            };
+            format!("{{\"name\":\"{name}\"{dirs}{files}}}",
                     name=self.elem.to_str().expect("invalid osstring conversion"),
-                    subs=subs.iter().map(|s| s.to_json_string()).collect::<Vec<_>>().join(","),
-                    files=files.join(","))
+                    dirs=dirs,
+                    files=files)
         }
     }
 
diff --git a/src/librustdoc/html/static/source-script.js b/src/librustdoc/html/static/source-script.js
index 509c628ce5a..567022b4139 100644
--- a/src/librustdoc/html/static/source-script.js
+++ b/src/librustdoc/html/static/source-script.js
@@ -39,28 +39,32 @@ function createDirEntry(elem, parent, fullPath, currentFile, hasFoundFile) {
     children.className = "children";
     var folders = document.createElement("div");
     folders.className = "folders";
-    for (var i = 0; i < elem.dirs.length; ++i) {
-        if (createDirEntry(elem.dirs[i], folders, fullPath, currentFile,
-                           hasFoundFile) === true) {
-            addClass(name, "expand");
-            hasFoundFile = true;
+    if (elem.dirs) {
+        for (var i = 0; i < elem.dirs.length; ++i) {
+            if (createDirEntry(elem.dirs[i], folders, fullPath, currentFile,
+                               hasFoundFile) === true) {
+                addClass(name, "expand");
+                hasFoundFile = true;
+            }
         }
     }
     children.appendChild(folders);
 
     var files = document.createElement("div");
     files.className = "files";
-    for (i = 0; i < elem.files.length; ++i) {
-        var file = document.createElement("a");
-        file.innerText = elem.files[i];
-        file.href = window.rootPath + "src/" + fullPath + elem.files[i] + ".html";
-        if (hasFoundFile === false &&
-                currentFile === fullPath + elem.files[i]) {
-            file.className = "selected";
-            addClass(name, "expand");
-            hasFoundFile = true;
+    if (elem.files) {
+        for (i = 0; i < elem.files.length; ++i) {
+            var file = document.createElement("a");
+            file.innerText = elem.files[i];
+            file.href = window.rootPath + "src/" + fullPath + elem.files[i] + ".html";
+            if (hasFoundFile === false &&
+                    currentFile === fullPath + elem.files[i]) {
+                file.className = "selected";
+                addClass(name, "expand");
+                hasFoundFile = true;
+            }
+            files.appendChild(file);
         }
-        files.appendChild(file);
     }
     search.fullPath = fullPath;
     children.appendChild(files);