microsoft / microsoft/TypeScript

Refactor to extract existing function to outer scope

Open
#26,479 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: LS: Refactorings In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.1.0-dev.20180813

Code

function a(x: number) {
    function b(y: number) {
        return x + y;
    }
    return b(1);
}

Expected behavior:

Refactor on b to convert to:

function a(x: number) {
    return b(1, x);
}
function b(y: number, x: number) {
    return x + y;
}

Actual behavior:

No such refactor.
We already have the ability to refactor code to an outer scope. So a workaround is to highlight the entire body of b and refactor to a function in global scope, then delete b and rename the refactored function to b, then manually update call sites.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names the nested function b and the existing refactor-to-outer-scope capability; start by locating that refactor entry point and its tests. Reproduce the nested-function case, then verify that the refactor moves b, passes captured x as an argument, and updates the call site.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
developer-experience
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.