* fix #13491 runnableExamples rendering * fix a runnableExamples thanks to this bugfix
This commit is contained in:
parent
6cb26d8010
commit
d161d27cdd
4 changed files with 70 additions and 27 deletions
|
|
@ -456,18 +456,6 @@ proc nodeToHighlightedHtml(d: PDoc; n: PNode; result: var Rope; renderFlags: TRe
|
||||||
|
|
||||||
proc exampleOutputDir(d: PDoc): AbsoluteDir = d.conf.getNimcacheDir / RelativeDir"runnableExamples"
|
proc exampleOutputDir(d: PDoc): AbsoluteDir = d.conf.getNimcacheDir / RelativeDir"runnableExamples"
|
||||||
|
|
||||||
proc writeExample(d: PDoc; ex: PNode, rdoccmd: string) =
|
|
||||||
if d.conf.errorCounter > 0: return
|
|
||||||
let outputDir = d.exampleOutputDir
|
|
||||||
createDir(outputDir)
|
|
||||||
inc d.exampleCounter
|
|
||||||
let outp = outputDir / RelativeFile(extractFilename(d.filename.changeFileExt"" &
|
|
||||||
"_examples" & $d.exampleCounter & ".nim"))
|
|
||||||
#let nimcache = outp.changeFileExt"" & "_nimcache"
|
|
||||||
renderModule(ex, d.filename, outp.string, conf = d.conf)
|
|
||||||
if rdoccmd notin d.exampleGroups: d.exampleGroups[rdoccmd] = ExampleGroup(rdoccmd: rdoccmd, docCmd: d.conf.docCmd, index: d.exampleGroups.len)
|
|
||||||
d.exampleGroups[rdoccmd].code.add "import r\"$1\"\n" % outp.string
|
|
||||||
|
|
||||||
proc runAllExamples(d: PDoc) =
|
proc runAllExamples(d: PDoc) =
|
||||||
# This used to be: `let backend = if isDefined(d.conf, "js"): "js"` (etc), however
|
# This used to be: `let backend = if isDefined(d.conf, "js"): "js"` (etc), however
|
||||||
# using `-d:js` (etc) cannot work properly, e.g. would fail with `importjs`
|
# using `-d:js` (etc) cannot work properly, e.g. would fail with `importjs`
|
||||||
|
|
@ -498,6 +486,8 @@ proc runAllExamples(d: PDoc) =
|
||||||
rawMessage(d.conf, hintSuccess, ["runnableExamples: " & outp.string])
|
rawMessage(d.conf, hintSuccess, ["runnableExamples: " & outp.string])
|
||||||
# removeFile(outp.changeFileExt(ExeExt)) # it's in nimcache, no need to remove
|
# removeFile(outp.changeFileExt(ExeExt)) # it's in nimcache, no need to remove
|
||||||
|
|
||||||
|
proc quoted(a: string): string = result.addQuoted(a)
|
||||||
|
|
||||||
proc prepareExample(d: PDoc; n: PNode): tuple[rdoccmd: string, code: string] =
|
proc prepareExample(d: PDoc; n: PNode): tuple[rdoccmd: string, code: string] =
|
||||||
## returns `rdoccmd` and source code for this runnableExamples
|
## returns `rdoccmd` and source code for this runnableExamples
|
||||||
var rdoccmd = ""
|
var rdoccmd = ""
|
||||||
|
|
@ -508,19 +498,48 @@ proc prepareExample(d: PDoc; n: PNode): tuple[rdoccmd: string, code: string] =
|
||||||
if n1.kind notin nkStrKinds: globalError(d.conf, n1.info, "string litteral expected")
|
if n1.kind notin nkStrKinds: globalError(d.conf, n1.info, "string litteral expected")
|
||||||
rdoccmd = n1.strVal
|
rdoccmd = n1.strVal
|
||||||
|
|
||||||
var docComment = newTree(nkCommentStmt)
|
let useRenderModule = false
|
||||||
let loc = d.conf.toFileLineCol(n.info)
|
let loc = d.conf.toFileLineCol(n.info)
|
||||||
|
let code = extractRunnableExamplesSource(d.conf, n)
|
||||||
|
|
||||||
docComment.comment = "autogenerated by docgen\nloc: $1\nrdoccmd: $2" % [loc, rdoccmd]
|
if d.conf.errorCounter > 0:
|
||||||
var runnableExamples = newTree(nkStmtList,
|
return (rdoccmd, code)
|
||||||
docComment,
|
|
||||||
newTree(nkImportStmt, newStrNode(nkStrLit, d.filename)))
|
let comment = "autogenerated by docgen\nloc: $1\nrdoccmd: $2" % [loc, rdoccmd]
|
||||||
runnableExamples.info = n.info
|
let outputDir = d.exampleOutputDir
|
||||||
let ret = extractRunnableExamplesSource(d.conf, n)
|
createDir(outputDir)
|
||||||
for a in n.lastSon: runnableExamples.add a
|
inc d.exampleCounter
|
||||||
# we could also use `ret` instead here, to keep sources verbatim
|
let outp = outputDir / RelativeFile(extractFilename(d.filename.changeFileExt"" & ("_examples$1.nim" % $d.exampleCounter)))
|
||||||
writeExample(d, runnableExamples, rdoccmd)
|
|
||||||
result = (rdoccmd, ret)
|
if useRenderModule:
|
||||||
|
var docComment = newTree(nkCommentStmt)
|
||||||
|
docComment.comment = comment
|
||||||
|
var runnableExamples = newTree(nkStmtList,
|
||||||
|
docComment,
|
||||||
|
newTree(nkImportStmt, newStrNode(nkStrLit, d.filename)))
|
||||||
|
runnableExamples.info = n.info
|
||||||
|
for a in n.lastSon: runnableExamples.add a
|
||||||
|
|
||||||
|
# buggy, refs bug #17292
|
||||||
|
# still worth fixing as it can affect other code relying on `renderModule`,
|
||||||
|
# so we keep this code path here for now, which could still be useful in some
|
||||||
|
# other situations.
|
||||||
|
renderModule(runnableExamples, outp.string, conf = d.conf)
|
||||||
|
|
||||||
|
else:
|
||||||
|
let code2 = """
|
||||||
|
#[
|
||||||
|
$1
|
||||||
|
]#
|
||||||
|
import $2
|
||||||
|
$3
|
||||||
|
""" % [comment, d.filename.quoted, code]
|
||||||
|
writeFile(outp.string, code2)
|
||||||
|
|
||||||
|
if rdoccmd notin d.exampleGroups:
|
||||||
|
d.exampleGroups[rdoccmd] = ExampleGroup(rdoccmd: rdoccmd, docCmd: d.conf.docCmd, index: d.exampleGroups.len)
|
||||||
|
d.exampleGroups[rdoccmd].code.add "import $1\n" % outp.string.quoted
|
||||||
|
result = (rdoccmd, code)
|
||||||
when false:
|
when false:
|
||||||
proc extractImports(n: PNode; result: PNode) =
|
proc extractImports(n: PNode; result: PNode) =
|
||||||
if n.kind in {nkImportStmt, nkImportExceptStmt, nkFromStmt}:
|
if n.kind in {nkImportStmt, nkImportExceptStmt, nkFromStmt}:
|
||||||
|
|
|
||||||
|
|
@ -1647,7 +1647,7 @@ proc renderTree*(n: PNode, renderFlags: TRenderFlags = {}): string =
|
||||||
|
|
||||||
proc `$`*(n: PNode): string = n.renderTree
|
proc `$`*(n: PNode): string = n.renderTree
|
||||||
|
|
||||||
proc renderModule*(n: PNode, infile, outfile: string,
|
proc renderModule*(n: PNode, outfile: string,
|
||||||
renderFlags: TRenderFlags = {};
|
renderFlags: TRenderFlags = {};
|
||||||
fid = FileIndex(-1);
|
fid = FileIndex(-1);
|
||||||
conf: ConfigRef = nil) =
|
conf: ConfigRef = nil) =
|
||||||
|
|
|
||||||
|
|
@ -54,7 +54,6 @@ proc createProcType(p, b: NimNode): NimNode {.compileTime.} =
|
||||||
|
|
||||||
macro `=>`*(p, b: untyped): untyped =
|
macro `=>`*(p, b: untyped): untyped =
|
||||||
## Syntax sugar for anonymous procedures. It also supports pragmas.
|
## Syntax sugar for anonymous procedures. It also supports pragmas.
|
||||||
# TODO: xxx pending #13491: uncomment in runnableExamples
|
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
proc passTwoAndTwo(f: (int, int) -> int): int = f(2, 2)
|
proc passTwoAndTwo(f: (int, int) -> int): int = f(2, 2)
|
||||||
|
|
||||||
|
|
@ -69,8 +68,8 @@ macro `=>`*(p, b: untyped): untyped =
|
||||||
myBot.call = (name: string) {.noSideEffect.} => "Hello " & name & ", I'm a bot."
|
myBot.call = (name: string) {.noSideEffect.} => "Hello " & name & ", I'm a bot."
|
||||||
assert myBot.call("John") == "Hello John, I'm a bot."
|
assert myBot.call("John") == "Hello John, I'm a bot."
|
||||||
|
|
||||||
# let f = () => (discard) # simplest proc that returns void
|
let f = () => (discard) # simplest proc that returns void
|
||||||
# f()
|
f()
|
||||||
|
|
||||||
var
|
var
|
||||||
params = @[ident"auto"]
|
params = @[ident"auto"]
|
||||||
|
|
|
||||||
|
|
@ -109,6 +109,31 @@ when true: # runnableExamples with rdoccmd
|
||||||
# passing seq (to run with multiple compilation options)
|
# passing seq (to run with multiple compilation options)
|
||||||
runnableExamples(@["-b:cpp", "-b:js"]): discard
|
runnableExamples(@["-b:cpp", "-b:js"]): discard
|
||||||
|
|
||||||
|
runnableExamples:
|
||||||
|
block: # bug #17279
|
||||||
|
when int.sizeof == 8:
|
||||||
|
let x = 0xffffffffffffffff
|
||||||
|
doAssert x == -1
|
||||||
|
|
||||||
|
# bug #13491
|
||||||
|
block:
|
||||||
|
proc fun(): int = doAssert false
|
||||||
|
doAssertRaises(AssertionError, (discard fun()))
|
||||||
|
|
||||||
|
block:
|
||||||
|
template foo(body) = discard
|
||||||
|
foo (discard)
|
||||||
|
|
||||||
|
block:
|
||||||
|
template fn(body: untyped): untyped = true
|
||||||
|
doAssert(fn do: nonexistant)
|
||||||
|
import std/macros
|
||||||
|
macro foo*(x, y) =
|
||||||
|
result = newLetStmt(x[0][0], x[0][1])
|
||||||
|
foo:
|
||||||
|
a = 1
|
||||||
|
do: discard
|
||||||
|
|
||||||
# also check for runnableExamples at module scope
|
# also check for runnableExamples at module scope
|
||||||
runnableExamples:
|
runnableExamples:
|
||||||
block:
|
block:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue