Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(plugin-compiler): runtime 方法替换兼容 return 语句 #187

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions packages/plugin-compiler/src/parsers/scriptParserPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -494,14 +494,16 @@ export class ScriptParserPlugin implements Plugin {

function visitor(node: ts.Node, ctx: ts.TransformationContext) {
const factory = ctx.factory
// 判断 statement 的同时,要判断一下 node.expression 是否存在,如果业务上返回的不是一个 expression,调用 ts.isCallExpression 会报错
const isReturnStatement = ts.isReturnStatement(node) && node.expression

// FIXME: 这里的条件无法匹配
// 箭头函数(ArrowFunction)
// 二元表达式(BinaryExpression)
// 三元表达式(ConditionalExpression)
// 需要提供逻辑进一步支持以上几种情况
if (
ts.isExpressionStatement(node) &&
(ts.isExpressionStatement(node) || isReturnStatement) &&
ts.isCallExpression(node.expression) &&
ts.isIdentifier(node.expression.expression)
) {
Expand Down Expand Up @@ -529,14 +531,18 @@ export class ScriptParserPlugin implements Plugin {
)
}

return factory.updateExpressionStatement(
node,
factory.createCallExpression(
factory.createIdentifier(MOR_IDENTIFIERS[identfierName]),
node.expression.typeArguments,
args
)
const updateExpression = factory.createCallExpression(
factory.createIdentifier(MOR_IDENTIFIERS[identfierName]),
node.expression.typeArguments,
args
)

return isReturnStatement
? factory.updateReturnStatement(node, updateExpression)
: factory.updateExpressionStatement(
node as ts.ExpressionStatement,
updateExpression
)
}
}

Expand Down