@inlang/paraglide-js 2.24.1 → 2.25.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 (85) hide show
  1. package/dist/bundler-plugins/esbuild.d.ts +1 -1
  2. package/dist/bundler-plugins/per-locale-build/index.d.ts +3 -3
  3. package/dist/bundler-plugins/per-locale-build/index.js +59 -15
  4. package/dist/bundler-plugins/per-locale-build/index.js.map +1 -1
  5. package/dist/bundler-plugins/rolldown.d.ts +1 -1
  6. package/dist/bundler-plugins/rollup.d.ts +1 -1
  7. package/dist/bundler-plugins/rspack.d.ts +1 -1
  8. package/dist/bundler-plugins/unplugin.d.ts +35 -1
  9. package/dist/bundler-plugins/unplugin.js +404 -123
  10. package/dist/bundler-plugins/unplugin.js.map +1 -1
  11. package/dist/bundler-plugins/unplugin.test.js +357 -202
  12. package/dist/bundler-plugins/unplugin.test.js.map +1 -1
  13. package/dist/bundler-plugins/vite.d.ts +2 -2
  14. package/dist/bundler-plugins/vite.js.map +1 -1
  15. package/dist/bundler-plugins/vite.test.js +2 -1
  16. package/dist/bundler-plugins/vite.test.js.map +1 -1
  17. package/dist/bundler-plugins/webpack.d.ts +1 -1
  18. package/dist/cli/commands/compile/command.js +156 -36
  19. package/dist/cli/commands/compile/command.js.map +1 -1
  20. package/dist/cli/commands/compile/command.test.js +311 -7
  21. package/dist/cli/commands/compile/command.test.js.map +1 -1
  22. package/dist/cli/defaults.d.ts +0 -2
  23. package/dist/cli/defaults.js +0 -2
  24. package/dist/cli/defaults.js.map +1 -1
  25. package/dist/cli/steps/initialize-inlang-project.js +2 -1
  26. package/dist/cli/steps/initialize-inlang-project.js.map +1 -1
  27. package/dist/cli/steps/prompt-for-outdir.js +1 -1
  28. package/dist/cli/steps/prompt-for-outdir.js.map +1 -1
  29. package/dist/compiler/compile-annotation.d.ts +2 -2
  30. package/dist/compiler/compile-annotation.js +9 -9
  31. package/dist/compiler/compile-annotation.js.map +1 -1
  32. package/dist/compiler/compile-local-variable.d.ts +2 -1
  33. package/dist/compiler/compile-local-variable.js +4 -4
  34. package/dist/compiler/compile-local-variable.js.map +1 -1
  35. package/dist/compiler/compile-local-variable.test.js +82 -0
  36. package/dist/compiler/compile-local-variable.test.js.map +1 -1
  37. package/dist/compiler/compile-message.js +10 -2
  38. package/dist/compiler/compile-message.js.map +1 -1
  39. package/dist/compiler/compile-message.test.js +132 -2
  40. package/dist/compiler/compile-message.test.js.map +1 -1
  41. package/dist/compiler/compile-pattern.js +4 -2
  42. package/dist/compiler/compile-pattern.js.map +1 -1
  43. package/dist/compiler/compile-pattern.test.js +31 -0
  44. package/dist/compiler/compile-pattern.test.js.map +1 -1
  45. package/dist/compiler/compiler-options.d.ts +1 -1
  46. package/dist/compiler/compiler-options.js.map +1 -1
  47. package/dist/compiler/server/create-server-file.js +18 -4
  48. package/dist/compiler/server/create-server-file.js.map +1 -1
  49. package/dist/compiler/server/create-server-file.test.js +76 -0
  50. package/dist/compiler/server/create-server-file.test.js.map +1 -1
  51. package/dist/compiler/variable-access.d.ts +2 -0
  52. package/dist/compiler/variable-access.js +6 -0
  53. package/dist/compiler/variable-access.js.map +1 -1
  54. package/dist/index.d.ts +1 -0
  55. package/dist/index.js +1 -0
  56. package/dist/index.js.map +1 -1
  57. package/dist/services/config/config-schema.d.ts +69 -0
  58. package/dist/services/config/config-schema.js +78 -0
  59. package/dist/services/config/config-schema.js.map +1 -0
  60. package/dist/services/config/defaults.d.ts +11 -0
  61. package/dist/services/config/defaults.js +12 -0
  62. package/dist/services/config/defaults.js.map +1 -0
  63. package/dist/services/config/discover-config-file.d.ts +19 -0
  64. package/dist/services/config/discover-config-file.js +39 -0
  65. package/dist/services/config/discover-config-file.js.map +1 -0
  66. package/dist/services/config/index.d.ts +24 -0
  67. package/dist/services/config/index.js +25 -0
  68. package/dist/services/config/index.js.map +1 -0
  69. package/dist/services/config/load-paraglide-config.d.ts +44 -0
  70. package/dist/services/config/load-paraglide-config.js +109 -0
  71. package/dist/services/config/load-paraglide-config.js.map +1 -0
  72. package/dist/services/config/load-paraglide-config.test.d.ts +1 -0
  73. package/dist/services/config/load-paraglide-config.test.js +201 -0
  74. package/dist/services/config/load-paraglide-config.test.js.map +1 -0
  75. package/dist/services/config/resolve-compiler-options.d.ts +24 -0
  76. package/dist/services/config/resolve-compiler-options.js +51 -0
  77. package/dist/services/config/resolve-compiler-options.js.map +1 -0
  78. package/dist/services/config/resolve-compiler-options.test.d.ts +1 -0
  79. package/dist/services/config/resolve-compiler-options.test.js +60 -0
  80. package/dist/services/config/resolve-compiler-options.test.js.map +1 -0
  81. package/dist/services/env-variables/index.js +1 -1
  82. package/dist/services/env-variables/index.js.map +1 -1
  83. package/dist/services/logger/index.js +2 -0
  84. package/dist/services/logger/index.js.map +1 -1
  85. package/package.json +4 -2
@@ -1 +1 @@
1
- {"version":3,"file":"initialize-inlang-project.js","sourceRoot":"","sources":["../../../src/cli/steps/initialize-inlang-project.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,wBAAwB,EACxB,mBAAmB,EACnB,UAAU,EACV,sBAAsB,GAEtB,MAAM,aAAa,CAAC;AAGrB,OAAO,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AAC7E,OAAO,QAAQ,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,MAAM,SAAS,CAAC;AAEzB,MAAM,CAAC,MAAM,uBAAuB,GAYhC,KAAK,EAAE,GAAG,EAAE,EAAE;IACjB,+BAA+B;IAC/B,8DAA8D;IAC9D,sEAAsE;IAEtE,yCAAyC;IACzC,gEAAgE;IAChE,0BAA0B;IAC1B,gBAAgB;IAChB,gBAAgB;IAChB,wBAAwB;IACxB,OAAO;IAEP,YAAY;IACZ,YAAY;IACZ,aAAa;IACb,iBAAiB;IACjB,MAAM;IACN,WAAW;IACX,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,GAAG,MAAM,oBAAoB,CAAC,GAAG,CAAC,CAAC;IACjE,OAAO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,CAAC,CAAC;IACrC,IAAI,IAAI,CAAC,MAAM,IAAI,CAAC;QAAE,OAAO,EAAE,CAAC;IAEhC,MAAM,WAAW,GAAG,IAAI,CAAC,CAAC,CAAC,CAAC;IAC5B,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;QAC/B,OAAO,EAAE,CAAC;IACX,CAAC;IAED,OAAO,IAAI,CAAC,MAAM,CACjB,CAAC,YAAY,EAAE,GAAG,EAAE,EAAE,CAAC,mBAAmB,CAAC,YAAY,EAAE,GAAG,CAAC,EAC7D,WAAW,CACX,CAAC;AACH,CAAC;AAED,SAAS,mBAAmB,CAAC,IAAY,EAAE,IAAY;IACtD,IAAI,YAAY,GAAG,EAAE,CAAC;IACtB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC7D,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;YACzB,YAAY,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC;QACzB,CAAC;aAAM,CAAC;YACP,MAAM;QACP,CAAC;IACF,CAAC;IACD,OAAO,YAAY,CAAC;AACrB,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,OAAO,GACnB,mbAAmb,CAAC;AAErb,qDAAqD;AACrD,sCAAsC","sourcesContent":["import {\n\tloadProjectFromDirectory,\n\tloadProjectInMemory,\n\tnewProject,\n\tsaveProjectToDirectory,\n\ttype InlangProject,\n} from \"@inlang/sdk\";\nimport type { Logger } from \"../../services/logger/index.js\";\nimport type { CliStep } from \"../utils.js\";\nimport { DEFAULT_PROJECT_PATH, getNewProjectTemplate } from \"../defaults.js\";\nimport nodePath from \"node:path\";\nimport fs from \"node:fs\";\n\nexport const initializeInlangProject: CliStep<\n\t{\n\t\tfs: typeof fs.promises;\n\t\tsyncFs: typeof fs;\n\t\tlogger: Logger;\n\t\troot: string;\n\t},\n\t{\n\t\tproject: InlangProject;\n\t\t/** Relative path to the project */\n\t\tprojectPath: string;\n\t}\n> = async (ctx) => {\n\t// const existingProjectPaths =\n\t// \t// checking for undefined if node version is lower than 22\n\t// \ttypeof fs.globSync !== \"undefined\" ? fs.globSync(\"*.inlang\") : [];\n\n\t// if (existingProjectPaths.length > 0) {\n\t// \tconst { project, projectPath } = await existingProjectFlow({\n\t// \t\texistingProjectPaths,\n\t// \t\tfs: ctx.fs,\n\t// \t\tsyncFs: fs,\n\t// \t\tlogger: ctx.logger,\n\t// \t});\n\n\t// \treturn {\n\t// \t\t...ctx,\n\t// \t\tproject,\n\t// \t\tprojectPath,\n\t// \t};\n\t// } else {\n\tconst { project, projectPath } = await createNewProjectFlow(ctx);\n\treturn {\n\t\t...ctx,\n\t\tproject,\n\t\tprojectPath,\n\t};\n\t// }\n};\n\n// export const existingProjectFlow = async (ctx: {\n// \t/** An array of absolute paths to existing projects. */\n// \texistingProjectPaths: string[];\n// \tfs: typeof fs.promises;\n// \tsyncFs: typeof fs;\n// \tlogger: Logger;\n// }): Promise<{ project: InlangProject; projectPath: string }> => {\n// \tconst NEW_PROJECT_VALUE = \"newProject\";\n\n// \tconst commonPrefix = getCommonPrefix(ctx.existingProjectPaths);\n// \tconst options = ctx.existingProjectPaths.map((path) => {\n// \t\treturn {\n// \t\t\tlabel: path.replace(commonPrefix, \"\"),\n// \t\t\tvalue: path,\n// \t\t};\n// \t});\n\n// \tconst selection = (await prompt(\n// \t\t`Do you want to use an existing Inlang Project or create a new one?`,\n// \t\t{\n// \t\t\ttype: \"select\",\n// \t\t\toptions: [\n// \t\t\t\t{ label: \"Create a new project\", value: NEW_PROJECT_VALUE },\n// \t\t\t\t...options,\n// \t\t\t],\n// \t\t}\n// \t)) as unknown as string; // the prompt type is incorrect\n\n// \t//if the user wants to create a new project - create one & use it\n// \tif (selection === NEW_PROJECT_VALUE) return createNewProjectFlow(ctx);\n\n// \tconst projectPath = selection;\n// \tconst project = await loadProjectFromDirectory({\n// \t\tpath: projectPath,\n// \t\tfs: ctx.syncFs,\n// \t});\n\n// \tif ((await project.errors.get()).length > 0) {\n// \t\tctx.logger.error(\n// \t\t\t\"Aborting paragilde initialization. - The selected project has errors. Either fix them, or remove the project and create a new one.\"\n// \t\t);\n// \t\tfor (const error of await project.errors.get()) {\n// \t\t\tctx.logger.error(error);\n// \t\t}\n// \t\tprocess.exit(1);\n// \t}\n\n// \treturn { project, projectPath };\n// };\n\n// function parseLocaleInput(input: string): {\n// \tvalidlocales: string[];\n// \tinvalidlocales: string[];\n// } {\n// \tconst locales = input\n// \t\t.replaceAll(/[,:\\s]/g, \" \") //replace common separators with spaces\n// \t\t.split(\" \")\n// \t\t.filter(Boolean) //remove empty segments\n// \t\t.map((tag) => tag.toLowerCase());\n\n// \tconst validlocales: string[] = [];\n// \tconst invalidlocales: string[] = [];\n\n// \tfor (const tag of locales) {\n// \t\tif (isValidlocale(tag)) {\n// \t\t\tvalidlocales.push(tag);\n// \t\t} else {\n// \t\t\tinvalidlocales.push(tag);\n// \t\t}\n// \t}\n\n// \treturn {\n// \t\tvalidlocales,\n// \t\tinvalidlocales,\n// \t};\n// }\n\n// async function promptForLocales(\n// \tinitialLocales: string[] = []\n// ): Promise<string[]> {\n// \tconst localesInput =\n// \t\t(await prompt(\"Which locales do you want to support?\", {\n// \t\t\ttype: \"text\",\n// \t\t\tplaceholder: \"en, de-ch, ar\",\n// \t\t\tinitial: initialLocales.length ? initialLocales.join(\", \") : undefined,\n// \t\t})) ?? \"\";\n\n// \tconst { invalidlocales: invalidLocales, validlocales } =\n// \t\tparseLocaleInput(localesInput);\n\n// \tif (validlocales.length === 0) {\n// \t\tconsola.warn(\"You must specify at least one locale\");\n// \t\treturn await promptForLocales();\n// \t}\n\n// \tif (invalidLocales.length > 0) {\n// \t\tconst message =\n// \t\t\tinvalidLocales.length === 1\n// \t\t\t\t? invalidLocales[0] +\n// \t\t\t\t\t\" isn't a valid locale. Please stick to IEEE BCP-47 Language Tags\"\n// \t\t\t\t: invalidLocales.map((tag) => `\"${tag}\"`).join(\", \") +\n// \t\t\t\t\t\" aren't valid locales. Please stick to IEEE BCP-47 Language Tags\";\n\n// \t\tconsola.warn(message);\n// \t\treturn await promptForLocales(validlocales);\n// \t}\n\n// \treturn validlocales;\n// }\nexport const createNewProjectFlow = async (ctx: {\n\tfs: typeof fs.promises;\n\tsyncFs: typeof fs;\n\tlogger: Logger;\n}): Promise<{\n\tproject: InlangProject;\n\t/** An absolute path to the created project */\n\tprojectPath: string;\n}> => {\n\tconst settings = getNewProjectTemplate();\n\n\tconst messagePath = settings[\"plugin.inlang.messageFormat\"].pathPattern;\n\n\t//create the messages dir if it doesn't exist\n\tconst messageDir = nodePath.dirname(\n\t\tnodePath.resolve(process.cwd(), messagePath)\n\t);\n\tawait ctx.fs.mkdir(messageDir, { recursive: true });\n\n\tawait Promise.allSettled(\n\t\tsettings.locales.map(async (locale) => {\n\t\t\tconst languageFile = nodePath.resolve(messageDir, locale + \".json\");\n\t\t\tconst exists = await ctx.fs\n\t\t\t\t.access(languageFile)\n\t\t\t\t.then(() => true)\n\t\t\t\t.catch(() => false);\n\t\t\tif (exists) return;\n\t\t\tawait ctx.fs.writeFile(\n\t\t\t\tlanguageFile,\n\t\t\t\tJSON.stringify(\n\t\t\t\t\t{\n\t\t\t\t\t\t$schema: \"https://inlang.com/schema/inlang-message-format\",\n\t\t\t\t\t\t// add an example message for instant aha moment\n\t\t\t\t\t\texample_message:\n\t\t\t\t\t\t\tlocale === \"en\"\n\t\t\t\t\t\t\t\t? \"Hello world {username}\"\n\t\t\t\t\t\t\t\t: \"Guten Tag {username}\",\n\t\t\t\t\t},\n\t\t\t\t\tnull,\n\t\t\t\t\t2\n\t\t\t\t)\n\t\t\t);\n\t\t})\n\t);\n\n\tconst projectPath = DEFAULT_PROJECT_PATH;\n\n\t//create default project\n\tawait ctx.fs.mkdir(projectPath, { recursive: true });\n\n\tconst tempProject = await loadProjectInMemory({\n\t\tblob: await newProject({\n\t\t\tsettings,\n\t\t}),\n\t});\n\n\tawait saveProjectToDirectory({\n\t\tproject: tempProject,\n\t\tfs: ctx.fs,\n\t\tpath: projectPath,\n\t});\n\n\tconst project = await loadProjectFromDirectory({\n\t\tpath: projectPath,\n\t\tfs: ctx.syncFs,\n\t});\n\n\tif ((await project.errors.get()).length > 0) {\n\t\tctx.logger.error(\n\t\t\t\"Failed to create a new inlang project.\\n\\nThis is likely an internal bug. Please file an issue at https://github.com/opral/inlang-paraglide-js.\"\n\t\t);\n\t\tfor (const error of await project.errors.get()) {\n\t\t\tctx.logger.error(error);\n\t\t}\n\t\treturn process.exit(1);\n\t} else {\n\t\tctx.logger.success(\"Created a new inlang project.\");\n\t}\n\treturn { project, projectPath };\n};\n\n/**\n * Get's the common prefix of a set of strings.\n * If only one string is passed the prefix will be the empty string\n *\n * @example\n * ```ts\n * getCommonPrefix([\"foo\", \"foobar\"]) // \"foo\"\n * getCommonPrefix([\"foobar\"]) // \"\"\n * ```\n */\nexport function getCommonPrefix(strings: string[]): string {\n\tconst strs = strings.filter(Boolean);\n\tif (strs.length <= 1) return \"\";\n\n\tconst firstString = strs[0];\n\tif (firstString === undefined) {\n\t\treturn \"\";\n\t}\n\n\treturn strs.reduce(\n\t\t(commonPrefix, str) => longestCommonPrefix(commonPrefix, str),\n\t\tfirstString\n\t);\n}\n\nfunction longestCommonPrefix(strA: string, strB: string): string {\n\tlet commonPrefix = \"\";\n\tfor (let i = 0; i < Math.min(strA.length, strB.length); i++) {\n\t\tif (strA[i] === strB[i]) {\n\t\t\tcommonPrefix += strA[i];\n\t\t} else {\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn commonPrefix;\n}\n\n/**\n * Follows the IETF BCP 47 locale schema with modifications.\n */\nexport const pattern =\n\t\"^((?<grandfathered>(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)|(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang))|((?<language>([A-Za-z]{2,3}(-(?<extlang>[A-Za-z]{3}(-[A-Za-z]{3}){0,2}))?))(-(?<script>[A-Za-z]{4}))?(-(?<region>[A-Za-z]{2}|[0-9]{3}))?(-(?<variant>[A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3}))*))$\";\n\n// const isValidlocale = (locale: string): boolean =>\n// \tRegExp(`${pattern}`).test(locale);\n"]}
1
+ {"version":3,"file":"initialize-inlang-project.js","sourceRoot":"","sources":["../../../src/cli/steps/initialize-inlang-project.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,wBAAwB,EACxB,mBAAmB,EACnB,UAAU,EACV,sBAAsB,GAEtB,MAAM,aAAa,CAAC;AAGrB,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAC;AACvD,OAAO,EAAE,oBAAoB,EAAE,MAAM,mCAAmC,CAAC;AACzE,OAAO,QAAQ,MAAM,WAAW,CAAC;AACjC,OAAO,EAAE,MAAM,SAAS,CAAC;AAEzB,MAAM,CAAC,MAAM,uBAAuB,GAYhC,KAAK,EAAE,GAAG,EAAE,EAAE;IACjB,+BAA+B;IAC/B,8DAA8D;IAC9D,sEAAsE;IAEtE,yCAAyC;IACzC,gEAAgE;IAChE,0BAA0B;IAC1B,gBAAgB;IAChB,gBAAgB;IAChB,wBAAwB;IACxB,OAAO;IAEP,YAAY;IACZ,YAAY;IACZ,aAAa;IACb,iBAAiB;IACjB,MAAM;IACN,WAAW;IACX,MAAM,EAAE,OAAO,EAAE,WAAW,EAAE,GAAG,MAAM,oBAAoB,CAAC,GAAG,CAAC,CAAC;IACjE,OAAO;QACN,GAAG,GAAG;QACN,OAAO;QACP,WAAW;KACX,CAAC;IACF,IAAI;AACL,CAAC,CAAC;AAEF,mDAAmD;AACnD,2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AAG,IAAI,CAAC,CAAC,CAAC,CAAC;IAC5B,IAAI,WAAW,KAAK,SAAS,EAAE,CAAC;QAC/B,OAAO,EAAE,CAAC;IACX,CAAC;IAED,OAAO,IAAI,CAAC,MAAM,CACjB,CAAC,YAAY,EAAE,GAAG,EAAE,EAAE,CAAC,mBAAmB,CAAC,YAAY,EAAE,GAAG,CAAC,EAC7D,WAAW,CACX,CAAC;AACH,CAAC;AAED,SAAS,mBAAmB,CAAC,IAAY,EAAE,IAAY;IACtD,IAAI,YAAY,GAAG,EAAE,CAAC;IACtB,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,GAAG,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC;QAC7D,IAAI,IAAI,CAAC,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC;YACzB,YAAY,IAAI,IAAI,CAAC,CAAC,CAAC,CAAC;QACzB,CAAC;aAAM,CAAC;YACP,MAAM;QACP,CAAC;IACF,CAAC;IACD,OAAO,YAAY,CAAC;AACrB,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,MAAM,OAAO,GACnB,mbAAmb,CAAC;AAErb,qDAAqD;AACrD,sCAAsC","sourcesContent":["import {\n\tloadProjectFromDirectory,\n\tloadProjectInMemory,\n\tnewProject,\n\tsaveProjectToDirectory,\n\ttype InlangProject,\n} from \"@inlang/sdk\";\nimport type { Logger } from \"../../services/logger/index.js\";\nimport type { CliStep } from \"../utils.js\";\nimport { getNewProjectTemplate } from \"../defaults.js\";\nimport { DEFAULT_PROJECT_PATH } from \"../../services/config/defaults.js\";\nimport nodePath from \"node:path\";\nimport fs from \"node:fs\";\n\nexport const initializeInlangProject: CliStep<\n\t{\n\t\tfs: typeof fs.promises;\n\t\tsyncFs: typeof fs;\n\t\tlogger: Logger;\n\t\troot: string;\n\t},\n\t{\n\t\tproject: InlangProject;\n\t\t/** Relative path to the project */\n\t\tprojectPath: string;\n\t}\n> = async (ctx) => {\n\t// const existingProjectPaths =\n\t// \t// checking for undefined if node version is lower than 22\n\t// \ttypeof fs.globSync !== \"undefined\" ? fs.globSync(\"*.inlang\") : [];\n\n\t// if (existingProjectPaths.length > 0) {\n\t// \tconst { project, projectPath } = await existingProjectFlow({\n\t// \t\texistingProjectPaths,\n\t// \t\tfs: ctx.fs,\n\t// \t\tsyncFs: fs,\n\t// \t\tlogger: ctx.logger,\n\t// \t});\n\n\t// \treturn {\n\t// \t\t...ctx,\n\t// \t\tproject,\n\t// \t\tprojectPath,\n\t// \t};\n\t// } else {\n\tconst { project, projectPath } = await createNewProjectFlow(ctx);\n\treturn {\n\t\t...ctx,\n\t\tproject,\n\t\tprojectPath,\n\t};\n\t// }\n};\n\n// export const existingProjectFlow = async (ctx: {\n// \t/** An array of absolute paths to existing projects. */\n// \texistingProjectPaths: string[];\n// \tfs: typeof fs.promises;\n// \tsyncFs: typeof fs;\n// \tlogger: Logger;\n// }): Promise<{ project: InlangProject; projectPath: string }> => {\n// \tconst NEW_PROJECT_VALUE = \"newProject\";\n\n// \tconst commonPrefix = getCommonPrefix(ctx.existingProjectPaths);\n// \tconst options = ctx.existingProjectPaths.map((path) => {\n// \t\treturn {\n// \t\t\tlabel: path.replace(commonPrefix, \"\"),\n// \t\t\tvalue: path,\n// \t\t};\n// \t});\n\n// \tconst selection = (await prompt(\n// \t\t`Do you want to use an existing Inlang Project or create a new one?`,\n// \t\t{\n// \t\t\ttype: \"select\",\n// \t\t\toptions: [\n// \t\t\t\t{ label: \"Create a new project\", value: NEW_PROJECT_VALUE },\n// \t\t\t\t...options,\n// \t\t\t],\n// \t\t}\n// \t)) as unknown as string; // the prompt type is incorrect\n\n// \t//if the user wants to create a new project - create one & use it\n// \tif (selection === NEW_PROJECT_VALUE) return createNewProjectFlow(ctx);\n\n// \tconst projectPath = selection;\n// \tconst project = await loadProjectFromDirectory({\n// \t\tpath: projectPath,\n// \t\tfs: ctx.syncFs,\n// \t});\n\n// \tif ((await project.errors.get()).length > 0) {\n// \t\tctx.logger.error(\n// \t\t\t\"Aborting paragilde initialization. - The selected project has errors. Either fix them, or remove the project and create a new one.\"\n// \t\t);\n// \t\tfor (const error of await project.errors.get()) {\n// \t\t\tctx.logger.error(error);\n// \t\t}\n// \t\tprocess.exit(1);\n// \t}\n\n// \treturn { project, projectPath };\n// };\n\n// function parseLocaleInput(input: string): {\n// \tvalidlocales: string[];\n// \tinvalidlocales: string[];\n// } {\n// \tconst locales = input\n// \t\t.replaceAll(/[,:\\s]/g, \" \") //replace common separators with spaces\n// \t\t.split(\" \")\n// \t\t.filter(Boolean) //remove empty segments\n// \t\t.map((tag) => tag.toLowerCase());\n\n// \tconst validlocales: string[] = [];\n// \tconst invalidlocales: string[] = [];\n\n// \tfor (const tag of locales) {\n// \t\tif (isValidlocale(tag)) {\n// \t\t\tvalidlocales.push(tag);\n// \t\t} else {\n// \t\t\tinvalidlocales.push(tag);\n// \t\t}\n// \t}\n\n// \treturn {\n// \t\tvalidlocales,\n// \t\tinvalidlocales,\n// \t};\n// }\n\n// async function promptForLocales(\n// \tinitialLocales: string[] = []\n// ): Promise<string[]> {\n// \tconst localesInput =\n// \t\t(await prompt(\"Which locales do you want to support?\", {\n// \t\t\ttype: \"text\",\n// \t\t\tplaceholder: \"en, de-ch, ar\",\n// \t\t\tinitial: initialLocales.length ? initialLocales.join(\", \") : undefined,\n// \t\t})) ?? \"\";\n\n// \tconst { invalidlocales: invalidLocales, validlocales } =\n// \t\tparseLocaleInput(localesInput);\n\n// \tif (validlocales.length === 0) {\n// \t\tconsola.warn(\"You must specify at least one locale\");\n// \t\treturn await promptForLocales();\n// \t}\n\n// \tif (invalidLocales.length > 0) {\n// \t\tconst message =\n// \t\t\tinvalidLocales.length === 1\n// \t\t\t\t? invalidLocales[0] +\n// \t\t\t\t\t\" isn't a valid locale. Please stick to IEEE BCP-47 Language Tags\"\n// \t\t\t\t: invalidLocales.map((tag) => `\"${tag}\"`).join(\", \") +\n// \t\t\t\t\t\" aren't valid locales. Please stick to IEEE BCP-47 Language Tags\";\n\n// \t\tconsola.warn(message);\n// \t\treturn await promptForLocales(validlocales);\n// \t}\n\n// \treturn validlocales;\n// }\nexport const createNewProjectFlow = async (ctx: {\n\tfs: typeof fs.promises;\n\tsyncFs: typeof fs;\n\tlogger: Logger;\n}): Promise<{\n\tproject: InlangProject;\n\t/** An absolute path to the created project */\n\tprojectPath: string;\n}> => {\n\tconst settings = getNewProjectTemplate();\n\n\tconst messagePath = settings[\"plugin.inlang.messageFormat\"].pathPattern;\n\n\t//create the messages dir if it doesn't exist\n\tconst messageDir = nodePath.dirname(\n\t\tnodePath.resolve(process.cwd(), messagePath)\n\t);\n\tawait ctx.fs.mkdir(messageDir, { recursive: true });\n\n\tawait Promise.allSettled(\n\t\tsettings.locales.map(async (locale) => {\n\t\t\tconst languageFile = nodePath.resolve(messageDir, locale + \".json\");\n\t\t\tconst exists = await ctx.fs\n\t\t\t\t.access(languageFile)\n\t\t\t\t.then(() => true)\n\t\t\t\t.catch(() => false);\n\t\t\tif (exists) return;\n\t\t\tawait ctx.fs.writeFile(\n\t\t\t\tlanguageFile,\n\t\t\t\tJSON.stringify(\n\t\t\t\t\t{\n\t\t\t\t\t\t$schema: \"https://inlang.com/schema/inlang-message-format\",\n\t\t\t\t\t\t// add an example message for instant aha moment\n\t\t\t\t\t\texample_message:\n\t\t\t\t\t\t\tlocale === \"en\"\n\t\t\t\t\t\t\t\t? \"Hello world {username}\"\n\t\t\t\t\t\t\t\t: \"Guten Tag {username}\",\n\t\t\t\t\t},\n\t\t\t\t\tnull,\n\t\t\t\t\t2\n\t\t\t\t)\n\t\t\t);\n\t\t})\n\t);\n\n\tconst projectPath = DEFAULT_PROJECT_PATH;\n\n\t//create default project\n\tawait ctx.fs.mkdir(projectPath, { recursive: true });\n\n\tconst tempProject = await loadProjectInMemory({\n\t\tblob: await newProject({\n\t\t\tsettings,\n\t\t}),\n\t});\n\n\tawait saveProjectToDirectory({\n\t\tproject: tempProject,\n\t\tfs: ctx.fs,\n\t\tpath: projectPath,\n\t});\n\n\tconst project = await loadProjectFromDirectory({\n\t\tpath: projectPath,\n\t\tfs: ctx.syncFs,\n\t});\n\n\tif ((await project.errors.get()).length > 0) {\n\t\tctx.logger.error(\n\t\t\t\"Failed to create a new inlang project.\\n\\nThis is likely an internal bug. Please file an issue at https://github.com/opral/inlang-paraglide-js.\"\n\t\t);\n\t\tfor (const error of await project.errors.get()) {\n\t\t\tctx.logger.error(error);\n\t\t}\n\t\treturn process.exit(1);\n\t} else {\n\t\tctx.logger.success(\"Created a new inlang project.\");\n\t}\n\treturn { project, projectPath };\n};\n\n/**\n * Get's the common prefix of a set of strings.\n * If only one string is passed the prefix will be the empty string\n *\n * @example\n * ```ts\n * getCommonPrefix([\"foo\", \"foobar\"]) // \"foo\"\n * getCommonPrefix([\"foobar\"]) // \"\"\n * ```\n */\nexport function getCommonPrefix(strings: string[]): string {\n\tconst strs = strings.filter(Boolean);\n\tif (strs.length <= 1) return \"\";\n\n\tconst firstString = strs[0];\n\tif (firstString === undefined) {\n\t\treturn \"\";\n\t}\n\n\treturn strs.reduce(\n\t\t(commonPrefix, str) => longestCommonPrefix(commonPrefix, str),\n\t\tfirstString\n\t);\n}\n\nfunction longestCommonPrefix(strA: string, strB: string): string {\n\tlet commonPrefix = \"\";\n\tfor (let i = 0; i < Math.min(strA.length, strB.length); i++) {\n\t\tif (strA[i] === strB[i]) {\n\t\t\tcommonPrefix += strA[i];\n\t\t} else {\n\t\t\tbreak;\n\t\t}\n\t}\n\treturn commonPrefix;\n}\n\n/**\n * Follows the IETF BCP 47 locale schema with modifications.\n */\nexport const pattern =\n\t\"^((?<grandfathered>(en-GB-oed|i-ami|i-bnn|i-default|i-enochian|i-hak|i-klingon|i-lux|i-mingo|i-navajo|i-pwn|i-tao|i-tay|i-tsu|sgn-BE-FR|sgn-BE-NL|sgn-CH-DE)|(art-lojban|cel-gaulish|no-bok|no-nyn|zh-guoyu|zh-hakka|zh-min|zh-min-nan|zh-xiang))|((?<language>([A-Za-z]{2,3}(-(?<extlang>[A-Za-z]{3}(-[A-Za-z]{3}){0,2}))?))(-(?<script>[A-Za-z]{4}))?(-(?<region>[A-Za-z]{2}|[0-9]{3}))?(-(?<variant>[A-Za-z0-9]{5,8}|[0-9][A-Za-z0-9]{3}))*))$\";\n\n// const isValidlocale = (locale: string): boolean =>\n// \tRegExp(`${pattern}`).test(locale);\n"]}
@@ -1,4 +1,4 @@
1
- import { DEFAULT_OUTDIR } from "../defaults.js";
1
+ import { DEFAULT_OUTDIR } from "../../services/config/defaults.js";
2
2
  import { prompt } from "../utils.js";
3
3
  export const promptForOutdir = async (ctx) => {
4
4
  const response = await prompt("Which outdir should Paraglide write generated files to?", {
@@ -1 +1 @@
1
- {"version":3,"file":"prompt-for-outdir.js","sourceRoot":"","sources":["../../../src/cli/steps/prompt-for-outdir.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAChD,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,MAAM,CAAC,MAAM,eAAe,GAQxB,KAAK,EAAE,GAAG,EAAE,EAAE;IACjB,MAAM,QAAQ,GAAG,MAAM,MAAM,CAC5B,yDAAyD,EACzD;QACC,IAAI,EAAE,MAAM;QACZ,OAAO,EAAE,cAAc;QACvB,OAAO,EAAE,cAAc;QACvB,WAAW,EACV,8EAA8E;KAC/E,CACD,CAAC;IAEF,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QAChC,GAAG,CAAC,MAAM,CAAC,IAAI,CACd,sEAAsE,CACtE,CAAC;QACF,OAAO,MAAM,eAAe,CAAC,GAAG,CAAC,CAAC;IACnC,CAAC;IAED,OAAO;QACN,GAAG,GAAG;QACN,MAAM,EAAE,QAAQ;KAChB,CAAC;AACH,CAAC,CAAC","sourcesContent":["import type { Logger } from \"../../services/logger/index.js\";\nimport { DEFAULT_OUTDIR } from \"../defaults.js\";\nimport { prompt } from \"../utils.js\";\nimport type { CliStep } from \"../utils.js\";\n\nexport const promptForOutdir: CliStep<\n\t{\n\t\tlogger: Logger;\n\t},\n\t{\n\t\t/** Relative path to the output directory */\n\t\toutdir: string;\n\t}\n> = async (ctx) => {\n\tconst response = await prompt(\n\t\t\"Which outdir should Paraglide write generated files to?\",\n\t\t{\n\t\t\ttype: \"text\",\n\t\t\tinitial: DEFAULT_OUTDIR,\n\t\t\tdefault: DEFAULT_OUTDIR,\n\t\t\tplaceholder:\n\t\t\t\t\"Relative path from the package root to the desired compiler output directory\",\n\t\t}\n\t);\n\n\tif (!response.startsWith(\"./\")) {\n\t\tctx.logger.warn(\n\t\t\t\"You must enter a valid relative path starting from the package root.\"\n\t\t);\n\t\treturn await promptForOutdir(ctx);\n\t}\n\n\treturn {\n\t\t...ctx,\n\t\toutdir: response,\n\t};\n};\n"]}
1
+ {"version":3,"file":"prompt-for-outdir.js","sourceRoot":"","sources":["../../../src/cli/steps/prompt-for-outdir.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AAGrC,MAAM,CAAC,MAAM,eAAe,GAQxB,KAAK,EAAE,GAAG,EAAE,EAAE;IACjB,MAAM,QAAQ,GAAG,MAAM,MAAM,CAC5B,yDAAyD,EACzD;QACC,IAAI,EAAE,MAAM;QACZ,OAAO,EAAE,cAAc;QACvB,OAAO,EAAE,cAAc;QACvB,WAAW,EACV,8EAA8E;KAC/E,CACD,CAAC;IAEF,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QAChC,GAAG,CAAC,MAAM,CAAC,IAAI,CACd,sEAAsE,CACtE,CAAC;QACF,OAAO,MAAM,eAAe,CAAC,GAAG,CAAC,CAAC;IACnC,CAAC;IAED,OAAO;QACN,GAAG,GAAG;QACN,MAAM,EAAE,QAAQ;KAChB,CAAC;AACH,CAAC,CAAC","sourcesContent":["import type { Logger } from \"../../services/logger/index.js\";\nimport { DEFAULT_OUTDIR } from \"../../services/config/defaults.js\";\nimport { prompt } from \"../utils.js\";\nimport type { CliStep } from \"../utils.js\";\n\nexport const promptForOutdir: CliStep<\n\t{\n\t\tlogger: Logger;\n\t},\n\t{\n\t\t/** Relative path to the output directory */\n\t\toutdir: string;\n\t}\n> = async (ctx) => {\n\tconst response = await prompt(\n\t\t\"Which outdir should Paraglide write generated files to?\",\n\t\t{\n\t\t\ttype: \"text\",\n\t\t\tinitial: DEFAULT_OUTDIR,\n\t\t\tdefault: DEFAULT_OUTDIR,\n\t\t\tplaceholder:\n\t\t\t\t\"Relative path from the package root to the desired compiler output directory\",\n\t\t}\n\t);\n\n\tif (!response.startsWith(\"./\")) {\n\t\tctx.logger.warn(\n\t\t\t\"You must enter a valid relative path starting from the package root.\"\n\t\t);\n\t\treturn await promptForOutdir(ctx);\n\t}\n\n\treturn {\n\t\t...ctx,\n\t\toutdir: response,\n\t};\n};\n"]}
@@ -1,4 +1,4 @@
1
- import type { FunctionReference } from "@inlang/sdk";
1
+ import type { Declaration, FunctionReference } from "@inlang/sdk";
2
2
  export declare function isRegistryFunction(name: string): boolean;
3
3
  export declare function registryFunctionNamesForDisplay(): string;
4
4
  /**
@@ -9,4 +9,4 @@ export declare function registryFunctionNamesForDisplay(): string;
9
9
  * compileAnnotation("i?.count", "en", { type: "function-reference", name: "number", options: [] })
10
10
  * >> 'registry.number("en", i?.count, {})'
11
11
  */
12
- export declare function compileAnnotation(str: string, locale: string, annotation?: FunctionReference): string;
12
+ export declare function compileAnnotation(str: string, locale: string, annotation?: FunctionReference, declarations?: Declaration[]): string;
@@ -1,4 +1,4 @@
1
- import { compileInputAccess } from "./variable-access.js";
1
+ import { compileVariableAccess } from "./variable-access.js";
2
2
  import { escapeForDoubleQuoteString } from "../services/codegen/escape.js";
3
3
  /**
4
4
  * The functions shipped in the generated registry.js file.
@@ -25,20 +25,20 @@ export function registryFunctionNamesForDisplay() {
25
25
  * compileAnnotation("i?.count", "en", { type: "function-reference", name: "number", options: [] })
26
26
  * >> 'registry.number("en", i?.count, {})'
27
27
  */
28
- export function compileAnnotation(str, locale, annotation) {
28
+ export function compileAnnotation(str, locale, annotation, declarations) {
29
29
  if (!annotation) {
30
30
  return str;
31
31
  }
32
32
  if (annotation.name === "relativetime") {
33
33
  validateRelativeTimeOptions(annotation);
34
34
  }
35
- return `registry.${annotation.name}("${locale}", ${str}, ${compileOptions(annotation.name, annotation.options)})`;
35
+ return `registry.${annotation.name}("${locale}", ${str}, ${compileOptions(annotation.name, annotation.options, declarations)})`;
36
36
  }
37
- function compileOptions(annotationName, options) {
37
+ function compileOptions(annotationName, options, declarations) {
38
38
  if (options.length === 0) {
39
39
  return "{}";
40
40
  }
41
- const entries = options.map((option) => `${option.name}: ${compileOptionLiteralOrVarRef(annotationName, option.name, option.value)}`);
41
+ const entries = options.map((option) => `${option.name}: ${compileOptionLiteralOrVarRef(annotationName, option.name, option.value, declarations)}`);
42
42
  const code = "{ " + entries.join(", ") + " }";
43
43
  return code;
44
44
  }
@@ -65,17 +65,17 @@ const booleanOptionNamesByAnnotation = {
65
65
  datetime: new Set(["hour12"]),
66
66
  };
67
67
  const jsNonNegativeIntegerPattern = /^(?:0|[1-9]\d*)$/;
68
- function compileOptionLiteralOrVarRef(annotationName, optionName, value) {
68
+ function compileOptionLiteralOrVarRef(annotationName, optionName, value, declarations) {
69
69
  if (value.type === "variable-reference") {
70
70
  if (annotationName === "relativetime" && optionName === "unit") {
71
- return `/** @type {import("../registry.js").RelativeTimeFormatUnit} */ (${compileInputAccess(value.name)})`;
71
+ return `/** @type {import("../registry.js").RelativeTimeFormatUnit} */ (${compileVariableAccess(value.name, declarations)})`;
72
72
  }
73
- return compileInputAccess(value.name);
73
+ return compileVariableAccess(value.name, declarations);
74
74
  }
75
75
  if (annotationName === "relativetime" &&
76
76
  optionName === "unit" &&
77
77
  isDollarVariableReference(value.value)) {
78
- return `/** @type {import("../registry.js").RelativeTimeFormatUnit} */ (${compileInputAccess(value.value.slice(1))})`;
78
+ return `/** @type {import("../registry.js").RelativeTimeFormatUnit} */ (${compileVariableAccess(value.value.slice(1), declarations)})`;
79
79
  }
80
80
  if (shouldEmitNumberLiteral(annotationName, optionName, value.value)) {
81
81
  return value.value;
@@ -1 +1 @@
1
- {"version":3,"file":"compile-annotation.js","sourceRoot":"","sources":["../../src/compiler/compile-annotation.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,0BAA0B,EAAE,MAAM,+BAA+B,CAAC;AAE3E;;;;GAIG;AACH,MAAM,qBAAqB,GAAG,IAAI,GAAG,CAAC;IACrC,QAAQ;IACR,QAAQ;IACR,UAAU;IACV,cAAc;CACd,CAAC,CAAC;AAEH,MAAM,UAAU,kBAAkB,CAAC,IAAY;IAC9C,OAAO,qBAAqB,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;AACxC,CAAC;AAED,MAAM,UAAU,+BAA+B;IAC9C,OAAO,KAAK,CAAC,IAAI,CAAC,qBAAqB,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACrD,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,iBAAiB,CAChC,GAAW,EACX,MAAc,EACd,UAA8B;IAE9B,IAAI,CAAC,UAAU,EAAE,CAAC;QACjB,OAAO,GAAG,CAAC;IACZ,CAAC;IACD,IAAI,UAAU,CAAC,IAAI,KAAK,cAAc,EAAE,CAAC;QACxC,2BAA2B,CAAC,UAAU,CAAC,CAAC;IACzC,CAAC;IACD,OAAO,YAAY,UAAU,CAAC,IAAI,KAAK,MAAM,MAAM,GAAG,KAAK,cAAc,CAAC,UAAU,CAAC,IAAI,EAAE,UAAU,CAAC,OAAO,CAAC,GAAG,CAAC;AACnH,CAAC;AAED,SAAS,cAA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ference,\n} from \"@inlang/sdk\";\nimport { compileInputAccess } from \"./variable-access.js\";\nimport { escapeForDoubleQuoteString } from \"../services/codegen/escape.js\";\n\n/**\n * The functions shipped in the generated registry.js file.\n *\n * @see createRegistry()\n */\nconst registryFunctionNames = new Set([\n\t\"plural\",\n\t\"number\",\n\t\"datetime\",\n\t\"relativetime\",\n]);\n\nexport function isRegistryFunction(name: string): boolean {\n\treturn registryFunctionNames.has(name);\n}\n\nexport function registryFunctionNamesForDisplay(): string {\n\treturn Array.from(registryFunctionNames).join(\", \");\n}\n\n/**\n * Wraps a compiled expression value in a `registry.*` call if an\n * annotation is present.\n *\n * @example\n * compileAnnotation(\"i?.count\", \"en\", { type: \"function-reference\", name: \"number\", options: [] })\n * >> 'registry.number(\"en\", i?.count, {})'\n */\nexport function compileAnnotation(\n\tstr: string,\n\tlocale: string,\n\tannotation?: FunctionReference\n): string {\n\tif (!annotation) {\n\t\treturn str;\n\t}\n\tif (annotation.name === \"relativetime\") {\n\t\tvalidateRelativeTimeOptions(annotation);\n\t}\n\treturn `registry.${annotation.name}(\"${locale}\", ${str}, ${compileOptions(annotation.name, annotation.options)})`;\n}\n\nfunction compileOptions(\n\tannotationName: string,\n\toptions: FunctionReference[\"options\"]\n): string {\n\tif (options.length === 0) {\n\t\treturn \"{}\";\n\t}\n\tconst entries: string[] = options.map(\n\t\t(option) =>\n\t\t\t`${option.name}: ${compileOptionLiteralOrVarRef(\n\t\t\t\tannotationName,\n\t\t\t\toption.name,\n\t\t\t\toption.value\n\t\t\t)}`\n\t);\n\tconst code = \"{ \" + entries.join(\", \") + \" }\";\n\n\treturn code;\n}\n\nconst numericOptionNamesByAnnotation: Record<string, ReadonlySet<string>> = {\n\tnumber: new Set([\n\t\t\"minimumIntegerDigits\",\n\t\t\"minimumFractionDigits\",\n\t\t\"maximumFractionDigits\",\n\t\t\"minimumSignificantDigits\",\n\t\t\"maximumSignificantDigits\",\n\t\t\"roundingIncrement\",\n\t]),\n\tplural: new Set([\n\t\t\"minimumIntegerDigits\",\n\t\t\"minimumFractionDigits\",\n\t\t\"maximumFractionDigits\",\n\t\t\"minimumSignificantDigits\",\n\t\t\"maximumSignificantDigits\",\n\t]),\n\tdatetime: new Set([\"fractionalSecondDigits\"]),\n};\n\nconst booleanOptionNamesByAnnotation: Record<string, ReadonlySet<string>> = {\n\tnumber: new Set([\"useGrouping\"]),\n\tdatetime: new Set([\"hour12\"]),\n};\n\nconst jsNonNegativeIntegerPattern = /^(?:0|[1-9]\\d*)$/;\n\nfunction compileOptionLiteralOrVarRef(\n\tannotationName: string,\n\toptionName: string,\n\tvalue: Literal | VariableReference\n): string {\n\tif (value.type === \"variable-reference\") {\n\t\tif (annotationName === \"relativetime\" && optionName === \"unit\") {\n\t\t\treturn `/** @type {import(\"../registry.js\").RelativeTimeFormatUnit} */ (${compileInputAccess(value.name)})`;\n\t\t}\n\t\treturn compileInputAccess(value.name);\n\t}\n\n\tif (\n\t\tannotationName === \"relativetime\" &&\n\t\toptionName === \"unit\" &&\n\t\tisDollarVariableReference(value.value)\n\t) {\n\t\treturn `/** @type {import(\"../registry.js\").RelativeTimeFormatUnit} */ (${compileInputAccess(value.value.slice(1))})`;\n\t}\n\n\tif (shouldEmitNumberLiteral(annotationName, optionName, value.value)) {\n\t\treturn value.value;\n\t}\n\n\tif (shouldEmitBooleanLiteral(annotationName, optionName, value.value)) {\n\t\treturn value.value;\n\t}\n\n\treturn `\"${escapeForDoubleQuoteString(value.value)}\"`;\n}\n\nfunction shouldEmitNumberLiteral(\n\tannotationName: string,\n\toptionName: string,\n\tliteral: string\n): boolean {\n\treturn (\n\t\tnumericOptionNamesByAnnotation[annotationName]?.has(optionName) === true &&\n\t\tjsNonNegativeIntegerPattern.test(literal)\n\t);\n}\n\nfunction shouldEmitBooleanLiteral(\n\tannotationName: string,\n\toptionName: string,\n\tliteral: string\n): boolean {\n\treturn (\n\t\tbooleanOptionNamesByAnnotation[annotationName]?.has(optionName) === true &&\n\t\t(literal === \"true\" || literal === \"false\")\n\t);\n}\n\nconst relativeTimeUnits = new Set([\n\t\"year\",\n\t\"years\",\n\t\"quarter\",\n\t\"quarters\",\n\t\"month\",\n\t\"months\",\n\t\"week\",\n\t\"weeks\",\n\t\"day\",\n\t\"days\",\n\t\"hour\",\n\t\"hours\",\n\t\"minute\",\n\t\"minutes\",\n\t\"second\",\n\t\"seconds\",\n]);\n\nfunction validateRelativeTimeOptions(annotation: FunctionReference): void {\n\tconst unitOptions = annotation.options.filter(\n\t\t(option) => option.name === \"unit\"\n\t);\n\n\tif (unitOptions.length === 0) {\n\t\tthrow new Error('The \"relativetime\" formatter requires a \"unit\" option.');\n\t}\n\n\tif (unitOptions.length > 1) {\n\t\tthrow new Error(\n\t\t\t'The \"relativetime\" formatter requires exactly one \"unit\" option.'\n\t\t);\n\t}\n\n\tconst [unitOption] = unitOptions;\n\tif (!unitOption || unitOption.value.type === \"variable-reference\") {\n\t\treturn;\n\t}\n\n\tif (isDollarVariableReference(unitOption.value.value)) {\n\t\treturn;\n\t}\n\n\tif (!relativeTimeUnits.has(unitOption.value.value)) {\n\t\tthrow new Error(\n\t\t\t`Invalid \"relativetime\" unit \"${unitOption.value.value}\". Expected one of: ${Array.from(\n\t\t\t\trelativeTimeUnits\n\t\t\t).join(\", \")}.`\n\t\t);\n\t}\n}\n\nfunction isDollarVariableReference(value: string): boolean {\n\treturn value.startsWith(\"$\") && value.length > 1;\n}\n"]}
1
+ {"version":3,"file":"compile-annotation.js","sourceRoot":"","sources":["../../src/compiler/compile-annotation.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,EAAE,0BAA0B,EAAE,MAAM,+BAA+B,CAAC;AAE3E;;;;GAIG;AACH,MAAM,qBAAqB,GAAG,IAAI,GAAG,CAAC;IACrC,QAAQ;IACR,QAAQ;IACR,UAAU;IACV,cAAc;CACd,CAAC,CAAC;AAEH,MAAM,UAAU,kBAAkB,CAAC,IAAY;I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,CAAC;IAED,MAAM,CAAC,UAAU,CAAC,GAAG,WAAW,CAAC;IACjC,IAAI,CAAC,UAAU,IAAI,UAAU,CAAC,KAAK,CAAC,IAAI,KAAK,oBAAoB,EAAE,CAAC;QACnE,OAAO;IACR,CAAC;IAED,IAAI,yBAAyB,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;QACvD,OAAO;IACR,CAAC;IAED,IAAI,CAAC,iBAAiB,CAAC,GAAG,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE,CAAC;QACpD,MAAM,IAAI,KAAK,CACd,gCAAgC,UAAU,CAAC,KAAK,CAAC,KAAK,uBAAuB,KAAK,CAAC,IAAI,CACtF,iBAAiB,CACjB,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CACf,CAAC;IACH,CAAC;AACF,CAAC;AAED,SAAS,yBAAyB,CAAC,KAAa;IAC/C,OAAO,KAAK,CAAC,UAAU,CAAC,GAAG,CAAC,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,CAAC;AAClD,CAAC","sourcesContent":["import type {\n\tDeclaration,\n\tFunctionReference,\n\tLiteral,\n\tVariableReference,\n} from \"@inlang/sdk\";\nimport { compileVariableAccess } from \"./variable-access.js\";\nimport { escapeForDoubleQuoteString } from \"../services/codegen/escape.js\";\n\n/**\n * The functions shipped in the generated registry.js file.\n *\n * @see createRegistry()\n */\nconst registryFunctionNames = new Set([\n\t\"plural\",\n\t\"number\",\n\t\"datetime\",\n\t\"relativetime\",\n]);\n\nexport function isRegistryFunction(name: string): boolean {\n\treturn registryFunctionNames.has(name);\n}\n\nexport function registryFunctionNamesForDisplay(): string {\n\treturn Array.from(registryFunctionNames).join(\", \");\n}\n\n/**\n * Wraps a compiled expression value in a `registry.*` call if an\n * annotation is present.\n *\n * @example\n * compileAnnotation(\"i?.count\", \"en\", { type: \"function-reference\", name: \"number\", options: [] })\n * >> 'registry.number(\"en\", i?.count, {})'\n */\nexport function compileAnnotation(\n\tstr: string,\n\tlocale: string,\n\tannotation?: FunctionReference,\n\tdeclarations?: Declaration[]\n): string {\n\tif (!annotation) {\n\t\treturn str;\n\t}\n\tif (annotation.name === \"relativetime\") {\n\t\tvalidateRelativeTimeOptions(annotation);\n\t}\n\treturn `registry.${annotation.name}(\"${locale}\", ${str}, ${compileOptions(annotation.name, annotation.options, declarations)})`;\n}\n\nfunction compileOptions(\n\tannotationName: string,\n\toptions: FunctionReference[\"options\"],\n\tdeclarations?: Declaration[]\n): string {\n\tif (options.length === 0) {\n\t\treturn \"{}\";\n\t}\n\tconst entries: string[] = options.map(\n\t\t(option) =>\n\t\t\t`${option.name}: ${compileOptionLiteralOrVarRef(\n\t\t\t\tannotationName,\n\t\t\t\toption.name,\n\t\t\t\toption.value,\n\t\t\t\tdeclarations\n\t\t\t)}`\n\t);\n\tconst code = \"{ \" + entries.join(\", \") + \" }\";\n\n\treturn code;\n}\n\nconst numericOptionNamesByAnnotation: Record<string, ReadonlySet<string>> = {\n\tnumber: new Set([\n\t\t\"minimumIntegerDigits\",\n\t\t\"minimumFractionDigits\",\n\t\t\"maximumFractionDigits\",\n\t\t\"minimumSignificantDigits\",\n\t\t\"maximumSignificantDigits\",\n\t\t\"roundingIncrement\",\n\t]),\n\tplural: new Set([\n\t\t\"minimumIntegerDigits\",\n\t\t\"minimumFractionDigits\",\n\t\t\"maximumFractionDigits\",\n\t\t\"minimumSignificantDigits\",\n\t\t\"maximumSignificantDigits\",\n\t]),\n\tdatetime: new Set([\"fractionalSecondDigits\"]),\n};\n\nconst booleanOptionNamesByAnnotation: Record<string, ReadonlySet<string>> = {\n\tnumber: new Set([\"useGrouping\"]),\n\tdatetime: new Set([\"hour12\"]),\n};\n\nconst jsNonNegativeIntegerPattern = /^(?:0|[1-9]\\d*)$/;\n\nfunction compileOptionLiteralOrVarRef(\n\tannotationName: string,\n\toptionName: string,\n\tvalue: Literal | VariableReference,\n\tdeclarations?: Declaration[]\n): string {\n\tif (value.type === \"variable-reference\") {\n\t\tif (annotationName === \"relativetime\" && optionName === \"unit\") {\n\t\t\treturn `/** @type {import(\"../registry.js\").RelativeTimeFormatUnit} */ (${compileVariableAccess(value.name, declarations)})`;\n\t\t}\n\t\treturn compileVariableAccess(value.name, declarations);\n\t}\n\n\tif (\n\t\tannotationName === \"relativetime\" &&\n\t\toptionName === \"unit\" &&\n\t\tisDollarVariableReference(value.value)\n\t) {\n\t\treturn `/** @type {import(\"../registry.js\").RelativeTimeFormatUnit} */ (${compileVariableAccess(value.value.slice(1), declarations)})`;\n\t}\n\n\tif (shouldEmitNumberLiteral(annotationName, optionName, value.value)) {\n\t\treturn value.value;\n\t}\n\n\tif (shouldEmitBooleanLiteral(annotationName, optionName, value.value)) {\n\t\treturn value.value;\n\t}\n\n\treturn `\"${escapeForDoubleQuoteString(value.value)}\"`;\n}\n\nfunction shouldEmitNumberLiteral(\n\tannotationName: string,\n\toptionName: string,\n\tliteral: string\n): boolean {\n\treturn (\n\t\tnumericOptionNamesByAnnotation[annotationName]?.has(optionName) === true &&\n\t\tjsNonNegativeIntegerPattern.test(literal)\n\t);\n}\n\nfunction shouldEmitBooleanLiteral(\n\tannotationName: string,\n\toptionName: string,\n\tliteral: string\n): boolean {\n\treturn (\n\t\tbooleanOptionNamesByAnnotation[annotationName]?.has(optionName) === true &&\n\t\t(literal === \"true\" || literal === \"false\")\n\t);\n}\n\nconst relativeTimeUnits = new Set([\n\t\"year\",\n\t\"years\",\n\t\"quarter\",\n\t\"quarters\",\n\t\"month\",\n\t\"months\",\n\t\"week\",\n\t\"weeks\",\n\t\"day\",\n\t\"days\",\n\t\"hour\",\n\t\"hours\",\n\t\"minute\",\n\t\"minutes\",\n\t\"second\",\n\t\"seconds\",\n]);\n\nfunction validateRelativeTimeOptions(annotation: FunctionReference): void {\n\tconst unitOptions = annotation.options.filter(\n\t\t(option) => option.name === \"unit\"\n\t);\n\n\tif (unitOptions.length === 0) {\n\t\tthrow new Error('The \"relativetime\" formatter requires a \"unit\" option.');\n\t}\n\n\tif (unitOptions.length > 1) {\n\t\tthrow new Error(\n\t\t\t'The \"relativetime\" formatter requires exactly one \"unit\" option.'\n\t\t);\n\t}\n\n\tconst [unitOption] = unitOptions;\n\tif (!unitOption || unitOption.value.type === \"variable-reference\") {\n\t\treturn;\n\t}\n\n\tif (isDollarVariableReference(unitOption.value.value)) {\n\t\treturn;\n\t}\n\n\tif (!relativeTimeUnits.has(unitOption.value.value)) {\n\t\tthrow new Error(\n\t\t\t`Invalid \"relativetime\" unit \"${unitOption.value.value}\". Expected one of: ${Array.from(\n\t\t\t\trelativeTimeUnits\n\t\t\t).join(\", \")}.`\n\t\t);\n\t}\n}\n\nfunction isDollarVariableReference(value: string): boolean {\n\treturn value.startsWith(\"$\") && value.length > 1;\n}\n"]}
@@ -1,4 +1,4 @@
1
- import type { LocalVariable } from "@inlang/sdk";
1
+ import type { Declaration, LocalVariable } from "@inlang/sdk";
2
2
  /**
3
3
  * Compiles a local variable.
4
4
  *
@@ -13,4 +13,5 @@ import type { LocalVariable } from "@inlang/sdk";
13
13
  export declare function compileLocalVariable(args: {
14
14
  locale: string;
15
15
  declaration: LocalVariable;
16
+ declarations?: Declaration[];
16
17
  }): string;
@@ -1,4 +1,4 @@
1
- import { compileInputAccess } from "./variable-access.js";
1
+ import { compileVariableAccess } from "./variable-access.js";
2
2
  import { escapeForDoubleQuoteString } from "../services/codegen/escape.js";
3
3
  import { compileAnnotation } from "./compile-annotation.js";
4
4
  /**
@@ -14,15 +14,15 @@ import { compileAnnotation } from "./compile-annotation.js";
14
14
  */
15
15
  export function compileLocalVariable(args) {
16
16
  const annotation = args.declaration.value.annotation;
17
- const value = compileAnnotation(compileLiteralOrVarRef(args.declaration.value.arg), args.locale, annotation);
17
+ const value = compileAnnotation(compileLiteralOrVarRef(args.declaration.value.arg, args.declarations), args.locale, annotation, args.declarations);
18
18
  return `const ${args.declaration.name} = ${value};`;
19
19
  }
20
- function compileLiteralOrVarRef(value) {
20
+ function compileLiteralOrVarRef(value, declarations) {
21
21
  switch (value.type) {
22
22
  case "literal":
23
23
  return `"${escapeForDoubleQuoteString(value.value)}"`;
24
24
  case "variable-reference":
25
- return compileInputAccess(value.name);
25
+ return compileVariableAccess(value.name, declarations);
26
26
  }
27
27
  }
28
28
  //# sourceMappingURL=compile-local-variable.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"compile-local-variable.js","sourceRoot":"","sources":["../../src/compiler/compile-local-variable.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,0BAA0B,EAAE,MAAM,+BAA+B,CAAC;AAC3E,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D;;;;;;;;;;GAUG;AACH,MAAM,UAAU,oBAAoB,CAAC,IAGpC;IACA,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,UAAU,CAAC;IAErD,MAAM,KAAK,GAAG,iBAAiB,CAC9B,sBAAsB,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,GAAG,CAAC,EAClD,IAAI,CAAC,MAAM,EACX,UAAU,CACV,CAAC;IAEF,OAAO,SAAS,IAAI,CAAC,WAAW,CAAC,IAAI,MAAM,KAAK,GAAG,CAAC;AACrD,CAAC;AAED,SAAS,sBAAsB,CAAC,KAAkC;IACjE,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;QACpB,KAAK,SAAS;YACb,OAAO,IAAI,0BAA0B,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC;QACvD,KAAK,oBAAoB;YACxB,OAAO,kBAAkB,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACxC,CAAC;AACF,CAAC","sourcesContent":["import type { Literal, LocalVariable, VariableReference } from \"@inlang/sdk\";\nimport { compileInputAccess } from \"./variable-access.js\";\nimport { escapeForDoubleQuoteString } from \"../services/codegen/escape.js\";\nimport { compileAnnotation } from \"./compile-annotation.js\";\n\n/**\n * Compiles a local variable.\n *\n * @example\n * const code = compileLocalVariable({\n * type: \"local-variable\",\n * name: \"myVar\",\n * value: { type: \"literal\", value: \"Hello\" }\n * });\n * >> code === \"const myVar = 'Hello';\"\n */\nexport function compileLocalVariable(args: {\n\tlocale: string;\n\tdeclaration: LocalVariable;\n}): string {\n\tconst annotation = args.declaration.value.annotation;\n\n\tconst value = compileAnnotation(\n\t\tcompileLiteralOrVarRef(args.declaration.value.arg),\n\t\targs.locale,\n\t\tannotation\n\t);\n\n\treturn `const ${args.declaration.name} = ${value};`;\n}\n\nfunction compileLiteralOrVarRef(value: Literal | VariableReference): string {\n\tswitch (value.type) {\n\t\tcase \"literal\":\n\t\t\treturn `\"${escapeForDoubleQuoteString(value.value)}\"`;\n\t\tcase \"variable-reference\":\n\t\t\treturn compileInputAccess(value.name);\n\t}\n}\n"]}
1
+ {"version":3,"file":"compile-local-variable.js","sourceRoot":"","sources":["../../src/compiler/compile-local-variable.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,EAAE,0BAA0B,EAAE,MAAM,+BAA+B,CAAC;AAC3E,OAAO,EAAE,iBAAiB,EAAE,MAAM,yBAAyB,CAAC;AAE5D;;;;;;;;;;GAUG;AACH,MAAM,UAAU,oBAAoB,CAAC,IAIpC;IACA,MAAM,UAAU,GAAG,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,UAAU,CAAC;IAErD,MAAM,KAAK,GAAG,iBAAiB,CAC9B,sBAAsB,CAAC,IAAI,CAAC,WAAW,CAAC,KAAK,CAAC,GAAG,EAAE,IAAI,CAAC,YAAY,CAAC,EACrE,IAAI,CAAC,MAAM,EACX,UAAU,EACV,IAAI,CAAC,YAAY,CACjB,CAAC;IAEF,OAAO,SAAS,IAAI,CAAC,WAAW,CAAC,IAAI,MAAM,KAAK,GAAG,CAAC;AACrD,CAAC;AAED,SAAS,sBAAsB,CAC9B,KAAkC,EAClC,YAA4B;IAE5B,QAAQ,KAAK,CAAC,IAAI,EAAE,CAAC;QACpB,KAAK,SAAS;YACb,OAAO,IAAI,0BAA0B,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC;QACvD,KAAK,oBAAoB;YACxB,OAAO,qBAAqB,CAAC,KAAK,CAAC,IAAI,EAAE,YAAY,CAAC,CAAC;IACzD,CAAC;AACF,CAAC","sourcesContent":["import type {\n\tDeclaration,\n\tLiteral,\n\tLocalVariable,\n\tVariableReference,\n} from \"@inlang/sdk\";\nimport { compileVariableAccess } from \"./variable-access.js\";\nimport { escapeForDoubleQuoteString } from \"../services/codegen/escape.js\";\nimport { compileAnnotation } from \"./compile-annotation.js\";\n\n/**\n * Compiles a local variable.\n *\n * @example\n * const code = compileLocalVariable({\n * type: \"local-variable\",\n * name: \"myVar\",\n * value: { type: \"literal\", value: \"Hello\" }\n * });\n * >> code === \"const myVar = 'Hello';\"\n */\nexport function compileLocalVariable(args: {\n\tlocale: string;\n\tdeclaration: LocalVariable;\n\tdeclarations?: Declaration[];\n}): string {\n\tconst annotation = args.declaration.value.annotation;\n\n\tconst value = compileAnnotation(\n\t\tcompileLiteralOrVarRef(args.declaration.value.arg, args.declarations),\n\t\targs.locale,\n\t\tannotation,\n\t\targs.declarations\n\t);\n\n\treturn `const ${args.declaration.name} = ${value};`;\n}\n\nfunction compileLiteralOrVarRef(\n\tvalue: Literal | VariableReference,\n\tdeclarations?: Declaration[]\n): string {\n\tswitch (value.type) {\n\t\tcase \"literal\":\n\t\t\treturn `\"${escapeForDoubleQuoteString(value.value)}\"`;\n\t\tcase \"variable-reference\":\n\t\t\treturn compileVariableAccess(value.name, declarations);\n\t}\n}\n"]}
@@ -25,6 +25,27 @@ test("compiles a variable reference local variable", () => {
25
25
  });
26
26
  expect(code).toEqual("const myVar = i?.name;");
27
27
  });
28
+ test("compiles a variable reference to an earlier local variable", () => {
29
+ const code = compileLocalVariable({
30
+ locale: "en",
31
+ declarations: [
32
+ {
33
+ type: "local-variable",
34
+ name: "earlierLocal",
35
+ value: { type: "expression", arg: { type: "literal", value: "42" } },
36
+ },
37
+ ],
38
+ declaration: {
39
+ type: "local-variable",
40
+ name: "myVar",
41
+ value: {
42
+ type: "expression",
43
+ arg: { type: "variable-reference", name: "earlierLocal" },
44
+ },
45
+ },
46
+ });
47
+ expect(code).toEqual("const myVar = earlierLocal;");
48
+ });
28
49
  test("compiles a variable reference local variable with a non-identifier name", () => {
29
50
  const code = compileLocalVariable({
30
51
  locale: "en",
@@ -83,6 +104,38 @@ test("compiles a local variable with an annotation and options", () => {
83
104
  });
84
105
  expect(code).toEqual('const myVar = registry.myFunction("en", "Hello", { option1: "value1", option2: i?.varRef });');
85
106
  });
107
+ test("compiles formatter options referencing earlier local variables", () => {
108
+ const code = compileLocalVariable({
109
+ locale: "en",
110
+ declarations: [
111
+ { type: "input-variable", name: "amount" },
112
+ {
113
+ type: "local-variable",
114
+ name: "digits",
115
+ value: { type: "expression", arg: { type: "literal", value: "2" } },
116
+ },
117
+ ],
118
+ declaration: {
119
+ type: "local-variable",
120
+ name: "formattedAmount",
121
+ value: {
122
+ type: "expression",
123
+ arg: { type: "variable-reference", name: "amount" },
124
+ annotation: {
125
+ type: "function-reference",
126
+ name: "number",
127
+ options: [
128
+ {
129
+ name: "minimumFractionDigits",
130
+ value: { type: "variable-reference", name: "digits" },
131
+ },
132
+ ],
133
+ },
134
+ },
135
+ },
136
+ });
137
+ expect(code).toEqual('const formattedAmount = registry.number("en", i?.amount, { minimumFractionDigits: digits });');
138
+ });
86
139
  test("compiles number formatter fraction digit options as numeric literals", () => {
87
140
  const code = compileLocalVariable({
88
141
  locale: "en",
@@ -332,6 +385,35 @@ test("accepts dollar-prefixed relative time formatter unit options from message
332
385
  });
333
386
  expect(code).toEqual('const formattedDuration = registry.relativetime("en", i?.duration, { unit: /** @type {import("../registry.js").RelativeTimeFormatUnit} */ (i?.unit) });');
334
387
  });
388
+ test("compiles dollar-prefixed relative time units referencing local variables", () => {
389
+ const code = compileLocalVariable({
390
+ locale: "en",
391
+ declarations: [
392
+ { type: "input-variable", name: "duration" },
393
+ {
394
+ type: "local-variable",
395
+ name: "localUnit",
396
+ value: { type: "expression", arg: { type: "literal", value: "day" } },
397
+ },
398
+ ],
399
+ declaration: {
400
+ type: "local-variable",
401
+ name: "formattedDuration",
402
+ value: {
403
+ type: "expression",
404
+ arg: { type: "variable-reference", name: "duration" },
405
+ annotation: {
406
+ type: "function-reference",
407
+ name: "relativetime",
408
+ options: [
409
+ { name: "unit", value: { type: "literal", value: "$localUnit" } },
410
+ ],
411
+ },
412
+ },
413
+ },
414
+ });
415
+ expect(code).toEqual('const formattedDuration = registry.relativetime("en", i?.duration, { unit: /** @type {import("../registry.js").RelativeTimeFormatUnit} */ (localUnit) });');
416
+ });
335
417
  test("accepts dollar-prefixed relative time formatter unit options with non-identifier input names", () => {
336
418
  const code = compileLocalVariable({
337
419
  locale: "en",
@@ -1 +1 @@
1
- {"version":3,"file":"compile-local-variable.test.js","sourceRoot":"","sources":["../../src/compiler/compile-local-variable.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAEnE,IAAI,CAAC,mCAAmC,EAAE,GAAG,EAAE;IAC9C,MAAM,IAAI,GAAG,oBAAoB,CAAC;QACjC,MAAM,EAAE,IAAI;QACZ,WAAW,EAAE;YACZ,IAAI,EAAE,gBAAgB;YACtB,IAAI,EAAE,OAAO;YACb,KAAK,EAAE,EAAE,IAAI,EAAE,YAAY,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;SACvE;KACD,CAAC,CAAC;IACH,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,wBAAwB,CAAC,CAAC;AAChD,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,8CAA8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B,EAAE;yBACnD;qBACD;iBACD;aACD;SACD;KACD,CAAC,CAAC;IAEH,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CACnB,sKAAsK,CACtK,CAAC;AACH,CAAC,CAAC,CAAC","sourcesContent":["import { test, expect } from \"vitest\";\nimport { compileLocalVariable } from \"./compile-local-variable.js\";\n\ntest(\"compiles a literal local variable\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"myVar\",\n\t\t\tvalue: { type: \"expression\", arg: { type: \"literal\", value: \"Hello\" } },\n\t\t},\n\t});\n\texpect(code).toEqual('const myVar = \"Hello\";');\n});\n\ntest(\"compiles a variable reference local variable\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"myVar\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"name\" },\n\t\t\t},\n\t\t},\n\t});\n\texpect(code).toEqual(\"const myVar = i?.name;\");\n});\n\ntest(\"compiles a variable reference local variable with a non-identifier name\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"myVar\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"half!\" },\n\t\t\t},\n\t\t},\n\t});\n\texpect(code).toEqual('const myVar = i?.[\"half!\"];');\n});\n\ntest(\"compiles a local variable with an annotation and empty options\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"myVar\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"literal\", value: \"Hello\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"myFunction\",\n\t\t\t\t\toptions: [],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\texpect(code).toEqual('const myVar = registry.myFunction(\"en\", \"Hello\", {});');\n});\n\ntest(\"compiles a local variable with an annotation and options\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"myVar\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"literal\", value: \"Hello\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"myFunction\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{ name: \"option1\", value: { type: \"literal\", value: \"value1\" } },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"option2\",\n\t\t\t\t\t\t\tvalue: { type: \"variable-reference\", name: \"varRef\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\texpect(code).toEqual(\n\t\t'const myVar = registry.myFunction(\"en\", \"Hello\", { option1: \"value1\", option2: i?.varRef });'\n\t);\n});\n\ntest(\"compiles number formatter fraction digit options as numeric literals\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedValue\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"value\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"number\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"minimumFractionDigits\",\n\t\t\t\t\t\t\tvalue: { type: \"literal\", value: \"1\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"maximumFractionDigits\",\n\t\t\t\t\t\t\tvalue: { type: \"literal\", value: \"1\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedValue = registry.number(\"en\", i?.value, { minimumFractionDigits: 1, maximumFractionDigits: 1 });'\n\t);\n});\n\ntest(\"compiles number formatter useGrouping option as boolean literal\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedValue\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"value\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"number\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"useGrouping\",\n\t\t\t\t\t\t\tvalue: { type: \"literal\", value: \"false\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedValue = registry.number(\"en\", i?.value, { useGrouping: false });'\n\t);\n});\n\ntest(\"keeps negative digit options as strings to avoid emitting invalid numeric literals\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedValue\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"value\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"number\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"minimumFractionDigits\",\n\t\t\t\t\t\t\tvalue: { type: \"literal\", value: \"-1\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedValue = registry.number(\"en\", i?.value, { minimumFractionDigits: \"-1\" });'\n\t);\n});\n\ntest(\"compiles relative time formatter with a literal unit\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedDuration\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{ name: \"unit\", value: { type: \"literal\", value: \"day\" } },\n\t\t\t\t\t\t{ name: \"numeric\", value: { type: \"literal\", value: \"auto\" } },\n\t\t\t\t\t\t{ name: \"style\", value: { type: \"literal\", value: \"short\" } },\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedDuration = registry.relativetime(\"en\", i?.duration, { unit: \"day\", numeric: \"auto\", style: \"short\" });'\n\t);\n});\n\ntest(\"compiles relative time formatter with a dynamic unit cast\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedDuration\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"unit\",\n\t\t\t\t\t\t\tvalue: { type: \"variable-reference\", name: \"unit\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{ name: \"style\", value: { type: \"literal\", value: \"short\" } },\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedDuration = registry.relativetime(\"en\", i?.duration, { unit: /** @type {import(\"../registry.js\").RelativeTimeFormatUnit} */ (i?.unit), style: \"short\" });'\n\t);\n});\n\ntest(\"throws if relative time formatter is missing a unit option\", () => {\n\texpect(() =>\n\t\tcompileLocalVariable({\n\t\t\tlocale: \"en\",\n\t\t\tdeclaration: {\n\t\t\t\ttype: \"local-variable\",\n\t\t\t\tname: \"formattedDuration\",\n\t\t\t\tvalue: {\n\t\t\t\t\ttype: \"expression\",\n\t\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\t\tannotation: {\n\t\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\t\toptions: [],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t})\n\t).toThrow('The \"relativetime\" formatter requires a \"unit\" option.');\n});\n\ntest(\"throws if relative time formatter has duplicate unit options\", () => {\n\texpect(() =>\n\t\tcompileLocalVariable({\n\t\t\tlocale: \"en\",\n\t\t\tdeclaration: {\n\t\t\t\ttype: \"local-variable\",\n\t\t\t\tname: \"formattedDuration\",\n\t\t\t\tvalue: {\n\t\t\t\t\ttype: \"expression\",\n\t\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\t\tannotation: {\n\t\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\t\toptions: [\n\t\t\t\t\t\t\t{ name: \"unit\", value: { type: \"literal\", value: \"day\" } },\n\t\t\t\t\t\t\t{ name: \"unit\", value: { type: \"literal\", value: \"hour\" } },\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t})\n\t).toThrow('The \"relativetime\" formatter requires exactly one \"unit\" option.');\n});\n\ntest(\"throws if relative time formatter has an invalid literal unit\", () => {\n\texpect(() =>\n\t\tcompileLocalVariable({\n\t\t\tlocale: \"en\",\n\t\t\tdeclaration: {\n\t\t\t\ttype: \"local-variable\",\n\t\t\t\tname: \"formattedDuration\",\n\t\t\t\tvalue: {\n\t\t\t\t\ttype: \"expression\",\n\t\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\t\tannotation: {\n\t\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\t\toptions: [\n\t\t\t\t\t\t\t{ name: \"unit\", value: { type: \"literal\", value: \"century\" } },\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t})\n\t).toThrow('Invalid \"relativetime\" unit \"century\".');\n});\n\ntest(\"accepts plural relative time formatter units\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedDuration\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{ name: \"unit\", value: { type: \"literal\", value: \"days\" } },\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedDuration = registry.relativetime(\"en\", i?.duration, { unit: \"days\" });'\n\t);\n});\n\ntest(\"accepts dynamic relative time formatter units\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedDuration\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"unit\",\n\t\t\t\t\t\t\tvalue: { type: \"variable-reference\", name: \"unit\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedDuration = registry.relativetime(\"en\", i?.duration, { unit: /** @type {import(\"../registry.js\").RelativeTimeFormatUnit} */ (i?.unit) });'\n\t);\n});\n\ntest(\"accepts dollar-prefixed relative time formatter unit options from message files\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedDuration\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{ name: \"unit\", value: { type: \"literal\", value: \"$unit\" } },\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedDuration = registry.relativetime(\"en\", i?.duration, { unit: /** @type {import(\"../registry.js\").RelativeTimeFormatUnit} */ (i?.unit) });'\n\t);\n});\n\ntest(\"accepts dollar-prefixed relative time formatter unit options with non-identifier input names\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedDuration\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"unit\",\n\t\t\t\t\t\t\tvalue: { type: \"literal\", value: \"$relative-unit\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedDuration = registry.relativetime(\"en\", i?.duration, { unit: /** @type {import(\"../registry.js\").RelativeTimeFormatUnit} */ (i?.[\"relative-unit\"]) });'\n\t);\n});\n"]}
1
+ {"version":3,"file":"compile-local-variable.test.js","sourceRoot":"","sources":["../../src/compiler/compile-local-variable.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACtC,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AAEnE,IAAI,CAAC,mCAAmC,EAAE,GAAG,EAAE;IAC9C,MAAM,IAAI,GAAG,oBAAoB,CAAC;QACjC,MAAM,EAAE,IAAI;QACZ,WAAW,EAAE;YACZ,IAAI,EAAE,gBAAgB;YACtB,IAAI,EAAE,OAAO;YACb,KAAK,EAAE,EAAE,IAAI,EAAE,YAAY,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,OAAO,EAAE,EAAE;SACvE;KACD,CAAC,CAAC;IACH,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CAAC,wBAAwB,CAAC,CAAC;AAChD,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,8CAA8C,EAAE,GAAG,EAAE;IACzD,MAAM,IAAI,GAAG,oBAAoB,CAAC;QACjC,MAAM,EAAE,IAAI;QACZ,WAAW,EAAE;YACZ,IAAI,EAAE,gBAAgB;YACtB,IAAI,EAAE,OAAO;YACb,KAAK,EAAE;gBACN,IAAI,EAAE,YAAY;gBAClB,GAAG,EAAE,EAAE,IAAI,EAAE,oBAAoB,EAAE,IAAI,EAAE,MAAM,EAAE;aACjD;SACD;KACD,CAAC,CAAC;IACH,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CAA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AAoB,EAAE,IAAI,EAAE,UAAU,EAAE;gBACrD,UAAU,EAAE;oBACX,IAAI,EAAE,oBAAoB;oBAC1B,IAAI,EAAE,cAAc;oBACpB,OAAO,EAAE;wBACR,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,YAAY,EAAE,EAAE;qBACjE;iBACD;aACD;SACD;KACD,CAAC,CAAC;IAEH,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CACnB,2JAA2J,CAC3J,CAAC;AACH,CAAC,CAAC,CAAC;AAEH,IAAI,CAAC,8FAA8F,EAAE,GAAG,EAAE;IACzG,MAAM,IAAI,GAAG,oBAAoB,CAAC;QACjC,MAAM,EAAE,IAAI;QACZ,WAAW,EAAE;YACZ,IAAI,EAAE,gBAAgB;YACtB,IAAI,EAAE,mBAAmB;YACzB,KAAK,EAAE;gBACN,IAAI,EAAE,YAAY;gBAClB,GAAG,EAAE,EAAE,IAAI,EAAE,oBAAoB,EAAE,IAAI,EAAE,UAAU,EAAE;gBACrD,UAAU,EAAE;oBACX,IAAI,EAAE,oBAAoB;oBAC1B,IAAI,EAAE,cAAc;oBACpB,OAAO,EAAE;wBACR;4BACC,IAAI,EAAE,MAAM;4BACZ,KAAK,EAAE,EAAE,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,gBAAgB,EAAE;yBACnD;qBACD;iBACD;aACD;SACD;KACD,CAAC,CAAC;IAEH,MAAM,CAAC,IAAI,CAAC,CAAC,OAAO,CACnB,sKAAsK,CACtK,CAAC;AACH,CAAC,CAAC,CAAC","sourcesContent":["import { test, expect } from \"vitest\";\nimport { compileLocalVariable } from \"./compile-local-variable.js\";\n\ntest(\"compiles a literal local variable\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"myVar\",\n\t\t\tvalue: { type: \"expression\", arg: { type: \"literal\", value: \"Hello\" } },\n\t\t},\n\t});\n\texpect(code).toEqual('const myVar = \"Hello\";');\n});\n\ntest(\"compiles a variable reference local variable\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"myVar\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"name\" },\n\t\t\t},\n\t\t},\n\t});\n\texpect(code).toEqual(\"const myVar = i?.name;\");\n});\n\ntest(\"compiles a variable reference to an earlier local variable\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclarations: [\n\t\t\t{\n\t\t\t\ttype: \"local-variable\",\n\t\t\t\tname: \"earlierLocal\",\n\t\t\t\tvalue: { type: \"expression\", arg: { type: \"literal\", value: \"42\" } },\n\t\t\t},\n\t\t],\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"myVar\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"earlierLocal\" },\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\"const myVar = earlierLocal;\");\n});\n\ntest(\"compiles a variable reference local variable with a non-identifier name\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"myVar\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"half!\" },\n\t\t\t},\n\t\t},\n\t});\n\texpect(code).toEqual('const myVar = i?.[\"half!\"];');\n});\n\ntest(\"compiles a local variable with an annotation and empty options\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"myVar\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"literal\", value: \"Hello\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"myFunction\",\n\t\t\t\t\toptions: [],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\texpect(code).toEqual('const myVar = registry.myFunction(\"en\", \"Hello\", {});');\n});\n\ntest(\"compiles a local variable with an annotation and options\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"myVar\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"literal\", value: \"Hello\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"myFunction\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{ name: \"option1\", value: { type: \"literal\", value: \"value1\" } },\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"option2\",\n\t\t\t\t\t\t\tvalue: { type: \"variable-reference\", name: \"varRef\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\texpect(code).toEqual(\n\t\t'const myVar = registry.myFunction(\"en\", \"Hello\", { option1: \"value1\", option2: i?.varRef });'\n\t);\n});\n\ntest(\"compiles formatter options referencing earlier local variables\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclarations: [\n\t\t\t{ type: \"input-variable\", name: \"amount\" },\n\t\t\t{\n\t\t\t\ttype: \"local-variable\",\n\t\t\t\tname: \"digits\",\n\t\t\t\tvalue: { type: \"expression\", arg: { type: \"literal\", value: \"2\" } },\n\t\t\t},\n\t\t],\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedAmount\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"amount\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"number\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"minimumFractionDigits\",\n\t\t\t\t\t\t\tvalue: { type: \"variable-reference\", name: \"digits\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedAmount = registry.number(\"en\", i?.amount, { minimumFractionDigits: digits });'\n\t);\n});\n\ntest(\"compiles number formatter fraction digit options as numeric literals\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedValue\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"value\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"number\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"minimumFractionDigits\",\n\t\t\t\t\t\t\tvalue: { type: \"literal\", value: \"1\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"maximumFractionDigits\",\n\t\t\t\t\t\t\tvalue: { type: \"literal\", value: \"1\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedValue = registry.number(\"en\", i?.value, { minimumFractionDigits: 1, maximumFractionDigits: 1 });'\n\t);\n});\n\ntest(\"compiles number formatter useGrouping option as boolean literal\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedValue\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"value\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"number\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"useGrouping\",\n\t\t\t\t\t\t\tvalue: { type: \"literal\", value: \"false\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedValue = registry.number(\"en\", i?.value, { useGrouping: false });'\n\t);\n});\n\ntest(\"keeps negative digit options as strings to avoid emitting invalid numeric literals\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedValue\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"value\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"number\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"minimumFractionDigits\",\n\t\t\t\t\t\t\tvalue: { type: \"literal\", value: \"-1\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedValue = registry.number(\"en\", i?.value, { minimumFractionDigits: \"-1\" });'\n\t);\n});\n\ntest(\"compiles relative time formatter with a literal unit\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedDuration\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{ name: \"unit\", value: { type: \"literal\", value: \"day\" } },\n\t\t\t\t\t\t{ name: \"numeric\", value: { type: \"literal\", value: \"auto\" } },\n\t\t\t\t\t\t{ name: \"style\", value: { type: \"literal\", value: \"short\" } },\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedDuration = registry.relativetime(\"en\", i?.duration, { unit: \"day\", numeric: \"auto\", style: \"short\" });'\n\t);\n});\n\ntest(\"compiles relative time formatter with a dynamic unit cast\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedDuration\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"unit\",\n\t\t\t\t\t\t\tvalue: { type: \"variable-reference\", name: \"unit\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t\t{ name: \"style\", value: { type: \"literal\", value: \"short\" } },\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedDuration = registry.relativetime(\"en\", i?.duration, { unit: /** @type {import(\"../registry.js\").RelativeTimeFormatUnit} */ (i?.unit), style: \"short\" });'\n\t);\n});\n\ntest(\"throws if relative time formatter is missing a unit option\", () => {\n\texpect(() =>\n\t\tcompileLocalVariable({\n\t\t\tlocale: \"en\",\n\t\t\tdeclaration: {\n\t\t\t\ttype: \"local-variable\",\n\t\t\t\tname: \"formattedDuration\",\n\t\t\t\tvalue: {\n\t\t\t\t\ttype: \"expression\",\n\t\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\t\tannotation: {\n\t\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\t\toptions: [],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t})\n\t).toThrow('The \"relativetime\" formatter requires a \"unit\" option.');\n});\n\ntest(\"throws if relative time formatter has duplicate unit options\", () => {\n\texpect(() =>\n\t\tcompileLocalVariable({\n\t\t\tlocale: \"en\",\n\t\t\tdeclaration: {\n\t\t\t\ttype: \"local-variable\",\n\t\t\t\tname: \"formattedDuration\",\n\t\t\t\tvalue: {\n\t\t\t\t\ttype: \"expression\",\n\t\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\t\tannotation: {\n\t\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\t\toptions: [\n\t\t\t\t\t\t\t{ name: \"unit\", value: { type: \"literal\", value: \"day\" } },\n\t\t\t\t\t\t\t{ name: \"unit\", value: { type: \"literal\", value: \"hour\" } },\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t})\n\t).toThrow('The \"relativetime\" formatter requires exactly one \"unit\" option.');\n});\n\ntest(\"throws if relative time formatter has an invalid literal unit\", () => {\n\texpect(() =>\n\t\tcompileLocalVariable({\n\t\t\tlocale: \"en\",\n\t\t\tdeclaration: {\n\t\t\t\ttype: \"local-variable\",\n\t\t\t\tname: \"formattedDuration\",\n\t\t\t\tvalue: {\n\t\t\t\t\ttype: \"expression\",\n\t\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\t\tannotation: {\n\t\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\t\toptions: [\n\t\t\t\t\t\t\t{ name: \"unit\", value: { type: \"literal\", value: \"century\" } },\n\t\t\t\t\t\t],\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t})\n\t).toThrow('Invalid \"relativetime\" unit \"century\".');\n});\n\ntest(\"accepts plural relative time formatter units\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedDuration\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{ name: \"unit\", value: { type: \"literal\", value: \"days\" } },\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedDuration = registry.relativetime(\"en\", i?.duration, { unit: \"days\" });'\n\t);\n});\n\ntest(\"accepts dynamic relative time formatter units\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedDuration\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"unit\",\n\t\t\t\t\t\t\tvalue: { type: \"variable-reference\", name: \"unit\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedDuration = registry.relativetime(\"en\", i?.duration, { unit: /** @type {import(\"../registry.js\").RelativeTimeFormatUnit} */ (i?.unit) });'\n\t);\n});\n\ntest(\"accepts dollar-prefixed relative time formatter unit options from message files\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedDuration\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{ name: \"unit\", value: { type: \"literal\", value: \"$unit\" } },\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedDuration = registry.relativetime(\"en\", i?.duration, { unit: /** @type {import(\"../registry.js\").RelativeTimeFormatUnit} */ (i?.unit) });'\n\t);\n});\n\ntest(\"compiles dollar-prefixed relative time units referencing local variables\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclarations: [\n\t\t\t{ type: \"input-variable\", name: \"duration\" },\n\t\t\t{\n\t\t\t\ttype: \"local-variable\",\n\t\t\t\tname: \"localUnit\",\n\t\t\t\tvalue: { type: \"expression\", arg: { type: \"literal\", value: \"day\" } },\n\t\t\t},\n\t\t],\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedDuration\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{ name: \"unit\", value: { type: \"literal\", value: \"$localUnit\" } },\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedDuration = registry.relativetime(\"en\", i?.duration, { unit: /** @type {import(\"../registry.js\").RelativeTimeFormatUnit} */ (localUnit) });'\n\t);\n});\n\ntest(\"accepts dollar-prefixed relative time formatter unit options with non-identifier input names\", () => {\n\tconst code = compileLocalVariable({\n\t\tlocale: \"en\",\n\t\tdeclaration: {\n\t\t\ttype: \"local-variable\",\n\t\t\tname: \"formattedDuration\",\n\t\t\tvalue: {\n\t\t\t\ttype: \"expression\",\n\t\t\t\targ: { type: \"variable-reference\", name: \"duration\" },\n\t\t\t\tannotation: {\n\t\t\t\t\ttype: \"function-reference\",\n\t\t\t\t\tname: \"relativetime\",\n\t\t\t\t\toptions: [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\tname: \"unit\",\n\t\t\t\t\t\t\tvalue: { type: \"literal\", value: \"$relative-unit\" },\n\t\t\t\t\t\t},\n\t\t\t\t\t],\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t});\n\n\texpect(code).toEqual(\n\t\t'const formattedDuration = registry.relativetime(\"en\", i?.duration, { unit: /** @type {import(\"../registry.js\").RelativeTimeFormatUnit} */ (i?.[\"relative-unit\"]) });'\n\t);\n});\n"]}
@@ -34,7 +34,11 @@ function compileMessageWithOneVariant(declarations, message, variants, matchType
34
34
  const compiledLocalVariables = [];
35
35
  for (const declaration of declarations) {
36
36
  if (declaration.type === "local-variable") {
37
- compiledLocalVariables.push(compileLocalVariable({ declaration, locale: message.locale }));
37
+ compiledLocalVariables.push(compileLocalVariable({
38
+ declaration,
39
+ declarations,
40
+ locale: message.locale,
41
+ }));
38
42
  }
39
43
  }
40
44
  if (!hasMarkup) {
@@ -126,7 +130,11 @@ function compileMessageWithMultipleVariants(declarations, message, variants, mat
126
130
  const compiledLocalVariables = [];
127
131
  for (const declaration of declarations) {
128
132
  if (declaration.type === "local-variable") {
129
- compiledLocalVariables.push(compileLocalVariable({ declaration, locale: message.locale }));
133
+ compiledLocalVariables.push(compileLocalVariable({
134
+ declaration,
135
+ declarations,
136
+ locale: message.locale,
137
+ }));
130
138
  }
131
139
  }
132
140
  if (!hasMarkup) {
@@ -1 +1 @@
1
- {"version":3,"file":"compile-message.js","sourceRoot":"","sources":["../../src/compiler/compile-message.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,E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c;QAC5B,IAAI,CAAC,IAAI,KAAK,YAAY;QAC1B,IAAI,CAAC,IAAI,KAAK,mBAAmB,CAClC,CAAC;AACH,CAAC","sourcesContent":["import type { Declaration, Message, Pattern, Variant } from \"@inlang/sdk\";\nimport { compilePattern } from \"./compile-pattern.js\";\nimport type { Compiled } from \"./types.js\";\nimport { inputsType, type InputMatchTypes } from \"./jsdoc-types.js\";\nimport { compileLocalVariable } from \"./compile-local-variable.js\";\nimport { renderInputMatchCondition } from \"./match-literals.js\";\nimport { compileInputAccess } from \"./variable-access.js\";\n\n/**\n * Returns the compiled message as a string\n *\n */\nexport const compileMessage = (\n\tdeclarations: Declaration[],\n\tmessage: Message,\n\tvariants: Variant[],\n\tmatchTypes?: InputMatchTypes,\n\tinputTypeAliasName?: string\n): Compiled<Message> => {\n\t// return empty string instead?\n\tif (variants.length == 0) {\n\t\tthrow new Error(\"Message must have at least one variant\");\n\t}\n\n\tconst hasMultipleVariants = variants.length > 1;\n\treturn hasMultipleVariants\n\t\t? compileMessageWithMultipleVariants(\n\t\t\t\tdeclarations,\n\t\t\t\tmessage,\n\t\t\t\tvariants,\n\t\t\t\tmatchTypes,\n\t\t\t\tinputTypeAliasName\n\t\t\t)\n\t\t: compileMessageWithOneVariant(\n\t\t\t\tdeclarations,\n\t\t\t\tmessage,\n\t\t\t\tvariants,\n\t\t\t\tmatchTypes,\n\t\t\t\tinputTypeAliasName\n\t\t\t);\n};\n\nfunction compileMessageWithOneVariant(\n\tdeclarations: Declaration[],\n\tmessage: Message,\n\tvariants: Variant[],\n\tmatchTypes?: InputMatchTypes,\n\tinputTypeAliasName?: string\n): Compiled<Message> {\n\tconst variant = variants[0];\n\tif (!variant || variants.length !== 1) {\n\t\tthrow new Error(\"Message must have exactly one variant\");\n\t}\n\n\tconst hasMarkup = patternHasMarkup(variant.pattern);\n\tconst inputs = declarations.filter((decl) => decl.type === \"input-variable\");\n\tconst hasInputs = inputs.length > 0;\n\tconst messageInputType = inputTypeAliasName ?? inputsType(inputs, matchTypes);\n\tconst compiledPattern = compilePattern({\n\t\tpattern: variant.pattern,\n\t\tdeclarations,\n\t\tlocale: message.locale,\n\t});\n\n\tconst compiledLocalVariables = [];\n\n\tfor (const declaration of declarations) {\n\t\tif (declaration.type === \"local-variable\") {\n\t\t\tcompiledLocalVariables.push(\n\t\t\t\tcompileLocalVariable({ declaration, locale: message.locale })\n\t\t\t);\n\t\t}\n\t}\n\n\tif (!hasMarkup) {\n\t\tconst code = `/** @type {(inputs: ${messageInputType}) => LocalizedString} */ (${hasInputs ? \"i\" : \"\"}) => {\n\t${compiledLocalVariables.join(\"\\n\\t\")}return /** @type {LocalizedString} */ (${compiledPattern.code})\n};`;\n\n\t\treturn { code, node: message };\n\t}\n\n\tconst compiledPartsPattern = compilePattern({\n\t\tpattern: variant.pattern,\n\t\tdeclarations,\n\t\tmode: \"parts\",\n\t\tlocale: message.locale,\n\t});\n\tconst localVariablesCode = compiledLocalVariables.length\n\t\t? compiledLocalVariables.join(\"\\n\\t\") + \"\\n\\t\"\n\t\t: \"\";\n\tconst inputType = messageInputType;\n\tconst messageInput = hasInputs ? \"i\" : \"\";\n\n\tconst partsCode = `/** @type {((inputs: ${inputType}) => LocalizedString) & { parts: (inputs: ${inputType}) => import('../runtime.js').MessagePart[] }} */ (\n\t/* @__PURE__ */ Object.assign(\n\t\t/** @type {(inputs: ${inputType}) => LocalizedString} */ ((${messageInput}) => {\n\t\t\t${localVariablesCode}return /** @type {LocalizedString} */ (${compiledPattern.code})\n\t\t}),\n\t\t{\n\t\t\tparts: /** @type {(inputs: ${inputType}) => import('../runtime.js').MessagePart[]} */ ((${messageInput}) => {\n\t\t\t\t${localVariablesCode}return /** @type {import('../runtime.js').MessagePart[]} */ (${compiledPartsPattern.code})\n\t\t\t})\n\t\t}\n\t)\n);`;\n\n\treturn { code: partsCode, node: message };\n}\n\nfunction compileMessageWithMultipleVariants(\n\tdeclarations: Declaration[],\n\tmessage: Message,\n\tvariants: Variant[],\n\tmatchTypes?: InputMatchTypes,\n\tinputTypeAliasName?: string\n): Compiled<Message> {\n\tif (variants.length <= 1) {\n\t\tthrow new Error(\"Message must have more than one variant\");\n\t}\n\n\tconst hasMarkup = variants.some((variant) =>\n\t\tpatternHasMarkup(variant.pattern)\n\t);\n\tconst inputs = declarations.filter((decl) => decl.type === \"input-variable\");\n\tconst hasInputs = inputs.length > 0;\n\tconst messageInputType = inputTypeAliasName ?? inputsType(inputs, matchTypes);\n\n\t// TODO make sure that matchers use keys instead of indexes\n\tconst compiledVariants = [];\n\tconst compiledPartsVariants = [];\n\n\tlet hasCatchAll = false;\n\n\tfor (const variant of variants) {\n\t\tconst compiledPattern = compilePattern({\n\t\t\tpattern: variant.pattern,\n\t\t\tdeclarations,\n\t\t\tlocale: message.locale,\n\t\t});\n\t\tconst compiledPartsPattern = hasMarkup\n\t\t\t? compilePattern({\n\t\t\t\t\tpattern: variant.pattern,\n\t\t\t\t\tdeclarations,\n\t\t\t\t\tmode: \"parts\",\n\t\t\t\t\tlocale: message.locale,\n\t\t\t\t})\n\t\t\t: undefined;\n\n\t\tconst isCatchAll = variant.matches.every(\n\t\t\t(match) => match.type === \"catchall-match\"\n\t\t);\n\n\t\tif (isCatchAll) {\n\t\t\tcompiledVariants.push(\n\t\t\t\t`return /** @type {LocalizedString} */ (${compiledPattern.code})`\n\t\t\t);\n\t\t\tif (compiledPartsPattern) {\n\t\t\t\tcompiledPartsVariants.push(\n\t\t\t\t\t`return /** @type {import('../runtime.js').MessagePart[]} */ (${compiledPartsPattern.code})`\n\t\t\t\t);\n\t\t\t}\n\t\t\thasCatchAll = true;\n\t\t}\n\n\t\tconst conditions: string[] = [];\n\n\t\tfor (const match of variant.matches) {\n\t\t\t// catch all matches are not used in the conditions\n\t\t\tif (match.type !== \"literal-match\") {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst variableType = declarations.find(\n\t\t\t\t(decl) => decl.name === match.key\n\t\t\t)?.type;\n\t\t\tif (variableType === \"input-variable\") {\n\t\t\t\tconditions.push(\n\t\t\t\t\trenderInputMatchCondition(compileInputAccess(match.key), match.value)\n\t\t\t\t);\n\t\t\t} else if (variableType === \"local-variable\") {\n\t\t\t\tconditions.push(`${match.key} === ${JSON.stringify(match.value)}`);\n\t\t\t}\n\t\t}\n\n\t\tif (conditions.length === 0) continue;\n\t\tcompiledVariants.push(\n\t\t\t`if (${conditions.join(\" && \")}) return /** @type {LocalizedString} */ (${compiledPattern.code});`\n\t\t);\n\t\tif (compiledPartsPattern) {\n\t\t\tcompiledPartsVariants.push(\n\t\t\t\t`if (${conditions.join(\" && \")}) return /** @type {import('../runtime.js').MessagePart[]} */ (${compiledPartsPattern.code});`\n\t\t\t);\n\t\t}\n\t}\n\n\tconst compiledLocalVariables = [];\n\n\tfor (const declaration of declarations) {\n\t\tif (declaration.type === \"local-variable\") {\n\t\t\tcompiledLocalVariables.push(\n\t\t\t\tcompileLocalVariable({ declaration, locale: message.locale })\n\t\t\t);\n\t\t}\n\t}\n\n\tif (!hasMarkup) {\n\t\tconst code = `/** @type {(inputs: ${messageInputType}) => LocalizedString} */ (${hasInputs ? \"i\" : \"\"}) => {${compiledLocalVariables.join(\"\\n\\t\")}\n\t${compiledVariants.join(\"\\n\\t\")}\n\t${hasCatchAll ? \"\" : `return /** @type {LocalizedString} */ (\"${message.bundleId}\");`}\n};`;\n\n\t\treturn { code, node: message };\n\t}\n\n\tconst localVariablesCode = compiledLocalVariables.length\n\t\t? compiledLocalVariables.join(\"\\n\\t\") + \"\\n\\t\"\n\t\t: \"\";\n\tconst stringVariantsCode = compiledVariants.length\n\t\t? compiledVariants.join(\"\\n\\t\") + \"\\n\\t\"\n\t\t: \"\";\n\tconst partsVariantsCode = compiledPartsVariants.length\n\t\t? compiledPartsVariants.join(\"\\n\\t\") + \"\\n\\t\"\n\t\t: \"\";\n\tconst inputType = messageInputType;\n\tconst fallbackParts = `[{ type: \"text\", value: ${JSON.stringify(message.bundleId)} }]`;\n\tconst messageInput = hasInputs ? \"i\" : \"\";\n\n\tconst code = `/** @type {((inputs: ${inputType}) => LocalizedString) & { parts: (inputs: ${inputType}) => import('../runtime.js').MessagePart[] }} */ (\n\t/* @__PURE__ */ Object.assign(\n\t\t/** @type {(inputs: ${inputType}) => LocalizedString} */ ((${messageInput}) => {\n\t\t\t${localVariablesCode}${stringVariantsCode}${\n\t\t\t\thasCatchAll\n\t\t\t\t\t? \"\"\n\t\t\t\t\t: `return /** @type {LocalizedString} */ (${JSON.stringify(message.bundleId)});`\n\t\t\t}\n\t\t}),\n\t\t{\n\t\t\tparts: /** @type {(inputs: ${inputType}) => import('../runtime.js').MessagePart[]} */ ((${messageInput}) => {\n\t\t\t\t${localVariablesCode}${partsVariantsCode}${\n\t\t\t\t\thasCatchAll\n\t\t\t\t\t\t? \"\"\n\t\t\t\t\t\t: `return /** @type {import('../runtime.js').MessagePart[]} */ (${fallbackParts});`\n\t\t\t\t}\n\t\t\t})\n\t\t}\n\t)\n);`;\n\n\treturn { code, node: message };\n}\n\nfunction patternHasMarkup(pattern: Pattern): boolean {\n\treturn pattern.some(\n\t\t(part) =>\n\t\t\tpart.type === \"markup-start\" ||\n\t\t\tpart.type === \"markup-end\" ||\n\t\t\tpart.type === \"markup-standalone\"\n\t);\n}\n"]}
1
+ {"version":3,"file":"compile-message.js","sourceRoot":"","sources":["../../src/compiler/compile-message.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,cAAc,EAAE,MAAM,sBAAsB,CAAC;AAEtD,OAAO,EAAE,UAAU,EAAwB,MAAM,kBAAkB,CAAC;AACpE,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,OAAO,EAAE,yBAAyB,EAAE,MAAM,qBAAqB,CAAC;AAChE,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D;;;GAGG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,CAC7B,YAA2B,EAC3B,OAAgB,EAChB,QAAmB,EACnB,UAA4B,EAC5B,k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,CAAC,MAAM;QACrD,CAAC,CAAC,qBAAqB,CAAC,IAAI,CAAC,MAAM,CAAC,GAAG,MAAM;QAC7C,CAAC,CAAC,EAAE,CAAC;IACN,MAAM,SAAS,GAAG,gBAAgB,CAAC;IACnC,MAAM,aAAa,GAAG,2BAA2B,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAC;IACvF,MAAM,YAAY,GAAG,SAAS,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC;IAE1C,MAAM,IAAI,GAAG,wBAAwB,SAAS,6CAA6C,SAAS;;wBAE7E,SAAS,8BAA8B,YAAY;KACtE,kBAAkB,GAAG,kBAAkB,GACxC,WAAW;QACV,CAAC,CAAC,EAAE;QACJ,CAAC,CAAC,0CAA0C,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,QAAQ,CAAC,IAC9E;;;gCAG6B,SAAS,oDAAoD,YAAY;MACnG,kBAAkB,GAAG,iBAAiB,GACvC,WAAW;QACV,CAAC,CAAC,EAAE;QACJ,CAAC,CAAC,gEAAgE,aAAa,IACjF;;;;GAID,CAAC;IAEH,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,OAAO,EAAE,CAAC;AAChC,CAAC;AAED,SAAS,gBAAgB,CAAC,OAAgB;IACzC,OAAO,OAAO,CAAC,IAAI,CAClB,CAAC,IAAI,EAAE,EAAE,CACR,IAAI,CAAC,IAAI,KAAK,cAAc;QAC5B,IAAI,CAAC,IAAI,KAAK,YAAY;QAC1B,IAAI,CAAC,IAAI,KAAK,mBAAmB,CAClC,CAAC;AACH,CAAC","sourcesContent":["import type { Declaration, Message, Pattern, Variant } from \"@inlang/sdk\";\nimport { compilePattern } from \"./compile-pattern.js\";\nimport type { Compiled } from \"./types.js\";\nimport { inputsType, type InputMatchTypes } from \"./jsdoc-types.js\";\nimport { compileLocalVariable } from \"./compile-local-variable.js\";\nimport { renderInputMatchCondition } from \"./match-literals.js\";\nimport { compileInputAccess } from \"./variable-access.js\";\n\n/**\n * Returns the compiled message as a string\n *\n */\nexport const compileMessage = (\n\tdeclarations: Declaration[],\n\tmessage: Message,\n\tvariants: Variant[],\n\tmatchTypes?: InputMatchTypes,\n\tinputTypeAliasName?: string\n): Compiled<Message> => {\n\t// return empty string instead?\n\tif (variants.length == 0) {\n\t\tthrow new Error(\"Message must have at least one variant\");\n\t}\n\n\tconst hasMultipleVariants = variants.length > 1;\n\treturn hasMultipleVariants\n\t\t? compileMessageWithMultipleVariants(\n\t\t\t\tdeclarations,\n\t\t\t\tmessage,\n\t\t\t\tvariants,\n\t\t\t\tmatchTypes,\n\t\t\t\tinputTypeAliasName\n\t\t\t)\n\t\t: compileMessageWithOneVariant(\n\t\t\t\tdeclarations,\n\t\t\t\tmessage,\n\t\t\t\tvariants,\n\t\t\t\tmatchTypes,\n\t\t\t\tinputTypeAliasName\n\t\t\t);\n};\n\nfunction compileMessageWithOneVariant(\n\tdeclarations: Declaration[],\n\tmessage: Message,\n\tvariants: Variant[],\n\tmatchTypes?: InputMatchTypes,\n\tinputTypeAliasName?: string\n): Compiled<Message> {\n\tconst variant = variants[0];\n\tif (!variant || variants.length !== 1) {\n\t\tthrow new Error(\"Message must have exactly one variant\");\n\t}\n\n\tconst hasMarkup = patternHasMarkup(variant.pattern);\n\tconst inputs = declarations.filter((decl) => decl.type === \"input-variable\");\n\tconst hasInputs = inputs.length > 0;\n\tconst messageInputType = inputTypeAliasName ?? inputsType(inputs, matchTypes);\n\tconst compiledPattern = compilePattern({\n\t\tpattern: variant.pattern,\n\t\tdeclarations,\n\t\tlocale: message.locale,\n\t});\n\n\tconst compiledLocalVariables = [];\n\n\tfor (const declaration of declarations) {\n\t\tif (declaration.type === \"local-variable\") {\n\t\t\tcompiledLocalVariables.push(\n\t\t\t\tcompileLocalVariable({\n\t\t\t\t\tdeclaration,\n\t\t\t\t\tdeclarations,\n\t\t\t\t\tlocale: message.locale,\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\t}\n\n\tif (!hasMarkup) {\n\t\tconst code = `/** @type {(inputs: ${messageInputType}) => LocalizedString} */ (${hasInputs ? \"i\" : \"\"}) => {\n\t${compiledLocalVariables.join(\"\\n\\t\")}return /** @type {LocalizedString} */ (${compiledPattern.code})\n};`;\n\n\t\treturn { code, node: message };\n\t}\n\n\tconst compiledPartsPattern = compilePattern({\n\t\tpattern: variant.pattern,\n\t\tdeclarations,\n\t\tmode: \"parts\",\n\t\tlocale: message.locale,\n\t});\n\tconst localVariablesCode = compiledLocalVariables.length\n\t\t? compiledLocalVariables.join(\"\\n\\t\") + \"\\n\\t\"\n\t\t: \"\";\n\tconst inputType = messageInputType;\n\tconst messageInput = hasInputs ? \"i\" : \"\";\n\n\tconst partsCode = `/** @type {((inputs: ${inputType}) => LocalizedString) & { parts: (inputs: ${inputType}) => import('../runtime.js').MessagePart[] }} */ (\n\t/* @__PURE__ */ Object.assign(\n\t\t/** @type {(inputs: ${inputType}) => LocalizedString} */ ((${messageInput}) => {\n\t\t\t${localVariablesCode}return /** @type {LocalizedString} */ (${compiledPattern.code})\n\t\t}),\n\t\t{\n\t\t\tparts: /** @type {(inputs: ${inputType}) => import('../runtime.js').MessagePart[]} */ ((${messageInput}) => {\n\t\t\t\t${localVariablesCode}return /** @type {import('../runtime.js').MessagePart[]} */ (${compiledPartsPattern.code})\n\t\t\t})\n\t\t}\n\t)\n);`;\n\n\treturn { code: partsCode, node: message };\n}\n\nfunction compileMessageWithMultipleVariants(\n\tdeclarations: Declaration[],\n\tmessage: Message,\n\tvariants: Variant[],\n\tmatchTypes?: InputMatchTypes,\n\tinputTypeAliasName?: string\n): Compiled<Message> {\n\tif (variants.length <= 1) {\n\t\tthrow new Error(\"Message must have more than one variant\");\n\t}\n\n\tconst hasMarkup = variants.some((variant) =>\n\t\tpatternHasMarkup(variant.pattern)\n\t);\n\tconst inputs = declarations.filter((decl) => decl.type === \"input-variable\");\n\tconst hasInputs = inputs.length > 0;\n\tconst messageInputType = inputTypeAliasName ?? inputsType(inputs, matchTypes);\n\n\t// TODO make sure that matchers use keys instead of indexes\n\tconst compiledVariants = [];\n\tconst compiledPartsVariants = [];\n\n\tlet hasCatchAll = false;\n\n\tfor (const variant of variants) {\n\t\tconst compiledPattern = compilePattern({\n\t\t\tpattern: variant.pattern,\n\t\t\tdeclarations,\n\t\t\tlocale: message.locale,\n\t\t});\n\t\tconst compiledPartsPattern = hasMarkup\n\t\t\t? compilePattern({\n\t\t\t\t\tpattern: variant.pattern,\n\t\t\t\t\tdeclarations,\n\t\t\t\t\tmode: \"parts\",\n\t\t\t\t\tlocale: message.locale,\n\t\t\t\t})\n\t\t\t: undefined;\n\n\t\tconst isCatchAll = variant.matches.every(\n\t\t\t(match) => match.type === \"catchall-match\"\n\t\t);\n\n\t\tif (isCatchAll) {\n\t\t\tcompiledVariants.push(\n\t\t\t\t`return /** @type {LocalizedString} */ (${compiledPattern.code})`\n\t\t\t);\n\t\t\tif (compiledPartsPattern) {\n\t\t\t\tcompiledPartsVariants.push(\n\t\t\t\t\t`return /** @type {import('../runtime.js').MessagePart[]} */ (${compiledPartsPattern.code})`\n\t\t\t\t);\n\t\t\t}\n\t\t\thasCatchAll = true;\n\t\t}\n\n\t\tconst conditions: string[] = [];\n\n\t\tfor (const match of variant.matches) {\n\t\t\t// catch all matches are not used in the conditions\n\t\t\tif (match.type !== \"literal-match\") {\n\t\t\t\tcontinue;\n\t\t\t}\n\t\t\tconst variableType = declarations.find(\n\t\t\t\t(decl) => decl.name === match.key\n\t\t\t)?.type;\n\t\t\tif (variableType === \"input-variable\") {\n\t\t\t\tconditions.push(\n\t\t\t\t\trenderInputMatchCondition(compileInputAccess(match.key), match.value)\n\t\t\t\t);\n\t\t\t} else if (variableType === \"local-variable\") {\n\t\t\t\tconditions.push(`${match.key} === ${JSON.stringify(match.value)}`);\n\t\t\t}\n\t\t}\n\n\t\tif (conditions.length === 0) continue;\n\t\tcompiledVariants.push(\n\t\t\t`if (${conditions.join(\" && \")}) return /** @type {LocalizedString} */ (${compiledPattern.code});`\n\t\t);\n\t\tif (compiledPartsPattern) {\n\t\t\tcompiledPartsVariants.push(\n\t\t\t\t`if (${conditions.join(\" && \")}) return /** @type {import('../runtime.js').MessagePart[]} */ (${compiledPartsPattern.code});`\n\t\t\t);\n\t\t}\n\t}\n\n\tconst compiledLocalVariables = [];\n\n\tfor (const declaration of declarations) {\n\t\tif (declaration.type === \"local-variable\") {\n\t\t\tcompiledLocalVariables.push(\n\t\t\t\tcompileLocalVariable({\n\t\t\t\t\tdeclaration,\n\t\t\t\t\tdeclarations,\n\t\t\t\t\tlocale: message.locale,\n\t\t\t\t})\n\t\t\t);\n\t\t}\n\t}\n\n\tif (!hasMarkup) {\n\t\tconst code = `/** @type {(inputs: ${messageInputType}) => LocalizedString} */ (${hasInputs ? \"i\" : \"\"}) => {${compiledLocalVariables.join(\"\\n\\t\")}\n\t${compiledVariants.join(\"\\n\\t\")}\n\t${hasCatchAll ? \"\" : `return /** @type {LocalizedString} */ (\"${message.bundleId}\");`}\n};`;\n\n\t\treturn { code, node: message };\n\t}\n\n\tconst localVariablesCode = compiledLocalVariables.length\n\t\t? compiledLocalVariables.join(\"\\n\\t\") + \"\\n\\t\"\n\t\t: \"\";\n\tconst stringVariantsCode = compiledVariants.length\n\t\t? compiledVariants.join(\"\\n\\t\") + \"\\n\\t\"\n\t\t: \"\";\n\tconst partsVariantsCode = compiledPartsVariants.length\n\t\t? compiledPartsVariants.join(\"\\n\\t\") + \"\\n\\t\"\n\t\t: \"\";\n\tconst inputType = messageInputType;\n\tconst fallbackParts = `[{ type: \"text\", value: ${JSON.stringify(message.bundleId)} }]`;\n\tconst messageInput = hasInputs ? \"i\" : \"\";\n\n\tconst code = `/** @type {((inputs: ${inputType}) => LocalizedString) & { parts: (inputs: ${inputType}) => import('../runtime.js').MessagePart[] }} */ (\n\t/* @__PURE__ */ Object.assign(\n\t\t/** @type {(inputs: ${inputType}) => LocalizedString} */ ((${messageInput}) => {\n\t\t\t${localVariablesCode}${stringVariantsCode}${\n\t\t\t\thasCatchAll\n\t\t\t\t\t? \"\"\n\t\t\t\t\t: `return /** @type {LocalizedString} */ (${JSON.stringify(message.bundleId)});`\n\t\t\t}\n\t\t}),\n\t\t{\n\t\t\tparts: /** @type {(inputs: ${inputType}) => import('../runtime.js').MessagePart[]} */ ((${messageInput}) => {\n\t\t\t\t${localVariablesCode}${partsVariantsCode}${\n\t\t\t\t\thasCatchAll\n\t\t\t\t\t\t? \"\"\n\t\t\t\t\t\t: `return /** @type {import('../runtime.js').MessagePart[]} */ (${fallbackParts});`\n\t\t\t\t}\n\t\t\t})\n\t\t}\n\t)\n);`;\n\n\treturn { code, node: message };\n}\n\nfunction patternHasMarkup(pattern: Pattern): boolean {\n\treturn pattern.some(\n\t\t(part) =>\n\t\t\tpart.type === \"markup-start\" ||\n\t\t\tpart.type === \"markup-end\" ||\n\t\t\tpart.type === \"markup-standalone\"\n\t);\n}\n"]}