Skip to content

Commit

Permalink
use lingui core internals for vue vt and trans macro
Browse files Browse the repository at this point in the history
  • Loading branch information
timofei-iatsenko committed May 22, 2024
1 parent ab300de commit 41dc5f6
Show file tree
Hide file tree
Showing 25 changed files with 1,155 additions and 2,140 deletions.
16 changes: 8 additions & 8 deletions packages/babel-plugin-lingui-macro/src/icu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const escapedMetaOptionsRe = new RegExp(`^_(${metaOptions.join("|")})$`)
export type ParsedResult = {
message: string
values?: Record<string, Expression>
jsxElements?: Record<string, JSXElement>
elements?: Record<string, JSXElement>
}

export type TextToken = {
Expand Down Expand Up @@ -54,18 +54,18 @@ export class ICUMessageFormat {
...message,
message: props.message + message.message,
values: { ...props.values, ...message.values },
jsxElements: { ...props.jsxElements, ...message.jsxElements },
elements: { ...props.elements, ...message.elements },
}),
{
message: "",
values: {},
jsxElements: {},
elements: {},
}
)
}

public processToken(token: Token): ParsedResult {
const jsxElements: ParsedResult["jsxElements"] = {}
const jsxElements: ParsedResult["elements"] = {}

if (token.type === "text") {
return {
Expand Down Expand Up @@ -101,7 +101,7 @@ export class ICUMessageFormat {
const {
message,
values: childValues,
jsxElements: childJsxElements,
elements: childJsxElements,
} = this.fromTokens(value)

Object.assign(values, childValues)
Expand All @@ -116,7 +116,7 @@ export class ICUMessageFormat {
return {
message: `{${token.name}, ${token.format}, ${formatOptions}}`,
values,
jsxElements,
elements: jsxElements,
}
default:
return {
Expand All @@ -132,7 +132,7 @@ export class ICUMessageFormat {
const {
message: childMessage,
values: childValues,
jsxElements: childJsxElements,
elements: childJsxElements,
} = this.fromTokens(child)

message += childMessage
Expand All @@ -144,7 +144,7 @@ export class ICUMessageFormat {
? `<${token.name}>${message}</${token.name}>`
: `<${token.name}/>`,
values: elementValues,
jsxElements,
elements: jsxElements,
}
}

Expand Down
39 changes: 31 additions & 8 deletions packages/babel-plugin-lingui-macro/src/messageDescriptorUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ICUMessageFormat, Tokens } from "./icu"
import { ICUMessageFormat, Tokens, ParsedResult } from "./icu"
import {
SourceLocation,
ObjectProperty,
Expand Down Expand Up @@ -30,7 +30,25 @@ export function createMessageDescriptorFromTokens(
comment?: TextWithLoc | ObjectProperty
} = {}
) {
const { message, values, jsxElements } = buildICUFromTokens(tokens)
return createMessageDescriptor(
buildICUFromTokens(tokens),
oldLoc,
stripNonEssentialProps,
defaults
)
}

export function createMessageDescriptor(
result: Partial<ParsedResult>,
oldLoc: SourceLocation,
stripNonEssentialProps: boolean,
defaults: {
id?: TextWithLoc | ObjectProperty
context?: TextWithLoc | ObjectProperty
comment?: TextWithLoc | ObjectProperty
} = {}
) {
const { message, values, elements } = result

const properties: ObjectProperty[] = []

Expand Down Expand Up @@ -85,12 +103,17 @@ export function createMessageDescriptorFromTokens(
}
}

properties.push(createValuesProperty(MsgDescriptorPropKey.values, values))
properties.push(
createValuesProperty(MsgDescriptorPropKey.components, jsxElements)
)
if (values) {
properties.push(createValuesProperty(MsgDescriptorPropKey.values, values))
}

return createMessageDescriptor(
if (elements) {
properties.push(
createValuesProperty(MsgDescriptorPropKey.components, elements)
)
}

return createMessageDescriptorObjectExpression(
properties,
// preserve line numbers for extractor
oldLoc
Expand Down Expand Up @@ -145,7 +168,7 @@ function getTextFromExpression(exp: Expression): string {
}
}

function createMessageDescriptor(
function createMessageDescriptorObjectExpression(
properties: ObjectProperty[],
oldLoc?: SourceLocation
): ObjectExpression {
Expand Down
11 changes: 11 additions & 0 deletions packages/extractor-vue/src/vue-extractor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { parse, compileTemplate, SFCBlock } from "@vue/compiler-sfc"
import { extractor } from "@lingui/cli/api"
import type { ExtractorCtx, ExtractorType } from "@lingui/conf"
import { isElementNode } from "@lingui/vue/src/common/predicates"

export const vueExtractor: ExtractorType = {
match(filename: string) {
Expand All @@ -27,7 +28,17 @@ export const vueExtractor: ExtractorType = {
filename,
inMap: descriptor.template.map,
id: filename,

compilerOptions: {
nodeTransforms: [
// will be called for each ast "node"
// we want to run our test on the 1st real node
(node, context) => {
// context.
console.log(node, context)
},
],

isTS:
isTsBlock(descriptor.script) || isTsBlock(descriptor.scriptSetup),
},
Expand Down
5 changes: 5 additions & 0 deletions packages/vue/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@
"dist/"
],
"dependencies": {
"@babel/core": "^7.20.12",
"@babel/generator": "^7.20.12",
"@babel/types": "^7.20.12",
"@lingui/babel-plugin-lingui-macro": "4.8.0-next.1",
"@lingui/cli": "4.8.0-next.1",
"@lingui/core": "4.8.0-next.1",
"@lingui/message-utils": "4.8.0-next.1",
Expand All @@ -88,6 +92,7 @@
"vue": "^3.3.4"
},
"devDependencies": {
"@lingui/conf": "4.8.0-next.1",
"@types/babel__core": "^7.20.5",
"unbuild": "2.0.0"
}
Expand Down
219 changes: 0 additions & 219 deletions packages/vue/src/common/Trans.test.ts

This file was deleted.

Loading

0 comments on commit 41dc5f6

Please sign in to comment.