fix corner case with custom pragma (#42)

The AST changes when you call the same custom pragma more than once.
This commit is contained in:
Ștefan Talpalaru 2022-03-11 14:28:28 +01:00 committed by GitHub
parent 585059d2fb
commit 9826fddd1c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 2 deletions

View File

@ -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

View File

@ -10,7 +10,8 @@
import
test_ignore,
test_config_file,
test_envvar
test_envvar,
test_pragma
when defined(windows):
import test_winreg

27
tests/test_pragma.nim Normal file
View File

@ -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)