From 1963486dad5e8fc53982a31589feee1e8ef8aeb7 Mon Sep 17 00:00:00 2001 From: im3dabasia Date: Tue, 24 Dec 2024 17:19:52 +0530 Subject: [PATCH] fix: Update regex in valid-sprintf rule to handle '%%' --- .../eslint-plugin/rules/__tests__/valid-sprintf.js | 12 ++++++++++++ packages/eslint-plugin/utils/constants.js | 14 +++++++------- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/packages/eslint-plugin/rules/__tests__/valid-sprintf.js b/packages/eslint-plugin/rules/__tests__/valid-sprintf.js index 9b2b7de255d47b..8f5b77458fbaeb 100644 --- a/packages/eslint-plugin/rules/__tests__/valid-sprintf.js +++ b/packages/eslint-plugin/rules/__tests__/valid-sprintf.js @@ -71,6 +71,18 @@ sprintf( { code: `sprintf( '%(greeting)s %(toWhom)s', 'Hello', 'World' )`, }, + { + code: `sprintf( 'Rotated at %d %% degrees', 90 )`, + }, + { + code: `sprintf( 'Rotated at %d%% degrees', 90 )`, + }, + { + code: `sprintf( __( 'Rotated at %d%% degrees' ), 90 )`, + }, + { + code: `sprintf( 'Rotated at %1$d %% degrees, %2$d %% angles', 90, 180 )`, + }, ], invalid: [ { diff --git a/packages/eslint-plugin/utils/constants.js b/packages/eslint-plugin/utils/constants.js index a19add74964c0e..44e881fb867c78 100644 --- a/packages/eslint-plugin/utils/constants.js +++ b/packages/eslint-plugin/utils/constants.js @@ -37,13 +37,13 @@ const TRANSLATION_FUNCTIONS = new Set( [ '__', '_x', '_n', '_nx' ] ); * @type {RegExp} */ const REGEXP_SPRINTF_PLACEHOLDER = - /%(((\d+)\$)|(\(([$_a-zA-Z][$_a-zA-Z0-9]*)\)))?[ +0#-]*\d*(\.(\d+|\*))?(ll|[lhqL])?([cduxXefgsp%])/g; -// ▲ ▲ ▲ ▲ ▲ ▲ ▲ type -// │ │ │ │ │ └ Length (unsupported) -// │ │ │ │ └ Precision / max width -// │ │ │ └ Min width (unsupported) -// │ │ └ Flags (unsupported) -// └ Index └ Name (for named arguments) + /(?