-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(language-service): noSyncWithConstant refacto (#6)
- Loading branch information
1 parent
f4175ad
commit 0812ab0
Showing
11 changed files
with
134 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@effect/language-service": patch | ||
--- | ||
|
||
Add refactor to handle no-sync-with-constants diagnostic |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
//7:14 | ||
import * as Effect from "@effect/core/io/Effect" | ||
import { pipe } from "@fp-ts/data/Function" | ||
|
||
|
||
const result = pipe( | ||
Effect.sync(() => "hello"), | ||
Effect.map((hello) => hello + ", world!"), | ||
Effect.flatMap((msg) => Effect.log(msg)) | ||
) |
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
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
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,60 @@ | ||
import * as T from "@effect/core/io/Effect" | ||
import * as AST from "@effect/language-service/ast" | ||
import { | ||
isEffectSyncWithConstantCall, | ||
noSyncWithConstantMethodsMap | ||
} from "@effect/language-service/diagnostics/noSyncWithConstant" | ||
import { createRefactor } from "@effect/language-service/refactors/definition" | ||
import { getEffectModuleIdentifier, isLiteralConstantValue } from "@effect/language-service/utils" | ||
import * as Ch from "@tsplus/stdlib/collections/Chunk" | ||
import { pipe } from "@tsplus/stdlib/data/Function" | ||
import * as O from "@tsplus/stdlib/data/Maybe" | ||
|
||
export default createRefactor({ | ||
name: "effect/addPipe", | ||
description: "Rewrite using pipe", | ||
apply: (sourceFile, textRange) => | ||
T.gen(function*($) { | ||
const ts = yield* $(T.service(AST.TypeScriptApi)) | ||
const effectIdentifier = getEffectModuleIdentifier(ts)(sourceFile) | ||
|
||
const nodes = pipe( | ||
AST.getNodesContainingRange(ts)(sourceFile, textRange), | ||
Ch.reverse, | ||
Ch.from, | ||
Ch.filter(AST.isNodeInRange(textRange)) | ||
) | ||
|
||
for (const methodName of Object.keys(noSyncWithConstantMethodsMap)) { | ||
const suggestedMethodName: string = noSyncWithConstantMethodsMap[methodName]! | ||
const refactor = pipe( | ||
nodes, | ||
Ch.filter(isEffectSyncWithConstantCall(ts)(effectIdentifier, methodName)), | ||
Ch.head, | ||
O.map((node) => ({ | ||
description: `Rewrite ${methodName} to ${suggestedMethodName}`, | ||
apply: T.gen(function*($) { | ||
const changeTracker = yield* $(T.service(AST.ChangeTrackerApi)) | ||
const arg = node.arguments[0] | ||
if (ts.isArrowFunction(arg) && isLiteralConstantValue(ts)(arg.body)) { | ||
const newNode = ts.factory.updateCallExpression( | ||
node, | ||
ts.factory.createPropertyAccessExpression( | ||
ts.factory.createIdentifier(effectIdentifier), | ||
suggestedMethodName | ||
), | ||
node.typeArguments, | ||
ts.factory.createNodeArray([arg.body]) | ||
) | ||
|
||
changeTracker.replaceNode(sourceFile, node, newNode) | ||
} | ||
}) | ||
})) | ||
) | ||
if (O.isSome(refactor)) return refactor | ||
} | ||
|
||
return O.none | ||
}) | ||
}) |
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
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