-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(v5): New refactorings: wrap with useMemo, useCallback useEffect,…
… rename state veriable (#112) BREAKING CHANGE: Hooks support will now be activated automatically based on React version (16.8 and up) instead of a flag * feat(rename-state-variable): New Refactoring! Rename State Variable * feat(hooks-support): activate hooks support based on react version * removed excess deps * refactoring + wrap with effect refactoring * feat(wrap-with-useCallback): New refactoring - wrap function with useCallback * ts fix * feat(wrap-with-usememo): New! Wrap expressions with useMemo * docs: updated teh docs * fix(wrap-with-memo): improved case detection * docs: added new refactorings * docs * docs
- Loading branch information
Boris Litvinsky
authored
May 15, 2020
1 parent
8d2878c
commit eba2954
Showing
16 changed files
with
337 additions
and
63 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
|
@@ -151,4 +151,4 @@ | |
"url": "https://github.com/wix/vscode-glean.git" | ||
}, | ||
"icon": "assets/icon.png" | ||
} | ||
} |
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,56 @@ | ||
import { codeToAst } from "../parsing"; | ||
import { selectedText, allText, selectedTextStart, selectedTextEnd, activeFileName, activeEditor, showInputBox } from "../editor"; | ||
import traverse from "@babel/traverse"; | ||
import { transformFromAst } from "@babel/core"; | ||
import { window, Position, Range } from "vscode"; | ||
import { persistFileSystemChanges, replaceTextInFile } from "../file-system"; | ||
import { capitalizeFirstLetter } from "../utils"; | ||
import * as t from "@babel/types"; | ||
import { findPathInContext } from "../ast-helpers"; | ||
|
||
function isPathOnLines(path, start, end) { | ||
if (!path.node) return false; | ||
const pathStart = path.node.loc.start; | ||
const pathEnd = path.node.loc.end; | ||
return ( | ||
(pathStart.line === start.line && pathStart.column === start.character) && | ||
(pathEnd.line === end.line && pathEnd.column === end.character)) | ||
|
||
} | ||
|
||
export function isStateVariable(text) { | ||
try { | ||
const allAST = codeToAst(allText()); | ||
const containerPath = findPathInContext(allAST, text); | ||
const variableDeclarationParent = containerPath.scope.bindings[containerPath.node.name].path.parentPath; | ||
return variableDeclarationParent && variableDeclarationParent.node.declarations[0].init.callee.name === 'useState' | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
|
||
export async function renameState() { | ||
const selectedStateVariable = selectedText(); | ||
const varName = await showInputBox(null, 'Name of the state variable'); | ||
|
||
const allAST = codeToAst(allText()); | ||
const containerPath = findPathInContext(allAST, selectedStateVariable); | ||
const variableDeclarationParent = containerPath.findParent(parent => t.isVariableDeclaration(parent)); | ||
if (variableDeclarationParent && variableDeclarationParent.node.declarations[0].init.callee.name === 'useState') { | ||
containerPath.scope.bindings[selectedStateVariable].path.parentPath.get('declarations.0.id.elements.0').scope.rename(selectedStateVariable, varName); | ||
containerPath.scope.bindings[varName].path.parentPath.get('declarations.0.id.elements.1').scope.rename(`set${capitalizeFirstLetter(selectedStateVariable)}`, `set${capitalizeFirstLetter(varName)}`); | ||
|
||
const processedJSX = transformFromAst(allAST).code; | ||
const endLine = activeEditor().document.lineAt(activeEditor().document.lineCount - 1).range; | ||
const change = replaceTextInFile( | ||
processedJSX, | ||
new Position(0, 0), | ||
endLine.end, | ||
activeFileName() | ||
); | ||
|
||
|
||
return persistFileSystemChanges(change) | ||
|
||
} | ||
} |
Oops, something went wrong.