Skip to content

Need control over trivia when replacing nodes #18626

Closed
@amcasey

Description

@amcasey

Consider the following code:

// a
var q = /*b*/ //c
    /*d*/ 1 /*e*/ //f
    /*g*/ + /*h*/ //i
    /*j*/ 2 /*k*/ //l
    /*m*/; /*n*/ //o

Suppose we extract the range from 1 to 2 (inclusive) into a separate function. We would like comments e-j to move to the new function and the others to remain where they are:

// a
var q = /*b*/ //c
    /*d*/ newFunction() /*k*/ //l
    /*m*/; /*n*/ //o

function newFunction() {
    return 1 /*e*/ //f
    /*g*/ + /*h*/ //i
    /*j*/ 2;
}

Presently, there doesn't seem to be a way to express this. We can cause the leading and trailing trivia to remain at the call site by replacing a range ({pos: expr.getStart(), end: expr.end}), rather than a node (expr), but there doesn't seem to be a way to strip the leading and trailing trivia in the body of the extracted function.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Refactoringse.g. extract to constant or function, rename symbolFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions