@inlang/paraglide-js 1.0.0-prerelease.21 → 1.0.0-prerelease.23

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/index.js CHANGED
@@ -162,14 +162,14 @@ var require_main = __commonJS({
162
162
  const keys = _dotenvKey(options).split(",");
163
163
  const length = keys.length;
164
164
  let decrypted;
165
- for (let i = 0; i < length; i++) {
165
+ for (let i2 = 0; i2 < length; i2++) {
166
166
  try {
167
- const key = keys[i].trim();
167
+ const key = keys[i2].trim();
168
168
  const attrs = _instructions(result, key);
169
169
  decrypted = DotenvModule.decrypt(attrs.ciphertext, attrs.key);
170
170
  break;
171
171
  } catch (error) {
172
- if (i + 1 >= length) {
172
+ if (i2 + 1 >= length) {
173
173
  throw error;
174
174
  }
175
175
  }
@@ -367,9 +367,8 @@ import "@inlang/sdk";
367
367
 
368
368
  // src/compiler/compilePattern.ts
369
369
  init_define_ENV_DEFINED_IN_BUILD_STEP();
370
- import { isValidJSIdentifier } from "@inlang/valid-js-identifier";
371
370
 
372
- // src/services/escape/index.ts
371
+ // src/services/codegen/escape.ts
373
372
  init_define_ENV_DEFINED_IN_BUILD_STEP();
374
373
  function escapeForTemplateLiteral(text) {
375
374
  return text.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\${/g, "\\${");
@@ -377,6 +376,81 @@ function escapeForTemplateLiteral(text) {
377
376
  function escapeForSingleQuoteString(text) {
378
377
  return text.replace(/\\/g, "\\\\").replace(/'/g, "\\'");
379
378
  }
379
+ function escapeForDoubleQuoteString(text) {
380
+ return text.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
381
+ }
382
+
383
+ // src/services/valid-js-identifier/index.ts
384
+ init_define_ENV_DEFINED_IN_BUILD_STEP();
385
+
386
+ // src/services/valid-js-identifier/reservedWords.ts
387
+ init_define_ENV_DEFINED_IN_BUILD_STEP();
388
+ var KEYWORDS = [
389
+ "break",
390
+ "case",
391
+ "catch",
392
+ "class",
393
+ "const",
394
+ "continue",
395
+ "debugger",
396
+ "default",
397
+ "delete",
398
+ "do",
399
+ "else",
400
+ "export",
401
+ "extends",
402
+ "false",
403
+ "finally",
404
+ "for",
405
+ "function",
406
+ "if",
407
+ "import",
408
+ "in",
409
+ "instanceof",
410
+ "new",
411
+ "null",
412
+ "return",
413
+ "super",
414
+ "switch",
415
+ "this",
416
+ "throw",
417
+ "true",
418
+ "try",
419
+ "typeof",
420
+ "var",
421
+ "void",
422
+ "while",
423
+ "with",
424
+ //Strict mode reserved keywords
425
+ "let",
426
+ "static",
427
+ "yield",
428
+ "await",
429
+ //Reserved keywords for future use
430
+ "enum",
431
+ "implements",
432
+ "interface",
433
+ "package",
434
+ "private",
435
+ "protected",
436
+ "public"
437
+ ];
438
+
439
+ // src/services/valid-js-identifier/index.ts
440
+ function isValidJSIdentifier(str) {
441
+ return !KEYWORDS.includes(str) && canBeUsedAsVariableName(str);
442
+ }
443
+ function canBeUsedAsVariableName(str) {
444
+ if (str.trim() !== str) {
445
+ return false;
446
+ }
447
+ try {
448
+ new Function(str, "var " + str);
449
+ } catch (_) {
450
+ return false;
451
+ }
452
+ return true;
453
+ }
380
454
 
381
455
  // src/compiler/compilePattern.ts
382
456
  var compilePattern = (pattern) => {
@@ -407,16 +481,13 @@ var compilePattern = (pattern) => {
407
481
 
408
482
  // src/compiler/paramsType.ts
409
483
  init_define_ENV_DEFINED_IN_BUILD_STEP();
410
- import { isValidJSIdentifier as isValidJSIdentifier2 } from "@inlang/valid-js-identifier";
411
484
  var paramsType = (params, isMessagesIndex) => {
412
485
  if (Object.keys(params).length === 0) {
413
- if (isMessagesIndex)
414
- return "@param {{}} params";
415
- return "";
486
+ return isMessagesIndex ? "@param {{}} params" : "";
416
487
  }
417
488
  const fieldTypes = [];
418
489
  for (const [name, type] of Object.entries(params)) {
419
- if (isValidJSIdentifier2(name)) {
490
+ if (isValidJSIdentifier(name)) {
420
491
  fieldTypes.push(`${name}: ${type}`);
421
492
  } else {
422
493
  fieldTypes.push(`'${escapeForSingleQuoteString(name)}': ${type}`);
@@ -427,15 +498,32 @@ var paramsType = (params, isMessagesIndex) => {
427
498
 
428
499
  // src/compiler/optionsType.ts
429
500
  init_define_ENV_DEFINED_IN_BUILD_STEP();
501
+
502
+ // src/services/codegen/string-union.ts
503
+ init_define_ENV_DEFINED_IN_BUILD_STEP();
504
+ function toStringUnion(iterable) {
505
+ return [...iterable].map((item) => `"${escapeForDoubleQuoteString(item)}"`).join(" | ");
506
+ }
507
+
508
+ // src/compiler/optionsType.ts
430
509
  var optionsType = (args) => {
431
- return `@param {{ languageTag?: ${[...args.languageTags].map(quote).join(" | ") ?? "undefined"} }} options`;
510
+ return `@param {{ languageTag?: ${toStringUnion(args.languageTags) ?? "undefined"} }} options`;
432
511
  };
433
- var quote = (str) => `"${str}"`;
512
+
513
+ // src/services/codegen/identifier.ts
514
+ init_define_ENV_DEFINED_IN_BUILD_STEP();
515
+ function i(str) {
516
+ var _a;
517
+ str = str.replaceAll(/[^a-zA-Z0-9_]/g, "_");
518
+ if ((_a = str[0]) == null ? void 0 : _a.match(/[0-9]/)) {
519
+ str = "_" + str;
520
+ }
521
+ return str;
522
+ }
434
523
 
435
524
  // src/compiler/compileMessage.ts
436
- import { isValidJSIdentifier as isValidJSIdentifier3 } from "@inlang/valid-js-identifier";
437
525
  var compileMessage = (message) => {
438
- if (!isValidJSIdentifier3(message.id)) {
526
+ if (!isValidJSIdentifier(message.id)) {
439
527
  throw new Error(
440
528
  `Cannot compile message with ID "${message.id}".
441
529
 
@@ -470,8 +558,8 @@ To detect this issue during linting, use the valid-js-identifier lint rule: http
470
558
  };
471
559
  };
472
560
  var messageIndexFunction = (args) => {
473
- return `
474
- /**
561
+ const hasParams = Object.keys(args.params).length > 0;
562
+ return `/**
475
563
  * This message has been compiled by [inlang paraglide](https://inlang.com/m/gerre34r/library-inlang-paraglideJs).
476
564
  *
477
565
  * - Don't edit the message's code. Use the [inlang ide extension](https://inlang.com/m/r7kp499g/app-inlang-ideExtension),
@@ -484,18 +572,17 @@ var messageIndexFunction = (args) => {
484
572
  * @returns {string}
485
573
  */
486
574
  /* @__NO_SIDE_EFFECTS__ */
487
- export const ${args.message.id} = (params ${Object.keys(args.params).length > 0 ? "" : "= {}"}, options = {}) => {
575
+ export const ${args.message.id} = (params ${hasParams ? "" : "= {}"}, options = {}) => {
576
+ const messageFunction = {
577
+ ${[...args.languageTags].sort((a, b) => a.localeCompare(b)).map((tag) => ` ${isValidJSIdentifier(tag) ? tag : `"${tag}"`}: ${i(tag)}.${args.message.id}`).join(",\n")}
578
+ }[/** @type {${toStringUnion(args.languageTags)}} */ (options.languageTag ?? languageTag())]
488
579
 
489
- const tag = options.languageTag ?? languageTag();
490
- ${[...args.languageTags].sort((a, b) => a.localeCompare(b)).map(
491
- (tag) => ` if (tag === "${tag}") return ${tag.replaceAll("-", "_")}.${args.message.id}(${Object.keys(args.params).length > 0 ? "params" : ""})`
492
- ).join("\n")}
493
580
  // if the language tag does not exist, return undefined
494
581
  //
495
582
  // the missing translation lint rule catches errors like this in CI/CD
496
583
  // see https://inlang.com/m/4cxm3eqi/messageLintRule-inlang-missingTranslation
497
584
  // @ts-expect-error - for better DX treat a message function is always returning a string
498
- return undefined
585
+ return messageFunction ? messageFunction(${hasParams ? "params" : ""}) : undefined;
499
586
  }`;
500
587
  };
501
588
  var messageFunction = (args) => {
@@ -761,10 +848,10 @@ var ZodError = class extends Error {
761
848
  fieldErrors._errors.push(mapper(issue));
762
849
  } else {
763
850
  let curr = fieldErrors;
764
- let i = 0;
765
- while (i < issue.path.length) {
766
- const el = issue.path[i];
767
- const terminal = i === issue.path.length - 1;
851
+ let i2 = 0;
852
+ while (i2 < issue.path.length) {
853
+ const el = issue.path[i2];
854
+ const terminal = i2 === issue.path.length - 1;
768
855
  if (!terminal) {
769
856
  curr[el] = curr[el] || { _errors: [] };
770
857
  } else {
@@ -772,7 +859,7 @@ var ZodError = class extends Error {
772
859
  curr[el]._errors.push(mapper(issue));
773
860
  }
774
861
  curr = curr[el];
775
- i++;
862
+ i2++;
776
863
  }
777
864
  }
778
865
  }
@@ -2489,14 +2576,14 @@ var ZodArray = class _ZodArray extends ZodType {
2489
2576
  }
2490
2577
  }
2491
2578
  if (ctx.common.async) {
2492
- return Promise.all([...ctx.data].map((item, i) => {
2493
- return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i));
2579
+ return Promise.all([...ctx.data].map((item, i2) => {
2580
+ return def.type._parseAsync(new ParseInputLazyPath(ctx, item, ctx.path, i2));
2494
2581
  })).then((result2) => {
2495
2582
  return ParseStatus.mergeArray(status, result2);
2496
2583
  });
2497
2584
  }
2498
- const result = [...ctx.data].map((item, i) => {
2499
- return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i));
2585
+ const result = [...ctx.data].map((item, i2) => {
2586
+ return def.type._parseSync(new ParseInputLazyPath(ctx, item, ctx.path, i2));
2500
2587
  });
2501
2588
  return ParseStatus.mergeArray(status, result);
2502
2589
  }
@@ -3413,7 +3500,7 @@ var ZodSet = class _ZodSet extends ZodType {
3413
3500
  }
3414
3501
  return { status: status.value, value: parsedSet };
3415
3502
  }
3416
- const elements = [...ctx.data.values()].map((item, i) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i)));
3503
+ const elements = [...ctx.data.values()].map((item, i2) => valueType._parse(new ParseInputLazyPath(ctx, item, ctx.path, i2)));
3417
3504
  if (ctx.common.async) {
3418
3505
  return Promise.all(elements).then((elements2) => finalizeSet(elements2));
3419
3506
  } else {
@@ -4415,9 +4502,7 @@ var compile = (args) => {
4415
4502
  "messages.js": `
4416
4503
  /* eslint-disable */
4417
4504
  import { languageTag } from "./runtime.js"
4418
- ${Object.keys(resources).map(
4419
- (languageTag) => `import * as ${languageTag.replaceAll("-", "_")} from "./messages/${languageTag}.js"`
4420
- ).join("\n")}
4505
+ ${Object.keys(resources).map((languageTag) => `import * as ${i(languageTag)} from "./messages/${languageTag}.js"`).join("\n")}
4421
4506
 
4422
4507
  ${compiledMessages.map((message) => message.index).join("\n\n")}
4423
4508
  `,
@@ -6,3 +6,4 @@ export declare function escapeForTemplateLiteral(text: string): string;
6
6
  * Escapes some Text so that it can safely be used inside a single quote string.s
7
7
  */
8
8
  export declare function escapeForSingleQuoteString(text: string): string;
9
+ export declare function escapeForDoubleQuoteString(text: string): string;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Takes in a string and tries to convert it to a valid JS identifier.
3
+ * The output is deterministic.
4
+ *
5
+ * "i" is short for "identifier".
6
+ */
7
+ export declare function i(str: string): string;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare function toStringUnion(iterable: Iterable<string>): string;
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Checks if a string is a valid JS identifier.
3
+ * This is more reliable than just using regexes or `new Function()`.s
4
+ */
5
+ export declare function isValidJSIdentifier(str: string): boolean;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export declare const KEYWORDS: string[];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@inlang/paraglide-js",
3
3
  "type": "module",
4
- "version": "1.0.0-prerelease.21",
4
+ "version": "1.0.0-prerelease.23",
5
5
  "license": "Apache-2.0",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -38,9 +38,8 @@
38
38
  "json5": "2.2.3",
39
39
  "posthog-node": "3.1.3",
40
40
  "@inlang/detect-json-formatting": "1.0.0",
41
- "@inlang/valid-js-identifier": "0.0.1",
42
- "@inlang/sdk": "0.19.0",
43
- "@lix-js/fs": "0.4.0"
41
+ "@lix-js/fs": "0.4.0",
42
+ "@inlang/sdk": "0.20.0"
44
43
  },
45
44
  "devDependencies": {
46
45
  "@rollup/plugin-terser": "0.4.3",
@@ -55,7 +54,7 @@
55
54
  "typescript": "5.2.2",
56
55
  "vitest": "0.34.3",
57
56
  "@inlang/env-variables": "0.1.0",
58
- "@inlang/telemetry": "0.3.0"
57
+ "@inlang/telemetry": "0.3.1"
59
58
  },
60
59
  "exports": {
61
60
  "./internal": {