@sebbro/paraglide-lite 0.1.1 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/bundler-plugins/per-locale-build/index.js +5 -3
- package/dist/bundler-plugins/per-locale-build/index.js.map +1 -1
- package/dist/bundler-plugins/per-locale-build/vite-environments.js +12 -7
- package/dist/bundler-plugins/per-locale-build/vite-environments.js.map +1 -1
- package/dist/bundler-plugins/plugin-core.d.ts +38 -2
- package/dist/bundler-plugins/plugin-core.js +198 -11
- package/dist/bundler-plugins/plugin-core.js.map +1 -1
- package/dist/bundler-plugins/rolldown.test.js +15 -6
- package/dist/bundler-plugins/rolldown.test.js.map +1 -1
- package/dist/bundler-plugins/unplugin.js +5 -0
- package/dist/bundler-plugins/unplugin.js.map +1 -1
- package/dist/bundler-plugins/unplugin.test.js +67 -0
- package/dist/bundler-plugins/unplugin.test.js.map +1 -1
- package/dist/bundler-plugins/virtual-modules.test.js +64 -1
- package/dist/bundler-plugins/virtual-modules.test.js.map +1 -1
- package/dist/cli/commands/compile/command.js +8 -1
- package/dist/cli/commands/compile/command.js.map +1 -1
- package/dist/cli/commands/compile/command.test.js +51 -0
- package/dist/cli/commands/compile/command.test.js.map +1 -1
- package/dist/compiler/compile-annotation.d.ts +2 -2
- package/dist/compiler/compile-annotation.js +9 -9
- package/dist/compiler/compile-annotation.js.map +1 -1
- package/dist/compiler/compile-local-variable.d.ts +2 -1
- package/dist/compiler/compile-local-variable.js +4 -4
- package/dist/compiler/compile-local-variable.js.map +1 -1
- package/dist/compiler/compile-local-variable.test.js +82 -0
- package/dist/compiler/compile-local-variable.test.js.map +1 -1
- package/dist/compiler/compile-message.js +10 -2
- package/dist/compiler/compile-message.js.map +1 -1
- package/dist/compiler/compile-message.test.js +132 -2
- package/dist/compiler/compile-message.test.js.map +1 -1
- package/dist/compiler/compile-pattern.js +4 -2
- package/dist/compiler/compile-pattern.js.map +1 -1
- package/dist/compiler/compile-pattern.test.js +31 -0
- package/dist/compiler/compile-pattern.test.js.map +1 -1
- package/dist/compiler/compile.bench.js +1 -1
- package/dist/compiler/compile.bench.js.map +1 -1
- package/dist/compiler/compile.test.js +14 -0
- package/dist/compiler/compile.test.js.map +1 -1
- package/dist/compiler/compiler-options.d.ts +9 -1
- package/dist/compiler/compiler-options.js.map +1 -1
- package/dist/compiler/runtime/create-runtime.d.ts +1 -0
- package/dist/compiler/runtime/create-runtime.js +11 -0
- package/dist/compiler/runtime/create-runtime.js.map +1 -1
- package/dist/compiler/runtime/exec-url-pattern.d.ts +9 -0
- package/dist/compiler/runtime/exec-url-pattern.js +31 -0
- package/dist/compiler/runtime/exec-url-pattern.js.map +1 -0
- package/dist/compiler/runtime/extract-locale-from-url.js +9 -4
- package/dist/compiler/runtime/extract-locale-from-url.js.map +1 -1
- package/dist/compiler/runtime/generate-static-localized-urls.js +13 -6
- package/dist/compiler/runtime/generate-static-localized-urls.js.map +1 -1
- package/dist/compiler/runtime/generate-static-localized-urls.test.js +38 -0
- package/dist/compiler/runtime/generate-static-localized-urls.test.js.map +1 -1
- package/dist/compiler/runtime/localize-href.bench.d.ts +1 -0
- package/dist/compiler/runtime/localize-href.bench.js +68 -0
- package/dist/compiler/runtime/localize-href.bench.js.map +1 -0
- package/dist/compiler/runtime/localize-href.test.js +35 -0
- package/dist/compiler/runtime/localize-href.test.js.map +1 -1
- package/dist/compiler/runtime/localize-url.d.ts +14 -0
- package/dist/compiler/runtime/localize-url.js +338 -19
- package/dist/compiler/runtime/localize-url.js.map +1 -1
- package/dist/compiler/runtime/localize-url.test.js +139 -0
- package/dist/compiler/runtime/localize-url.test.js.map +1 -1
- package/dist/compiler/runtime/normalize-trailing-slash.d.ts +10 -0
- package/dist/compiler/runtime/normalize-trailing-slash.js +23 -0
- package/dist/compiler/runtime/normalize-trailing-slash.js.map +1 -0
- package/dist/compiler/runtime/route-strategy.js +4 -2
- package/dist/compiler/runtime/route-strategy.js.map +1 -1
- package/dist/compiler/runtime/should-redirect.js +4 -1
- package/dist/compiler/runtime/should-redirect.js.map +1 -1
- package/dist/compiler/runtime/should-redirect.test.js +26 -2
- package/dist/compiler/runtime/should-redirect.test.js.map +1 -1
- package/dist/compiler/runtime/type.d.ts +1 -0
- package/dist/compiler/runtime/type.js.map +1 -1
- package/dist/compiler/runtime/url-pattern.d.ts +11 -0
- package/dist/compiler/runtime/url-pattern.js +39 -1
- package/dist/compiler/runtime/url-pattern.js.map +1 -1
- package/dist/compiler/runtime/variables.d.ts +6 -0
- package/dist/compiler/runtime/variables.js +6 -0
- package/dist/compiler/runtime/variables.js.map +1 -1
- package/dist/compiler/server/create-server-file.js +18 -4
- package/dist/compiler/server/create-server-file.js.map +1 -1
- package/dist/compiler/server/create-server-file.test.js +76 -0
- package/dist/compiler/server/create-server-file.test.js.map +1 -1
- package/dist/compiler/server/middleware.js +6 -2
- package/dist/compiler/server/middleware.js.map +1 -1
- package/dist/compiler/server/middleware.test.js +30 -0
- package/dist/compiler/server/middleware.test.js.map +1 -1
- package/dist/compiler/variable-access.d.ts +2 -0
- package/dist/compiler/variable-access.js +6 -0
- package/dist/compiler/variable-access.js.map +1 -1
- package/dist/index.test.d.ts +1 -0
- package/dist/index.test.js +9 -0
- package/dist/index.test.js.map +1 -0
- package/dist/messages/load-messages.js +4 -5
- package/dist/messages/load-messages.js.map +1 -1
- package/dist/services/env-variables/index.js +1 -1
- package/dist/services/env-variables/index.js.map +1 -1
- package/dist/services/file-handling/write-output.bench.d.ts +1 -0
- package/dist/services/file-handling/write-output.bench.js +24 -0
- package/dist/services/file-handling/write-output.bench.js.map +1 -0
- package/dist/services/file-handling/write-output.d.ts +5 -0
- package/dist/services/file-handling/write-output.js +54 -9
- package/dist/services/file-handling/write-output.js.map +1 -1
- package/dist/services/file-handling/write-output.test.js +27 -0
- package/dist/services/file-handling/write-output.test.js.map +1 -1
- package/dist/services/logger/index.js +2 -0
- package/dist/services/logger/index.js.map +1 -1
- package/package.json +86 -86
|
@@ -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({
|
|
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({
|
|
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,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,kBAA2B,EACP,EAAE;IACtB,+BAA+B;IAC/B,IAAI,QAAQ,CAA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;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 \"../messages/ast.js\";\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,kBAA2B,EACP,EAAE;IACtB,+BAA+B;IAC/B,IAAI,QAAQ,CAAC,MAAM,IAAI,CAAC,EAAE,CAAC;QAC1B,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAC3D,CAAC;IAED,MAAM,mBAAmB,GAAG,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC;IAChD,OAAO,mBAAmB;QACzB,CAAC,CAAC,kCAAkC,CAClC,YAAY,EACZ,OAAO,EACP,QAAQ,EACR,UAAU,EACV,kBAAkB,CAClB;QACF,CAAC,CAAC,4BAA4B,CAC5B,YAAY,EACZ,OAAO,EACP,QAAQ,EACR,UAA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AA8B,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 \"../messages/ast.js\";\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"]}
|
|
@@ -22,11 +22,139 @@ test("compiles a message with a single variant", async () => {
|
|
|
22
22
|
btoa("export const some_message =" + compiled.code));
|
|
23
23
|
expect(some_message()).toBe("Hello");
|
|
24
24
|
});
|
|
25
|
+
test.each(["single", "multiple"])("compiles chained local variables in %s-variant messages", async (variantCount) => {
|
|
26
|
+
const declarations = [
|
|
27
|
+
{ type: "input-variable", name: "count" },
|
|
28
|
+
{
|
|
29
|
+
type: "local-variable",
|
|
30
|
+
name: "zebra",
|
|
31
|
+
value: {
|
|
32
|
+
type: "expression",
|
|
33
|
+
arg: { type: "variable-reference", name: "count" },
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
type: "local-variable",
|
|
38
|
+
name: "alpha",
|
|
39
|
+
value: {
|
|
40
|
+
type: "expression",
|
|
41
|
+
arg: { type: "variable-reference", name: "zebra" },
|
|
42
|
+
annotation: {
|
|
43
|
+
type: "function-reference",
|
|
44
|
+
name: "number",
|
|
45
|
+
options: [],
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
},
|
|
49
|
+
];
|
|
50
|
+
const message = {
|
|
51
|
+
locale: "en",
|
|
52
|
+
bundleId: "chained_locals",
|
|
53
|
+
id: "chained-locals-id",
|
|
54
|
+
selectors: variantCount === "multiple"
|
|
55
|
+
? [{ type: "variable-reference", name: "count" }]
|
|
56
|
+
: [],
|
|
57
|
+
};
|
|
58
|
+
const variants = [
|
|
59
|
+
{
|
|
60
|
+
id: "1",
|
|
61
|
+
messageId: "chained-locals-id",
|
|
62
|
+
matches: variantCount === "multiple"
|
|
63
|
+
? [{ type: "literal-match", key: "count", value: "1" }]
|
|
64
|
+
: [],
|
|
65
|
+
pattern: [
|
|
66
|
+
{
|
|
67
|
+
type: "expression",
|
|
68
|
+
arg: { type: "variable-reference", name: "alpha" },
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
},
|
|
72
|
+
];
|
|
73
|
+
if (variantCount === "multiple") {
|
|
74
|
+
variants.push({
|
|
75
|
+
id: "2",
|
|
76
|
+
messageId: "chained-locals-id",
|
|
77
|
+
matches: [{ type: "catchall-match", key: "count" }],
|
|
78
|
+
pattern: [
|
|
79
|
+
{
|
|
80
|
+
type: "expression",
|
|
81
|
+
arg: { type: "variable-reference", name: "alpha" },
|
|
82
|
+
},
|
|
83
|
+
],
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
const compiled = compileMessage(declarations, message, variants);
|
|
87
|
+
expect(compiled.code).toContain('const alpha = registry.number("en", zebra, {});');
|
|
88
|
+
const { chained_locals } = await import("data:text/javascript;base64," +
|
|
89
|
+
btoa(createRegistry() +
|
|
90
|
+
"\nexport const chained_locals = " +
|
|
91
|
+
compiled.code.replaceAll("registry.", "")));
|
|
92
|
+
expect(chained_locals({ count: 42 })).toBe("42");
|
|
93
|
+
});
|
|
94
|
+
test("evaluates formatter options that reference earlier local variables", async () => {
|
|
95
|
+
const declarations = [
|
|
96
|
+
{ type: "input-variable", name: "amount" },
|
|
97
|
+
{ type: "input-variable", name: "precision" },
|
|
98
|
+
{
|
|
99
|
+
type: "local-variable",
|
|
100
|
+
name: "digits",
|
|
101
|
+
value: {
|
|
102
|
+
type: "expression",
|
|
103
|
+
arg: { type: "variable-reference", name: "precision" },
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
type: "local-variable",
|
|
108
|
+
name: "formatted",
|
|
109
|
+
value: {
|
|
110
|
+
type: "expression",
|
|
111
|
+
arg: { type: "variable-reference", name: "amount" },
|
|
112
|
+
annotation: {
|
|
113
|
+
type: "function-reference",
|
|
114
|
+
name: "number",
|
|
115
|
+
options: [
|
|
116
|
+
{
|
|
117
|
+
name: "minimumFractionDigits",
|
|
118
|
+
value: { type: "variable-reference", name: "digits" },
|
|
119
|
+
},
|
|
120
|
+
],
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
];
|
|
125
|
+
const message = {
|
|
126
|
+
locale: "en",
|
|
127
|
+
bundleId: "local_options",
|
|
128
|
+
id: "local-options-id",
|
|
129
|
+
selectors: [],
|
|
130
|
+
};
|
|
131
|
+
const variants = [
|
|
132
|
+
{
|
|
133
|
+
id: "1",
|
|
134
|
+
messageId: "local-options-id",
|
|
135
|
+
matches: [],
|
|
136
|
+
pattern: [
|
|
137
|
+
{
|
|
138
|
+
type: "expression",
|
|
139
|
+
arg: { type: "variable-reference", name: "formatted" },
|
|
140
|
+
},
|
|
141
|
+
],
|
|
142
|
+
},
|
|
143
|
+
];
|
|
144
|
+
const compiled = compileMessage(declarations, message, variants);
|
|
145
|
+
const { local_options } = await import("data:text/javascript;base64," +
|
|
146
|
+
btoa(createRegistry() +
|
|
147
|
+
"\nexport const local_options = " +
|
|
148
|
+
compiled.code.replaceAll("registry.", "")));
|
|
149
|
+
expect(local_options({ amount: 42, precision: 2 })).toBe("42.00");
|
|
150
|
+
});
|
|
25
151
|
test("compiles pattern-level annotations to registry calls", async () => {
|
|
26
152
|
// https://github.com/opral/paraglide-js/issues/694
|
|
27
153
|
// i18next's `{{count, number}}` imports as an expression with a
|
|
28
154
|
// function-reference annotation directly on the pattern.
|
|
29
|
-
const declarations = [
|
|
155
|
+
const declarations = [
|
|
156
|
+
{ type: "input-variable", name: "count" },
|
|
157
|
+
];
|
|
30
158
|
const message = {
|
|
31
159
|
locale: "en",
|
|
32
160
|
bundleId: "views",
|
|
@@ -62,7 +190,9 @@ test("compiles pattern-level annotations to registry calls", async () => {
|
|
|
62
190
|
expect(views({ count: 1234567.891 })).toBe("1,234,567.891 views");
|
|
63
191
|
});
|
|
64
192
|
test("compiles pattern-level annotations in multi-variant messages", async () => {
|
|
65
|
-
const declarations = [
|
|
193
|
+
const declarations = [
|
|
194
|
+
{ type: "input-variable", name: "count" },
|
|
195
|
+
];
|
|
66
196
|
const message = {
|
|
67
197
|
locale: "en",
|
|
68
198
|
bundleId: "views_multi",
|