From 863fa6512849f69a0d23e47636646ac622ae8523 Mon Sep 17 00:00:00 2001 From: Michael Arnaldi Date: Sat, 25 Jun 2022 10:41:20 +0100 Subject: [PATCH] fix: fix Do transformation with pipeable (#203) --- .../package1/src/prelude/definition/Maybe.ts | 20 +++++++ src/compiler/transformers/tsplus.ts | 58 ++++++++----------- 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/effect/packages/package1/src/prelude/definition/Maybe.ts b/effect/packages/package1/src/prelude/definition/Maybe.ts index ef00f69ff39..dbe791c21c4 100644 --- a/effect/packages/package1/src/prelude/definition/Maybe.ts +++ b/effect/packages/package1/src/prelude/definition/Maybe.ts @@ -107,3 +107,23 @@ export function assertJustNothing(self: Nothing): never { export const result = Maybe.just(0).match(() => 0, () => 1) export const op = Maybe.just(0) + Maybe.just(1) + +/** + * @tsplus pipeable Maybe map + */ +export function map(f: (a: A) => B) { + return (self: Maybe): Maybe => self.isJust() ? Maybe.just(f(self.value)) : Maybe.nothing() +} + +/** + * @tsplus pipeable Maybe flatMap + */ +export function flatMap(f: (a: A) => Maybe) { + return (self: Maybe): Maybe => self.isJust() ? f(self.value) : Maybe.nothing() +} + +export const useDo = Do(($) => { + const x = $(Maybe.just(0)) + const y = $(Maybe.just(1)) + return x + y +}) \ No newline at end of file diff --git a/src/compiler/transformers/tsplus.ts b/src/compiler/transformers/tsplus.ts index 26e0b829915..b18aa73b8c0 100644 --- a/src/compiler/transformers/tsplus.ts +++ b/src/compiler/transformers/tsplus.ts @@ -607,13 +607,7 @@ namespace ts { } } } - currentScope = [ - factory.createReturnStatement(factory.createCallExpression( - getPathOfExtension(context, importer, { definition: functions.map.tsPlusFile, exportName: functions.map.tsPlusExportName }, source, sourceFileUniqueNames), - undefined, - args - )) - ]; + currentScope = [factory.createReturnStatement(produceTsPlusCallExpression(functions.map, args, context, importer, source, sourceFileUniqueNames))]; } else { const mapper = factory.createArrowFunction( @@ -641,13 +635,7 @@ namespace ts { } } } - currentScope = [ - factory.createReturnStatement(factory.createCallExpression( - getPathOfExtension(context, importer, { definition: functions.flatMap.tsPlusFile, exportName: functions.flatMap.tsPlusExportName }, source, sourceFileUniqueNames), - undefined, - args - )) - ]; + currentScope = [factory.createReturnStatement(produceTsPlusCallExpression(functions.flatMap, args, context, importer, source, sourceFileUniqueNames))]; } } else if (isExpressionStatement(statement) && isCallExpression(statement.expression) && checker.getNodeLinks(statement).tsPlusDoBindType) { @@ -671,13 +659,7 @@ namespace ts { } } } - currentScope = [ - factory.createReturnStatement(factory.createCallExpression( - getPathOfExtension(context, importer, { definition: functions.map.tsPlusFile, exportName: functions.map.tsPlusExportName }, source, sourceFileUniqueNames), - undefined, - [visitNode(statement.expression.arguments[0], visitor), mapper] - )) - ]; + currentScope = [factory.createReturnStatement(produceTsPlusCallExpression(functions.map, args, context, importer, source, sourceFileUniqueNames))]; } else { const mapper = factory.createArrowFunction( @@ -698,25 +680,15 @@ namespace ts { } } } - currentScope = [ - factory.createReturnStatement(factory.createCallExpression( - getPathOfExtension(context, importer, { definition: functions.flatMap.tsPlusFile, exportName: functions.flatMap.tsPlusExportName }, source, sourceFileUniqueNames), - undefined, - args - )) - ]; + currentScope = [factory.createReturnStatement(produceTsPlusCallExpression(functions.flatMap, args, context, importer, source, sourceFileUniqueNames))]; } } else if (isReturnStatement(statement) && statement.expression && isCallExpression(statement.expression) && checker.getNodeLinks(statement).tsPlusDoBindType) { isLast = false - currentScope = [ - factory.createReturnStatement(visitNode(statement.expression.arguments[0], visitor)) - ]; + currentScope = [factory.createReturnStatement(visitNode(statement.expression.arguments[0], visitor))]; } else { - currentScope.push( - visitNode(statement, visitor) - ) + currentScope.push(visitNode(statement, visitor)) } } if (currentScope.length === 1 && isReturnStatement(currentScope[0]) && currentScope[0].expression) { @@ -995,6 +967,24 @@ namespace ts { return visitEachChild(node, visitor, context); } } + + function produceTsPlusCallExpression(fn: TsPlusSignature, args: Expression[], context: TransformationContext, importer: TsPlusImporter, source: SourceFile, sourceFileUniqueNames: SourceFileUniqueNames) { + const expr = getPathOfExtension(context, importer, { definition: fn.tsPlusFile, exportName: fn.tsPlusExportName }, source, sourceFileUniqueNames) + return fn.tsPlusPipeable ? factory.createCallExpression( + factory.createCallExpression( + expr, + undefined, + args.slice(1) + ), + undefined, + [args[0]] + ) : factory.createCallExpression( + expr, + undefined, + args + ); + } + function addSourceFileUniqueNamesVisitor(hoistedStatements: Array, sourceFileUniqueNames: SourceFileUniqueNames, context: TransformationContext) { return function (node: Node): VisitResult { switch (node.kind) {