Skip to content

Commit ad25472

Browse files
committed
feature #1519 [Translator] Clean code to suppress warnings (smnandre)
This PR was squashed before being merged into the 2.x branch. Discussion ---------- [Translator] Clean code to suppress warnings I've had enough of those TS warnings 😅 ```console /home/runner/work/ux/ux/src/Translator/assets/src/formatters/formatter.ts 82:38 warning Forbidden non-null assertion `@typescript`-eslint/no-non-null-assertion 86:47 warning Forbidden non-null assertion `@typescript`-eslint/no-non-null-assertion 86:107 warning Forbidden non-null assertion `@typescript`-eslint/no-non-null-assertion 87:62 warning Forbidden non-null assertion `@typescript`-eslint/no-non-null-assertion 87:124 warning Forbidden non-null assertion `@typescript`-eslint/no-non-null-assertion 89:30 warning Forbidden non-null assertion `@typescript`-eslint/no-non-null-assertion 90:33 warning Forbidden non-null assertion `@typescript`-eslint/no-non-null-assertion 92:34 warning Forbidden non-null assertion `@typescript`-eslint/no-non-null-assertion /home/runner/work/ux/ux/src/Translator/assets/src/translator.ts 28:26 warning 'Translations' is defined but never used `@typescript`-eslint/no-unused-vars ``` (ex: https://github.com/symfony/ux/actions/runs/7951722335/job/21705480408) -- The tests are green, but if someone want to checkproof ? Commits ------- c0a1948 [Translator] Clean code to suppress warnings
2 parents de3820c + c0a1948 commit ad25472

File tree

3 files changed

+28
-37
lines changed

3 files changed

+28
-37
lines changed

src/Translator/assets/dist/translator_controller.js

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,41 +38,34 @@ function format(id, parameters = {}, locale) {
3838
parts = id.split('|');
3939
}
4040
else {
41-
const matches = id.match(/(?:\|\||[^|])+/g);
42-
if (matches !== null) {
43-
parts = matches;
44-
}
41+
parts = id.match(/(?:\|\||[^|])+/g) || [];
4542
}
4643
const intervalRegex = /^(?<interval>({\s*(-?\d+(\.\d+)?[\s*,\s*\-?\d+(.\d+)?]*)\s*})|(?<left_delimiter>[[\]])\s*(?<left>-Inf|-?\d+(\.\d+)?)\s*,\s*(?<right>\+?Inf|-?\d+(\.\d+)?)\s*(?<right_delimiter>[[\]]))\s*(?<message>.*?)$/s;
4744
const standardRules = [];
4845
for (let part of parts) {
4946
part = part.trim().replace(/\|\|/g, '|');
50-
let matches = part.match(intervalRegex);
51-
if (matches !== null) {
47+
const matches = part.match(intervalRegex);
48+
if (matches) {
49+
const matchGroups = matches.groups || {};
5250
if (matches[2]) {
5351
for (const n of matches[3].split(',')) {
5452
if (number === Number(n)) {
55-
return strtr(matches.groups['message'], parameters);
53+
return strtr(matchGroups.message, parameters);
5654
}
5755
}
5856
}
5957
else {
60-
const leftNumber = '-Inf' === matches.groups['left'] ? Number.NEGATIVE_INFINITY : Number(matches.groups['left']);
61-
const rightNumber = ['Inf', '+Inf'].includes(matches.groups['right']) ? Number.POSITIVE_INFINITY : Number(matches.groups['right']);
62-
if (('[' === matches.groups['left_delimiter'] ? number >= leftNumber : number > leftNumber)
63-
&& (']' === matches.groups['right_delimiter'] ? number <= rightNumber : number < rightNumber)) {
64-
return strtr(matches.groups['message'], parameters);
58+
const leftNumber = '-Inf' === matchGroups.left ? Number.NEGATIVE_INFINITY : Number(matchGroups.left);
59+
const rightNumber = ['Inf', '+Inf'].includes(matchGroups.right) ? Number.POSITIVE_INFINITY : Number(matchGroups.right);
60+
if (('[' === matchGroups.left_delimiter ? number >= leftNumber : number > leftNumber)
61+
&& (']' === matchGroups.right_delimiter ? number <= rightNumber : number < rightNumber)) {
62+
return strtr(matchGroups.message, parameters);
6563
}
6664
}
6765
}
6866
else {
69-
matches = part.match(/^\w+:\s*(.*?)$/);
70-
if (matches !== null) {
71-
standardRules.push(matches[1]);
72-
}
73-
else {
74-
standardRules.push(part);
75-
}
67+
const ruleMatch = part.match(/^\w+:\s*(.*?)$/);
68+
standardRules.push(ruleMatch ? ruleMatch[1] : part);
7669
}
7770
}
7871
const position = getPluralizationRule(number, locale);

src/Translator/assets/src/formatters/formatter.ts

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,7 @@ export function format(id: string, parameters: Record<string, string | number> =
6262
if (/^\|+$/.test(id)) {
6363
parts = id.split('|');
6464
} else {
65-
const matches = id.match(/(?:\|\||[^|])+/g);
66-
if (matches !== null) {
67-
parts = matches;
68-
}
65+
parts = id.match(/(?:\|\||[^|])+/g) || [];
6966
}
7067

7168
const intervalRegex = /^(?<interval>({\s*(-?\d+(\.\d+)?[\s*,\s*\-?\d+(.\d+)?]*)\s*})|(?<left_delimiter>[[\]])\s*(?<left>-Inf|-?\d+(\.\d+)?)\s*,\s*(?<right>\+?Inf|-?\d+(\.\d+)?)\s*(?<right_delimiter>[[\]]))\s*(?<message>.*?)$/s;
@@ -74,31 +71,31 @@ export function format(id: string, parameters: Record<string, string | number> =
7471
for (let part of parts) {
7572
part = part.trim().replace(/\|\|/g, '|');
7673

77-
let matches = part.match(intervalRegex);
78-
if (matches !== null) {
74+
const matches = part.match(intervalRegex);
75+
if (matches) {
76+
/**
77+
* @type {interval: string, left_delimiter: string, left: string, right_delimiter: string, right: string, message: string}
78+
*/
79+
const matchGroups: { [p: string]: string } = matches.groups || {};
7980
if (matches[2]) {
8081
for (const n of matches[3].split(',')) {
8182
if (number === Number(n)) {
82-
return strtr(matches.groups!['message'], parameters);
83+
return strtr(matchGroups.message, parameters);
8384
}
8485
}
8586
} else {
86-
const leftNumber = '-Inf' === matches.groups!['left'] ? Number.NEGATIVE_INFINITY : Number(matches.groups!['left']);
87-
const rightNumber = ['Inf', '+Inf'].includes(matches.groups!['right']) ? Number.POSITIVE_INFINITY : Number(matches.groups!['right']);
87+
const leftNumber = '-Inf' === matchGroups.left ? Number.NEGATIVE_INFINITY : Number(matchGroups.left);
88+
const rightNumber = ['Inf', '+Inf'].includes(matchGroups.right) ? Number.POSITIVE_INFINITY : Number(matchGroups.right);
8889

89-
if (('[' === matches.groups!['left_delimiter'] ? number >= leftNumber : number > leftNumber)
90-
&& (']' === matches.groups!['right_delimiter'] ? number <= rightNumber : number < rightNumber)
90+
if (('[' === matchGroups.left_delimiter ? number >= leftNumber : number > leftNumber)
91+
&& (']' === matchGroups.right_delimiter ? number <= rightNumber : number < rightNumber)
9192
) {
92-
return strtr(matches.groups!['message'], parameters);
93+
return strtr(matchGroups.message, parameters);
9394
}
9495
}
9596
} else {
96-
matches = part.match(/^\w+:\s*(.*?)$/);
97-
if (matches !== null) {
98-
standardRules.push(matches[1]);
99-
} else {
100-
standardRules.push(part);
101-
}
97+
const ruleMatch = part.match(/^\w+:\s*(.*?)$/);
98+
standardRules.push(ruleMatch ? ruleMatch[1] : part);
10299
}
103100
}
104101

src/Translator/assets/src/translator.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ export type ParametersOf<M, D extends DomainType> = M extends Message<infer Tran
2525
: never
2626
: never;
2727

28+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
2829
export interface Message<Translations extends TranslationsType, Locale extends LocaleType> {
2930
id: string;
3031
translations: {

0 commit comments

Comments
 (0)