-
Notifications
You must be signed in to change notification settings - Fork 390
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(macro): use message descriptor for Trans
- Loading branch information
1 parent
afc962c
commit 94d9c78
Showing
3 changed files
with
133 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
packages/babel-plugin-lingui-macro/src/messageDescriptorUtils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import ICUMessageFormat, { Tokens, ParsedResult } from "./icu" | ||
import { SourceLocation, ObjectProperty, ObjectExpression } from "@babel/types" | ||
import { MESSAGE, ID, EXTRACT_MARK, COMMENT, CONTEXT } from "./constants" | ||
import * as types from "@babel/types" | ||
import { generateMessageId } from "@lingui/message-utils/generateMessageId" | ||
|
||
function buildICUFromTokens(tokens: Tokens) { | ||
const messageFormat = new ICUMessageFormat() | ||
return messageFormat.fromTokens(tokens) | ||
} | ||
|
||
export function createMessageDescriptorFromTokens( | ||
tokens: Tokens, | ||
oldLoc?: SourceLocation, | ||
defaults: { id?: string; context?: string; comment?: string } = {}, | ||
stripNonEssentialProps = true | ||
) { | ||
const { message, values, jsxElements } = buildICUFromTokens(tokens) | ||
|
||
const properties: ObjectProperty[] = [] | ||
|
||
properties.push( | ||
defaults.id | ||
? createStringObjectProperty(ID, defaults.id) | ||
: createIdProperty(message, defaults.context) | ||
) | ||
|
||
if (!stripNonEssentialProps) { | ||
properties.push(createStringObjectProperty(MESSAGE, message)) | ||
|
||
if (defaults.comment) { | ||
properties.push(createStringObjectProperty(COMMENT, defaults.comment)) | ||
} | ||
|
||
if (defaults.context) { | ||
properties.push(createStringObjectProperty(CONTEXT, defaults.context)) | ||
} | ||
} | ||
|
||
properties.push(createValuesProperty(values)) | ||
properties.push(createComponentsProperty(jsxElements)) | ||
|
||
return createMessageDescriptor( | ||
properties, | ||
// preserve line numbers for extractor | ||
oldLoc | ||
) | ||
} | ||
|
||
function createIdProperty(message: string, context?: string) { | ||
return createStringObjectProperty(ID, generateMessageId(message, context)) | ||
} | ||
|
||
function createValuesProperty(values: ParsedResult["values"]) { | ||
const valuesObject = Object.keys(values).map((key) => | ||
types.objectProperty(types.identifier(key), values[key]) | ||
) | ||
|
||
if (!valuesObject.length) return | ||
|
||
return types.objectProperty( | ||
types.identifier("values"), | ||
types.objectExpression(valuesObject) | ||
) | ||
} | ||
|
||
function createComponentsProperty(values: ParsedResult["jsxElements"]) { | ||
const valuesObject = Object.keys(values).map((key) => | ||
types.objectProperty(types.identifier(key), values[key]) | ||
) | ||
|
||
if (!valuesObject.length) return | ||
|
||
return types.objectProperty( | ||
types.identifier("components"), | ||
types.objectExpression(valuesObject) | ||
) | ||
} | ||
|
||
// if (Object.keys(jsxElements).length) { | ||
// attributes.push( | ||
// this.types.jsxAttribute( | ||
// this.types.jsxIdentifier("components"), | ||
// this.types.jsxExpressionContainer( | ||
// this.types.objectExpression( | ||
// Object.keys(jsxElements).map((key) => | ||
// this.types.objectProperty( | ||
// this.types.identifier(key), | ||
// jsxElements[key] | ||
// ) | ||
// ) | ||
// ) | ||
// ) | ||
// ) | ||
// ) | ||
// } | ||
export function createStringObjectProperty(key: string, value: string) { | ||
return types.objectProperty(types.identifier(key), types.stringLiteral(value)) | ||
} | ||
|
||
function createMessageDescriptor( | ||
properties: ObjectProperty[], | ||
oldLoc?: SourceLocation | ||
): ObjectExpression { | ||
const newDescriptor = types.objectExpression(properties.filter(Boolean)) | ||
types.addComment(newDescriptor, "leading", EXTRACT_MARK) | ||
if (oldLoc) { | ||
newDescriptor.loc = oldLoc | ||
} | ||
|
||
return newDescriptor | ||
} |