Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESLint: Improve regex for valid-sprintf rule to handle '%%' #68270

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading