Skip to content

Commit 5617f5b

Browse files
committed
Extract makeScope() namespace validation into a shared helper
The reserved-sentinel and empty/NA guards were duplicated byte-for-byte in ShinySession$makeScope() and MockShinySession$makeScope(). Pull them into a package-level validateScopeNamespace(), mirroring the existing validateDestroyNamespace() helper. Behavior-preserving.
1 parent 1e387c3 commit 5617f5b

2 files changed

Lines changed: 22 additions & 30 deletions

File tree

R/mock-session.R

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -554,21 +554,7 @@ MockShinySession <- R6Class(
554554
#' @param namespace Character vector indicating a namespace.
555555
#' @return A new session proxy.
556556
makeScope = function(namespace) {
557-
if (identical(namespace, destroyNsRoot)) {
558-
stop(
559-
"The module namespace '", destroyNsRoot,
560-
"' is reserved for internal use.",
561-
call. = FALSE
562-
)
563-
}
564-
# A length-0 namespace (`NULL` or `character(0)`) is the root; "" / NA are
565-
# not valid (they can't be a fastmap key, and `NS("")` yields a stray `-`).
566-
if (length(namespace) == 1L && (is.na(namespace) || !nzchar(namespace))) {
567-
stop(
568-
"A module namespace must be a non-empty, non-NA string; use `NULL` for the root scope.",
569-
call. = FALSE
570-
)
571-
}
557+
validateScopeNamespace(namespace)
572558
ns <- NS(namespace)
573559
# The scope's own namespace, captured because the proxy `destroy()` below
574560
# has a `namespace` parameter that would otherwise shadow it.

R/shiny.R

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,26 @@ validateDestroyNamespace <- function(namespace) {
460460
invisible(namespace)
461461
}
462462

463+
# Validate a namespace passed to `makeScope()`. The root (length 0: `NULL` or
464+
# `character(0)`) is allowed; "" / NA are rejected (they can't be a fastmap key,
465+
# and `NS("")` yields a stray `-`); the reserved sentinel is rejected too.
466+
validateScopeNamespace <- function(namespace) {
467+
if (identical(namespace, destroyNsRoot)) {
468+
stop(
469+
"The module namespace '", destroyNsRoot,
470+
"' is reserved for internal use.",
471+
call. = FALSE
472+
)
473+
}
474+
if (length(namespace) == 1L && (is.na(namespace) || !nzchar(namespace))) {
475+
stop(
476+
"A module namespace must be a non-empty, non-NA string; use `NULL` for the root scope.",
477+
call. = FALSE
478+
)
479+
}
480+
invisible(namespace)
481+
}
482+
463483
#' @include utils.R
464484
ShinySession <- R6Class(
465485
'ShinySession',
@@ -1024,21 +1044,7 @@ ShinySession <- R6Class(
10241044
self
10251045
},
10261046
makeScope = function(namespace) {
1027-
if (identical(namespace, destroyNsRoot)) {
1028-
stop(
1029-
"The module namespace '", destroyNsRoot,
1030-
"' is reserved for internal use.",
1031-
call. = FALSE
1032-
)
1033-
}
1034-
# A length-0 namespace (`NULL` or `character(0)`) is the root; "" / NA are
1035-
# not valid (they can't be a fastmap key, and `NS("")` yields a stray `-`).
1036-
if (length(namespace) == 1L && (is.na(namespace) || !nzchar(namespace))) {
1037-
stop(
1038-
"A module namespace must be a non-empty, non-NA string; use `NULL` for the root scope.",
1039-
call. = FALSE
1040-
)
1041-
}
1047+
validateScopeNamespace(namespace)
10421048
ns <- NS(namespace)
10431049
# The scope's own namespace, captured because the proxy `destroy()` below
10441050
# has a `namespace` parameter that would otherwise shadow it.

0 commit comments

Comments
 (0)