@gitlab/eslint-plugin 19.4.0 → 19.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,10 @@
1
+ # [19.5.0](https://gitlab.com/gitlab-org/frontend/eslint-plugin/compare/v19.4.0...v19.5.0) (2024-03-27)
2
+
3
+
4
+ ### Features
5
+
6
+ * **require-valid-i18n-helpers:** lint against string interpolation ([268d858](https://gitlab.com/gitlab-org/frontend/eslint-plugin/commit/268d858f8bd02cab06ea74f2c6844166489d0d89))
7
+
1
8
  # [19.4.0](https://gitlab.com/gitlab-org/frontend/eslint-plugin/compare/v19.3.0...v19.4.0) (2024-01-10)
2
9
 
3
10
 
@@ -5,6 +5,8 @@ following:
5
5
 
6
6
  - All helpers should be called with string-literal arguments to ensure the strings are properly
7
7
  collected by the `gettext:regenerate` script.
8
+ - The helpers' arguments should not use string interpolation as this also prevents the strings from
9
+ being collected.
8
10
  - `__` should not be called with a namespace.
9
11
  - `s__` requires a translation namespace to be provided.
10
12
  - `n__` should use the same namespace, or no namespace at all, for both the singular and plural forms.
@@ -21,6 +23,11 @@ const description = __(getDescription());
21
23
  // Extraneous namespace
22
24
  const globalTranslation = __('Global|Translation');
23
25
 
26
+ // String interpolation
27
+ const withStringInterpolation1 = __(`String ${interpolation}`);
28
+ const withStringInterpolation2 = s__("SomeContext", `String ${interpolation}`);
29
+ const withStringInterpolation3 = n__("%d apple", `%d ${apples}`, appleCount);
30
+
24
31
  // Missing translation namespace
25
32
  const namespacedTranslation = s__('NamespacedTranslation');
26
33
 
@@ -13,6 +13,11 @@ This rule is like [require-valid-i18n-helpers](./require-valid-i18n-helpers.md)
13
13
  {{ __(LABEL) }}
14
14
  {{ __(getDescription()) }}
15
15
 
16
+ <!-- String interpolation -->
17
+ {{ __(`String ${interpolation}`) }}
18
+ {{ s__("SomeContext", `String ${interpolation}`) }}
19
+ {{ n__("%d apple", `%d ${apples}`, appleCount) }}
20
+
16
21
  <!-- Missing translation namespace -->
17
22
  {{ s__('NamespacedTranslation') }}
18
23
  </div>
@@ -10,6 +10,8 @@ const P_GETTEXT = 's__';
10
10
  const N_GETTEXT = 'n__';
11
11
 
12
12
  const ERROR_MESSAGE_STRING_LITERAL = 'Translation helpers must be called with a string literal.';
13
+ const ERROR_MESSAGE_STRING_INTERPOLATION =
14
+ 'Translation helpers should not use string interpolation.';
13
15
  const ERROR_MESSAGE_EXTRANEOUS_NAMESPACE =
14
16
  '__ should not be used with a translation namespace, consider using s__ instead.';
15
17
  const ERROR_MESSAGE_MISSING_NAMESPACE = 's__ requires a translation namespace to be provided.';
@@ -38,6 +40,16 @@ const mustBeCalledWithStringLiteralArguments = (context, node) => {
38
40
  }
39
41
  };
40
42
 
43
+ const mustNotHaveStringInterpolation = (context, node) => {
44
+ const [arg1, arg2] = node.arguments;
45
+ if ([arg1, arg2].some((arg) => arg?.expressions?.length)) {
46
+ context.report({
47
+ node,
48
+ message: ERROR_MESSAGE_STRING_INTERPOLATION,
49
+ });
50
+ }
51
+ };
52
+
41
53
  const mustHaveNamespace = (context, node) => {
42
54
  if (node.arguments.length === 2) {
43
55
  return;
@@ -94,9 +106,21 @@ const namespacesMustMatch = (context, node) => {
94
106
  };
95
107
 
96
108
  const VALIDATORS = {
97
- [GETTEXT]: [mustBeCalledWithStringLiteralArguments, mustNotHaveNamespace],
98
- [P_GETTEXT]: [mustBeCalledWithStringLiteralArguments, mustHaveNamespace],
99
- [N_GETTEXT]: [mustBeCalledWithStringLiteralArguments, namespacesMustMatch],
109
+ [GETTEXT]: [
110
+ mustBeCalledWithStringLiteralArguments,
111
+ mustNotHaveStringInterpolation,
112
+ mustNotHaveNamespace,
113
+ ],
114
+ [P_GETTEXT]: [
115
+ mustBeCalledWithStringLiteralArguments,
116
+ mustNotHaveStringInterpolation,
117
+ mustHaveNamespace,
118
+ ],
119
+ [N_GETTEXT]: [
120
+ mustBeCalledWithStringLiteralArguments,
121
+ mustNotHaveStringInterpolation,
122
+ namespacesMustMatch,
123
+ ],
100
124
  };
101
125
 
102
126
  const validateI18nHelperCallFactory = (context) => (node) => {
@@ -113,6 +137,7 @@ const validateI18nHelperCallFactory = (context) => (node) => {
113
137
  module.exports = {
114
138
  validateI18nHelperCallFactory,
115
139
  ERROR_MESSAGE_STRING_LITERAL,
140
+ ERROR_MESSAGE_STRING_INTERPOLATION,
116
141
  ERROR_MESSAGE_EXTRANEOUS_NAMESPACE,
117
142
  ERROR_MESSAGE_MISSING_NAMESPACE,
118
143
  ERROR_MESSAGE_NAMESPACES_MISMATCH,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gitlab/eslint-plugin",
3
- "version": "19.4.0",
3
+ "version": "19.5.0",
4
4
  "description": "GitLab package for our custom eslint rules",
5
5
  "main": "lib/index.js",
6
6
  "scripts": {