Skip to content

Commit

Permalink
fix: Update regex in valid-sprintf rule to handle '%%'
Browse files Browse the repository at this point in the history
  • Loading branch information
im3dabasia committed Dec 24, 2024
1 parent 910c38f commit 1963486
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 7 deletions.
12 changes: 12 additions & 0 deletions packages/eslint-plugin/rules/__tests__/valid-sprintf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand Down
14 changes: 7 additions & 7 deletions packages/eslint-plugin/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
/(?<!%)%(((\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)

/**
* "Unordered" means there's no position specifier: '%s', not '%2$s'.
Expand Down

0 comments on commit 1963486

Please sign in to comment.