ai-localize-validators 2.0.6 → 3.0.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,5 +1,35 @@
1
1
  # ai-localize-validators
2
2
 
3
+ ## 3.0.0
4
+
5
+ ### Major Changes
6
+
7
+ - bug fixes
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies
12
+ - ai-localize-shared@3.0.0
13
+
14
+ ## 2.0.7
15
+
16
+ ### Minor Changes
17
+
18
+ - **Two-pass `MissingKeyValidator`** — now validates both the default language file (checks
19
+ all keys exist and have non-empty values) and each target language file (checks keys exist,
20
+ are non-empty, and don't equal the source placeholder). Catches missing translations in the
21
+ source language itself.
22
+ - **`UnusedKeyValidator` guards missing `sourceDir`** — no longer throws when `sourceDir`
23
+ does not exist; returns an empty unused-keys list instead.
24
+ - **Backtick + last-segment key matching** — translation call detection now handles
25
+ template-literal keys and matches by last dot-separated segment for namespaced keys.
26
+
27
+ ### Patch Changes
28
+
29
+ - Added `repository`, `homepage`, `bugs`, `author` fields to `package.json` for npm registry display.
30
+ - Added `sideEffects: false` to enable tree-shaking in bundlers.
31
+ - Added `prepublishOnly` script to ensure the package is built before publishing.
32
+
3
33
  ## 2.0.6
4
34
 
5
35
  ### Patch Changes
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-2026 ai-localize-core contributors
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.js CHANGED
@@ -57,6 +57,28 @@ var MissingKeyValidator = class {
57
57
  const defaultDir = path.join(this.localesDir, this.defaultLanguage);
58
58
  if (!fs.existsSync(defaultDir)) return { errors, missingByLanguage };
59
59
  const nsFiles = fs.readdirSync(defaultDir).filter((f) => f.endsWith(".json"));
60
+ for (const nsFile of nsFiles) {
61
+ const defaultFilePath = path.join(defaultDir, nsFile);
62
+ const defaultEntries = (0, import_ai_localize_shared.readJsonSafe)(defaultFilePath);
63
+ if (!defaultEntries) continue;
64
+ const namespace = nsFile.replace(".json", "");
65
+ for (const [key, value] of Object.entries(defaultEntries)) {
66
+ const isEmpty = value === null || value === void 0 || typeof value === "string" && value.trim() === "";
67
+ if (isEmpty) {
68
+ const fullKey = `${namespace}.${key}`;
69
+ errors.push({
70
+ type: "missing-key",
71
+ key: fullKey,
72
+ language: this.defaultLanguage,
73
+ message: `Default locale "${this.defaultLanguage}" has empty value for key "${fullKey}"`,
74
+ filePath: defaultFilePath
75
+ });
76
+ if (!missingByLanguage[this.defaultLanguage])
77
+ missingByLanguage[this.defaultLanguage] = [];
78
+ missingByLanguage[this.defaultLanguage].push(fullKey);
79
+ }
80
+ }
81
+ }
60
82
  const langs = this.targetLanguages.length > 0 ? this.targetLanguages : fs.readdirSync(this.localesDir, { withFileTypes: true }).filter((e) => e.isDirectory() && e.name !== this.defaultLanguage).map((e) => e.name);
61
83
  for (const nsFile of nsFiles) {
62
84
  const defaultEntries = (0, import_ai_localize_shared.readJsonSafe)(path.join(defaultDir, nsFile));
@@ -75,7 +97,7 @@ var MissingKeyValidator = class {
75
97
  type: "missing-key",
76
98
  key: fullKey,
77
99
  language: lang,
78
- message: `Missing translation for "${fullKey}" in "${lang}" (value equals source)`,
100
+ message: `Missing translation for "${fullKey}" in "${lang}"`,
79
101
  filePath: targetPath
80
102
  });
81
103
  if (!missingByLanguage[lang]) missingByLanguage[lang] = [];
@@ -194,11 +216,18 @@ var UnusedKeyValidator = class {
194
216
  validate() {
195
217
  const warnings = [];
196
218
  const unusedKeys = [];
219
+ if (!this.sourceDir || !fs4.existsSync(this.sourceDir)) {
220
+ return { warnings, unusedKeys };
221
+ }
197
222
  const allKeys = this.collectLocaleKeys();
223
+ if (allKeys.length === 0) return { warnings, unusedKeys };
198
224
  const sourceContent = this.collectSourceContent();
199
225
  for (const key of allKeys) {
200
- const shortKey = key.includes(".") ? key.split(".").slice(1).join(".") : key;
201
- const referenced = sourceContent.includes(`'${key}'`) || sourceContent.includes(`"${key}"`) || sourceContent.includes(`'${shortKey}'`) || sourceContent.includes(`"${shortKey}"`);
226
+ const parts = key.split(".");
227
+ const shortKey = parts.length > 1 ? parts.slice(1).join(".") : key;
228
+ const lastSegment = parts[parts.length - 1];
229
+ const referenced = sourceContent.includes(`'${key}'`) || sourceContent.includes(`"${key}"`) || sourceContent.includes("`" + key + "`") || sourceContent.includes(`'${shortKey}'`) || sourceContent.includes(`"${shortKey}"`) || sourceContent.includes("`" + shortKey + "`") || // Also match the last dot-segment alone (e.g. t('submitButton'))
230
+ lastSegment !== key && (sourceContent.includes(`'${lastSegment}'`) || sourceContent.includes(`"${lastSegment}"`) || sourceContent.includes("`" + lastSegment + "`"));
202
231
  if (!referenced) {
203
232
  unusedKeys.push(key);
204
233
  warnings.push({ type: "unused-key", key, message: `Key "${key}" not referenced in source` });
package/dist/index.mjs CHANGED
@@ -17,6 +17,28 @@ var MissingKeyValidator = class {
17
17
  const defaultDir = path.join(this.localesDir, this.defaultLanguage);
18
18
  if (!fs.existsSync(defaultDir)) return { errors, missingByLanguage };
19
19
  const nsFiles = fs.readdirSync(defaultDir).filter((f) => f.endsWith(".json"));
20
+ for (const nsFile of nsFiles) {
21
+ const defaultFilePath = path.join(defaultDir, nsFile);
22
+ const defaultEntries = readJsonSafe(defaultFilePath);
23
+ if (!defaultEntries) continue;
24
+ const namespace = nsFile.replace(".json", "");
25
+ for (const [key, value] of Object.entries(defaultEntries)) {
26
+ const isEmpty = value === null || value === void 0 || typeof value === "string" && value.trim() === "";
27
+ if (isEmpty) {
28
+ const fullKey = `${namespace}.${key}`;
29
+ errors.push({
30
+ type: "missing-key",
31
+ key: fullKey,
32
+ language: this.defaultLanguage,
33
+ message: `Default locale "${this.defaultLanguage}" has empty value for key "${fullKey}"`,
34
+ filePath: defaultFilePath
35
+ });
36
+ if (!missingByLanguage[this.defaultLanguage])
37
+ missingByLanguage[this.defaultLanguage] = [];
38
+ missingByLanguage[this.defaultLanguage].push(fullKey);
39
+ }
40
+ }
41
+ }
20
42
  const langs = this.targetLanguages.length > 0 ? this.targetLanguages : fs.readdirSync(this.localesDir, { withFileTypes: true }).filter((e) => e.isDirectory() && e.name !== this.defaultLanguage).map((e) => e.name);
21
43
  for (const nsFile of nsFiles) {
22
44
  const defaultEntries = readJsonSafe(path.join(defaultDir, nsFile));
@@ -35,7 +57,7 @@ var MissingKeyValidator = class {
35
57
  type: "missing-key",
36
58
  key: fullKey,
37
59
  language: lang,
38
- message: `Missing translation for "${fullKey}" in "${lang}" (value equals source)`,
60
+ message: `Missing translation for "${fullKey}" in "${lang}"`,
39
61
  filePath: targetPath
40
62
  });
41
63
  if (!missingByLanguage[lang]) missingByLanguage[lang] = [];
@@ -154,11 +176,18 @@ var UnusedKeyValidator = class {
154
176
  validate() {
155
177
  const warnings = [];
156
178
  const unusedKeys = [];
179
+ if (!this.sourceDir || !fs4.existsSync(this.sourceDir)) {
180
+ return { warnings, unusedKeys };
181
+ }
157
182
  const allKeys = this.collectLocaleKeys();
183
+ if (allKeys.length === 0) return { warnings, unusedKeys };
158
184
  const sourceContent = this.collectSourceContent();
159
185
  for (const key of allKeys) {
160
- const shortKey = key.includes(".") ? key.split(".").slice(1).join(".") : key;
161
- const referenced = sourceContent.includes(`'${key}'`) || sourceContent.includes(`"${key}"`) || sourceContent.includes(`'${shortKey}'`) || sourceContent.includes(`"${shortKey}"`);
186
+ const parts = key.split(".");
187
+ const shortKey = parts.length > 1 ? parts.slice(1).join(".") : key;
188
+ const lastSegment = parts[parts.length - 1];
189
+ const referenced = sourceContent.includes(`'${key}'`) || sourceContent.includes(`"${key}"`) || sourceContent.includes("`" + key + "`") || sourceContent.includes(`'${shortKey}'`) || sourceContent.includes(`"${shortKey}"`) || sourceContent.includes("`" + shortKey + "`") || // Also match the last dot-segment alone (e.g. t('submitButton'))
190
+ lastSegment !== key && (sourceContent.includes(`'${lastSegment}'`) || sourceContent.includes(`"${lastSegment}"`) || sourceContent.includes("`" + lastSegment + "`"));
162
191
  if (!referenced) {
163
192
  unusedKeys.push(key);
164
193
  warnings.push({ type: "unused-key", key, message: `Key "${key}" not referenced in source` });
package/package.json CHANGED
@@ -1,10 +1,22 @@
1
1
  {
2
2
  "name": "ai-localize-validators",
3
- "version": "2.0.6",
3
+ "version": "3.0.0",
4
4
  "description": "Locale file validation engine — missing, duplicate, placeholder and unused key validators",
5
+ "author": "ai-localize-core contributors",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "https://github.com/ai-localize/ai-localize-core.git",
10
+ "directory": "packages/validators"
11
+ },
12
+ "homepage": "https://github.com/ai-localize/ai-localize-core/tree/main/packages/validators#readme",
13
+ "bugs": {
14
+ "url": "https://github.com/ai-localize/ai-localize-core/issues"
15
+ },
5
16
  "main": "./dist/index.js",
6
17
  "module": "./dist/index.mjs",
7
18
  "types": "./dist/index.d.ts",
19
+ "sideEffects": false,
8
20
  "files": [
9
21
  "dist",
10
22
  "README.md",
@@ -33,14 +45,13 @@
33
45
  "node": ">=18.0.0"
34
46
  },
35
47
  "dependencies": {
36
- "ai-localize-shared": "2.0.6"
48
+ "ai-localize-shared": "3.0.0"
37
49
  },
38
50
  "devDependencies": {
39
51
  "tsup": "^8.0.1",
40
52
  "typescript": "^5.3.3",
41
53
  "vitest": "^1.2.1"
42
54
  },
43
- "license": "MIT",
44
55
  "publishConfig": {
45
56
  "access": "public"
46
57
  },