@parlajs/compiler 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.
Files changed (159) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +21 -2
  3. package/build/build.d.ts +18 -0
  4. package/build/build.d.ts.map +1 -0
  5. package/build/build.js +10 -0
  6. package/build/cli.d.ts +3 -0
  7. package/build/cli.d.ts.map +1 -0
  8. package/build/cli.js +8 -0
  9. package/build/command.d.ts +13 -0
  10. package/build/command.d.ts.map +1 -0
  11. package/build/command.js +156 -0
  12. package/build/compile/code.d.ts +13 -0
  13. package/build/compile/code.d.ts.map +1 -0
  14. package/build/compile/code.js +42 -0
  15. package/build/compile/compile.d.ts +11 -0
  16. package/build/compile/compile.d.ts.map +1 -0
  17. package/build/compile/compile.js +244 -0
  18. package/build/compile/error.d.ts +4 -0
  19. package/build/compile/error.d.ts.map +1 -0
  20. package/build/compile/error.js +3 -0
  21. package/build/compile/info.d.ts +49 -0
  22. package/build/compile/info.d.ts.map +1 -0
  23. package/build/compile/info.js +22 -0
  24. package/build/compile/scope.d.ts +7 -0
  25. package/build/compile/scope.d.ts.map +1 -0
  26. package/build/compile/scope.js +14 -0
  27. package/build/compile/single.d.ts +11 -0
  28. package/build/compile/single.d.ts.map +1 -0
  29. package/build/compile/single.js +36 -0
  30. package/build/compile/variants.d.ts +16 -0
  31. package/build/compile/variants.d.ts.map +1 -0
  32. package/build/compile/variants.js +48 -0
  33. package/build/config/checks.d.ts +21 -0
  34. package/build/config/checks.d.ts.map +1 -0
  35. package/build/config/checks.js +97 -0
  36. package/build/config/config.d.ts +71 -0
  37. package/build/config/config.d.ts.map +1 -0
  38. package/build/config/config.js +7 -0
  39. package/build/config/fallbacks.d.ts +6 -0
  40. package/build/config/fallbacks.d.ts.map +1 -0
  41. package/build/config/fallbacks.js +13 -0
  42. package/build/config/import.d.ts +31 -0
  43. package/build/config/import.d.ts.map +1 -0
  44. package/build/config/import.js +115 -0
  45. package/build/config/load.d.ts +13 -0
  46. package/build/config/load.d.ts.map +1 -0
  47. package/build/config/load.js +59 -0
  48. package/build/diagnostics.d.ts +31 -0
  49. package/build/diagnostics.d.ts.map +1 -0
  50. package/build/diagnostics.js +41 -0
  51. package/build/generate/checks.d.ts +9 -0
  52. package/build/generate/checks.d.ts.map +1 -0
  53. package/build/generate/checks.js +93 -0
  54. package/build/generate/chunk.d.ts +11 -0
  55. package/build/generate/chunk.d.ts.map +1 -0
  56. package/build/generate/chunk.js +57 -0
  57. package/build/generate/digits.d.ts +8 -0
  58. package/build/generate/digits.d.ts.map +1 -0
  59. package/build/generate/digits.js +57 -0
  60. package/build/generate/files.d.ts +7 -0
  61. package/build/generate/files.d.ts.map +1 -0
  62. package/build/generate/files.js +59 -0
  63. package/build/generate/locale.d.ts +21 -0
  64. package/build/generate/locale.d.ts.map +1 -0
  65. package/build/generate/locale.js +108 -0
  66. package/build/generate/modules.d.ts +27 -0
  67. package/build/generate/modules.d.ts.map +1 -0
  68. package/build/generate/modules.js +61 -0
  69. package/build/generate/namespace.d.ts +19 -0
  70. package/build/generate/namespace.d.ts.map +1 -0
  71. package/build/generate/namespace.js +65 -0
  72. package/build/generate/namespaces.d.ts +5 -0
  73. package/build/generate/namespaces.d.ts.map +1 -0
  74. package/build/generate/namespaces.js +41 -0
  75. package/build/generate/read.d.ts +21 -0
  76. package/build/generate/read.d.ts.map +1 -0
  77. package/build/generate/read.js +115 -0
  78. package/build/index.d.ts +9 -0
  79. package/build/index.d.ts.map +1 -0
  80. package/build/index.js +7 -0
  81. package/build/init/detect.d.ts +43 -0
  82. package/build/init/detect.d.ts.map +1 -0
  83. package/build/init/detect.js +69 -0
  84. package/build/init/init.d.ts +12 -0
  85. package/build/init/init.d.ts.map +1 -0
  86. package/build/init/init.js +126 -0
  87. package/build/options/date.d.ts +3 -0
  88. package/build/options/date.d.ts.map +1 -0
  89. package/build/options/date.js +77 -0
  90. package/build/options/literal.d.ts +10 -0
  91. package/build/options/literal.d.ts.map +1 -0
  92. package/build/options/literal.js +11 -0
  93. package/build/options/names.d.ts +5 -0
  94. package/build/options/names.d.ts.map +1 -0
  95. package/build/options/names.js +56 -0
  96. package/build/options/number.d.ts +3 -0
  97. package/build/options/number.d.ts.map +1 -0
  98. package/build/options/number.js +17 -0
  99. package/build/options/options.d.ts +7 -0
  100. package/build/options/options.d.ts.map +1 -0
  101. package/build/options/options.js +66 -0
  102. package/build/plurals.d.ts +6 -0
  103. package/build/plurals.d.ts.map +1 -0
  104. package/build/plurals.js +50 -0
  105. package/build/types/names.d.ts +9 -0
  106. package/build/types/names.d.ts.map +1 -0
  107. package/build/types/names.js +109 -0
  108. package/build/types/namespace.d.ts +17 -0
  109. package/build/types/namespace.d.ts.map +1 -0
  110. package/build/types/namespace.js +113 -0
  111. package/build/types/signature.d.ts +8 -0
  112. package/build/types/signature.d.ts.map +1 -0
  113. package/build/types/signature.js +87 -0
  114. package/build/vite.d.ts +9 -0
  115. package/build/vite.d.ts.map +1 -0
  116. package/build/vite.js +125 -0
  117. package/build/watch.d.ts +12 -0
  118. package/build/watch.d.ts.map +1 -0
  119. package/build/watch.js +93 -0
  120. package/package.json +84 -3
  121. package/src/build.ts +31 -0
  122. package/src/cli.ts +9 -0
  123. package/src/command.ts +195 -0
  124. package/src/compile/code.ts +51 -0
  125. package/src/compile/compile.ts +289 -0
  126. package/src/compile/error.ts +3 -0
  127. package/src/compile/info.ts +63 -0
  128. package/src/compile/scope.ts +14 -0
  129. package/src/compile/single.ts +46 -0
  130. package/src/compile/variants.ts +68 -0
  131. package/src/config/checks.ts +140 -0
  132. package/src/config/config.ts +83 -0
  133. package/src/config/fallbacks.ts +18 -0
  134. package/src/config/import.ts +146 -0
  135. package/src/config/load.ts +75 -0
  136. package/src/diagnostics.ts +98 -0
  137. package/src/generate/checks.ts +103 -0
  138. package/src/generate/chunk.ts +73 -0
  139. package/src/generate/digits.ts +74 -0
  140. package/src/generate/files.ts +83 -0
  141. package/src/generate/locale.ts +145 -0
  142. package/src/generate/modules.ts +94 -0
  143. package/src/generate/namespace.ts +119 -0
  144. package/src/generate/namespaces.ts +59 -0
  145. package/src/generate/read.ts +159 -0
  146. package/src/index.ts +27 -0
  147. package/src/init/detect.ts +103 -0
  148. package/src/init/init.ts +158 -0
  149. package/src/options/date.ts +87 -0
  150. package/src/options/literal.ts +21 -0
  151. package/src/options/names.ts +64 -0
  152. package/src/options/number.ts +20 -0
  153. package/src/options/options.ts +75 -0
  154. package/src/plurals.ts +68 -0
  155. package/src/types/names.ts +115 -0
  156. package/src/types/namespace.ts +148 -0
  157. package/src/types/signature.ts +111 -0
  158. package/src/vite.ts +143 -0
  159. package/src/watch.ts +115 -0
@@ -0,0 +1,10 @@
1
+ import type { Model } from 'messageformat';
2
+ export type Value = Model.Literal | Model.VariableRef;
3
+ export type Options = Record<string, Value>;
4
+ export declare const literal: (value: string) => Model.Literal;
5
+ /** A literal's value, which must be one of `allowed`; a variable cannot name fields at build time. */
6
+ export declare function chosen(name: string, { value, allowed }: {
7
+ value: Value;
8
+ allowed: ReadonlySet<string>;
9
+ }): string;
10
+ //# sourceMappingURL=literal.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"literal.d.ts","sourceRoot":"","sources":["../../src/options/literal.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAE3C,MAAM,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,GAAG,KAAK,CAAC,WAAW,CAAC;AAEtD,MAAM,MAAM,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,KAAK,CAAC,CAAC;AAE5C,eAAO,MAAM,OAAO,UAAW,MAAM,KAAG,KAAK,CAAC,OAAuC,CAAC;AAEtF,sGAAsG;AACtG,wBAAgB,MAAM,CACpB,IAAI,EAAE,MAAM,EACZ,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,OAAO,EAAE,WAAW,CAAC,MAAM,CAAC,CAAA;CAAE,GACjE,MAAM,CAQR"}
@@ -0,0 +1,11 @@
1
+ export const literal = (value) => ({ type: 'literal', value });
2
+ /** A literal's value, which must be one of `allowed`; a variable cannot name fields at build time. */
3
+ export function chosen(name, { value, allowed }) {
4
+ if (value.type !== 'literal') {
5
+ throw new Error(`${name} must be a literal`);
6
+ }
7
+ if (!allowed.has(value.value)) {
8
+ throw new Error(`${name} must be one of ${[...allowed].join(', ')}, not "${value.value}"`);
9
+ }
10
+ return value.value;
11
+ }
@@ -0,0 +1,5 @@
1
+ export declare const SETTINGS: string[];
2
+ export declare const FIELDS: string[];
3
+ export declare const NUMBERS: Readonly<Record<string, ReadonlySet<string>>>;
4
+ export declare const DATES: Readonly<Record<string, ReadonlySet<string>>>;
5
+ //# sourceMappingURL=names.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"names.d.ts","sourceRoot":"","sources":["../../src/options/names.ts"],"names":[],"mappings":"AAkBA,eAAO,MAAM,QAAQ,UAAqE,CAAC;AAE3F,eAAO,MAAM,MAAM,UAYlB,CAAC;AAEF,eAAO,MAAM,OAAO,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC,CAajE,CAAC;AAEF,eAAO,MAAM,KAAK,EAAE,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC,CAc/D,CAAC"}
@@ -0,0 +1,56 @@
1
+ const DIGITS = [
2
+ 'minimumIntegerDigits',
3
+ 'minimumSignificantDigits',
4
+ 'maximumSignificantDigits',
5
+ 'roundingIncrement',
6
+ 'roundingMode',
7
+ 'roundingPriority',
8
+ 'trailingZeroDisplay',
9
+ ];
10
+ const COMMON = ['signDisplay', 'useGrouping', 'numberingSystem', 'select', 'offset', 'scale'];
11
+ const FRACTION = ['minimumFractionDigits', 'maximumFractionDigits'];
12
+ const COMPACT = ['notation', 'compactDisplay'];
13
+ const FORMAT = [...COMMON, ...DIGITS, ...FRACTION, ...COMPACT];
14
+ export const SETTINGS = ['calendar', 'numberingSystem', 'timeZone', 'hour12', 'hourCycle'];
15
+ export const FIELDS = [
16
+ 'weekday',
17
+ 'era',
18
+ 'year',
19
+ 'month',
20
+ 'day',
21
+ 'dayPeriod',
22
+ 'hour',
23
+ 'minute',
24
+ 'second',
25
+ 'fractionalSecondDigits',
26
+ 'timeZoneName',
27
+ ];
28
+ export const NUMBERS = {
29
+ number: new Set([...FORMAT, 'style']),
30
+ integer: new Set([...COMMON, 'minimumIntegerDigits', 'maximumSignificantDigits', ...COMPACT]),
31
+ currency: new Set([
32
+ ...FORMAT,
33
+ 'fractionDigits',
34
+ 'currency',
35
+ 'currencyDisplay',
36
+ 'currencySign',
37
+ 'unitDisplay',
38
+ ]),
39
+ percent: new Set([...FORMAT, 'currencySign']),
40
+ unit: new Set([...FORMAT, 'unit', 'unitDisplay', 'currencyDisplay']),
41
+ };
42
+ export const DATES = {
43
+ date: new Set([...SETTINGS, ...FIELDS, 'style', 'length', 'fields']),
44
+ time: new Set([...SETTINGS, ...FIELDS, 'style', 'length', 'precision', 'timeZoneStyle']),
45
+ datetime: new Set([
46
+ ...SETTINGS,
47
+ ...FIELDS,
48
+ 'style',
49
+ 'dateStyle',
50
+ 'timeStyle',
51
+ 'dateFields',
52
+ 'dateLength',
53
+ 'timePrecision',
54
+ 'timeZoneStyle',
55
+ ]),
56
+ };
@@ -0,0 +1,3 @@
1
+ import type { Options } from './literal.ts';
2
+ export declare function numberOptions(options: Options): Options;
3
+ //# sourceMappingURL=number.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"number.d.ts","sourceRoot":"","sources":["../../src/options/number.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AAI5C,wBAAgB,aAAa,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAcvD"}
@@ -0,0 +1,17 @@
1
+ import { chosen, literal } from './literal.js';
2
+ const SELECTS = new Set(['plural', 'cardinal', 'ordinal', 'exact']);
3
+ export function numberOptions(options) {
4
+ const { fractionDigits, ...out } = options;
5
+ if (fractionDigits && !(fractionDigits.type === 'literal' && fractionDigits.value === 'auto')) {
6
+ out['minimumFractionDigits'] = fractionDigits;
7
+ out['maximumFractionDigits'] = fractionDigits;
8
+ }
9
+ const { select, useGrouping } = out;
10
+ if (select?.type === 'literal') {
11
+ chosen('select', { value: select, allowed: SELECTS });
12
+ }
13
+ if (useGrouping?.type === 'literal' && useGrouping.value === 'never') {
14
+ out['useGrouping'] = literal('false');
15
+ }
16
+ return out;
17
+ }
@@ -0,0 +1,7 @@
1
+ import type { Model } from 'messageformat';
2
+ import type { Options } from './literal.ts';
3
+ /** Which of a variable's options a function applied to it takes over. */
4
+ export declare function inheritedOptions(from: Model.FunctionRef, to: string): Options;
5
+ /** A built-in function's options as `Intl` takes them; throws for one it cannot honour. */
6
+ export declare function intlOptions({ name, options }: Model.FunctionRef): Options;
7
+ //# sourceMappingURL=options.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../../src/options/options.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AAG3C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,cAAc,CAAC;AA+B5C,yEAAyE;AACzE,wBAAgB,gBAAgB,CAAC,IAAI,EAAE,KAAK,CAAC,WAAW,EAAE,EAAE,EAAE,MAAM,GAAG,OAAO,CAiB7E;AAED,2FAA2F;AAC3F,wBAAgB,WAAW,CAAC,EAAE,IAAI,EAAE,OAAY,EAAE,EAAE,KAAK,CAAC,WAAW,GAAG,OAAO,CAmB9E"}
@@ -0,0 +1,66 @@
1
+ import { dateOptions } from './date.js';
2
+ import { DATES, NUMBERS } from './names.js';
3
+ import { numberOptions } from './number.js';
4
+ // An `Intl` object per message made a build 7 times slower.
5
+ const checked = new Set();
6
+ /** Throws what `Intl` would at run time, where every option is a literal. */
7
+ function check(name, options) {
8
+ const key = name + JSON.stringify(options);
9
+ if (checked.has(key)) {
10
+ return;
11
+ }
12
+ const given = {};
13
+ for (const [k, v] of Object.entries(options)) {
14
+ if (v.type !== 'literal') {
15
+ return;
16
+ }
17
+ given[k] = v.value === 'false' || v.value === 'true' ? v.value === 'true' : v.value;
18
+ }
19
+ if (Object.hasOwn(NUMBERS, name)) {
20
+ const style = name === 'number' || name === 'integer' ? {} : { style: name };
21
+ // oxlint-disable-next-line no-new -- only its errors matter
22
+ new Intl.NumberFormat('en', { ...given, ...style });
23
+ }
24
+ else {
25
+ // oxlint-disable-next-line no-new -- only its errors matter
26
+ new Intl.DateTimeFormat('en', given);
27
+ }
28
+ checked.add(key);
29
+ }
30
+ /** Which of a variable's options a function applied to it takes over. */
31
+ export function inheritedOptions(from, to) {
32
+ const own = NUMBERS[to];
33
+ const entries = Object.entries(from.options ?? {});
34
+ if (own && Object.hasOwn(NUMBERS, from.name)) {
35
+ // `offset` is a plural's: only its own `#` subtracts it.
36
+ return Object.fromEntries(entries.filter(([k]) => own.has(k) && k !== 'style' && k !== 'offset'));
37
+ }
38
+ if (from.name === to) {
39
+ return Object.fromEntries(entries);
40
+ }
41
+ return Object.hasOwn(DATES, to) && Object.hasOwn(DATES, from.name)
42
+ ? Object.fromEntries(entries.filter(([k]) => k === 'calendar' || k === 'timeZone' || k === 'hour12'))
43
+ : {};
44
+ }
45
+ /** A built-in function's options as `Intl` takes them; throws for one it cannot honour. */
46
+ export function intlOptions({ name, options = {} }) {
47
+ const known = NUMBERS[name] ?? DATES[name] ?? new Set();
48
+ const entries = Object.entries(options).filter(([k]) => k !== 'u:id');
49
+ for (const [key] of entries) {
50
+ if (!known.has(key)) {
51
+ throw new Error(`:${name} has no option "${key}"`);
52
+ }
53
+ }
54
+ if (entries.length === 0 && name !== 'currency' && name !== 'unit') {
55
+ return {};
56
+ }
57
+ const given = Object.fromEntries(entries);
58
+ try {
59
+ const out = Object.hasOwn(NUMBERS, name) ? numberOptions(given) : dateOptions(name, given);
60
+ check(name, out);
61
+ return out;
62
+ }
63
+ catch (error) {
64
+ throw new Error(`:${name}: ${error.message}`, { cause: error });
65
+ }
66
+ }
@@ -0,0 +1,6 @@
1
+ import type { MessageInfo } from './compile/info.ts';
2
+ /** Whether a message's plurals read the same under both locales' rules. */
3
+ export declare function samePlurals(a: string, b: string): boolean;
4
+ /** The categories `locale` uses for everyday numbers that a plural leaves to its catch-all. */
5
+ export declare function missingPlurals(locale: string, { plurals }: MessageInfo): Array<string>;
6
+ //# sourceMappingURL=plurals.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"plurals.d.ts","sourceRoot":"","sources":["../src/plurals.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAkCrD,2EAA2E;AAC3E,wBAAgB,WAAW,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAezD;AAKD,+FAA+F;AAC/F,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE,EAAE,WAAW,GAAG,KAAK,CAAC,MAAM,CAAC,CAWtF"}
@@ -0,0 +1,50 @@
1
+ // Italian's `many` is for millions: asking every catalog for it would be noise.
2
+ const COMMON = Array.from({ length: 1000 }, (_, n) => n);
3
+ const CATEGORIES = new Set(['zero', 'one', 'two', 'few', 'many', 'other']);
4
+ const known = new Map();
5
+ function makeRules(locale, type) {
6
+ if (Intl.PluralRules.supportedLocalesOf(locale).length === 0) {
7
+ // It would answer for the machine's own locale.
8
+ return undefined;
9
+ }
10
+ const rules = new Intl.PluralRules(locale, { type });
11
+ return { rules, numbers: Map.groupBy(COMMON.map(String), (n) => rules.select(Number(n))) };
12
+ }
13
+ /** `undefined` for a locale whose plurals `Intl` does not know. */
14
+ function rulesOf(locale, type) {
15
+ const key = `${locale} ${type}`;
16
+ if (!known.has(key)) {
17
+ known.set(key, makeRules(locale, type));
18
+ }
19
+ return known.get(key);
20
+ }
21
+ const same = new Map();
22
+ /** Whether a message's plurals read the same under both locales' rules. */
23
+ export function samePlurals(a, b) {
24
+ const key = `${a} ${b}`;
25
+ let answer = same.get(key);
26
+ if (answer === undefined) {
27
+ answer = ['cardinal', 'ordinal'].every((type) => {
28
+ const [one, two] = [rulesOf(a, type)?.rules, rulesOf(b, type)?.rules];
29
+ return (one !== undefined &&
30
+ two !== undefined &&
31
+ [...COMMON, 0.1, 0.5, 1.5, 2.5, 1e6].every((n) => one.select(n) === two.select(n)));
32
+ });
33
+ same.set(key, answer);
34
+ }
35
+ return answer;
36
+ }
37
+ const hasCategory = (keys) => [...keys].some((key) => CATEGORIES.has(key));
38
+ /** The categories `locale` uses for everyday numbers that a plural leaves to its catch-all. */
39
+ export function missingPlurals(locale, { plurals }) {
40
+ const missing = new Set();
41
+ // A catch-all alone is a choice: Italian's "file" is its own plural.
42
+ for (const { type, keys } of plurals.filter((p) => hasCategory(p.keys))) {
43
+ for (const [category, numbers] of rulesOf(locale, type)?.numbers ?? []) {
44
+ if (category !== 'other' && !keys.has(category) && !numbers.every((n) => keys.has(n))) {
45
+ missing.add(category);
46
+ }
47
+ }
48
+ }
49
+ return [...missing];
50
+ }
@@ -0,0 +1,9 @@
1
+ export declare const prop: (name: string) => string;
2
+ /**
3
+ * Each namespace's export: the name itself where it is an identifier, to match `t`'s key; else one
4
+ * derived from its interface (`foo-bar` → `fooBar`), numbered around the names already taken.
5
+ */
6
+ export declare function exportNames(ifaces: ReadonlyMap<string, string>): Map<string, string>;
7
+ /** {@link interfaceName} per namespace, numbered where two collide (`foo-bar`, `foo_bar`). */
8
+ export declare function interfaceNames(namespaces: ReadonlyArray<string>): Map<string, string>;
9
+ //# sourceMappingURL=names.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"names.d.ts","sourceRoot":"","sources":["../../src/types/names.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,IAAI,SAAU,MAAM,KAAG,MAA0D,CAAC;AAoE/F;;;GAGG;AACH,wBAAgB,WAAW,CAAC,MAAM,EAAE,WAAW,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAuBpF;AAED,8FAA8F;AAC9F,wBAAgB,cAAc,CAAC,UAAU,EAAE,aAAa,CAAC,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,CAcrF"}
@@ -0,0 +1,109 @@
1
+ const IDENT = /^[A-Za-z_$][\w$]*$/;
2
+ export const prop = (name) => (IDENT.test(name) ? name : JSON.stringify(name));
3
+ function interfaceName(namespace) {
4
+ const pascal = namespace
5
+ .split(/[^A-Za-z0-9]+/)
6
+ .filter(Boolean)
7
+ .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
8
+ .join('');
9
+ // `_` keeps a name like `-` from becoming `Messages`, whose plain part would shadow `Text`
10
+ return `${/^(?:\d|$)/.test(pascal) ? '_' : ''}${pascal}Messages`;
11
+ }
12
+ // Not valid as a `const` name in a module, or a built-in TypeScript rejects as one.
13
+ const RESERVED = new Set([
14
+ 'arguments',
15
+ 'await',
16
+ 'break',
17
+ 'case',
18
+ 'catch',
19
+ 'class',
20
+ 'const',
21
+ 'continue',
22
+ 'debugger',
23
+ 'default',
24
+ 'delete',
25
+ 'do',
26
+ 'else',
27
+ 'enum',
28
+ 'eval',
29
+ 'export',
30
+ 'extends',
31
+ 'false',
32
+ 'finally',
33
+ 'for',
34
+ 'function',
35
+ 'globalThis',
36
+ 'if',
37
+ 'implements',
38
+ 'import',
39
+ 'in',
40
+ 'instanceof',
41
+ 'interface',
42
+ 'let',
43
+ 'new',
44
+ 'null',
45
+ 'package',
46
+ 'private',
47
+ 'protected',
48
+ 'public',
49
+ 'return',
50
+ 'static',
51
+ 'super',
52
+ 'switch',
53
+ 'this',
54
+ 'throw',
55
+ 'true',
56
+ 'try',
57
+ 'typeof',
58
+ 'undefined',
59
+ 'var',
60
+ 'void',
61
+ 'while',
62
+ 'with',
63
+ 'yield',
64
+ ]);
65
+ const isOwnName = (ns) => IDENT.test(ns) && !RESERVED.has(ns);
66
+ /**
67
+ * Each namespace's export: the name itself where it is an identifier, to match `t`'s key; else one
68
+ * derived from its interface (`foo-bar` → `fooBar`), numbered around the names already taken.
69
+ */
70
+ export function exportNames(ifaces) {
71
+ const names = new Map();
72
+ for (const ns of ifaces.keys()) {
73
+ if (isOwnName(ns)) {
74
+ names.set(ns, ns);
75
+ }
76
+ }
77
+ const used = new Set(names.values());
78
+ for (const [ns, iface] of ifaces) {
79
+ if (isOwnName(ns)) {
80
+ continue;
81
+ }
82
+ const base = iface.replace(/Messages$/, '');
83
+ const camel = base.charAt(0).toLowerCase() + base.slice(1);
84
+ const derived = RESERVED.has(camel) ? `_${camel}` : camel;
85
+ let name = derived;
86
+ for (let n = 2; used.has(name); n += 1) {
87
+ name = `${derived}${n}`;
88
+ }
89
+ used.add(name);
90
+ names.set(ns, name);
91
+ }
92
+ return names;
93
+ }
94
+ /** {@link interfaceName} per namespace, numbered where two collide (`foo-bar`, `foo_bar`). */
95
+ export function interfaceNames(namespaces) {
96
+ const names = new Map();
97
+ // its kind would shadow the imported `NamespaceKind`
98
+ const used = new Set(['NamespaceMessages']);
99
+ for (const namespace of namespaces) {
100
+ const base = interfaceName(namespace);
101
+ let name = base;
102
+ for (let n = 2; used.has(name); n += 1) {
103
+ name = base.replace(/Messages$/, `${n}Messages`);
104
+ }
105
+ used.add(name);
106
+ names.set(namespace, name);
107
+ }
108
+ return names;
109
+ }
@@ -0,0 +1,17 @@
1
+ import type { MessageInfo } from '../compile/info.ts';
2
+ export interface NamespaceTypes {
3
+ /** The interfaces, without imports. */
4
+ readonly code: string;
5
+ /** Whether the namespace's interface takes `Node`: only markup needs it. */
6
+ readonly generic: boolean;
7
+ /** Whether `code` names `Text`. */
8
+ readonly usesText: boolean;
9
+ }
10
+ export declare function namespaceTypes(messages: Map<string, {
11
+ info: MessageInfo;
12
+ source: string;
13
+ }>, { name, defaults }: {
14
+ name: string;
15
+ defaults: ReadonlySet<string>;
16
+ }): NamespaceTypes;
17
+ //# sourceMappingURL=namespace.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"namespace.d.ts","sourceRoot":"","sources":["../../src/types/namespace.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AA0CtD,MAAM,WAAW,cAAc;IAC7B,uCAAuC;IACvC,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,4EAA4E;IAC5E,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC;IAC1B,mCAAmC;IACnC,QAAQ,CAAC,QAAQ,EAAE,OAAO,CAAC;CAC5B;AAsED,wBAAgB,cAAc,CAC5B,QAAQ,EAAE,GAAG,CAAC,MAAM,EAAE;IAAE,IAAI,EAAE,WAAW,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAE,CAAC,EAC5D,EAAE,IAAI,EAAE,QAAQ,EAAE,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,WAAW,CAAC,MAAM,CAAC,CAAA;CAAE,GAClE,cAAc,CAyBhB"}
@@ -0,0 +1,113 @@
1
+ import { prop } from './names.js';
2
+ import { OTHER, signature } from './signature.js';
3
+ const isLeaf = (n) => !(n instanceof Map);
4
+ /** Whether `node` holds a message with markup (`rich`) or one without. */
5
+ const holds = (node, rich) => isLeaf(node) ? Boolean(node.rich) === rich : [...node.values()].some((n) => holds(n, rich));
6
+ function render(tree, indent) {
7
+ const lines = [];
8
+ for (const [key, node] of tree) {
9
+ if (isLeaf(node)) {
10
+ if (node.doc !== undefined) {
11
+ // Not `/\s*\n\s*/g`, quadratic in a run of spaces.
12
+ const doc = node.doc.split('\n').map((line) => line.trim());
13
+ const text = doc
14
+ .filter(Boolean)
15
+ .join(' ')
16
+ .replaceAll('*/', String.raw `*\/`);
17
+ lines.push(`${indent}/** ${text} */`);
18
+ }
19
+ lines.push(`${indent}${prop(key)}${node.leaf};`);
20
+ }
21
+ else {
22
+ lines.push(`${indent}${prop(key)}: {`, render(node, `${indent} `), `${indent}};`);
23
+ }
24
+ }
25
+ return lines.join('\n');
26
+ }
27
+ function treeOf(entries) {
28
+ const tree = new Map();
29
+ for (const [path, leaf] of entries) {
30
+ const last = path.at(-1) ?? '';
31
+ let node = tree;
32
+ for (const segment of path.slice(0, -1)) {
33
+ let child = node.get(segment);
34
+ if (!child || isLeaf(child)) {
35
+ child = new Map();
36
+ node.set(segment, child);
37
+ }
38
+ node = child;
39
+ }
40
+ node.set(last, leaf);
41
+ }
42
+ return tree;
43
+ }
44
+ const pascal = (key) => key
45
+ .split(/[^A-Za-z0-9]+/)
46
+ .filter(Boolean)
47
+ .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
48
+ .join('');
49
+ /**
50
+ * Plain messages in a non-generic `XText`, and those with markup in `XMessages<Node> extends XText`,
51
+ * so instantiating `Node` skips every plain message. A group holding both splits the same way.
52
+ */
53
+ function declare(tree, { name, unique, out }) {
54
+ const plain = new Map();
55
+ const rich = new Map();
56
+ for (const [key, node] of tree) {
57
+ if (!holds(node, true)) {
58
+ plain.set(key, node);
59
+ }
60
+ else if (isLeaf(node) || !holds(node, false)) {
61
+ rich.set(key, node);
62
+ }
63
+ else {
64
+ const group = unique(name.replace(/Messages$/, `${pascal(key)}Messages`));
65
+ declare(node, { name: group, unique, out });
66
+ rich.set(key, { leaf: `: ${group}<Node>` });
67
+ }
68
+ }
69
+ const text = name.replace(/Messages$/, 'Text');
70
+ const body = (members) => render(members, ' ');
71
+ if (rich.size === 0) {
72
+ out.push(`export interface ${name} {\n${body(plain)}\n}`);
73
+ }
74
+ else if (plain.size === 0) {
75
+ out.push(`export interface ${name}<Node> {\n${body(rich)}\n}`);
76
+ }
77
+ else {
78
+ out.push(`export interface ${text} {\n${body(plain)}\n}`, `export interface ${name}<Node> extends ${text} {\n${body(rich)}\n}`);
79
+ }
80
+ }
81
+ function withAliases(code) {
82
+ const aliases = [
83
+ ...(code.includes('Markup<Node>') ? ['type Markup<Node> = (children: Node) => Node;'] : []),
84
+ ...(code.includes('Other<G>') ? [OTHER] : []),
85
+ ];
86
+ return aliases.length > 0 ? `${aliases.join('\n')}\n\n${code}\n\nexport {};` : code;
87
+ }
88
+ export function namespaceTypes(messages, { name, defaults }) {
89
+ const leaves = [...messages].map(([key, { info, source }]) => [
90
+ key.split('.'),
91
+ { leaf: signature(info, defaults), doc: source, rich: info.rich },
92
+ ]);
93
+ const tree = treeOf(leaves);
94
+ const used = new Set([name]);
95
+ const unique = (base) => {
96
+ let group = base;
97
+ for (let n = 2; used.has(group); n += 1) {
98
+ group = base.replace(/Messages$/, `${n}Messages`);
99
+ }
100
+ used.add(group);
101
+ return group;
102
+ };
103
+ const out = [];
104
+ declare(tree, { name, unique, out });
105
+ const code = out.join('\n\n');
106
+ return {
107
+ // One alias: TypeScript instantiates it once per node type, not once per tag. `export {}` keeps
108
+ // it private, as a .d.ts otherwise exports every top-level name.
109
+ code: withAliases(code),
110
+ generic: holds(tree, true),
111
+ usesText: leaves.some(([, leaf]) => leaf.leaf === ': Text'),
112
+ };
113
+ }
@@ -0,0 +1,8 @@
1
+ import type { MessageInfo, ParamKind } from '../compile/info.ts';
2
+ /** Collapses every use of one parameter into a single TS type. */
3
+ export declare function paramType(kinds: ReadonlyArray<ParamKind>): string;
4
+ /** Any `string` but a literal, which only a generic tells apart: a literal makes `Record` a required key. */
5
+ export declare const OTHER = "type Other<G extends string> = G extends string ? {} extends Record<G, 1> ? G : never : never;";
6
+ /** `defaults`: markup the runtime renders when a call leaves it out (the config's `markup`). */
7
+ export declare function signature(info: MessageInfo, defaults?: ReadonlySet<string>): string;
8
+ //# sourceMappingURL=signature.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"signature.d.ts","sourceRoot":"","sources":["../../src/types/signature.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAIjE,kEAAkE;AAClE,wBAAgB,SAAS,CAAC,KAAK,EAAE,aAAa,CAAC,SAAS,CAAC,GAAG,MAAM,CAsBjE;AAqDD,6GAA6G;AAC7G,eAAO,MAAM,KAAK,mGACgF,CAAC;AAEnG,gGAAgG;AAChG,wBAAgB,SAAS,CAAC,IAAI,EAAE,WAAW,EAAE,QAAQ,GAAE,WAAW,CAAC,MAAM,CAAa,GAAG,MAAM,CAyB9F"}
@@ -0,0 +1,87 @@
1
+ import { prop } from './names.js';
2
+ /** Collapses every use of one parameter into a single TS type. */
3
+ export function paramType(kinds) {
4
+ if (kinds.some((k) => k.kind === 'number')) {
5
+ return 'number';
6
+ }
7
+ if (kinds.some((k) => k.kind === 'date')) {
8
+ return 'Date | number';
9
+ }
10
+ const selects = kinds.filter((k) => k.kind === 'select');
11
+ if (selects.length > 0) {
12
+ // sorted: a translation may list the variants in another order
13
+ const keys = [...new Set(selects.flatMap((s) => s.keys))]
14
+ .toSorted()
15
+ .map((k) => JSON.stringify(k));
16
+ if (selects.some((s) => s.open)) {
17
+ keys.push('(string & {})');
18
+ }
19
+ return keys.join(' | ') || 'string';
20
+ }
21
+ if (kinds.some((k) => k.kind === 'string')) {
22
+ return 'string';
23
+ }
24
+ return 'string | number';
25
+ }
26
+ /** A select param's listed keys as a type, `undefined` unless it also takes any other string. */
27
+ function listedKeys(kinds) {
28
+ const selects = kinds.filter((k) => k.kind === 'select');
29
+ if (selects.every((s) => !s.open) ||
30
+ kinds.some((k) => k.kind === 'number' || k.kind === 'date')) {
31
+ return undefined;
32
+ }
33
+ const keys = new Set(selects.flatMap((s) => s.keys));
34
+ return keys.size > 0
35
+ ? [...keys.add('other')]
36
+ .toSorted()
37
+ .map((k) => JSON.stringify(k))
38
+ .join(' | ')
39
+ : undefined;
40
+ }
41
+ /** One params object: `names` typed by their uses, `selected` overriding a selector's type. */
42
+ function fields(info, { names, defaults, selected, }) {
43
+ const out = [...names]
44
+ .toSorted((a, b) => a.localeCompare(b))
45
+ .map((name) => {
46
+ const kinds = info.params.get(name) ?? [];
47
+ const keys = listedKeys(kinds);
48
+ const open = keys === undefined ? paramType(kinds) : `${keys} | Other<G>`;
49
+ const type = name === selected?.[0] ? selected[1] : open;
50
+ return `${prop(name)}: ${type}`;
51
+ });
52
+ let required = out.length;
53
+ for (const tag of [...info.markup].toSorted()) {
54
+ if (!info.params.has(tag)) {
55
+ const optional = defaults.has(tag);
56
+ out.push(`${prop(tag)}${optional ? '?' : ''}: Markup<Node>`);
57
+ required += optional ? 0 : 1;
58
+ }
59
+ }
60
+ return { fields: out, required };
61
+ }
62
+ /** Any `string` but a literal, which only a generic tells apart: a literal makes `Record` a required key. */
63
+ export const OTHER = 'type Other<G extends string> = G extends string ? {} extends Record<G, 1> ? G : never : never;';
64
+ /** `defaults`: markup the runtime renders when a call leaves it out (the config's `markup`). */
65
+ export function signature(info, defaults = new Set()) {
66
+ const ret = info.rich ? 'Node' : 'string';
67
+ const { branches } = info;
68
+ if (branches) {
69
+ // One member per key, so a key's own params are required only with it; `string` last.
70
+ const every = new Set([...branches.needs.values()].flatMap((names) => [...names]));
71
+ const members = [...branches.needs]
72
+ .toSorted(([a], [b]) => Number(a === null) - Number(b === null) || String(a).localeCompare(String(b)))
73
+ .map(([key, names]) => {
74
+ // a plain `string` may hold any key, so it needs every branch's params; a literal must be listed
75
+ const [type, needed] = key === null ? ['"other" | Other<G>', every] : [JSON.stringify(key), names];
76
+ return `{ ${fields(info, { names: needed, defaults, selected: [branches.param, type] }).fields.join('; ')} }`;
77
+ });
78
+ return `<G extends string>(params: ${members.join(' | ')}): ${ret}`;
79
+ }
80
+ const own = fields(info, { names: info.params.keys(), defaults });
81
+ if (own.fields.length === 0) {
82
+ return info.rich ? '(): Node' : ': Text';
83
+ }
84
+ // One `G` for every select, as a union of methods with different type params is not callable.
85
+ const generic = own.fields.some((f) => f.includes('Other<G>')) ? '<G extends string>' : '';
86
+ return `${generic}(params${own.required === 0 ? '?' : ''}: { ${own.fields.join('; ')} }): ${ret}`;
87
+ }
@@ -0,0 +1,9 @@
1
+ import type { Plugin } from 'vite';
2
+ import type { BuildOptions } from './build.ts';
3
+ export interface ParlaViteOptions extends Pick<BuildOptions, 'config'> {
4
+ /** Folder holding the config. Defaults to Vite's root. */
5
+ root?: string;
6
+ }
7
+ /** Runs `parla build` on start and on every catalog change. */
8
+ export declare function parla(options?: ParlaViteOptions): Plugin;
9
+ //# sourceMappingURL=vite.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vite.d.ts","sourceRoot":"","sources":["../src/vite.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAwB,MAAM,EAAiB,MAAM,MAAM,CAAC;AAGxE,OAAO,KAAK,EAAE,YAAY,EAAe,MAAM,YAAY,CAAC;AAG5D,MAAM,WAAW,gBAAiB,SAAQ,IAAI,CAAC,YAAY,EAAE,QAAQ,CAAC;IACpE,0DAA0D;IAC1D,IAAI,CAAC,EAAE,MAAM,CAAC;CACf;AAeD,+DAA+D;AAC/D,wBAAgB,KAAK,CAAC,OAAO,GAAE,gBAAqB,GAAG,MAAM,CAmH5D"}