Skip to content

Commit

Permalink
feat(@formatjs/ts-transformer): support TS5
Browse files Browse the repository at this point in the history
  • Loading branch information
longlho committed Apr 17, 2023
1 parent 3b2069f commit 8932ba4
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 220 deletions.
2 changes: 1 addition & 1 deletion packages/ts-transformer/package.json
Expand Up @@ -23,7 +23,7 @@
"chalk": "^4.0.0",
"json-stable-stringify": "^1.0.1",
"tslib": "^2.4.0",
"typescript": "^4.7"
"typescript": "^5.0.4"
},
"peerDependencies": {
"ts-jest": ">=27"
Expand Down
32 changes: 26 additions & 6 deletions packages/ts-transformer/src/transform.ts
Expand Up @@ -465,13 +465,27 @@ function isMemberMethodFormatMessageCall(
return ts.isIdentifier(method) && fnNames.has(method.text)
}

function extractMessageFromJsxComponent(
ts: TypeScript,
factory: typescript.NodeFactory,
node: typescript.JsxSelfClosingElement,
opts: Opts,
sf: typescript.SourceFile
): typescript.VisitResult<typescript.JsxSelfClosingElement>
function extractMessageFromJsxComponent(
ts: TypeScript,
factory: typescript.NodeFactory,
node: typescript.JsxOpeningElement,
opts: Opts,
sf: typescript.SourceFile
): typescript.VisitResult<typescript.JsxOpeningElement>
function extractMessageFromJsxComponent(
ts: TypeScript,
factory: typescript.NodeFactory,
node: typescript.JsxOpeningElement | typescript.JsxSelfClosingElement,
opts: Opts,
sf: typescript.SourceFile
): typeof node {
): typescript.VisitResult<typeof node> {
const {onMsgExtracted} = opts
if (!isSingularMessageDecl(ts, node, opts.additionalComponentNames || [])) {
return node
Expand Down Expand Up @@ -599,7 +613,7 @@ function extractMessagesFromCallExpression(
node: typescript.CallExpression,
opts: Opts,
sf: typescript.SourceFile
): typeof node {
): typescript.VisitResult<typescript.CallExpression> {
const {onMsgExtracted, additionalFunctionNames} = opts
if (isMultipleMessageDecl(ts, node)) {
const [arg, ...restArgs] = node.arguments
Expand Down Expand Up @@ -722,9 +736,15 @@ function getVisitor(
const newNode = ts.isCallExpression(node)
? extractMessagesFromCallExpression(ts, ctx.factory, node, opts, sf)
: ts.isJsxOpeningElement(node) || ts.isJsxSelfClosingElement(node)
? extractMessageFromJsxComponent(ts, ctx.factory, node, opts, sf)
? extractMessageFromJsxComponent(
ts,
ctx.factory,
node as typescript.JsxOpeningElement,
opts,
sf
)
: node
return ts.visitEachChild(newNode, visitor, ctx)
return ts.visitEachChild(newNode as typescript.Node, visitor, ctx)
}
return visitor
}
Expand All @@ -735,7 +755,7 @@ export function transformWithTs(ts: TypeScript, opts: Opts) {
const transformFn: typescript.TransformerFactory<
typescript.SourceFile
> = ctx => {
return (sf: typescript.SourceFile) => {
return sf => {
const pragmaResult = PRAGMA_REGEX.exec(sf.text)
if (pragmaResult) {
debug('Pragma found', pragmaResult)
Expand All @@ -753,7 +773,7 @@ export function transformWithTs(ts: TypeScript, opts: Opts) {
}
}
}
return ts.visitNode(sf, getVisitor(ts, ctx, sf, opts))
return ts.visitEachChild(sf, getVisitor(ts, ctx, sf, opts), ctx)
}
}

Expand Down

0 comments on commit 8932ba4

Please sign in to comment.