Skip to content

Commit

Permalink
fix: fix Do transformation with pipeable (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikearnaldi committed Jun 25, 2022
1 parent b07a451 commit 863fa65
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 34 deletions.
20 changes: 20 additions & 0 deletions effect/packages/package1/src/prelude/definition/Maybe.ts
Expand Up @@ -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<A, B>(f: (a: A) => B) {
return (self: Maybe<A>): Maybe<B> => self.isJust() ? Maybe.just(f(self.value)) : Maybe.nothing()
}

/**
* @tsplus pipeable Maybe flatMap
*/
export function flatMap<A, B>(f: (a: A) => Maybe<B>) {
return (self: Maybe<A>): Maybe<B> => self.isJust() ? f(self.value) : Maybe.nothing()
}

export const useDo = Do(($) => {
const x = $(Maybe.just(0))
const y = $(Maybe.just(1))
return x + y
})
58 changes: 24 additions & 34 deletions src/compiler/transformers/tsplus.ts
Expand Up @@ -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(
Expand Down Expand Up @@ -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) {
Expand All @@ -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(
Expand All @@ -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) {
Expand Down Expand Up @@ -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<Statement>, sourceFileUniqueNames: SourceFileUniqueNames, context: TransformationContext) {
return function (node: Node): VisitResult<Node> {
switch (node.kind) {
Expand Down

0 comments on commit 863fa65

Please sign in to comment.