@parlajs/x18n-plugin 0.0.0-stage → 0.1.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/LICENSE +21 -0
- package/README.md +23 -2
- package/build/ast.d.ts +29 -0
- package/build/ast.d.ts.map +1 -0
- package/build/ast.js +64 -0
- package/build/bindings.d.ts +31 -0
- package/build/bindings.d.ts.map +1 -0
- package/build/bindings.js +177 -0
- package/build/configs.d.ts +7 -0
- package/build/configs.d.ts.map +1 -0
- package/build/configs.js +46 -0
- package/build/declare.d.ts +8 -0
- package/build/declare.d.ts.map +1 -0
- package/build/declare.js +69 -0
- package/build/index.d.ts +10 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +23 -0
- package/build/namespaces.d.ts +11 -0
- package/build/namespaces.d.ts.map +1 -0
- package/build/namespaces.js +72 -0
- package/build/paths.d.ts +3 -0
- package/build/paths.d.ts.map +1 -0
- package/build/paths.js +7 -0
- package/build/placeholders.d.ts +3 -0
- package/build/placeholders.d.ts.map +1 -0
- package/build/placeholders.js +58 -0
- package/build/scanner.d.ts +4 -0
- package/build/scanner.d.ts.map +1 -0
- package/build/scanner.js +154 -0
- package/build/templates.d.ts +8 -0
- package/build/templates.d.ts.map +1 -0
- package/build/templates.js +107 -0
- package/build/types.d.ts +21 -0
- package/build/types.d.ts.map +1 -0
- package/build/types.js +1 -0
- package/build/values.d.ts +4 -0
- package/build/values.d.ts.map +1 -0
- package/build/values.js +29 -0
- package/package.json +64 -3
- package/src/ast.ts +90 -0
- package/src/bindings.ts +226 -0
- package/src/configs.ts +58 -0
- package/src/declare.ts +94 -0
- package/src/index.ts +34 -0
- package/src/namespaces.ts +93 -0
- package/src/paths.ts +9 -0
- package/src/placeholders.ts +63 -0
- package/src/scanner.ts +175 -0
- package/src/templates.ts +115 -0
- package/src/types.ts +27 -0
- package/src/values.ts +33 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Marco Pasqualetti
|
|
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/README.md
CHANGED
|
@@ -1,3 +1,24 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @parlajs/x18n-plugin
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[Parla](https://github.com/parlajs/parla)'s plugin for [x18n](https://x18n.dev): it reads each
|
|
4
|
+
catalog under its namespace, and every `t.ns.key()` on a `t` from `useParla`, `parla.load` and the like.
|
|
5
|
+
|
|
6
|
+
```ts
|
|
7
|
+
import parla from '@parlajs/x18n-plugin';
|
|
8
|
+
import { defineConfig } from '@x18n/cli';
|
|
9
|
+
|
|
10
|
+
export default defineConfig({
|
|
11
|
+
sourceLocale: 'en',
|
|
12
|
+
locales: ['en', 'it'],
|
|
13
|
+
resources: [],
|
|
14
|
+
namespace: { separator: ':' },
|
|
15
|
+
plugins: [parla()],
|
|
16
|
+
usage: { include: ['src/**/*.{ts,tsx}'], exclude: ['src/parla/**'] },
|
|
17
|
+
});
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
Docs: [`@parlajs/x18n-plugin`](https://parlajs.github.io/parla/tooling/x18n/).
|
|
21
|
+
|
|
22
|
+
## License
|
|
23
|
+
|
|
24
|
+
[MIT](./LICENSE)
|
package/build/ast.d.ts
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export type Node = {
|
|
2
|
+
type: string;
|
|
3
|
+
start: number;
|
|
4
|
+
end: number;
|
|
5
|
+
} & Record<string, unknown>;
|
|
6
|
+
export declare const isNode: (value: unknown) => value is Node;
|
|
7
|
+
export declare const stringValue: (node: unknown) => string | undefined;
|
|
8
|
+
export interface Segment {
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly start: number;
|
|
11
|
+
readonly end: number;
|
|
12
|
+
}
|
|
13
|
+
export interface Chain {
|
|
14
|
+
readonly root: Node;
|
|
15
|
+
/** Up to the first key chosen at run time. */
|
|
16
|
+
readonly segments: ReadonlyArray<Segment>;
|
|
17
|
+
/** Whether no key is chosen at run time: `t.cart[key]` is not static. */
|
|
18
|
+
readonly isStatic: boolean;
|
|
19
|
+
/** Every member expression of the chain. */
|
|
20
|
+
readonly members: ReadonlyArray<Node>;
|
|
21
|
+
}
|
|
22
|
+
/** A member's name, as `.items` or `['items']`; undefined for `[key]`. */
|
|
23
|
+
export declare function segmentOf({ computed, property }: Node): Segment | undefined;
|
|
24
|
+
/** `t.cart.items` → root `t`, segments `cart` and `items`; undefined unless its root is a name or `name()`. */
|
|
25
|
+
export declare function memberChain(node: Node): Chain | undefined;
|
|
26
|
+
export declare const keyOf: (key: ReadonlyArray<Segment>) => string;
|
|
27
|
+
/** Names of an object literal's keys, or undefined when a spread or computed key hides some. */
|
|
28
|
+
export declare function staticKeys(node: unknown): Array<string> | undefined;
|
|
29
|
+
//# sourceMappingURL=ast.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ast.d.ts","sourceRoot":"","sources":["../src/ast.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,IAAI,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;AAE1F,eAAO,MAAM,MAAM,UAAW,OAAO,KAAG,KAAK,IAAI,IACwC,CAAC;AAE1F,eAAO,MAAM,WAAW,SAAU,OAAO,KAAG,MAAM,GAAG,SAGtC,CAAC;AAEhB,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,KAAK;IACpB,QAAQ,CAAC,IAAI,EAAE,IAAI,CAAC;IACpB,8CAA8C;IAC9C,QAAQ,CAAC,QAAQ,EAAE,aAAa,CAAC,OAAO,CAAC,CAAC;IAC1C,yEAAyE;IACzE,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;IAC3B,4CAA4C;IAC5C,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC,IAAI,CAAC,CAAC;CACvC;AAED,0EAA0E;AAC1E,wBAAgB,SAAS,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAE,EAAE,IAAI,GAAG,OAAO,GAAG,SAAS,CAU3E;AAED,+GAA+G;AAC/G,wBAAgB,WAAW,CAAC,IAAI,EAAE,IAAI,GAAG,KAAK,GAAG,SAAS,CA2BzD;AAED,eAAO,MAAM,KAAK,QAAS,aAAa,CAAC,OAAO,CAAC,KAAG,MAA+C,CAAC;AAEpG,gGAAgG;AAChG,wBAAgB,UAAU,CAAC,IAAI,EAAE,OAAO,GAAG,KAAK,CAAC,MAAM,CAAC,GAAG,SAAS,CAiBnE"}
|
package/build/ast.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
export const isNode = (value) => typeof value === 'object' && value !== null && typeof value.type === 'string';
|
|
2
|
+
export const stringValue = (node) => isNode(node) && node.type === 'Literal' && typeof node['value'] === 'string'
|
|
3
|
+
? node['value']
|
|
4
|
+
: undefined;
|
|
5
|
+
/** A member's name, as `.items` or `['items']`; undefined for `[key]`. */
|
|
6
|
+
export function segmentOf({ computed, property }) {
|
|
7
|
+
if (!isNode(property)) {
|
|
8
|
+
return undefined;
|
|
9
|
+
}
|
|
10
|
+
const { start, end } = property;
|
|
11
|
+
if (!computed) {
|
|
12
|
+
return { name: property['name'], start, end };
|
|
13
|
+
}
|
|
14
|
+
const name = stringValue(property);
|
|
15
|
+
return name === undefined ? undefined : { name, start: start + 1, end: end - 1 };
|
|
16
|
+
}
|
|
17
|
+
/** `t.cart.items` → root `t`, segments `cart` and `items`; undefined unless its root is a name or `name()`. */
|
|
18
|
+
export function memberChain(node) {
|
|
19
|
+
const members = [];
|
|
20
|
+
let current = node;
|
|
21
|
+
while (isNode(current) && current.type === 'MemberExpression') {
|
|
22
|
+
members.unshift(current);
|
|
23
|
+
current = current['object'];
|
|
24
|
+
}
|
|
25
|
+
// Solid's accessor: `t().checkout.title()`.
|
|
26
|
+
if (isNode(current) &&
|
|
27
|
+
current.type === 'CallExpression' &&
|
|
28
|
+
current['arguments'].length === 0) {
|
|
29
|
+
current = current['callee'];
|
|
30
|
+
}
|
|
31
|
+
if (!isNode(current) || current.type !== 'Identifier' || members.length === 0) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
const segments = [];
|
|
35
|
+
for (const member of members) {
|
|
36
|
+
const segment = segmentOf(member);
|
|
37
|
+
if (!segment) {
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
segments.push(segment);
|
|
41
|
+
}
|
|
42
|
+
return { root: current, segments, isStatic: segments.length === members.length, members };
|
|
43
|
+
}
|
|
44
|
+
export const keyOf = (key) => key.map(({ name }) => name).join('.');
|
|
45
|
+
/** Names of an object literal's keys, or undefined when a spread or computed key hides some. */
|
|
46
|
+
export function staticKeys(node) {
|
|
47
|
+
if (!isNode(node) || node.type !== 'ObjectExpression') {
|
|
48
|
+
return undefined;
|
|
49
|
+
}
|
|
50
|
+
const keys = [];
|
|
51
|
+
for (const property of node['properties']) {
|
|
52
|
+
const { key } = property;
|
|
53
|
+
if (property.type !== 'Property' || property['computed'] || !isNode(key)) {
|
|
54
|
+
return undefined;
|
|
55
|
+
}
|
|
56
|
+
if (key.type === 'Identifier') {
|
|
57
|
+
keys.push(key['name']);
|
|
58
|
+
}
|
|
59
|
+
else if (key.type === 'Literal') {
|
|
60
|
+
keys.push(String(key['value']));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
return keys;
|
|
64
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { Node } from './ast.ts';
|
|
2
|
+
/** What a name holds: a `t`, one namespace's messages, a hook's result with a `t`, or a promise of `t`. */
|
|
3
|
+
type Holds = {
|
|
4
|
+
readonly namespaces: ReadonlyArray<string>;
|
|
5
|
+
} | {
|
|
6
|
+
readonly namespace: string;
|
|
7
|
+
} | {
|
|
8
|
+
readonly hook: ReadonlyArray<string>;
|
|
9
|
+
} | {
|
|
10
|
+
readonly promise: ReadonlyArray<string>;
|
|
11
|
+
};
|
|
12
|
+
/** A name that holds messages, visible inside `scope`. */
|
|
13
|
+
export type Binding = Holds & {
|
|
14
|
+
readonly name: string;
|
|
15
|
+
readonly scope: Node;
|
|
16
|
+
};
|
|
17
|
+
/** The innermost binding of `name` whose scope holds `position`. */
|
|
18
|
+
export declare function resolve(bindings: ReadonlyArray<Binding>, { name, position }: {
|
|
19
|
+
name: string;
|
|
20
|
+
position: number;
|
|
21
|
+
}): Binding | undefined;
|
|
22
|
+
interface Collect {
|
|
23
|
+
/** The innermost function around the node, or the program. */
|
|
24
|
+
readonly scope: Node;
|
|
25
|
+
readonly callees: ReadonlySet<string>;
|
|
26
|
+
readonly imports: ReadonlyMap<string, string>;
|
|
27
|
+
readonly out: Array<Binding>;
|
|
28
|
+
}
|
|
29
|
+
export declare function collectBindings(node: Node, collect: Collect): void;
|
|
30
|
+
export {};
|
|
31
|
+
//# sourceMappingURL=bindings.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bindings.d.ts","sourceRoot":"","sources":["../src/bindings.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAGrC,2GAA2G;AAC3G,KAAK,KAAK,GACN;IAAE,QAAQ,CAAC,UAAU,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;CAAE,GAC9C;IAAE,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAC9B;IAAE,QAAQ,CAAC,IAAI,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;CAAE,GACxC;IAAE,QAAQ,CAAC,OAAO,EAAE,aAAa,CAAC,MAAM,CAAC,CAAA;CAAE,CAAC;AAEhD,0DAA0D;AAC1D,MAAM,MAAM,OAAO,GAAG,KAAK,GAAG;IAAE,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAA;CAAE,CAAC;AAE9E,oEAAoE;AACpE,wBAAgB,OAAO,CACrB,QAAQ,EAAE,aAAa,CAAC,OAAO,CAAC,EAChC,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,GACrD,OAAO,GAAG,SAAS,CAUrB;AAmED,UAAU,OAAO;IACf,8DAA8D;IAC9D,QAAQ,CAAC,KAAK,EAAE,IAAI,CAAC;IACrB,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACtC,QAAQ,CAAC,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAC9C,QAAQ,CAAC,GAAG,EAAE,KAAK,CAAC,OAAO,CAAC,CAAC;CAC9B;AAmGD,wBAAgB,eAAe,CAAC,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,OAAO,GAAG,IAAI,CAyBlE"}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { isNode, segmentOf, stringValue } from './ast.js';
|
|
2
|
+
import { loadedBy, namespacesOf, translationsOf, WRAPPERS } from './namespaces.js';
|
|
3
|
+
/** The innermost binding of `name` whose scope holds `position`. */
|
|
4
|
+
export function resolve(bindings, { name, position }) {
|
|
5
|
+
let best;
|
|
6
|
+
for (const binding of bindings) {
|
|
7
|
+
const { scope } = binding;
|
|
8
|
+
const inside = binding.name === name && scope.start <= position && position < scope.end;
|
|
9
|
+
if (inside && (!best || scope.end - scope.start < best.scope.end - best.scope.start)) {
|
|
10
|
+
best = binding;
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
return best;
|
|
14
|
+
}
|
|
15
|
+
const FUNCTIONS = new Set(['FunctionDeclaration', 'FunctionExpression', 'ArrowFunctionExpression']);
|
|
16
|
+
/** The local name `t` is bound to in `{ t }` or `{ t: alias }`. */
|
|
17
|
+
function tName(pattern) {
|
|
18
|
+
for (const property of pattern['properties']) {
|
|
19
|
+
const { key } = property;
|
|
20
|
+
const { value } = property;
|
|
21
|
+
if (property.type === 'Property' &&
|
|
22
|
+
isNode(key) &&
|
|
23
|
+
key['name'] === 't' &&
|
|
24
|
+
isNode(value) &&
|
|
25
|
+
value.type === 'Identifier') {
|
|
26
|
+
return value['name'];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
/** `const t = …`, or `const { checkout: messages } = …`, of a `t` with these namespaces. */
|
|
32
|
+
function boundNames(id, namespaces) {
|
|
33
|
+
if (!isNode(id)) {
|
|
34
|
+
return [];
|
|
35
|
+
}
|
|
36
|
+
if (id.type === 'Identifier') {
|
|
37
|
+
return [{ name: id['name'], namespaces }];
|
|
38
|
+
}
|
|
39
|
+
const properties = id.type === 'ObjectPattern' ? id['properties'] : [];
|
|
40
|
+
return properties.flatMap(({ key, value }) => {
|
|
41
|
+
const namespace = isNode(key) ? (stringValue(key) ?? key['name']) : '';
|
|
42
|
+
return namespaces.includes(namespace) && isNode(value) && value.type === 'Identifier'
|
|
43
|
+
? [{ name: value['name'], namespace }]
|
|
44
|
+
: [];
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
/** `t: Translations<typeof ns>`, or `{ t }: { t: Translations<typeof ns> }`, in a function's params. */
|
|
48
|
+
function paramBinding(param, imports) {
|
|
49
|
+
const type = param['typeAnnotation']?.['typeAnnotation'];
|
|
50
|
+
if (param.type === 'Identifier') {
|
|
51
|
+
const namespaces = translationsOf(type, imports);
|
|
52
|
+
return namespaces && [param['name'], namespaces];
|
|
53
|
+
}
|
|
54
|
+
const name = param.type === 'ObjectPattern' ? tName(param) : undefined;
|
|
55
|
+
if (!isNode(type) || type.type !== 'TSTypeLiteral' || name === undefined) {
|
|
56
|
+
return undefined;
|
|
57
|
+
}
|
|
58
|
+
for (const member of type['members']) {
|
|
59
|
+
const { key } = member;
|
|
60
|
+
if (member.type === 'TSPropertySignature' && isNode(key) && key['name'] === 't') {
|
|
61
|
+
const annotation = member['typeAnnotation']?.['typeAnnotation'];
|
|
62
|
+
const namespaces = translationsOf(annotation, imports);
|
|
63
|
+
return namespaces && [name, namespaces];
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
return undefined;
|
|
67
|
+
}
|
|
68
|
+
/** What `await x`, `x!`, `(x)` or Svelte's `$derived(x)` wraps. */
|
|
69
|
+
function wrapped(node) {
|
|
70
|
+
if (node.type === 'AwaitExpression' || WRAPPERS.has(node.type)) {
|
|
71
|
+
return node['argument'] ?? node['expression'];
|
|
72
|
+
}
|
|
73
|
+
const { callee } = node;
|
|
74
|
+
const args = node['arguments'];
|
|
75
|
+
const isDerived = isNode(callee) && callee['name'] === '$derived' && args?.length === 1;
|
|
76
|
+
return node.type === 'CallExpression' && isDerived ? args[0] : undefined;
|
|
77
|
+
}
|
|
78
|
+
function unwrap(node) {
|
|
79
|
+
let current = node;
|
|
80
|
+
let awaited = false;
|
|
81
|
+
let next = isNode(current) ? wrapped(current) : undefined;
|
|
82
|
+
while (next !== undefined) {
|
|
83
|
+
awaited ||= current.type === 'AwaitExpression';
|
|
84
|
+
current = next;
|
|
85
|
+
next = isNode(current) ? wrapped(current) : undefined;
|
|
86
|
+
}
|
|
87
|
+
return { node: current, awaited };
|
|
88
|
+
}
|
|
89
|
+
/** A hook's result, or `x.load([…])` not awaited. */
|
|
90
|
+
function called(node, { awaited, callees, imports }) {
|
|
91
|
+
const { callee } = node;
|
|
92
|
+
const namespaces = namespacesOf(node['arguments'][0], imports);
|
|
93
|
+
if (!namespaces || !isNode(callee)) {
|
|
94
|
+
return undefined;
|
|
95
|
+
}
|
|
96
|
+
if (callee.type === 'Identifier' && callees.has(callee['name'])) {
|
|
97
|
+
return { hook: namespaces };
|
|
98
|
+
}
|
|
99
|
+
const isLoad = callee.type === 'MemberExpression' && segmentOf(callee)?.name === 'load';
|
|
100
|
+
return isLoad && !awaited ? { promise: namespaces } : undefined;
|
|
101
|
+
}
|
|
102
|
+
/** What a declaration's `init` holds, if anything Parla's. */
|
|
103
|
+
function holdsOf(init, context) {
|
|
104
|
+
const loaded = loadedBy(init, context.imports);
|
|
105
|
+
if (loaded) {
|
|
106
|
+
return { namespaces: loaded };
|
|
107
|
+
}
|
|
108
|
+
const { node, awaited } = unwrap(init);
|
|
109
|
+
if (!isNode(node)) {
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
112
|
+
if (node.type === 'CallExpression') {
|
|
113
|
+
return called(node, { ...context, awaited });
|
|
114
|
+
}
|
|
115
|
+
// `messages.t` of a hook's result, or a promise of `t`.
|
|
116
|
+
const held = node.type === 'MemberExpression' ? node['object'] : node;
|
|
117
|
+
const binding = isNode(held) && held.type === 'Identifier'
|
|
118
|
+
? resolve(context.out, { name: held['name'], position: context.position })
|
|
119
|
+
: undefined;
|
|
120
|
+
if (binding && 'hook' in binding && held !== node && segmentOf(node)?.name === 't') {
|
|
121
|
+
return { namespaces: binding.hook };
|
|
122
|
+
}
|
|
123
|
+
if (binding && 'promise' in binding && held === node) {
|
|
124
|
+
return awaited ? { namespaces: binding.promise } : { promise: binding.promise };
|
|
125
|
+
}
|
|
126
|
+
return undefined;
|
|
127
|
+
}
|
|
128
|
+
/** The names `id` binds to what `holds` holds. */
|
|
129
|
+
function bind(id, holds) {
|
|
130
|
+
if ('namespaces' in holds) {
|
|
131
|
+
return boundNames(id, holds.namespaces);
|
|
132
|
+
}
|
|
133
|
+
if (isNode(id) && id.type === 'Identifier') {
|
|
134
|
+
return [{ ...holds, name: id['name'] }];
|
|
135
|
+
}
|
|
136
|
+
const name = 'hook' in holds && isNode(id) && id.type === 'ObjectPattern' ? tName(id) : undefined;
|
|
137
|
+
return 'hook' in holds && name !== undefined ? [{ name, namespaces: holds.hook }] : [];
|
|
138
|
+
}
|
|
139
|
+
/** `promise.then((t) => …)` on a promise of `t`. */
|
|
140
|
+
function thenBindings(node, context) {
|
|
141
|
+
const { callee } = node;
|
|
142
|
+
if (node.type !== 'CallExpression' || !isNode(callee) || segmentOf(callee)?.name !== 'then') {
|
|
143
|
+
return [];
|
|
144
|
+
}
|
|
145
|
+
const [callback] = node['arguments'];
|
|
146
|
+
const [param] = isNode(callback) && FUNCTIONS.has(callback.type) ? callback['params'] : [];
|
|
147
|
+
const holds = holdsOf(callee['object'], context);
|
|
148
|
+
return param && holds && 'promise' in holds
|
|
149
|
+
? boundNames(param, holds.promise).map((bound) => ({ ...bound, scope: callback }))
|
|
150
|
+
: [];
|
|
151
|
+
}
|
|
152
|
+
export function collectBindings(node, collect) {
|
|
153
|
+
const { scope, out } = collect;
|
|
154
|
+
const inner = FUNCTIONS.has(node.type) ? node : scope;
|
|
155
|
+
for (const param of FUNCTIONS.has(node.type) ? node['params'] : []) {
|
|
156
|
+
const binding = paramBinding(param, collect.imports);
|
|
157
|
+
if (binding) {
|
|
158
|
+
out.push({ name: binding[0], namespaces: binding[1], scope: node });
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const context = { ...collect, position: node.start };
|
|
162
|
+
if (node.type === 'VariableDeclarator') {
|
|
163
|
+
const holds = holdsOf(node['init'], context);
|
|
164
|
+
for (const bound of holds ? bind(node['id'], holds) : []) {
|
|
165
|
+
out.push({ ...bound, scope: inner });
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
out.push(...thenBindings(node, context));
|
|
169
|
+
const within = inner === scope ? collect : { ...collect, scope: inner };
|
|
170
|
+
for (const value of Object.values(node)) {
|
|
171
|
+
for (const child of Array.isArray(value) ? value : [value]) {
|
|
172
|
+
if (isNode(child)) {
|
|
173
|
+
collectBindings(child, within);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Declared, LocaleFinder } from './types.ts';
|
|
2
|
+
/**
|
|
3
|
+
* Each catalog file under the namespace its parla config gives it, so `scopeAudit` may live in
|
|
4
|
+
* `scope_audit.json`; `pre`, so these names win over a `resources` pattern matching the same file.
|
|
5
|
+
*/
|
|
6
|
+
export declare function configFinder(configs: ReadonlyArray<string> | undefined, declared: Declared): LocaleFinder;
|
|
7
|
+
//# sourceMappingURL=configs.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"configs.d.ts","sourceRoot":"","sources":["../src/configs.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,QAAQ,EAAQ,YAAY,EAAE,MAAM,YAAY,CAAC;AAE/D;;;GAGG;AACH,wBAAgB,YAAY,CAC1B,OAAO,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,SAAS,EAC1C,QAAQ,EAAE,QAAQ,GACjB,YAAY,CA2Cd"}
|
package/build/configs.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { loadConfig } from '@parlajs/compiler';
|
|
2
|
+
import { declare } from './declare.js';
|
|
3
|
+
import { relative } from './paths.js';
|
|
4
|
+
/**
|
|
5
|
+
* Each catalog file under the namespace its parla config gives it, so `scopeAudit` may live in
|
|
6
|
+
* `scope_audit.json`; `pre`, so these names win over a `resources` pattern matching the same file.
|
|
7
|
+
*/
|
|
8
|
+
export function configFinder(configs, declared) {
|
|
9
|
+
let loaded;
|
|
10
|
+
const load = async (host) => {
|
|
11
|
+
loaded ??= (async () => {
|
|
12
|
+
const files = configs ??
|
|
13
|
+
(await host.glob(['**/parla.config.{ts,mts}'], {
|
|
14
|
+
ignore: ['**/node_modules/**'],
|
|
15
|
+
gitignore: true,
|
|
16
|
+
}));
|
|
17
|
+
const all = await Promise.all(files.map(async (file) => loadConfig(host.root, file)));
|
|
18
|
+
await declare(declared, {
|
|
19
|
+
configs: all,
|
|
20
|
+
readFile: async (path) => host.readFile(relative(host.root, path)),
|
|
21
|
+
});
|
|
22
|
+
return all;
|
|
23
|
+
})();
|
|
24
|
+
return loaded;
|
|
25
|
+
};
|
|
26
|
+
let resolved = [];
|
|
27
|
+
return {
|
|
28
|
+
id: 'parla:configs',
|
|
29
|
+
async find({ host }) {
|
|
30
|
+
resolved = await load(host);
|
|
31
|
+
return resolved.flatMap((config) => config.namespaces.flatMap((ns) => config.locales.map((locale) => ({
|
|
32
|
+
path: relative(host.root, ns.file(locale)),
|
|
33
|
+
locale,
|
|
34
|
+
namespace: ns.name,
|
|
35
|
+
}))));
|
|
36
|
+
},
|
|
37
|
+
resolveNew({ locale, namespace }, { host }) {
|
|
38
|
+
const file = resolved
|
|
39
|
+
.filter((config) => config.locales.includes(locale))
|
|
40
|
+
.flatMap((config) => config.namespaces)
|
|
41
|
+
.find((ns) => ns.name === namespace)
|
|
42
|
+
?.file(locale);
|
|
43
|
+
return file === undefined ? file : relative(host.root, file);
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { ResolvedConfig } from '@parlajs/compiler';
|
|
2
|
+
import type { Declared, ReadFile } from './types.ts';
|
|
3
|
+
/** Fills `declared` from the configs and their source catalogs. */
|
|
4
|
+
export declare function declare(declared: Declared, { configs, readFile }: {
|
|
5
|
+
configs: ReadonlyArray<ResolvedConfig>;
|
|
6
|
+
readFile: ReadFile;
|
|
7
|
+
}): Promise<void>;
|
|
8
|
+
//# sourceMappingURL=declare.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"declare.d.ts","sourceRoot":"","sources":["../src/declare.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAkC,cAAc,EAAE,MAAM,mBAAmB,CAAC;AAExF,OAAO,KAAK,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAiDrD,mEAAmE;AACnE,wBAAsB,OAAO,CAC3B,QAAQ,EAAE,QAAQ,EAClB,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE;IAAE,OAAO,EAAE,aAAa,CAAC,cAAc,CAAC,CAAC;IAAC,QAAQ,EAAE,QAAQ,CAAA;CAAE,GACpF,OAAO,CAAC,IAAI,CAAC,CAqCf"}
|
package/build/declare.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { compileMessage } from '@parlajs/compiler';
|
|
2
|
+
/** `[path, message]` for every leaf of a catalog; the compiler reports what is no message. */
|
|
3
|
+
function* leaves(catalog, prefix = '') {
|
|
4
|
+
if (!catalog || typeof catalog !== 'object' || Array.isArray(catalog)) {
|
|
5
|
+
return;
|
|
6
|
+
}
|
|
7
|
+
for (const [key, value] of Object.entries(catalog)) {
|
|
8
|
+
if (typeof value === 'string') {
|
|
9
|
+
yield [`${prefix}${key}`, value];
|
|
10
|
+
}
|
|
11
|
+
else {
|
|
12
|
+
yield* leaves(value, `${prefix}${key}.`);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
/** A catalog's messages, read as its format reads them; an unreadable one counts as empty. */
|
|
17
|
+
async function readCatalog(readFile, { path, adapter }) {
|
|
18
|
+
try {
|
|
19
|
+
const text = await readFile(path);
|
|
20
|
+
if (text === undefined) {
|
|
21
|
+
return {};
|
|
22
|
+
}
|
|
23
|
+
return (adapter?.parseFile ? adapter.parseFile(text) : JSON.parse(text));
|
|
24
|
+
}
|
|
25
|
+
catch {
|
|
26
|
+
return {};
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
/** Whether a message selects on keys that need different params. */
|
|
30
|
+
function hasBranches(value, { adapter, functions }) {
|
|
31
|
+
try {
|
|
32
|
+
return Boolean(adapter && compileMessage(adapter.parse(value), { custom: functions }).info.branches);
|
|
33
|
+
}
|
|
34
|
+
catch {
|
|
35
|
+
// Reported by `validate`.
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** Fills `declared` from the configs and their source catalogs. */
|
|
40
|
+
export async function declare(declared, { configs, readFile }) {
|
|
41
|
+
for (const config of configs) {
|
|
42
|
+
Object.assign(declared.functions, config.functions);
|
|
43
|
+
for (const [name, adapter] of Object.entries(config.formats)) {
|
|
44
|
+
if (!declared.formats.has(name)) {
|
|
45
|
+
declared.formats.set(name, adapter);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
declared.markup.push(...config.markup.filter((tag) => !declared.markup.includes(tag)));
|
|
49
|
+
}
|
|
50
|
+
// `placeholders` tells ICU from MF2 by name, so they read only the source locale.
|
|
51
|
+
const sources = configs.flatMap((config) => config.namespaces.flatMap((ns) => {
|
|
52
|
+
const adapter = config.formats[ns.format];
|
|
53
|
+
const own = ns.format !== 'icu' && ns.format !== 'mf2';
|
|
54
|
+
const locales = own ? config.locales : [config.sourceLocale];
|
|
55
|
+
return locales.map((locale) => ({ config, ns, adapter, own, locale }));
|
|
56
|
+
}));
|
|
57
|
+
const catalogs = await Promise.all(sources.map(async ({ ns, adapter, locale }) => readCatalog(readFile, { path: ns.file(locale), adapter })));
|
|
58
|
+
for (const [index, { config, ns, adapter, own, locale }] of sources.entries()) {
|
|
59
|
+
for (const [key, value] of leaves(catalogs[index] ?? {})) {
|
|
60
|
+
if (own && adapter) {
|
|
61
|
+
declared.syntaxes.set(value, adapter);
|
|
62
|
+
}
|
|
63
|
+
if (locale === config.sourceLocale &&
|
|
64
|
+
hasBranches(value, { adapter, functions: config.functions })) {
|
|
65
|
+
declared.branchy.add(`${ns.name}:${key}`);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Plugin } from '@x18n/core';
|
|
2
|
+
export interface ParlaOptions {
|
|
3
|
+
/** Hooks that take a list of namespaces and return `{ t }`. Default: both of Parla's. */
|
|
4
|
+
readonly callees?: ReadonlyArray<string>;
|
|
5
|
+
/** Parla configs whose namespaces x18n reads, relative to the root. Default: every `parla.config.ts` or `.mts`. */
|
|
6
|
+
readonly configs?: ReadonlyArray<string>;
|
|
7
|
+
}
|
|
8
|
+
/** The x18n plugin: Parla's catalogs, `t.ns.key()` usages, and ICU and MF2 params. */
|
|
9
|
+
export default function parla(options?: ParlaOptions): Plugin;
|
|
10
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,YAAY,CAAC;AAOzC,MAAM,WAAW,YAAY;IAC3B,yFAAyF;IACzF,QAAQ,CAAC,OAAO,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;IACzC,mHAAmH;IACnH,QAAQ,CAAC,OAAO,CAAC,EAAE,aAAa,CAAC,MAAM,CAAC,CAAC;CAC1C;AAED,sFAAsF;AACtF,MAAM,CAAC,OAAO,UAAU,KAAK,CAAC,OAAO,GAAE,YAAiB,GAAG,MAAM,CAkBhE"}
|
package/build/index.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { configFinder } from './configs.js';
|
|
2
|
+
import { placeholders } from './placeholders.js';
|
|
3
|
+
import { accessorScanner } from './scanner.js';
|
|
4
|
+
/** The x18n plugin: Parla's catalogs, `t.ns.key()` usages, and ICU and MF2 params. */
|
|
5
|
+
export default function parla(options = {}) {
|
|
6
|
+
const declared = {
|
|
7
|
+
functions: {},
|
|
8
|
+
formats: new Map(),
|
|
9
|
+
syntaxes: new Map(),
|
|
10
|
+
markup: [],
|
|
11
|
+
branchy: new Set(),
|
|
12
|
+
};
|
|
13
|
+
return {
|
|
14
|
+
name: 'parla',
|
|
15
|
+
enforce: 'pre',
|
|
16
|
+
runtime: 'node',
|
|
17
|
+
localeFinders: [configFinder(options.configs, declared)],
|
|
18
|
+
scanners: [
|
|
19
|
+
accessorScanner(new Set(options.callees ?? ['useParla', 'useParlaLoaded']), declared),
|
|
20
|
+
],
|
|
21
|
+
placeholders: [placeholders(declared)],
|
|
22
|
+
};
|
|
23
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Node } from './ast.ts';
|
|
2
|
+
/** Local names of the namespace objects a file imports, to their namespace. */
|
|
3
|
+
export declare function namespaceImports(program: Node): Map<string, string>;
|
|
4
|
+
/** `[a, b]`; undefined unless every element is an imported namespace object. */
|
|
5
|
+
export declare function namespacesOf(arg: unknown, imports: ReadonlyMap<string, string>): Array<string> | undefined;
|
|
6
|
+
/** `Translations<typeof a | typeof b>` → the namespaces of `a` and `b`; undefined for anything else. */
|
|
7
|
+
export declare function translationsOf(type: unknown, imports: ReadonlyMap<string, string>): Array<string> | undefined;
|
|
8
|
+
export declare const WRAPPERS: Set<string>;
|
|
9
|
+
/** The namespaces of `await x.load([a, b])` or `x.loaded([a, b])`, whatever `x` is. */
|
|
10
|
+
export declare function loadedBy(init: unknown, imports: ReadonlyMap<string, string>): Array<string> | undefined;
|
|
11
|
+
//# sourceMappingURL=namespaces.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"namespaces.d.ts","sourceRoot":"","sources":["../src/namespaces.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,UAAU,CAAC;AAMrC,+EAA+E;AAC/E,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,IAAI,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAenE;AAED,gFAAgF;AAChF,wBAAgB,YAAY,CAC1B,GAAG,EAAE,OAAO,EACZ,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,GACnC,KAAK,CAAC,MAAM,CAAC,GAAG,SAAS,CAU3B;AAED,wGAAwG;AACxG,wBAAgB,cAAc,CAC5B,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,GACnC,KAAK,CAAC,MAAM,CAAC,GAAG,SAAS,CAmB3B;AAED,eAAO,MAAM,QAAQ,aAInB,CAAC;AAEH,uFAAuF;AACvF,wBAAgB,QAAQ,CACtB,IAAI,EAAE,OAAO,EACb,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,GACnC,KAAK,CAAC,MAAM,CAAC,GAAG,SAAS,CAgB3B"}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { isNode, segmentOf, stringValue } from './ast.js';
|
|
2
|
+
// Parla writes each namespace to `namespaces/<name>.js`, so the path names it whatever the binding.
|
|
3
|
+
// A bundler needs no extension.
|
|
4
|
+
const NAMESPACE_MODULE = /\/namespaces\/(?<name>[^/]+?)(?:\.js)?$/;
|
|
5
|
+
/** Local names of the namespace objects a file imports, to their namespace. */
|
|
6
|
+
export function namespaceImports(program) {
|
|
7
|
+
const out = new Map();
|
|
8
|
+
for (const statement of program['body']) {
|
|
9
|
+
const name = statement.type === 'ImportDeclaration'
|
|
10
|
+
? stringValue(statement['source'])?.match(NAMESPACE_MODULE)?.groups?.['name']
|
|
11
|
+
: undefined;
|
|
12
|
+
for (const specifier of name ? statement['specifiers'] : []) {
|
|
13
|
+
const { local } = specifier;
|
|
14
|
+
if (specifier.type === 'ImportSpecifier' && isNode(local)) {
|
|
15
|
+
out.set(local['name'], name);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
return out;
|
|
20
|
+
}
|
|
21
|
+
/** `[a, b]`; undefined unless every element is an imported namespace object. */
|
|
22
|
+
export function namespacesOf(arg, imports) {
|
|
23
|
+
if (!isNode(arg) || arg.type !== 'ArrayExpression') {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
const namespaces = arg['elements'].map((element) => isNode(element) && element.type === 'Identifier'
|
|
27
|
+
? imports.get(element['name'])
|
|
28
|
+
: undefined);
|
|
29
|
+
return namespaces.every((ns) => ns !== undefined) ? namespaces : undefined;
|
|
30
|
+
}
|
|
31
|
+
/** `Translations<typeof a | typeof b>` → the namespaces of `a` and `b`; undefined for anything else. */
|
|
32
|
+
export function translationsOf(type, imports) {
|
|
33
|
+
if (!isNode(type) || type.type !== 'TSTypeReference') {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
const name = type['typeName'];
|
|
37
|
+
const arg = type['typeArguments']?.['params']?.at(0);
|
|
38
|
+
if (!isNode(name) || name['name'] !== 'Translations' || !arg) {
|
|
39
|
+
return undefined;
|
|
40
|
+
}
|
|
41
|
+
const queries = arg.type === 'TSUnionType' ? arg['types'] : [arg];
|
|
42
|
+
const namespaces = queries.map((query) => {
|
|
43
|
+
const expr = query['exprName'];
|
|
44
|
+
return query.type === 'TSTypeQuery' && isNode(expr)
|
|
45
|
+
? imports.get(expr['name'])
|
|
46
|
+
: undefined;
|
|
47
|
+
});
|
|
48
|
+
return namespaces.every((ns) => ns !== undefined) ? namespaces : undefined;
|
|
49
|
+
}
|
|
50
|
+
export const WRAPPERS = new Set([
|
|
51
|
+
'TSNonNullExpression',
|
|
52
|
+
'ParenthesizedExpression',
|
|
53
|
+
'ChainExpression',
|
|
54
|
+
]);
|
|
55
|
+
/** The namespaces of `await x.load([a, b])` or `x.loaded([a, b])`, whatever `x` is. */
|
|
56
|
+
export function loadedBy(init, imports) {
|
|
57
|
+
let node = init;
|
|
58
|
+
let awaited = false;
|
|
59
|
+
while (isNode(node) && (node.type === 'AwaitExpression' || WRAPPERS.has(node.type))) {
|
|
60
|
+
awaited ||= node.type === 'AwaitExpression';
|
|
61
|
+
node = node['argument'] ?? node['expression'];
|
|
62
|
+
}
|
|
63
|
+
if (!isNode(node) || node.type !== 'CallExpression') {
|
|
64
|
+
return undefined;
|
|
65
|
+
}
|
|
66
|
+
const { callee } = node;
|
|
67
|
+
const method = isNode(callee) && callee.type === 'MemberExpression' && segmentOf(callee)?.name;
|
|
68
|
+
const [arg] = node['arguments'];
|
|
69
|
+
return method === 'loaded' || (method === 'load' && awaited)
|
|
70
|
+
? namespacesOf(arg, imports)
|
|
71
|
+
: undefined;
|
|
72
|
+
}
|
package/build/paths.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"paths.d.ts","sourceRoot":"","sources":["../src/paths.ts"],"names":[],"mappings":"AAIA,mFAAmF;AACnF,wBAAgB,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAG3D"}
|
package/build/paths.js
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import path from 'node:path';
|
|
2
|
+
const posix = (file) => file.replaceAll(path.sep, '/');
|
|
3
|
+
/** `file` relative to `root`, as x18n names resources: with `/` on Windows too. */
|
|
4
|
+
export function relative(root, file) {
|
|
5
|
+
const [dir, target] = [posix(root), posix(file)];
|
|
6
|
+
return target.startsWith(`${dir}/`) ? target.slice(dir.length + 1) : target;
|
|
7
|
+
}
|