Skip to content

Commit

Permalink
fix corner case with custom pragma (#42)
Browse files Browse the repository at this point in the history
The AST changes when you call the same custom pragma more than once.
  • Loading branch information
stefantalpalaru authored Mar 11, 2022
1 parent 585059d commit 9826fdd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
15 changes: 14 additions & 1 deletion confutils/config_file.nim
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,20 @@ proc shortEnumName(n: NimNode): NimNode =
proc traversePragma(pragma: NimNode):
tuple[isCommandOrArgument: bool, defaultValue, namePragma: string] =
pragma.expectKind nnkPragma
for child in pragma:
var child: NimNode

for childNode in pragma:
child = childNode

if child.kind == nnkCall:
# A custom pragma was used more than once (e.g.: {.pragma: posixOnly, hidden.}) and the
# AST is now:
# ```
# Call
# Sym "hidden"
# ```
child = child[0]

case child.kind
of nnkSym:
let sym = $child
Expand Down
3 changes: 2 additions & 1 deletion tests/test_all.nim
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import
test_ignore,
test_config_file,
test_envvar
test_envvar,
test_pragma

when defined(windows):
import test_winreg
27 changes: 27 additions & 0 deletions tests/test_pragma.nim
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import
std/unittest,
../confutils,
../confutils/defs

{.pragma: customPragma, hidden.}

type
TestConf* = object
statusBarEnabled* {.
customPragma
desc: "Display a status bar at the bottom of the terminal screen"
defaultValue: true
name: "status-bar" }: bool

statusBarEnabled2* {.
customPragma
desc: "Display a status bar at the bottom of the terminal screen"
defaultValue: true
name: "status-bar2" }: bool

suite "test custom pragma":
test "funny AST when called twice":
let conf = TestConf.load()
doAssert(conf.statusBarEnabled == true)
doAssert(conf.statusBarEnabled2 == true)

0 comments on commit 9826fdd

Please sign in to comment.