-
Notifications
You must be signed in to change notification settings - Fork 4.6k
Add support for precision type placeholders to translator comments eslint #71145
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -56,19 +56,47 @@ const REGEXP_SPRINTF_PLACEHOLDER_UNORDERED = | |||||||||||||||||
| /(?:(?<!%)%[+-]?(?:(?:0|'.)?-?[0-9]*(?:\.(?:[ 0]|'.)?[0-9]+)?|(?:[ ])?-?[0-9]+(?:\.(?:[ 0]|'.)?[0-9]+)?)[bcdeEfFgGosuxX])/; | ||||||||||||||||||
|
|
||||||||||||||||||
| /** | ||||||||||||||||||
| * Regular expression matching comment placeholders. | ||||||||||||||||||
| * | ||||||||||||||||||
| * /(?:^|\s|,)\s*(%[sdf]|%?[a-zA-Z0-9_]+|%[0-9]+\$?[sdf]{0,1})(:)?/g; | ||||||||||||||||||
| * ▲ ▲ ▲ ▲ ▲ | ||||||||||||||||||
| * │ │ │ │ │ | ||||||||||||||||||
| * │ │ │ │ └─ Match colon at the end of the placeholder ( optional ) | ||||||||||||||||||
| * │ │ │ └─ Match a Index placeholder but allow variations (e.g. %1, %1s, %1$d) | ||||||||||||||||||
| * │ │ └─ Match a placeholder with index or named argument (e.g. %1, %name, %2) | ||||||||||||||||||
| * │ └─ Match Unamed placeholder (e.g. %s, %d) | ||||||||||||||||||
| * └─ Match the start of string, whitespace, or comma | ||||||||||||||||||
| * Regular expression to extract placeholder keys from translator comments. | ||||||||||||||||||
| * | ||||||||||||||||||
| * It matches common i18n placeholders and comment-only keys, with optional | ||||||||||||||||||
| * precision and a trailing colon (indicating a description). | ||||||||||||||||||
| * | ||||||||||||||||||
| * Breakdown of the regex: | ||||||||||||||||||
| *```md | ||||||||||||||||||
| * (?:^|\s|,) — Non-capturing group that matches the start of the string, a whitespace character, or a comma (ensures proper separation). | ||||||||||||||||||
| * | ||||||||||||||||||
| * \s* — Optional whitespace after the separator. | ||||||||||||||||||
| * | ||||||||||||||||||
| * ( — Capturing group for the full placeholder (used as key): | ||||||||||||||||||
| * %? — Optional `%` to allow bare keys like `1`, `label` in comments. | ||||||||||||||||||
| * ( — Group for matching placeholder variants: | ||||||||||||||||||
| * \(?<named>[a-zA-Z_][a-zA-Z0-9_]*\) — Named placeholder in the form: %(name) | ||||||||||||||||||
| * (?:\.\d+|\.\*)? — Optional precision: .2 or .* | ||||||||||||||||||
| * [sdf] — Format specifier: s, d, or f | ||||||||||||||||||
| * | ||||||||||||||||||
| * | | ||||||||||||||||||
| * (?<positional>[1-9][0-9]*)\$? — Positional placeholder like %1$ | ||||||||||||||||||
| * (?:\.\d+|\.\*)? — Optional precision | ||||||||||||||||||
| * [sdf] — Format specifier | ||||||||||||||||||
| * | ||||||||||||||||||
| * | — OR | ||||||||||||||||||
| * (?:\.\d+|\.\*)?[sdf] — Unnamed placeholder with optional precision | ||||||||||||||||||
| * | ||||||||||||||||||
| * | [1-9][0-9]* — Bare positional key like `1`, `2` | ||||||||||||||||||
| * | [sdf] — Just a format type | ||||||||||||||||||
| * | [a-zA-Z_][a-zA-Z0-9_]* — Bare named key (used in comments) | ||||||||||||||||||
| * ) | ||||||||||||||||||
| * ) | ||||||||||||||||||
| * | ||||||||||||||||||
| * (?<colon>:[ \t]+)? — Optional named group `colon`, matches a colon followed by space or tab, | ||||||||||||||||||
| * indicating that this placeholder has a description in the comment. | ||||||||||||||||||
| * | ||||||||||||||||||
| * Flags: | ||||||||||||||||||
| * g — global, so it matches all placeholders in the comment string. | ||||||||||||||||||
| * ``` | ||||||||||||||||||
| */ | ||||||||||||||||||
| const REGEXP_COMMENT_PLACEHOLDER = | ||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I feel a top-level arrow-based breakdown (similar to the one in Having those quick visual markers at the top makes it much easier to parse at a glance. WDYT? gutenberg/packages/eslint-plugin/utils/constants.js Lines 39 to 46 in e10b175
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did also thought of that but when I tried to write that due to the regex length, the visual marker syntax felt awkward to read as it was getting hard to mark the points that I wanted to explain, Hence I took this approach as it allowed me to break the regex into a way, I thought I could explain it |
||||||||||||||||||
| /(?:^|\s|,)\s*(%[sdf]|%?[a-zA-Z0-9_]+|%[0-9]+\$?[sdf]{0,1})(:)?/g; | ||||||||||||||||||
| /(?:^|\s|,)\s*(%?(?:\((?<named>[a-zA-Z_][a-zA-Z0-9_]*)\)(?:\.\d+|\.\*)?[sdf]|(?<positional>[1-9][0-9]*)\$?(?:\.\d+|\.\*)?[sdf]|(?:\.\d+|\.\*)?[sdf]|[1-9][0-9]*|[sdf]|[a-zA-Z_][a-zA-Z0-9_]*))(?<colon>:[ \t]+)?/g; | ||||||||||||||||||
|
|
||||||||||||||||||
| module.exports = { | ||||||||||||||||||
| TRANSLATION_FUNCTIONS, | ||||||||||||||||||
|
|
||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is not needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added this test because it was mention that, this was causing a false error flag on extra placeholder
Link: #70458 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I meant the inline comment 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, my bad 😅