i18next-cli 1.88.1 → 1.90.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/README.md +104 -12
- package/dist/cjs/cli.js +25 -6
- package/dist/cjs/config.js +13 -12
- package/dist/cjs/extractor/core/extractor.js +1 -3
- package/dist/cjs/extractor/core/translation-manager.js +9 -2
- package/dist/cjs/heuristic-config.js +51 -6
- package/dist/cjs/index.js +2 -1
- package/dist/cjs/init.js +8 -11
- package/dist/cjs/instrumenter/core/instrumenter.js +1372 -218
- package/dist/cjs/instrumenter/core/key-generator.js +20 -9
- package/dist/cjs/instrumenter/core/next-files.js +104 -0
- package/dist/cjs/instrumenter/core/string-detector.js +106 -29
- package/dist/cjs/instrumenter/core/transformer.js +165 -83
- package/dist/cjs/localize/agent-prompt.js +5 -2
- package/dist/cjs/localize/detect.js +3 -11
- package/dist/cjs/localize/localize.js +27 -5
- package/dist/cjs/utils/file-utils.js +48 -0
- package/dist/cjs/utils/jsx-attributes.js +3 -1
- package/dist/esm/cli.js +25 -6
- package/dist/esm/config.js +13 -12
- package/dist/esm/extractor/core/extractor.js +1 -3
- package/dist/esm/extractor/core/translation-manager.js +9 -2
- package/dist/esm/heuristic-config.js +52 -7
- package/dist/esm/index.js +2 -1
- package/dist/esm/init.js +8 -11
- package/dist/esm/instrumenter/core/instrumenter.js +1376 -222
- package/dist/esm/instrumenter/core/key-generator.js +20 -9
- package/dist/esm/instrumenter/core/next-files.js +101 -0
- package/dist/esm/instrumenter/core/string-detector.js +107 -31
- package/dist/esm/instrumenter/core/transformer.js +165 -83
- package/dist/esm/localize/agent-prompt.js +5 -2
- package/dist/esm/localize/detect.js +4 -12
- package/dist/esm/localize/localize.js +27 -5
- package/dist/esm/utils/file-utils.js +49 -2
- package/dist/esm/utils/jsx-attributes.js +3 -1
- package/package.json +1 -1
- package/types/cli.d.ts.map +1 -1
- package/types/config.d.ts.map +1 -1
- package/types/extractor/core/extractor.d.ts.map +1 -1
- package/types/extractor/core/translation-manager.d.ts.map +1 -1
- package/types/heuristic-config.d.ts.map +1 -1
- package/types/index.d.ts +1 -1
- package/types/index.d.ts.map +1 -1
- package/types/init.d.ts.map +1 -1
- package/types/instrumenter/core/instrumenter.d.ts.map +1 -1
- package/types/instrumenter/core/key-generator.d.ts +2 -0
- package/types/instrumenter/core/key-generator.d.ts.map +1 -1
- package/types/instrumenter/core/next-files.d.ts +25 -0
- package/types/instrumenter/core/next-files.d.ts.map +1 -0
- package/types/instrumenter/core/string-detector.d.ts +5 -1
- package/types/instrumenter/core/string-detector.d.ts.map +1 -1
- package/types/instrumenter/core/transformer.d.ts +16 -1
- package/types/instrumenter/core/transformer.d.ts.map +1 -1
- package/types/instrumenter/index.d.ts +1 -1
- package/types/instrumenter/index.d.ts.map +1 -1
- package/types/localize/agent-prompt.d.ts +1 -1
- package/types/localize/agent-prompt.d.ts.map +1 -1
- package/types/localize/detect.d.ts +2 -0
- package/types/localize/detect.d.ts.map +1 -1
- package/types/localize/localize.d.ts.map +1 -1
- package/types/types.d.ts +87 -1
- package/types/types.d.ts.map +1 -1
- package/types/utils/file-utils.d.ts +7 -0
- package/types/utils/file-utils.d.ts.map +1 -1
- package/types/utils/jsx-attributes.d.ts.map +1 -1
|
@@ -12,24 +12,31 @@
|
|
|
12
12
|
function generateKeyFromContent(content) {
|
|
13
13
|
// Remove punctuation and split by whitespace and word boundaries
|
|
14
14
|
const normalized = content
|
|
15
|
+
// A placeholder's name gives words of its own: `{{unreadNews}}` → unread News
|
|
16
|
+
.replace(/\{\{\s*([^}]*?)\s*\}\}/g, (_, name) => ` ${name.replace(/([a-z0-9])([A-Z])/g, '$1 $2')} `)
|
|
17
|
+
.replace(/_/g, ' ') // `_` separates plural and context suffixes in i18next keys: none in a generated one
|
|
18
|
+
.normalize('NFD').replace(/[\u0300-\u036f]/g, '') // `Français` → Francais, not Franais
|
|
15
19
|
.replace(/[^\w\s\d]/g, '') // Remove punctuation
|
|
16
20
|
.trim();
|
|
17
21
|
if (!normalized) {
|
|
18
22
|
return 'key';
|
|
19
23
|
}
|
|
20
|
-
// Split on whitespace and camelCase
|
|
24
|
+
// Split on whitespace and camelCase; whole words up to MAX_KEY_LENGTH, the first word always
|
|
21
25
|
const words = normalized.split(/\s+/);
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
}
|
|
29
|
-
.join('');
|
|
26
|
+
let camelCased = '';
|
|
27
|
+
for (const [index, word] of words.entries()) {
|
|
28
|
+
const part = index === 0 ? word.toLowerCase() : word.charAt(0).toUpperCase() + word.slice(1).toLowerCase();
|
|
29
|
+
if (index > 0 && camelCased.length + part.length > MAX_KEY_LENGTH)
|
|
30
|
+
break;
|
|
31
|
+
camelCased += part;
|
|
32
|
+
}
|
|
30
33
|
// If result is empty, use fallback
|
|
31
34
|
return camelCased.length > 0 ? camelCased : 'key';
|
|
32
35
|
}
|
|
36
|
+
/** A key names its text, it does not repeat it: a long sentence's key keeps its first words. */
|
|
37
|
+
const MAX_KEY_LENGTH = 40;
|
|
38
|
+
/** The content of a key reserved as the parent of nested keys: no text matches it. */
|
|
39
|
+
const PARENT_KEY = '\u0000';
|
|
33
40
|
/**
|
|
34
41
|
* Creates a new key registry with collision detection.
|
|
35
42
|
*/
|
|
@@ -37,6 +44,10 @@ function createKeyRegistry() {
|
|
|
37
44
|
const keys = new Map();
|
|
38
45
|
return {
|
|
39
46
|
keys,
|
|
47
|
+
reserve(key, content) {
|
|
48
|
+
if (!keys.has(key))
|
|
49
|
+
keys.set(key, content ?? PARENT_KEY);
|
|
50
|
+
},
|
|
40
51
|
add(baseKey, content) {
|
|
41
52
|
const existing = keys.get(baseKey);
|
|
42
53
|
// No collision - add the key
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { realpathSync } from 'node:fs';
|
|
2
|
+
import { resolve, sep, dirname, relative } from 'node:path';
|
|
3
|
+
import { getTsConfigAliases } from '../../config.js';
|
|
4
|
+
|
|
5
|
+
/** A `'use client'` directive: the first statement, after comments only. */
|
|
6
|
+
const USE_CLIENT_RE = /^(?:\s|\/\/[^\n]*\n|\/\*[\s\S]*?\*\/)*(['"])use client\1/;
|
|
7
|
+
/** The module specifiers of a file's static, dynamic and `require` imports and its re-exports. */
|
|
8
|
+
const IMPORT_RE = /\bfrom\s*['"]([^'"\n]+)['"]|\bimport\s*\(?\s*['"]([^'"\n]+)['"]|\brequire\(\s*['"]([^'"\n]+)['"]\s*\)/g;
|
|
9
|
+
const EXTENSIONS = ['', '.tsx', '.ts', '.jsx', '.js', '.mjs', '/index.tsx', '/index.ts', '/index.jsx', '/index.js'];
|
|
10
|
+
/**
|
|
11
|
+
* A file's absolute path with symbolic links resolved (`/var` is `/private/var` on macOS); one that does not exist
|
|
12
|
+
* (yet) resolves under the project root's real path.
|
|
13
|
+
*/
|
|
14
|
+
function real(file, cwd) {
|
|
15
|
+
const abs = resolve(cwd, file);
|
|
16
|
+
try {
|
|
17
|
+
return realpathSync(abs);
|
|
18
|
+
}
|
|
19
|
+
catch { }
|
|
20
|
+
try {
|
|
21
|
+
return abs.startsWith(cwd + sep) ? realpathSync(cwd) + abs.slice(cwd.length) : abs;
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
return abs;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
/** A file's path in the project, `/`-separated, without a leading `src/`: `app/page.tsx`, `pages/api/hello.js`. */
|
|
28
|
+
function projectPath(file, cwd = process.cwd()) {
|
|
29
|
+
return relative(real(cwd, cwd), real(file, cwd)).split(sep).join('/').replace(/^src\//, '');
|
|
30
|
+
}
|
|
31
|
+
/** The directory a file lies in under the project root: `app` or `src/app` (App Router), `pages` or `src/pages`. */
|
|
32
|
+
function routerOf(file, cwd) {
|
|
33
|
+
const top = projectPath(file, cwd).split('/')[0];
|
|
34
|
+
return top === 'app' || top === 'pages' ? top : null;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Classifies the files of a Next.js app by the components that render them, from the import graph: a file under
|
|
38
|
+
* `app/` is a Server Component unless it says `'use client'`; a `'use client'` file and all it imports are Client
|
|
39
|
+
* Components; a file under `pages/` and all it imports render with the Pages Router. Imports resolve relative,
|
|
40
|
+
* through the tsconfig `paths`, from the project root (a `baseUrl` import, `components/Card`) and with the `.js`
|
|
41
|
+
* of an ESM import for a `.ts` file; only the given files count.
|
|
42
|
+
*
|
|
43
|
+
* @param contents - The files' contents by path (relative to `cwd` or absolute)
|
|
44
|
+
* @returns The kind of each file and the files it imports, by the paths they were given with; a file nothing
|
|
45
|
+
* reaches is `unknown` (`pages` in an app without `app/`)
|
|
46
|
+
*/
|
|
47
|
+
async function classifyNextFiles(contents, cwd = process.cwd()) {
|
|
48
|
+
const aliases = Object.entries(await getTsConfigAliases());
|
|
49
|
+
// by real path: the tsconfig aliases are real paths, and an absolute input glob may name a link (`/var/…`)
|
|
50
|
+
const byAbs = new Map([...contents.keys()].map(f => [real(f, cwd), f]));
|
|
51
|
+
const root = real(cwd, cwd);
|
|
52
|
+
const resolveImport = (from, spec) => {
|
|
53
|
+
const alias = aliases.find(([key]) => spec === key || spec.startsWith(key + '/'));
|
|
54
|
+
const bases = spec.startsWith('.')
|
|
55
|
+
? [resolve(dirname(from), spec)]
|
|
56
|
+
: alias ? [resolve(alias[1], `.${spec.slice(alias[0].length)}`)] : [resolve(root, spec), resolve(root, 'src', spec)];
|
|
57
|
+
// resolve() again: an extension or `/index` joined to a Windows path
|
|
58
|
+
return bases.flatMap(base => [base, base.replace(/\.(?:m?js|jsx)$/, '')])
|
|
59
|
+
.flatMap(base => EXTENSIONS.map(ext => resolve(base + ext)))
|
|
60
|
+
.find(path => byAbs.has(path));
|
|
61
|
+
};
|
|
62
|
+
const imports = new Map();
|
|
63
|
+
const useClient = new Set();
|
|
64
|
+
for (const [abs, file] of byAbs) {
|
|
65
|
+
const code = contents.get(file);
|
|
66
|
+
if (USE_CLIENT_RE.test(code))
|
|
67
|
+
useClient.add(abs);
|
|
68
|
+
imports.set(abs, [...code.matchAll(IMPORT_RE)]
|
|
69
|
+
.map(m => resolveImport(abs, m[1] ?? m[2] ?? m[3]))
|
|
70
|
+
.filter((path) => !!path));
|
|
71
|
+
}
|
|
72
|
+
// Everything a set of files imports, transitively; a server walk stops at `'use client'`, the client boundary
|
|
73
|
+
const reach = (seeds, stopAtClient) => {
|
|
74
|
+
const seen = new Set();
|
|
75
|
+
const queue = [...seeds];
|
|
76
|
+
while (queue.length) {
|
|
77
|
+
const abs = queue.pop();
|
|
78
|
+
if (seen.has(abs) || (stopAtClient && useClient.has(abs)))
|
|
79
|
+
continue;
|
|
80
|
+
seen.add(abs);
|
|
81
|
+
queue.push(...imports.get(abs));
|
|
82
|
+
}
|
|
83
|
+
return seen;
|
|
84
|
+
};
|
|
85
|
+
const all = [...byAbs.keys()];
|
|
86
|
+
const server = reach(all.filter(abs => routerOf(abs, cwd) === 'app' && !useClient.has(abs)), true);
|
|
87
|
+
const appClient = reach([...useClient].filter(abs => routerOf(abs, cwd) !== 'pages'), false);
|
|
88
|
+
const pages = reach(all.filter(abs => routerOf(abs, cwd) === 'pages'), false);
|
|
89
|
+
const hasApp = all.some(abs => routerOf(abs, cwd) === 'app');
|
|
90
|
+
const kinds = new Map();
|
|
91
|
+
for (const [abs, file] of byAbs) {
|
|
92
|
+
// a file no route reaches may be rendered on either side, through an import that does not resolve
|
|
93
|
+
const kind = server.has(abs)
|
|
94
|
+
? (appClient.has(abs) || pages.has(abs) ? 'shared' : 'server')
|
|
95
|
+
: appClient.has(abs) ? 'client' : pages.has(abs) || !hasApp ? 'pages' : 'unknown';
|
|
96
|
+
kinds.set(file, kind);
|
|
97
|
+
}
|
|
98
|
+
return { kinds, imports: new Map([...imports].map(([abs, paths]) => [byAbs.get(abs), paths.map(path => byAbs.get(path))])) };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
export { classifyNextFiles, projectPath };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { lineColumnFromOffset } from '../../extractor/parsers/ast-utils.js';
|
|
2
|
-
import {
|
|
2
|
+
import { translatablePropertySet, translatableAttributeSet } from '../../utils/jsx-attributes.js';
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
5
|
* Detects if a string is a candidate for translation based on confidence heuristics.
|
|
@@ -25,10 +25,12 @@ import { translatableAttributeSet, translatablePropertySet } from '../../utils/j
|
|
|
25
25
|
* @param config - Toolkit configuration
|
|
26
26
|
* @param translatableAttributes - Lower-cased attribute names that hold text on the enclosing JSX element
|
|
27
27
|
* (from the per-element `extract.acceptedAttributes` rules); defaults to the built-in list
|
|
28
|
+
* @param isJsxText - The string is JSX text, where a single lower-case word (`<Tag>expired</Tag>`) is text
|
|
29
|
+
* rather than an identifier
|
|
28
30
|
* @returns CandidateString with confidence score, or null if should be skipped
|
|
29
31
|
*/
|
|
30
|
-
function detectCandidate(content, offset, endOffset, file, code, config, translatableAttributes = translatableAttributeSet) {
|
|
31
|
-
const skipReason = shouldSkip(content, file, code, offset,
|
|
32
|
+
function detectCandidate(content, offset, endOffset, file, code, config, translatableAttributes = translatableAttributeSet, isJsxText = false) {
|
|
33
|
+
const skipReason = shouldSkip(content, file, code, offset, translatableAttributes, isJsxText);
|
|
32
34
|
if (skipReason) {
|
|
33
35
|
return null;
|
|
34
36
|
}
|
|
@@ -69,12 +71,14 @@ function detectCandidate(content, offset, endOffset, file, code, config, transla
|
|
|
69
71
|
column
|
|
70
72
|
};
|
|
71
73
|
}
|
|
74
|
+
/** An HTML character reference in JSX text: ` `, `—`, `—`. */
|
|
75
|
+
const HTML_ENTITY_RE = /&(?:#\d+|#x[\da-f]+|[a-z][a-z\d]*);/gi;
|
|
72
76
|
/**
|
|
73
77
|
* Determines if a string should be skipped from instrumentation.
|
|
74
78
|
*
|
|
75
79
|
* @returns Skip reason if should be skipped, null otherwise
|
|
76
80
|
*/
|
|
77
|
-
function shouldSkip(content, file, code, offset,
|
|
81
|
+
function shouldSkip(content, file, code, offset, translatableAttributes, isJsxText) {
|
|
78
82
|
// Skip test files
|
|
79
83
|
if (file.match(/\.(test|spec)\.(ts|tsx|js|jsx)$/i)) {
|
|
80
84
|
return 'Test file';
|
|
@@ -87,6 +91,10 @@ function shouldSkip(content, file, code, offset, endOffset, translatableAttribut
|
|
|
87
91
|
if (/^\s+$/.test(content)) {
|
|
88
92
|
return 'Whitespace only';
|
|
89
93
|
}
|
|
94
|
+
// Skip separators, symbols and numbers with units: no letter outside HTML entities (` / `, `·`, `100%`)
|
|
95
|
+
if (!/\p{L}/u.test(content.replace(HTML_ENTITY_RE, ''))) {
|
|
96
|
+
return 'No letters';
|
|
97
|
+
}
|
|
90
98
|
// Skip pure numbers (including decimals and negative numbers)
|
|
91
99
|
if (/^-?\d+(\.\d+)?$/.test(content)) {
|
|
92
100
|
return 'Pure number';
|
|
@@ -95,16 +103,21 @@ function shouldSkip(content, file, code, offset, endOffset, translatableAttribut
|
|
|
95
103
|
if (isURLLike(content)) {
|
|
96
104
|
return 'URL or path';
|
|
97
105
|
}
|
|
98
|
-
// Skip technical strings: attribute values, class names, technical IDs
|
|
99
|
-
|
|
106
|
+
// Skip technical strings: attribute values, class names, technical IDs; in a text slot (JSX text, a
|
|
107
|
+
// translatable attribute) a single word is text (`<Tag>expired</Tag>`, `aria-label='actions'`)
|
|
108
|
+
const textSlot = isJsxText || translatableAttributes.has(attributeNameBefore(code, offset) ?? '');
|
|
109
|
+
if (isTechnicalString(content) && !(textSlot && /^\p{L}+$/u.test(content) && !/\p{Ll}\p{Lu}/u.test(content))) {
|
|
100
110
|
return 'Technical string';
|
|
101
111
|
}
|
|
112
|
+
if (isCodeSnippet(content)) {
|
|
113
|
+
return 'Code snippet';
|
|
114
|
+
}
|
|
102
115
|
// Skip developer-facing error codes (all-caps, underscore-delimited)
|
|
103
116
|
if (isErrorCode(content)) {
|
|
104
117
|
return 'Error code pattern';
|
|
105
118
|
}
|
|
106
119
|
// Skip strings that are already wrapped in t() or Trans components
|
|
107
|
-
if (isAlreadyInstrumented(code, offset
|
|
120
|
+
if (isAlreadyInstrumented(code, offset)) {
|
|
108
121
|
return 'Already instrumented';
|
|
109
122
|
}
|
|
110
123
|
// Skip console.log/warn/error arguments
|
|
@@ -127,8 +140,9 @@ function isURLLike(str) {
|
|
|
127
140
|
// File paths: ./path, ../path, /absolute/path, C:\windows\path
|
|
128
141
|
if (/^(\.\.?\/|\/|[A-Za-z]:\\)/.test(str))
|
|
129
142
|
return true;
|
|
130
|
-
// Import paths
|
|
131
|
-
|
|
143
|
+
// Import paths and module ids (`react-dom/client`, `lodash.debounce`): no spaces, a `/` or a `.` inside;
|
|
144
|
+
// a word that ends a sentence (`Done.`, `first.`) is not one
|
|
145
|
+
if (/^['"]?[@a-z][\w-]*/.test(str) && !str.includes(' ') && /[./][\w@]/.test(str)) {
|
|
132
146
|
return true;
|
|
133
147
|
}
|
|
134
148
|
return false;
|
|
@@ -137,8 +151,8 @@ function isURLLike(str) {
|
|
|
137
151
|
* Checks if a string is technical (class name, HTML attribute, IDs, etc).
|
|
138
152
|
*/
|
|
139
153
|
function isTechnicalString(str) {
|
|
140
|
-
// kebab-case or
|
|
141
|
-
if (/^[a-z0-9-_]+$/.test(str)
|
|
154
|
+
// kebab-case or snake_case, or one lowercase word: a CSS class or a technical ID, however long
|
|
155
|
+
if (/^[a-z0-9-_]+$/.test(str)) {
|
|
142
156
|
return true;
|
|
143
157
|
}
|
|
144
158
|
// HTML attribute patterns like type="text", role="button", etc.
|
|
@@ -149,8 +163,58 @@ function isTechnicalString(str) {
|
|
|
149
163
|
if (/^[A-Z]{2,3}$/.test(str)) {
|
|
150
164
|
return true;
|
|
151
165
|
}
|
|
166
|
+
// camelCase identifiers: object keys, event and feature names (`aiReview`, `apiKeys`)
|
|
167
|
+
if (/^[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*)+$/.test(str)) {
|
|
168
|
+
return true;
|
|
169
|
+
}
|
|
170
|
+
if (isCSSValue(str)) {
|
|
171
|
+
return true;
|
|
172
|
+
}
|
|
173
|
+
// An HTTP authorization value (`Bearer ${token}`)
|
|
174
|
+
if (/^(?:Bearer|Basic|Digest|Token) \{\{\w+\}\}$/.test(str)) {
|
|
175
|
+
return true;
|
|
176
|
+
}
|
|
177
|
+
// One token holding `|`, `=` or an inner `:` is a rule, query or key path (`required|min:2`, `max:50`, `a=b`);
|
|
178
|
+
// a trailing colon is a label (`Note:`)
|
|
179
|
+
if (/^\S+$/.test(str) && /[|=]|:(?!$)/.test(str)) {
|
|
180
|
+
return true;
|
|
181
|
+
}
|
|
152
182
|
return false;
|
|
153
183
|
}
|
|
184
|
+
/** A CSS keyword or named color, the words a CSS value may hold besides numbers, colors and functions. */
|
|
185
|
+
const CSS_WORDS = new Set([
|
|
186
|
+
'inset', 'solid', 'dashed', 'dotted', 'double', 'none', 'auto', 'inherit', 'initial', 'unset', 'transparent',
|
|
187
|
+
'currentcolor', 'bold', 'normal', 'italic', 'center', 'left', 'right', 'top', 'bottom', 'flex', 'grid', 'block',
|
|
188
|
+
'inline', 'absolute', 'relative', 'fixed', 'sticky', 'hidden', 'visible', 'ease', 'ease-in', 'ease-out',
|
|
189
|
+
'ease-in-out', 'linear', 'black', 'white', 'red', 'green', 'blue', 'gray', 'grey', 'orange', 'yellow', 'purple'
|
|
190
|
+
]);
|
|
191
|
+
/**
|
|
192
|
+
* Checks if a string is a CSS value: `var(--brand)`, `inset 0 0 5px 1px var(--shadow)`, `1px solid #ccc`. Only
|
|
193
|
+
* numbers (with a unit), colors, CSS functions and CSS keywords, at least one of them no keyword.
|
|
194
|
+
*/
|
|
195
|
+
function isCSSValue(str) {
|
|
196
|
+
let rest = str.trim();
|
|
197
|
+
// CSS functions, nested ones from the inside out: var(--a, calc(1px + 2px))
|
|
198
|
+
for (let before = ''; before !== rest;) {
|
|
199
|
+
before = rest;
|
|
200
|
+
rest = rest.replace(/[a-z-]+\([^()]*\)/gi, ' \u0001 ');
|
|
201
|
+
}
|
|
202
|
+
const tokens = rest.split(/[\s,/]+/).filter(Boolean);
|
|
203
|
+
const valueToken = (t) => t === '\u0001' || /^#[\da-f]{3,8}$/i.test(t) ||
|
|
204
|
+
/^-?\d*\.?\d+(?:px|em|rem|%|vh|vw|vmin|vmax|ch|ex|pt|s|ms|deg|fr)?$/i.test(t);
|
|
205
|
+
return tokens.length > 0 &&
|
|
206
|
+
tokens.every(t => valueToken(t) || CSS_WORDS.has(t.toLowerCase()) || t === '!important') &&
|
|
207
|
+
tokens.some(t => valueToken(t) && !/^-?\d*\.?\d+$/.test(t));
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Checks if a string is a code or shell snippet shown to the user as is: several lines, one of them code
|
|
211
|
+
* (a statement, a block brace or semicolon at its end, a comment, a command-line flag).
|
|
212
|
+
*/
|
|
213
|
+
function isCodeSnippet(str) {
|
|
214
|
+
if (!str.includes('\n'))
|
|
215
|
+
return false;
|
|
216
|
+
return str.split('\n').some(line => /[{};]\s*$|^\s*(import|export|const|let|var|function|return|npm|npx|yarn|pnpm)\s|^\s*\/\/|\s--[a-z]/.test(line));
|
|
217
|
+
}
|
|
154
218
|
/**
|
|
155
219
|
* Checks if a string looks like a developer-facing error code.
|
|
156
220
|
* Examples: ERROR_NOT_FOUND, ERR_INVALID_TOKEN, etc.
|
|
@@ -165,18 +229,12 @@ function isErrorCode(str) {
|
|
|
165
229
|
/**
|
|
166
230
|
* Checks if a string is already wrapped in a t() call or Trans component.
|
|
167
231
|
*/
|
|
168
|
-
function isAlreadyInstrumented(code, offset
|
|
169
|
-
//
|
|
232
|
+
function isAlreadyInstrumented(code, offset) {
|
|
233
|
+
// The string is the first argument of t(), $t(), i18next.t(), props.t(): `t(` right before it, not just any
|
|
234
|
+
// name ending in t (`alert(`, `toast(`, `format(`). The instrumenter's AST walk skips whole t() calls and
|
|
235
|
+
// <Trans> elements, so their other arguments and children never get here.
|
|
170
236
|
const beforeStr = code.substring(Math.max(0, offset - 20), offset);
|
|
171
|
-
|
|
172
|
-
return true;
|
|
173
|
-
}
|
|
174
|
-
// Look forward for ) or Trans opening
|
|
175
|
-
const afterStr = code.substring(endOffset, Math.min(code.length, endOffset + 20));
|
|
176
|
-
if (afterStr.startsWith(')') || afterStr.includes('Trans')) {
|
|
177
|
-
return true;
|
|
178
|
-
}
|
|
179
|
-
return false;
|
|
237
|
+
return /(?:^|[^\w$])\$?t\(\s*$/.test(beforeStr);
|
|
180
238
|
}
|
|
181
239
|
/**
|
|
182
240
|
* Checks if this string is inside a console.log/warn/error call.
|
|
@@ -187,7 +245,18 @@ function isConsoleArgument(code, offset) {
|
|
|
187
245
|
}
|
|
188
246
|
// Translatable attribute and property sets are defined in
|
|
189
247
|
// utils/jsx-attributes.ts and shared with the linter.
|
|
190
|
-
|
|
248
|
+
// lower-cased, as the property name is looked up lower-cased (`helpText` → `helptext`)
|
|
249
|
+
const TRANSLATABLE_PROPERTIES = new Set([...translatablePropertySet].map(p => p.toLowerCase()));
|
|
250
|
+
/** Object property names whose values are not text: headers, addresses, identifiers, styling. */
|
|
251
|
+
const TECHNICAL_PROPERTIES = new Set([
|
|
252
|
+
'authorization', 'url', 'uri', 'href', 'src', 'path', 'pathname', 'endpoint', 'route', 'id', 'key',
|
|
253
|
+
'class', 'classname', 'classes', 'cls', 'style', 'styles', 'method', 'mimetype', 'contenttype', 'accept', 'encoding',
|
|
254
|
+
'icon', 'variant', 'color', 'type', 'code'
|
|
255
|
+
]);
|
|
256
|
+
/** The lower-cased name of the JSX attribute whose value starts at `offset` (`title='…'`), or null. */
|
|
257
|
+
function attributeNameBefore(code, offset) {
|
|
258
|
+
return code.substring(Math.max(0, offset - 50), offset).match(/([\w-]+)=$/)?.[1].toLowerCase() ?? null;
|
|
259
|
+
}
|
|
191
260
|
/**
|
|
192
261
|
* Checks if the string appears to be an HTML/JSX attribute value.
|
|
193
262
|
* Returns true only for *technical* attribute values that should be skipped.
|
|
@@ -232,7 +301,7 @@ function calculateConfidence(content, code, offset, translatableAttributes) {
|
|
|
232
301
|
confidence += 0.1;
|
|
233
302
|
}
|
|
234
303
|
// Contains common sentence starters (user-facing)
|
|
235
|
-
if (/^(the|a|an|you|your|we|our|hello|welcome|please|thank|sorry)/i.test(content)) {
|
|
304
|
+
if (/^(the|a|an|you|your|we|our|hello|welcome|please|thank|sorry)\b/i.test(content)) {
|
|
236
305
|
confidence += 0.15;
|
|
237
306
|
}
|
|
238
307
|
// Contains punctuation (sentences)
|
|
@@ -248,7 +317,7 @@ function calculateConfidence(content, code, offset, translatableAttributes) {
|
|
|
248
317
|
confidence += 0.05;
|
|
249
318
|
}
|
|
250
319
|
// Contains action verb (likely a button or instruction)
|
|
251
|
-
if (/^(click|save|delete|create|update|submit|continue|back|next|yes|no|ok|close|open|download|upload|add|edit|remove|cancel|confirm|send|search|find|apply|reset|clear|done|finish|start|stop|retry|sign|log)/i.test(content)) {
|
|
320
|
+
if (/^(click|save|delete|create|update|submit|continue|back|next|yes|no|ok|close|open|download|upload|add|edit|remove|cancel|confirm|send|search|find|apply|reset|clear|done|finish|start|stop|retry|sign|log)\b/i.test(content)) {
|
|
252
321
|
confidence += 0.1;
|
|
253
322
|
}
|
|
254
323
|
// Reduce confidence if appears in specific technical contexts
|
|
@@ -263,20 +332,27 @@ function calculateConfidence(content, code, offset, translatableAttributes) {
|
|
|
263
332
|
if (attrCtxMatch) {
|
|
264
333
|
const attrName = attrCtxMatch[1].toLowerCase();
|
|
265
334
|
if (translatableAttributes.has(attrName)) {
|
|
266
|
-
confidence += 0.
|
|
335
|
+
confidence += 0.2; // Translatable attribute: its value is text, a single word too (`title='Publishing'`)
|
|
267
336
|
}
|
|
268
337
|
else {
|
|
269
338
|
confidence -= 0.2;
|
|
270
339
|
}
|
|
271
340
|
}
|
|
272
341
|
// Object-property context: { label: 'All', description: 'Some text' }
|
|
273
|
-
// When the property name is a translatable-sounding key, boost confidence
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
342
|
+
// When the property name is a translatable-sounding key, boost confidence;
|
|
343
|
+
// a technical one ({ Authorization: `Bearer ${token}` }, { iconName: 'Lock' }) lowers it
|
|
344
|
+
// …and so does a variable a technical name is given (`const classes = …`, `cls = …`)
|
|
345
|
+
// (a quoted key has both quotes: `'…API key' :`, a ternary's string before its `:`, names no property)
|
|
346
|
+
const keyMatch = beforeContext.match(/(?:(['"])([\w-]+)\1|([A-Za-z_$][\w$]*))\s*:\s*$/);
|
|
347
|
+
const rawName = keyMatch ? (keyMatch[2] ?? keyMatch[3]) : beforeContext.match(/(\w+)\s+=\s*$/)?.[1];
|
|
348
|
+
if (rawName && !attrCtxMatch) {
|
|
349
|
+
const propName = rawName.toLowerCase().replace(/-/g, ''); // 'Content-Type' → contenttype
|
|
277
350
|
if (TRANSLATABLE_PROPERTIES.has(propName)) {
|
|
278
351
|
confidence += 0.25;
|
|
279
352
|
}
|
|
353
|
+
else if (TECHNICAL_PROPERTIES.has(propName) || /[a-z0-9](Id|ID|Key|Url|URL|Path|Class|ClassName|Classes|Icon|Type)$/.test(rawName)) {
|
|
354
|
+
confidence -= 0.3;
|
|
355
|
+
}
|
|
280
356
|
}
|
|
281
357
|
// Regular expressions or patterns
|
|
282
358
|
if (/\//.test(content) && /regex|pattern|match|search/i.test(beforeContext + afterContext)) {
|
|
@@ -286,4 +362,4 @@ function calculateConfidence(content, code, offset, translatableAttributes) {
|
|
|
286
362
|
return Math.max(0, Math.min(1, confidence));
|
|
287
363
|
}
|
|
288
364
|
|
|
289
|
-
export { detectCandidate };
|
|
365
|
+
export { HTML_ENTITY_RE, detectCandidate };
|