From 2aeb0d516b5e4cde1abb68a0c0d5393cf8c65915 Mon Sep 17 00:00:00 2001 From: ringabout <43030857+ringabout@users.noreply.github.com> Date: Thu, 4 Aug 2022 14:49:51 +0800 Subject: [PATCH] fixes #20132; fixes the broken jsondoc comand [backport] (#20135) * fixes #20132; fixes the broken jsondoc comand * add testcase --- compiler/docgen.nim | 3 ++- tests/misc/mjsondoc.nim | 11 +++++++++++ tests/misc/trunner.nim | 17 +++++++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tests/misc/mjsondoc.nim diff --git a/compiler/docgen.nim b/compiler/docgen.nim index 0ca1b8c52a4e9..52bb93c19a579 100644 --- a/compiler/docgen.nim +++ b/compiler/docgen.nim @@ -1360,9 +1360,10 @@ proc finishGenerateDoc*(d: var PDoc) = var str: string renderRstToOut(d[], resolved, str) entry.json[entry.rstField] = %str - d.jEntriesFinal.add entry.json d.jEntriesPre[i].rst = nil + d.jEntriesFinal.add entry.json # generates docs + proc add(d: PDoc; j: JsonItem) = if j.json != nil or j.rst != nil: d.jEntriesPre.add j diff --git a/tests/misc/mjsondoc.nim b/tests/misc/mjsondoc.nim new file mode 100644 index 0000000000000..e4642f0b490a8 --- /dev/null +++ b/tests/misc/mjsondoc.nim @@ -0,0 +1,11 @@ +proc doSomething*(x, y: int): int = + ## do something + x + y + +const + a* = 1 ## echo 1234 + b* = "test" + +type + MyEnum* = enum + foo, bar diff --git a/tests/misc/trunner.nim b/tests/misc/trunner.nim index 5d12c38b6fa9a..67615cee94555 100644 --- a/tests/misc/trunner.nim +++ b/tests/misc/trunner.nim @@ -216,6 +216,23 @@ sub/mmain.idx""", context let cmd = fmt"{nim} r --backend:{mode} --hints:off --nimcache:{nimcache} {file}" check execCmdEx(cmd) == ("ok3\n", 0) + block: # nim jsondoc # bug #20132 + let file = testsDir / "misc/mjsondoc.nim" + let output = "nimcache_tjsondoc.json" + defer: removeFile(output) + let (msg, exitCode) = execCmdEx(fmt"{nim} jsondoc -o:{output} {file}") + doAssert exitCode == 0, msg + + let data = parseJson(readFile(output))["entries"] + doAssert data.len == 4 + let doSomething = data[0] + doAssert doSomething["name"].getStr == "doSomething" + doAssert doSomething["type"].getStr == "skProc" + doAssert doSomething["line"].getInt == 1 + doAssert doSomething["col"].getInt == 0 + doAssert doSomething["code"].getStr == "proc doSomething(x, y: int): int {.raises: [], tags: [], forbids: [].}" + + block: # further issues with `--backend` let file = testsDir / "misc/mbackend.nim" var cmd = fmt"{nim} doc -b:cpp --hints:off --nimcache:{nimcache} {file}"