microsoft / microsoft/TypeScript

Extract to function in upper scope

Open
#40,723 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

We have Extract to function in module scope and Extract to inner function in function xxx(which is Extract to function in current scope), we should also have Extract to function in upper scope. With it, we can:

const g = 'g';
export function A() {
  const a_local = 'a';
  return function B() {
    const b_local = 'b';
    return function C() {
      const c_local = 'c';
      // the bottom will be extracted to function upper scope
      console.log(g, a_local, b_local, c_local);
      // the above will be extracted to function upper scope
    }
  }
}

After the extraction, code will be:

const g = 'g';
export function A() {
  const a_local = 'a';
  return function B() {
    const b_local = 'b';
    function newFunction(c_local: string) {
      console.log(g, a_local, b_local, c_local);
    }
    return function C() {
      const c_local = 'c';
      newFunction(c_local);
    }
  }
}

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

Start by locating the implementations and tests for “Extract to function in module scope” and “Extract to function in current scope.” Extend the existing refactoring behavior so extraction can target the upper function scope while passing variables from deeper scopes as parameters, and add coverage matching the TypeScript example.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
developer-experience, tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.