Skip to content

Extract to function refactoring generates invalid code for arrow function that references this #38073

Closed
@mjbvz

Description

@mjbvz

TypeScript Version: 3.9.0-dev.20200420

Search Terms:

  • extract to function
  • refactor / refactoring

Repro

class Foo {
    x = 1
    
    bar() {
        console.log(() => { this.x; });
    }
}
  1. Select console.log(() => { this.x; }); and run extract to function

Bug:
The generate code uses this inside the extracted function:

class Foo {
    x = 1

    bar() {
        newFunction();
    }
}

function newFunction() {
    console.log(() => { this.x; });
}

The extract to function refactoring probably should not be offered in this case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Refactoringse.g. extract to constant or function, rename symbol

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions