From 5039a149859ad42e8d0435d13b90531a2a202c3a Mon Sep 17 00:00:00 2001 From: Sourav Agrawal Date: Mon, 29 Sep 2025 13:29:57 +0530 Subject: [PATCH] fix: highlight environment on string containing dot (#5409) Co-authored-by: nivedin Co-authored-by: Nivedin <53208152+nivedin@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- .../hoppscotch-common/src/helpers/curl/curlparser.ts | 7 +------ .../src/helpers/editor/extensions/HoppEnvironment.ts | 9 ++++++--- .../hoppscotch-common/src/helpers/environment-regex.ts | 8 ++++++++ .../inspection/inspectors/environment.inspector.ts | 3 +-- 4 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 packages/hoppscotch-common/src/helpers/environment-regex.ts diff --git a/packages/hoppscotch-common/src/helpers/curl/curlparser.ts b/packages/hoppscotch-common/src/helpers/curl/curlparser.ts index c116bd82..d721c4db 100644 --- a/packages/hoppscotch-common/src/helpers/curl/curlparser.ts +++ b/packages/hoppscotch-common/src/helpers/curl/curlparser.ts @@ -25,15 +25,10 @@ import { getMethod } from "./sub_helpers/method" import { preProcessCurlCommand } from "./sub_helpers/preproc" import { getQueries } from "./sub_helpers/queries" import { concatParams, getURLObject } from "./sub_helpers/url" +import { HOPP_ENVIRONMENT_REGEX } from "../environment-regex" const defaultRESTReq = getDefaultRESTRequest() -/** - * Regex to match environment variables in the format `<>`. - * This is used to identify if the request contains environment variables that need to be handled specially. - */ -const HOPP_ENVIRONMENT_REGEX = /(<<[a-zA-Z0-9-_]+>>)/g - /** * * @param str The string to test for environment variables. diff --git a/packages/hoppscotch-common/src/helpers/editor/extensions/HoppEnvironment.ts b/packages/hoppscotch-common/src/helpers/editor/extensions/HoppEnvironment.ts index 10c9ec27..a7442326 100644 --- a/packages/hoppscotch-common/src/helpers/editor/extensions/HoppEnvironment.ts +++ b/packages/hoppscotch-common/src/helpers/editor/extensions/HoppEnvironment.ts @@ -37,8 +37,11 @@ import IconLibrary from "~icons/lucide/library?raw" import { isComment } from "./helpers" import { transformInheritedCollectionVariablesToAggregateEnv } from "~/helpers/utils/inheritedCollectionVarTransformer" import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties" +import { + ENV_VAR_NAME_REGEX, + HOPP_ENVIRONMENT_REGEX, +} from "~/helpers/environment-regex" -const HOPP_ENVIRONMENT_REGEX = /(<<[a-zA-Z0-9-_]+>>)/g const HOPP_ENV_HIGHLIGHT = "cursor-help transition rounded px-1 focus:outline-none mx-0.5 env-highlight" const HOPP_REQUEST_VARIABLE_HIGHLIGHT = "request-variable-highlight" @@ -98,9 +101,9 @@ const cursorTooltipField = (aggregateEnvs: AggregateEnvironment[]) => // Tracking the start and the end of the words let start = pos let end = pos - while (start > from && /[a-zA-Z0-9-_]+/.test(text[start - from - 1])) + while (start > from && ENV_VAR_NAME_REGEX.test(text[start - from - 1])) start-- - while (end < to && /[a-zA-Z0-9-_]+/.test(text[end - from])) end++ + while (end < to && ENV_VAR_NAME_REGEX.test(text[end - from])) end++ if ( (start === pos && side < 0) || diff --git a/packages/hoppscotch-common/src/helpers/environment-regex.ts b/packages/hoppscotch-common/src/helpers/environment-regex.ts new file mode 100644 index 00000000..b462c2aa --- /dev/null +++ b/packages/hoppscotch-common/src/helpers/environment-regex.ts @@ -0,0 +1,8 @@ +// Regex to match environment variables in the format `<>`/`<>` etc. +// This is used to identify if the request contains environment variables that need to be handled specially. + +const ENV_VAR_NAME_PATTERN = "[a-zA-Z0-9_.-]+" +const HOPP_ENVIRONMENT_REGEX = new RegExp(`(<<${ENV_VAR_NAME_PATTERN}>>)`, "g") +const ENV_VAR_NAME_REGEX = new RegExp(ENV_VAR_NAME_PATTERN) + +export { HOPP_ENVIRONMENT_REGEX, ENV_VAR_NAME_REGEX } diff --git a/packages/hoppscotch-common/src/services/inspection/inspectors/environment.inspector.ts b/packages/hoppscotch-common/src/services/inspection/inspectors/environment.inspector.ts index 947b16fd..323eb2c3 100644 --- a/packages/hoppscotch-common/src/services/inspection/inspectors/environment.inspector.ts +++ b/packages/hoppscotch-common/src/services/inspection/inspectors/environment.inspector.ts @@ -25,8 +25,7 @@ import { SecretEnvironmentService } from "~/services/secret-environment.service" import { RESTTabService } from "~/services/tab/rest" import { CurrentValueService } from "~/services/current-environment-value.service" import { transformInheritedCollectionVariablesToAggregateEnv } from "~/helpers/utils/inheritedCollectionVarTransformer" - -const HOPP_ENVIRONMENT_REGEX = /(<<[a-zA-Z0-9-_]+>>)/g +import { HOPP_ENVIRONMENT_REGEX } from "~/helpers/environment-regex" const isENVInString = (str: string) => HOPP_ENVIRONMENT_REGEX.test(str)