@vocab/core 1.2.1 → 1.2.2

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.
@@ -451,7 +451,7 @@ function extractHasTags(ast) {
451
451
  }
452
452
  function extractParamTypes(ast) {
453
453
  let params = {};
454
- let imports = new Set();
454
+ let vocabTypesImports = new Set();
455
455
  for (const element of ast) {
456
456
  if (icuMessageformatParser.isArgumentElement(element)) {
457
457
  params[element.value] = 'string';
@@ -462,7 +462,7 @@ function extractParamTypes(ast) {
462
462
  const children = Object.values(element.options).map(o => o.value);
463
463
  for (const child of children) {
464
464
  const [subParams, subImports] = extractParamTypes(child);
465
- imports = new Set([...imports, ...subImports]);
465
+ vocabTypesImports = new Set([...vocabTypesImports, ...subImports]);
466
466
  params = {
467
467
  ...params,
468
468
  ...subParams
@@ -472,19 +472,25 @@ function extractParamTypes(ast) {
472
472
  params[element.value] = 'Date | number';
473
473
  } else if (icuMessageformatParser.isTagElement(element)) {
474
474
  params[element.value] = 'FormatXMLElementFn<T>';
475
- imports.add(`import { FormatXMLElementFn } from '@vocab/types';`);
475
+ vocabTypesImports.add('FormatXMLElementFn');
476
476
  const [subParams, subImports] = extractParamTypes(element.children);
477
- imports = new Set([...imports, ...subImports]);
477
+ vocabTypesImports = new Set([...vocabTypesImports, ...subImports]);
478
478
  params = {
479
479
  ...params,
480
480
  ...subParams
481
481
  };
482
482
  } else if (icuMessageformatParser.isSelectElement(element)) {
483
- params[element.value] = Object.keys(element.options).map(o => `'${o}'`).join(' | ');
483
+ const options = Object.keys(element.options);
484
+
485
+ // `other` will always be an option as the parser enforces this by default
486
+ const nonOtherOptions = options.filter(o => o !== 'other');
487
+ const nonOtherOptionsUnion = nonOtherOptions.map(o => `'${o}'`).join(' | ');
488
+ params[element.value] = `StringWithSuggestions<${nonOtherOptionsUnion}>`;
489
+ vocabTypesImports.add('StringWithSuggestions');
484
490
  const children = Object.values(element.options).map(o => o.value);
485
491
  for (const child of children) {
486
492
  const [subParams, subImports] = extractParamTypes(child);
487
- imports = new Set([...imports, ...subImports]);
493
+ vocabTypesImports = new Set([...vocabTypesImports, ...subImports]);
488
494
  params = {
489
495
  ...params,
490
496
  ...subParams
@@ -492,7 +498,7 @@ function extractParamTypes(ast) {
492
498
  }
493
499
  }
494
500
  }
495
- return [params, imports];
501
+ return [params, vocabTypesImports];
496
502
  }
497
503
  function serialiseObjectToType(v) {
498
504
  let result = '';
@@ -506,6 +512,10 @@ function serialiseObjectToType(v) {
506
512
  return `{ ${result} }`;
507
513
  }
508
514
  const banner = `// This file is automatically generated by Vocab.\n// To make changes update translation.json files directly.`;
515
+ const serializeModuleImports = (imports, moduleName) => {
516
+ const importNames = Array.from(imports);
517
+ return `import { ${Array.from(importNames).join(', ')} } from '${moduleName}'`;
518
+ };
509
519
  function serialiseTranslationRuntime(value, imports, loadedTranslation) {
510
520
  trace('Serialising translations:', loadedTranslation);
511
521
  const translationsType = {};
@@ -526,7 +536,7 @@ function serialiseTranslationRuntime(value, imports, loadedTranslation) {
526
536
  const languagesUnionAsString = Object.keys(loadedTranslation.languages).map(l => `'${l}'`).join(' | ');
527
537
  return `${banner}
528
538
 
529
- ${Array.from(imports).join('\n')}
539
+ ${serializeModuleImports(imports, '@vocab/types')}
530
540
  import { createLanguage, createTranslationFile } from '@vocab/core/runtime';
531
541
 
532
542
  const translations = createTranslationFile<${languagesUnionAsString}, ${serialiseObjectToType(translationsType)}>({${content}});
@@ -549,8 +559,8 @@ async function generateRuntime(loadedTranslation) {
549
559
  if (translatedLanguage[key]) {
550
560
  const ast = icuMessageformatParser.parse(translatedLanguage[key].message);
551
561
  hasTags = hasTags || extractHasTags(ast);
552
- const [parsedParams, parsedImports] = extractParamTypes(ast);
553
- imports = new Set([...imports, ...parsedImports]);
562
+ const [parsedParams, vocabTypesImports] = extractParamTypes(ast);
563
+ imports = new Set([...imports, ...vocabTypesImports]);
554
564
  params = {
555
565
  ...params,
556
566
  ...parsedParams
@@ -451,7 +451,7 @@ function extractHasTags(ast) {
451
451
  }
452
452
  function extractParamTypes(ast) {
453
453
  let params = {};
454
- let imports = new Set();
454
+ let vocabTypesImports = new Set();
455
455
  for (const element of ast) {
456
456
  if (icuMessageformatParser.isArgumentElement(element)) {
457
457
  params[element.value] = 'string';
@@ -462,7 +462,7 @@ function extractParamTypes(ast) {
462
462
  const children = Object.values(element.options).map(o => o.value);
463
463
  for (const child of children) {
464
464
  const [subParams, subImports] = extractParamTypes(child);
465
- imports = new Set([...imports, ...subImports]);
465
+ vocabTypesImports = new Set([...vocabTypesImports, ...subImports]);
466
466
  params = {
467
467
  ...params,
468
468
  ...subParams
@@ -472,19 +472,25 @@ function extractParamTypes(ast) {
472
472
  params[element.value] = 'Date | number';
473
473
  } else if (icuMessageformatParser.isTagElement(element)) {
474
474
  params[element.value] = 'FormatXMLElementFn<T>';
475
- imports.add(`import { FormatXMLElementFn } from '@vocab/types';`);
475
+ vocabTypesImports.add('FormatXMLElementFn');
476
476
  const [subParams, subImports] = extractParamTypes(element.children);
477
- imports = new Set([...imports, ...subImports]);
477
+ vocabTypesImports = new Set([...vocabTypesImports, ...subImports]);
478
478
  params = {
479
479
  ...params,
480
480
  ...subParams
481
481
  };
482
482
  } else if (icuMessageformatParser.isSelectElement(element)) {
483
- params[element.value] = Object.keys(element.options).map(o => `'${o}'`).join(' | ');
483
+ const options = Object.keys(element.options);
484
+
485
+ // `other` will always be an option as the parser enforces this by default
486
+ const nonOtherOptions = options.filter(o => o !== 'other');
487
+ const nonOtherOptionsUnion = nonOtherOptions.map(o => `'${o}'`).join(' | ');
488
+ params[element.value] = `StringWithSuggestions<${nonOtherOptionsUnion}>`;
489
+ vocabTypesImports.add('StringWithSuggestions');
484
490
  const children = Object.values(element.options).map(o => o.value);
485
491
  for (const child of children) {
486
492
  const [subParams, subImports] = extractParamTypes(child);
487
- imports = new Set([...imports, ...subImports]);
493
+ vocabTypesImports = new Set([...vocabTypesImports, ...subImports]);
488
494
  params = {
489
495
  ...params,
490
496
  ...subParams
@@ -492,7 +498,7 @@ function extractParamTypes(ast) {
492
498
  }
493
499
  }
494
500
  }
495
- return [params, imports];
501
+ return [params, vocabTypesImports];
496
502
  }
497
503
  function serialiseObjectToType(v) {
498
504
  let result = '';
@@ -506,6 +512,10 @@ function serialiseObjectToType(v) {
506
512
  return `{ ${result} }`;
507
513
  }
508
514
  const banner = `// This file is automatically generated by Vocab.\n// To make changes update translation.json files directly.`;
515
+ const serializeModuleImports = (imports, moduleName) => {
516
+ const importNames = Array.from(imports);
517
+ return `import { ${Array.from(importNames).join(', ')} } from '${moduleName}'`;
518
+ };
509
519
  function serialiseTranslationRuntime(value, imports, loadedTranslation) {
510
520
  trace('Serialising translations:', loadedTranslation);
511
521
  const translationsType = {};
@@ -526,7 +536,7 @@ function serialiseTranslationRuntime(value, imports, loadedTranslation) {
526
536
  const languagesUnionAsString = Object.keys(loadedTranslation.languages).map(l => `'${l}'`).join(' | ');
527
537
  return `${banner}
528
538
 
529
- ${Array.from(imports).join('\n')}
539
+ ${serializeModuleImports(imports, '@vocab/types')}
530
540
  import { createLanguage, createTranslationFile } from '@vocab/core/runtime';
531
541
 
532
542
  const translations = createTranslationFile<${languagesUnionAsString}, ${serialiseObjectToType(translationsType)}>({${content}});
@@ -549,8 +559,8 @@ async function generateRuntime(loadedTranslation) {
549
559
  if (translatedLanguage[key]) {
550
560
  const ast = icuMessageformatParser.parse(translatedLanguage[key].message);
551
561
  hasTags = hasTags || extractHasTags(ast);
552
- const [parsedParams, parsedImports] = extractParamTypes(ast);
553
- imports = new Set([...imports, ...parsedImports]);
562
+ const [parsedParams, vocabTypesImports] = extractParamTypes(ast);
563
+ imports = new Set([...imports, ...vocabTypesImports]);
554
564
  params = {
555
565
  ...params,
556
566
  ...parsedParams
@@ -435,7 +435,7 @@ function extractHasTags(ast) {
435
435
  }
436
436
  function extractParamTypes(ast) {
437
437
  let params = {};
438
- let imports = new Set();
438
+ let vocabTypesImports = new Set();
439
439
  for (const element of ast) {
440
440
  if (isArgumentElement(element)) {
441
441
  params[element.value] = 'string';
@@ -446,7 +446,7 @@ function extractParamTypes(ast) {
446
446
  const children = Object.values(element.options).map(o => o.value);
447
447
  for (const child of children) {
448
448
  const [subParams, subImports] = extractParamTypes(child);
449
- imports = new Set([...imports, ...subImports]);
449
+ vocabTypesImports = new Set([...vocabTypesImports, ...subImports]);
450
450
  params = {
451
451
  ...params,
452
452
  ...subParams
@@ -456,19 +456,25 @@ function extractParamTypes(ast) {
456
456
  params[element.value] = 'Date | number';
457
457
  } else if (isTagElement(element)) {
458
458
  params[element.value] = 'FormatXMLElementFn<T>';
459
- imports.add(`import { FormatXMLElementFn } from '@vocab/types';`);
459
+ vocabTypesImports.add('FormatXMLElementFn');
460
460
  const [subParams, subImports] = extractParamTypes(element.children);
461
- imports = new Set([...imports, ...subImports]);
461
+ vocabTypesImports = new Set([...vocabTypesImports, ...subImports]);
462
462
  params = {
463
463
  ...params,
464
464
  ...subParams
465
465
  };
466
466
  } else if (isSelectElement(element)) {
467
- params[element.value] = Object.keys(element.options).map(o => `'${o}'`).join(' | ');
467
+ const options = Object.keys(element.options);
468
+
469
+ // `other` will always be an option as the parser enforces this by default
470
+ const nonOtherOptions = options.filter(o => o !== 'other');
471
+ const nonOtherOptionsUnion = nonOtherOptions.map(o => `'${o}'`).join(' | ');
472
+ params[element.value] = `StringWithSuggestions<${nonOtherOptionsUnion}>`;
473
+ vocabTypesImports.add('StringWithSuggestions');
468
474
  const children = Object.values(element.options).map(o => o.value);
469
475
  for (const child of children) {
470
476
  const [subParams, subImports] = extractParamTypes(child);
471
- imports = new Set([...imports, ...subImports]);
477
+ vocabTypesImports = new Set([...vocabTypesImports, ...subImports]);
472
478
  params = {
473
479
  ...params,
474
480
  ...subParams
@@ -476,7 +482,7 @@ function extractParamTypes(ast) {
476
482
  }
477
483
  }
478
484
  }
479
- return [params, imports];
485
+ return [params, vocabTypesImports];
480
486
  }
481
487
  function serialiseObjectToType(v) {
482
488
  let result = '';
@@ -490,6 +496,10 @@ function serialiseObjectToType(v) {
490
496
  return `{ ${result} }`;
491
497
  }
492
498
  const banner = `// This file is automatically generated by Vocab.\n// To make changes update translation.json files directly.`;
499
+ const serializeModuleImports = (imports, moduleName) => {
500
+ const importNames = Array.from(imports);
501
+ return `import { ${Array.from(importNames).join(', ')} } from '${moduleName}'`;
502
+ };
493
503
  function serialiseTranslationRuntime(value, imports, loadedTranslation) {
494
504
  trace('Serialising translations:', loadedTranslation);
495
505
  const translationsType = {};
@@ -510,7 +520,7 @@ function serialiseTranslationRuntime(value, imports, loadedTranslation) {
510
520
  const languagesUnionAsString = Object.keys(loadedTranslation.languages).map(l => `'${l}'`).join(' | ');
511
521
  return `${banner}
512
522
 
513
- ${Array.from(imports).join('\n')}
523
+ ${serializeModuleImports(imports, '@vocab/types')}
514
524
  import { createLanguage, createTranslationFile } from '@vocab/core/runtime';
515
525
 
516
526
  const translations = createTranslationFile<${languagesUnionAsString}, ${serialiseObjectToType(translationsType)}>({${content}});
@@ -533,8 +543,8 @@ async function generateRuntime(loadedTranslation) {
533
543
  if (translatedLanguage[key]) {
534
544
  const ast = parse(translatedLanguage[key].message);
535
545
  hasTags = hasTags || extractHasTags(ast);
536
- const [parsedParams, parsedImports] = extractParamTypes(ast);
537
- imports = new Set([...imports, ...parsedImports]);
546
+ const [parsedParams, vocabTypesImports] = extractParamTypes(ast);
547
+ imports = new Set([...imports, ...vocabTypesImports]);
538
548
  params = {
539
549
  ...params,
540
550
  ...parsedParams
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vocab/core",
3
- "version": "1.2.1",
3
+ "version": "1.2.2",
4
4
  "main": "dist/vocab-core.cjs.js",
5
5
  "module": "dist/vocab-core.esm.js",
6
6
  "exports": {