@babelize/engine 0.3.8 → 0.3.10
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/dist/analysis/analyze.d.ts +3 -0
- package/dist/analysis/analyze.js +1 -0
- package/dist/analysis/scan.d.ts +21 -0
- package/dist/analysis/scan.js +87 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.js +10 -1
- package/dist/local/hosted-provider.d.ts +13 -0
- package/dist/local/hosted-provider.js +28 -2
- package/dist/local/translate-core.d.ts +6 -0
- package/dist/local/translate-core.js +1 -0
- package/dist/modules/extractor/extract.js +31 -5
- package/dist/modules/finalization/finalize.d.ts +6 -0
- package/dist/modules/finalization/finalize.js +25 -5
- package/dist/modules/finalization/nextjs-finalize.d.ts +44 -4
- package/dist/modules/finalization/nextjs-finalize.js +294 -23
- package/dist/modules/format-injection/index.js +3 -2
- package/dist/modules/injection/inject.js +2 -1
- package/dist/modules/sync-detector/sync-detector.js +4 -1
- package/dist/modules/translator/translate.js +8 -0
- package/dist/modules/workspace/validate.js +7 -0
- package/dist/runtime/types.d.ts +8 -0
- package/dist/utils/ast-helpers.js +35 -4
- package/dist/utils/endonyms.d.ts +2 -0
- package/dist/utils/endonyms.js +38 -0
- package/dist/utils/source-scope.d.ts +33 -0
- package/dist/utils/source-scope.js +115 -0
- package/dist/utils/ui-copy.d.ts +14 -0
- package/dist/utils/ui-copy.js +51 -0
- package/package.json +3 -1
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Which source files a run may extract from and rewrite, as globs relative to
|
|
3
|
+
* the workspace root (`src/app/**`, `src/registry`). From `.babelize/config.json`
|
|
4
|
+
* `include`/`exclude`, or `--include`/`--exclude` (#378). Files outside it are
|
|
5
|
+
* never read for strings or injected into, on top of the built-in exclusions.
|
|
6
|
+
*/
|
|
7
|
+
export interface SourceScope {
|
|
8
|
+
/** Only these paths (default: every source file). */
|
|
9
|
+
include?: string[];
|
|
10
|
+
/** Never these paths, even when included. */
|
|
11
|
+
exclude?: string[];
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Glob lists from config or flags: non-empty strings, `./` and trailing `/`
|
|
15
|
+
* dropped. A plain path (`src/registry`) also covers everything under it.
|
|
16
|
+
*/
|
|
17
|
+
export declare function normalizeScopePatterns(value: unknown): string[] | undefined;
|
|
18
|
+
/** `include`/`exclude` from `.babelize/config.json`; missing or malformed is no scope. */
|
|
19
|
+
export declare function readSourceScope(workspacePath: string): Promise<SourceScope>;
|
|
20
|
+
/**
|
|
21
|
+
* The config's scope with each list given by the caller (flags) replacing the
|
|
22
|
+
* config's. An empty list given clears it (`exclude: []`: nothing excluded).
|
|
23
|
+
*/
|
|
24
|
+
export declare function mergeSourceScope(config: SourceScope, override?: SourceScope): SourceScope;
|
|
25
|
+
/** Whether `scope` limits anything. */
|
|
26
|
+
export declare function hasSourceScope(scope: SourceScope | undefined): boolean;
|
|
27
|
+
/** A test for workspace-relative paths (either separator). No scope: everything is in. */
|
|
28
|
+
export declare function createScopeMatcher(scope: SourceScope | undefined): (relPath: string) => boolean;
|
|
29
|
+
/** `files` (absolute) inside the workspace's scope. */
|
|
30
|
+
export declare function filterToScope(workspace: {
|
|
31
|
+
path: string;
|
|
32
|
+
sourceScope?: SourceScope;
|
|
33
|
+
}, files: string[]): string[];
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
36
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
37
|
+
};
|
|
38
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
+
exports.normalizeScopePatterns = normalizeScopePatterns;
|
|
40
|
+
exports.readSourceScope = readSourceScope;
|
|
41
|
+
exports.mergeSourceScope = mergeSourceScope;
|
|
42
|
+
exports.hasSourceScope = hasSourceScope;
|
|
43
|
+
exports.createScopeMatcher = createScopeMatcher;
|
|
44
|
+
exports.filterToScope = filterToScope;
|
|
45
|
+
const fs_1 = require("fs");
|
|
46
|
+
const path = __importStar(require("path"));
|
|
47
|
+
const picomatch_1 = __importDefault(require("picomatch"));
|
|
48
|
+
/**
|
|
49
|
+
* Glob lists from config or flags: non-empty strings, `./` and trailing `/`
|
|
50
|
+
* dropped. A plain path (`src/registry`) also covers everything under it.
|
|
51
|
+
*/
|
|
52
|
+
function normalizeScopePatterns(value) {
|
|
53
|
+
if (!Array.isArray(value))
|
|
54
|
+
return undefined;
|
|
55
|
+
const patterns = value
|
|
56
|
+
.filter((p) => typeof p === 'string')
|
|
57
|
+
.map((p) => p.trim().replace(/\\/g, '/').replace(/^\.\//, '').replace(/\/+$/, ''))
|
|
58
|
+
.filter((p) => p.length > 0 && p !== '.');
|
|
59
|
+
return patterns.length > 0 ? patterns : undefined;
|
|
60
|
+
}
|
|
61
|
+
function expand(patterns) {
|
|
62
|
+
return patterns.flatMap((p) => (picomatch_1.default.scan(p).isGlob ? [p] : [p, `${p}/**`]));
|
|
63
|
+
}
|
|
64
|
+
/** `include`/`exclude` from `.babelize/config.json`; missing or malformed is no scope. */
|
|
65
|
+
async function readSourceScope(workspacePath) {
|
|
66
|
+
let raw;
|
|
67
|
+
try {
|
|
68
|
+
raw = JSON.parse(await fs_1.promises.readFile(path.join(workspacePath, '.babelize', 'config.json'), 'utf8'));
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return {};
|
|
72
|
+
}
|
|
73
|
+
if (!raw || typeof raw !== 'object')
|
|
74
|
+
return {};
|
|
75
|
+
const { include, exclude } = raw;
|
|
76
|
+
return compactScope({ include: normalizeScopePatterns(include), exclude: normalizeScopePatterns(exclude) });
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* The config's scope with each list given by the caller (flags) replacing the
|
|
80
|
+
* config's. An empty list given clears it (`exclude: []`: nothing excluded).
|
|
81
|
+
*/
|
|
82
|
+
function mergeSourceScope(config, override = {}) {
|
|
83
|
+
return compactScope({
|
|
84
|
+
include: Array.isArray(override.include) ? normalizeScopePatterns(override.include) : config.include,
|
|
85
|
+
exclude: Array.isArray(override.exclude) ? normalizeScopePatterns(override.exclude) : config.exclude,
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
function compactScope(scope) {
|
|
89
|
+
return {
|
|
90
|
+
...(scope.include ? { include: scope.include } : {}),
|
|
91
|
+
...(scope.exclude ? { exclude: scope.exclude } : {}),
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
/** Whether `scope` limits anything. */
|
|
95
|
+
function hasSourceScope(scope) {
|
|
96
|
+
return !!(scope?.include?.length || scope?.exclude?.length);
|
|
97
|
+
}
|
|
98
|
+
/** A test for workspace-relative paths (either separator). No scope: everything is in. */
|
|
99
|
+
function createScopeMatcher(scope) {
|
|
100
|
+
if (!hasSourceScope(scope))
|
|
101
|
+
return () => true;
|
|
102
|
+
const included = scope.include?.length ? (0, picomatch_1.default)(expand(scope.include), { dot: true }) : () => true;
|
|
103
|
+
const excluded = scope.exclude?.length ? (0, picomatch_1.default)(expand(scope.exclude), { dot: true }) : () => false;
|
|
104
|
+
return (relPath) => {
|
|
105
|
+
const rel = relPath.replace(/\\/g, '/').replace(/^\.\//, '');
|
|
106
|
+
return included(rel) && !excluded(rel);
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
/** `files` (absolute) inside the workspace's scope. */
|
|
110
|
+
function filterToScope(workspace, files) {
|
|
111
|
+
if (!hasSourceScope(workspace.sourceScope))
|
|
112
|
+
return files;
|
|
113
|
+
const inScope = createScopeMatcher(workspace.sourceScope);
|
|
114
|
+
return files.filter((file) => inScope(path.relative(workspace.path, file)));
|
|
115
|
+
}
|
package/dist/utils/ui-copy.d.ts
CHANGED
|
@@ -31,3 +31,17 @@ export declare function isUiCopyForName(name: string | null, text: string): bool
|
|
|
31
31
|
* returns, conditionals, …) aren't initializers and are left as they are.
|
|
32
32
|
*/
|
|
33
33
|
export declare function isUiCopyLiteral(node: t.Node, parent: t.Node | null | undefined, text: string): boolean;
|
|
34
|
+
export declare function isDirectionValue(text: string): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* Every string in a string-literal union type declared in `ast`
|
|
37
|
+
* (`type Mode = "native" | "english"`, `dir?: "ltr" | "rtl"`). Code that
|
|
38
|
+
* assigns, returns or defaults to one of these is choosing an option, not
|
|
39
|
+
* showing copy; translating it breaks the type and the comparison (#379).
|
|
40
|
+
*/
|
|
41
|
+
export declare function literalUnionMembers(ast: t.Node): Set<string>;
|
|
42
|
+
/**
|
|
43
|
+
* `<LanguageSwitcher label="native" />`: one lowercase token given to a
|
|
44
|
+
* component (not a DOM element) is one of its options, like `variant` or
|
|
45
|
+
* `size`, not copy. Labels people read are capitalized or several words.
|
|
46
|
+
*/
|
|
47
|
+
export declare function isComponentOptionValue(attribute: t.JSXAttribute, element: t.JSXOpeningElement, text: string): boolean;
|
package/dist/utils/ui-copy.js
CHANGED
|
@@ -38,6 +38,9 @@ exports.isMultiWordCopy = isMultiWordCopy;
|
|
|
38
38
|
exports.looksLikeCopy = looksLikeCopy;
|
|
39
39
|
exports.isUiCopyForName = isUiCopyForName;
|
|
40
40
|
exports.isUiCopyLiteral = isUiCopyLiteral;
|
|
41
|
+
exports.isDirectionValue = isDirectionValue;
|
|
42
|
+
exports.literalUnionMembers = literalUnionMembers;
|
|
43
|
+
exports.isComponentOptionValue = isComponentOptionValue;
|
|
41
44
|
const t = __importStar(require("@babel/types"));
|
|
42
45
|
/**
|
|
43
46
|
* UI copy vs program data in object / array / variable initializers.
|
|
@@ -129,3 +132,51 @@ function isUiCopyLiteral(node, parent, text) {
|
|
|
129
132
|
return looksLikeCopy(text);
|
|
130
133
|
return true;
|
|
131
134
|
}
|
|
135
|
+
/** Text direction (`dir`, `direction`): compared and passed on, never shown (#379). */
|
|
136
|
+
const DIRECTION_VALUES = new Set(['ltr', 'rtl', 'auto']);
|
|
137
|
+
function isDirectionValue(text) {
|
|
138
|
+
return DIRECTION_VALUES.has(text.trim());
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Every string in a string-literal union type declared in `ast`
|
|
142
|
+
* (`type Mode = "native" | "english"`, `dir?: "ltr" | "rtl"`). Code that
|
|
143
|
+
* assigns, returns or defaults to one of these is choosing an option, not
|
|
144
|
+
* showing copy; translating it breaks the type and the comparison (#379).
|
|
145
|
+
*/
|
|
146
|
+
function literalUnionMembers(ast) {
|
|
147
|
+
const members = new Set();
|
|
148
|
+
const visit = (node) => {
|
|
149
|
+
if (!node || typeof node !== 'object')
|
|
150
|
+
return;
|
|
151
|
+
if (Array.isArray(node)) {
|
|
152
|
+
for (const child of node)
|
|
153
|
+
visit(child);
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
const n = node;
|
|
157
|
+
if (t.isTSUnionType(n)) {
|
|
158
|
+
for (const type of n.types) {
|
|
159
|
+
if (t.isTSLiteralType(type) && t.isStringLiteral(type.literal))
|
|
160
|
+
members.add(type.literal.value);
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
for (const key of t.VISITOR_KEYS[n.type] ?? [])
|
|
164
|
+
visit(n[key]);
|
|
165
|
+
};
|
|
166
|
+
visit(ast);
|
|
167
|
+
return members;
|
|
168
|
+
}
|
|
169
|
+
/** Props whose value is always shown, even as one lowercase word (`alt="logo"`). */
|
|
170
|
+
const SHOWN_PROP = /^(?:alt|placeholder|title|aria-.+|accessibility.+)$/;
|
|
171
|
+
/**
|
|
172
|
+
* `<LanguageSwitcher label="native" />`: one lowercase token given to a
|
|
173
|
+
* component (not a DOM element) is one of its options, like `variant` or
|
|
174
|
+
* `size`, not copy. Labels people read are capitalized or several words.
|
|
175
|
+
*/
|
|
176
|
+
function isComponentOptionValue(attribute, element, text) {
|
|
177
|
+
if (!t.isJSXIdentifier(attribute.name) || SHOWN_PROP.test(attribute.name.name))
|
|
178
|
+
return false;
|
|
179
|
+
const tag = element.name;
|
|
180
|
+
const isComponent = t.isJSXMemberExpression(tag) || (t.isJSXIdentifier(tag) && /^[A-Z]/.test(tag.name));
|
|
181
|
+
return isComponent && /^[a-z][a-z0-9]*(?:-[a-z0-9]+)*$/.test(text.trim());
|
|
182
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@babelize/engine",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.10",
|
|
4
4
|
"description": "Babelize translation pipeline (extract, keygen, translate, inject). Infrastructure is injected through provider/store interfaces.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"babelize",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"gettext-parser": "^9.1.1",
|
|
42
42
|
"magic-string": "^0.30",
|
|
43
43
|
"node-html-parser": "^8.0.4",
|
|
44
|
+
"picomatch": "^2.3.2",
|
|
44
45
|
"recast": "^0.23",
|
|
45
46
|
"zod": "^4.6.6"
|
|
46
47
|
},
|
|
@@ -52,6 +53,7 @@
|
|
|
52
53
|
"@types/fs-extra": "^11.0.4",
|
|
53
54
|
"@types/jest": "^30.0.0",
|
|
54
55
|
"@types/node": "^25.0.3",
|
|
56
|
+
"@types/picomatch": "^2.3.4",
|
|
55
57
|
"ajv": "^8.17.1",
|
|
56
58
|
"eslint": "^9.39.2",
|
|
57
59
|
"intl-messageformat": "^10.7.16",
|