-
-
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.
add toggle definition site lazy (#43)
- Loading branch information
1 parent
48ab2bf
commit 42a032c
Showing
5 changed files
with
97 additions
and
1 deletion.
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 toggle lazy const initializer |
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,9 @@ | ||
// 3:10,5:10,7:10 | ||
|
||
const test1 = 1 | ||
|
||
const test2 = () => 1 | ||
|
||
const test3 = () => { | ||
console.log("Hello") | ||
} |
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,41 @@ | ||
import { createRefactor } from "@effect/language-service/refactors/definition" | ||
import * as AST from "@effect/language-service/utils/AST" | ||
import { pipe } from "@effect/language-service/utils/Function" | ||
import * as O from "@effect/language-service/utils/Option" | ||
import * as Ch from "@effect/language-service/utils/ReadonlyArray" | ||
|
||
export default createRefactor({ | ||
name: "effect/toggleLazyConst", | ||
description: "Toggle type annotation", | ||
apply: (ts) => | ||
(sourceFile, textRange) => | ||
pipe( | ||
AST.getNodesContainingRange(ts)(sourceFile, textRange), | ||
Ch.filter(ts.isVariableDeclaration), | ||
Ch.filter((node) => AST.isNodeInRange(textRange)(node.name)), | ||
Ch.filter((node) => | ||
!!node.initializer && !(ts.isArrowFunction(node.initializer) && ts.isBlock(node.initializer.body)) | ||
), | ||
Ch.head, | ||
O.map( | ||
(node) => ({ | ||
kind: "refactor.rewrite.effect.toggleLazyConst", | ||
description: "Toggle lazy const", | ||
apply: (changeTracker) => { | ||
const initializer = node.initializer! | ||
|
||
if (ts.isArrowFunction(initializer) && initializer.parameters.length === 0) { | ||
// delete eventual closing bracked | ||
changeTracker.deleteRange(sourceFile, { pos: initializer.body.end, end: initializer.end }) | ||
// remove () => { | ||
changeTracker.deleteRange(sourceFile, { pos: initializer.pos, end: initializer.body.pos }) | ||
return | ||
} | ||
|
||
// adds () => before | ||
changeTracker.insertText(sourceFile, initializer.pos, " () =>") | ||
} | ||
}) | ||
) | ||
) | ||
}) |
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