- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #114 from OliverJAsh/feature/convert-functions
Convert function declaration to arrow function
- 9.4.8
- 9.4.7
- 9.4.6
- 9.4.5
- 9.4.4
- 9.4.3
- 9.4.2
- 9.4.1
- 9.4.0
- 9.3.0
- 9.2.1
- 9.2.0
- 9.1.3
- 9.1.2
- 9.1.1
- 9.1.0
- 9.0.2
- 9.0.1
- 9.0.0
- 8.1.4
- 8.1.3
- 8.1.2
- 8.1.1
- 8.1.0
- 8.0.1
- 8.0.0
- 7.1.1
- 7.1.0
- 7.0.0
- 6.19.0
- 6.18.2
- 6.18.1
- 6.18.0
- 6.17.0
- 6.16.0
- 6.15.3
- 6.15.2
- 6.15.1
- 6.15.0
- 6.14.4
- 6.14.3
- 6.14.2
- 6.14.1
- 6.14.0
- 6.13.0
- 6.12.0
- 6.11.0
- 6.10.0
- 6.9.1
- 6.9.0
- 6.8.0
- 6.7.1
- 6.7.0
- 6.6.0
- 6.5.2
- 6.5.1
- 6.5.0
- 6.4.0
- 6.3.1
- 6.3.0
- 6.2.0
- 6.1.1
- 6.1.0
- 6.0.0
- 5.4.2
- 5.4.1
- 5.4.0
- 5.3.1
- 5.3.0
- 5.2.0
- 5.1.0
- 5.0.1
- 5.0.0
- 4.13.1
- 4.13.0
- 4.12.1
- 4.12.0
- 4.11.0
- 4.10.0
- 4.9.3
- 4.9.2
- 4.9.2-beta
- 4.9.2-alpha
- 4.9.1
- 4.9.0
- 4.8.0
- 4.7.0
Showing
6 changed files
with
179 additions
and
0 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
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
69 changes: 69 additions & 0 deletions
69
...tion-declaration-to-arrow-function/convert-function-declaration-to-arrow-function.test.ts
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,69 @@ | ||
import { Editor, ErrorReason, Code } from "../../editor/editor"; | ||
import { Selection } from "../../editor/selection"; | ||
import { InMemoryEditor } from "../../editor/adapters/in-memory-editor"; | ||
import { testEach } from "../../tests-helpers"; | ||
|
||
import { convertFunctionDeclarationToArrowFunction } from "./convert-function-declaration-to-arrow-function"; | ||
|
||
describe("Convert Function Declaration To Arrow Function", () => { | ||
let showErrorMessage: Editor["showError"]; | ||
|
||
beforeEach(() => { | ||
showErrorMessage = jest.fn(); | ||
}); | ||
|
||
testEach<{ code: Code; selection?: Selection; expected: Code }>( | ||
"should convert function declaration to arrow function", | ||
[ | ||
{ | ||
description: "non-generic", | ||
code: `function fn(a: string): number { return 1; }`, | ||
expected: `const fn = (a: string): number => { return 1; };` | ||
}, | ||
{ | ||
description: "non-generic async", | ||
code: `async function fn(a: string): number { return 1; }`, | ||
expected: `const fn = async (a: string): number => { return 1; };` | ||
}, | ||
{ | ||
description: "generic", | ||
code: `function fn<T>(t: T): T { return t; }`, | ||
expected: `const fn = <T>(t: T): T => { return t; };` | ||
}, | ||
{ | ||
description: "generic async", | ||
code: `async function fn<T>(t: T): T { return t; }`, | ||
expected: `const fn = async <T>(t: T): T => { return t; };` | ||
} | ||
], | ||
async ({ code, selection = Selection.cursorAt(0, 0), expected }) => { | ||
const result = await doConvertFunctionDeclarationToArrowFunction( | ||
code, | ||
selection | ||
); | ||
|
||
expect(result).toBe(expected); | ||
} | ||
); | ||
|
||
it("should show an error message if refactoring can't be made", async () => { | ||
const code = `// This is a comment, can't be refactored`; | ||
const selection = Selection.cursorAt(0, 0); | ||
|
||
await doConvertFunctionDeclarationToArrowFunction(code, selection); | ||
|
||
expect(showErrorMessage).toBeCalledWith( | ||
ErrorReason.DidNotFindFunctionDeclarationToConvert | ||
); | ||
}); | ||
|
||
async function doConvertFunctionDeclarationToArrowFunction( | ||
code: Code, | ||
selection: Selection | ||
): Promise<Code> { | ||
const editor = new InMemoryEditor(code); | ||
editor.showError = showErrorMessage; | ||
await convertFunctionDeclarationToArrowFunction(code, selection, editor); | ||
return editor.code; | ||
} | ||
}); |
61 changes: 61 additions & 0 deletions
61
...-function-declaration-to-arrow-function/convert-function-declaration-to-arrow-function.ts
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,61 @@ | ||
import { Editor, Code, ErrorReason } from "../../editor/editor"; | ||
import { Selection } from "../../editor/selection"; | ||
import * as t from "../../ast"; | ||
|
||
export { convertFunctionDeclarationToArrowFunction, createVisitor }; | ||
|
||
async function convertFunctionDeclarationToArrowFunction( | ||
code: Code, | ||
selection: Selection, | ||
editor: Editor | ||
) { | ||
const updatedCode = updateCode(t.parse(code), selection); | ||
|
||
if (!updatedCode.hasCodeChanged) { | ||
editor.showError(ErrorReason.DidNotFindFunctionDeclarationToConvert); | ||
return; | ||
} | ||
|
||
await editor.write(updatedCode.code); | ||
} | ||
|
||
function updateCode(ast: t.AST, selection: Selection): t.Transformed { | ||
return t.transformAST( | ||
ast, | ||
createVisitor(selection, path => { | ||
const { node } = path; | ||
const name = node.id ? node.id.name : "converted"; | ||
const identifier = t.identifier(name); | ||
|
||
const arrowFunctionExpression = t.arrowFunctionExpression( | ||
node.params, | ||
node.body, | ||
node.async | ||
); | ||
arrowFunctionExpression.returnType = node.returnType; | ||
arrowFunctionExpression.typeParameters = node.typeParameters; | ||
|
||
const declarator = t.variableDeclarator( | ||
identifier, | ||
arrowFunctionExpression | ||
); | ||
|
||
path.replaceWith(t.variableDeclaration("const", [declarator])); | ||
|
||
path.stop(); | ||
}) | ||
); | ||
} | ||
|
||
function createVisitor( | ||
selection: Selection, | ||
onMatch: (path: t.NodePath<t.FunctionDeclaration>) => void | ||
): t.Visitor { | ||
return { | ||
FunctionDeclaration(path) { | ||
if (!selection.isInsidePath(path)) return; | ||
|
||
onMatch(path); | ||
} | ||
}; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/refactorings/convert-function-declaration-to-arrow-function/index.ts
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,20 @@ | ||
import { | ||
convertFunctionDeclarationToArrowFunction, | ||
createVisitor | ||
} from "./convert-function-declaration-to-arrow-function"; | ||
|
||
import { RefactoringWithActionProvider } from "../../types"; | ||
|
||
const config: RefactoringWithActionProvider = { | ||
command: { | ||
key: "convertFunctionDeclarationToArrowFunction", | ||
operation: convertFunctionDeclarationToArrowFunction, | ||
title: "Convert Function Declaration To Arrow Function" | ||
}, | ||
actionProvider: { | ||
message: "Convert function declaration to arrow function", | ||
createVisitor | ||
} | ||
}; | ||
|
||
export default config; |