question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Refactor "move to function in module scope" moves function past the following variable declaration

See original GitHub issue

TypeScript Version: 3.1.0-dev.20180807

Search Terms: refactor function module

Code

import "fs";

function foo() {
	const bar = 1; // select this line
}

let flag = false;
function doSomethingWithFlag() {
}
  1. select the line with the comment
  2. choose “extract to function in module scope”

Expected behavior:

import "fs";

function newFunction() {
	return 1;
}

function foo() {
	const bar = newFunction();
}

let flag = false;
function doSomethingWithFlag() {
}

or

import "fs";

function foo() {
	const bar = newFunction();
}

function newFunction() {
	return 1;
}

let flag = false;
function doSomethingWithFlag() {
}

Actual behavior:

import "fs";

function foo() {
	const bar = newFunction();
}

let flag = false;
function newFunction() {
	return 1;
}

function doSomethingWithFlag() {
}

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
jessetrinitycommented, Aug 13, 2020

@AlCalzone was there more context when you originally opened this issue that would help us out?

0reactions
AlCalzonecommented, Aug 16, 2020

For me it was just the placement of the function. The refactor works, but it seems unlogical where the function is placed. In the above example, newFunction and foo belong together and flag and doSomethingWithFlag logically belong together. After the refactor, this is not immediately clear, especially if there is more than one variable declaration between the functions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Refactoring JavaScript | WebStorm Documentation - JetBrains
The Move Symbol Refactoring works for classes, functions, and variables in ES6 modules. Move a class, a function, or a variable.
Read more >
Move declaration near reference refactoring - Microsoft Learn
What: Lets you move variable declarations closer to their usage. When: You have variable declarations that can be in a narrower scope.
Read more >
Summary and Criticism of Fowler's “Refactoring” | by Mark Looi
Changing function declaration is often an intermediate step to a further refactoring; in the example, it sets up moving a function into ...
Read more >
Refactoring TypeScript - Help | JetBrains Rider
The Move Symbol Refactoring works for classes, functions, and variables in ES6 modules. To move a class, a function, or a variable. Select...
Read more >
Refactoring - Eclipse Help
For example, the Refactor > Rename command can automatically locate the declaration(s) and uses of a particular subroutine, and change its name in...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found