@samitouri / QOS-React-2 / commits / c7e494b553

[React DevTools] Fix regex for formateWithStyles function (#24486)

The previous regex to detect string substitutions is not quite right, this PR fixes it by: Check to make sure we are starting either at the beginning of the line or we match a character that's not % to make sure we capture all the % in a row. Make sure there are an odd number of % (the first X pairs are escaped % characters. The odd % followed by a letter is the string substitution)

Luna Ruan committed May 3, 2022 at 15:52 UTC c7e494b55320768863b821be96896b28f0a280ef
2 files changed +3 -2
packages/react-devtools-shared/src/backend/utils.js
+2 -1
@@ -1,3 +1,4 @@
1 +/**
2 /**
3 * Copyright (c) Facebook, Inc. and its affiliates.
4 *
@@ -188,7 +189,7 @@ export function formatWithStyles(
189 }
190
191 // Matches any of %(o|O|d|i|s|f), but not %%(o|O|d|i|s|f)
191 - const REGEXP = /([^%]|^)(%([oOdisf]))/g;
192 + const REGEXP = /([^%]|^)((%%)*)(%([oOdisf]))/g;
193 if (inputArgs[0].match(REGEXP)) {
194 return [`%c${inputArgs[0]}`, style, ...inputArgs.slice(1)];
195 } else {
packages/react-devtools-shared/src/hook.js
+1 -1
@@ -189,7 +189,7 @@ export function installHook(target: any): DevToolsHook | null {
189 }
190
191 // Matches any of %(o|O|d|i|s|f), but not %%(o|O|d|i|s|f)
192 - const REGEXP = /([^%]|^)(%([oOdisf]))/g;
192 + const REGEXP = /([^%]|^)((%%)*)(%([oOdisf]))/g;
193 if (inputArgs[0].match(REGEXP)) {
194 return [`%c${inputArgs[0]}`, style, ...inputArgs.slice(1)];
195 } else {