Skip to content

Introduce and consume suppressLeadingAndTrailingTrivia #19135

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 12, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/compiler/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2612,6 +2612,16 @@ namespace ts {
return node;
}

/**
* Sets flags that control emit behavior of a node.
*/
/* @internal */
export function addEmitFlags<T extends Node>(node: T, emitFlags: EmitFlags) {
const emitNode = getOrCreateEmitNode(node);
emitNode.flags = emitNode.flags | emitFlags;
return node;
}

/**
* Gets a custom text range to use when emitting source maps.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/harness/unittests/extractConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,14 @@ const f = () => {
testExtractConstant("extractConstant_ArrowFunction_Expression",
`const f = () => [#|2 + 1|];`);

testExtractConstant("extractConstant_PreserveTrivia", `
// a
var q = /*b*/ //c
/*d*/ [#|1 /*e*/ //f
/*g*/ + /*h*/ //i
/*j*/ 2|] /*k*/ //l
/*m*/; /*n*/ //o`);

testExtractConstantFailed("extractConstant_Void", `
function f(): void { }
[#|f();|]`);
Expand Down
8 changes: 8 additions & 0 deletions src/harness/unittests/extractFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,14 @@ function f() {
[#|let x;|]
return { x };
}`);

testExtractFunction("extractFunction_PreserveTrivia", `
// a
var q = /*b*/ //c
/*d*/ [#|1 /*e*/ //f
/*g*/ + /*h*/ //i
/*j*/ 2|] /*k*/ //l
/*m*/; /*n*/ //o`);
});

function testExtractFunction(caption: string, text: string) {
Expand Down
6 changes: 3 additions & 3 deletions src/harness/unittests/tsserverProjectSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4412,9 +4412,9 @@ namespace ts.projectSystem {
fileName: "/a.ts",
textChanges: [
{
start: { line: 2, offset: 1 },
end: { line: 3, offset: 1 },
newText: " newFunction();\n",
start: { line: 2, offset: 3 },
end: { line: 2, offset: 5 },
newText: "newFunction();",
},
{
start: { line: 3, offset: 2 },
Expand Down
18 changes: 8 additions & 10 deletions src/services/refactors/extractSymbol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -740,6 +740,8 @@ namespace ts.refactor.extractSymbol {
}

const { body, returnValueProperty } = transformFunctionBody(node, exposedVariableDeclarations, writes, substitutions, !!(range.facts & RangeFacts.HasReturn));
suppressLeadingAndTrailingTrivia(body);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if you create a fresh node that re-use tokens, do you suppressLeadingAndTrailingTrivia on the new node or the old node?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As we discussed offline, you probably want to suppress it on the new node. Note that the token shouldn't appear in two places in the tree - that will mess up the positions. You have to make a copy.


let newFunction: MethodDeclaration | FunctionDeclaration;

if (isClassLike(scope)) {
Expand Down Expand Up @@ -926,15 +928,10 @@ namespace ts.refactor.extractSymbol {
}
}

if (isReadonlyArray(range.range)) {
changeTracker.replaceNodesWithNodes(context.file, range.range, newNodes, {
nodeSeparator: context.newLineCharacter,
suffix: context.newLineCharacter // insert newline only when replacing statements
});
}
else {
changeTracker.replaceNodeWithNodes(context.file, range.range, newNodes, { nodeSeparator: context.newLineCharacter });
}
const replacementRange = isReadonlyArray(range.range)
? { pos: first(range.range).getStart(), end: last(range.range).end }
: { pos: range.range.getStart(), end: range.range.end };
changeTracker.replaceRangeWithNodes(context.file, replacementRange, newNodes, { nodeSeparator: context.newLineCharacter });

const edits = changeTracker.getChanges();
const renameRange = isReadonlyArray(range.range) ? first(range.range) : range.range;
Expand Down Expand Up @@ -982,6 +979,7 @@ namespace ts.refactor.extractSymbol {
: checker.typeToTypeNode(checker.getContextualType(node), scope, NodeBuilderFlags.NoTruncation);

const initializer = transformConstantInitializer(node, substitutions);
suppressLeadingAndTrailingTrivia(initializer);

const changeTracker = textChanges.ChangeTracker.fromContext(context);

Expand Down Expand Up @@ -1014,7 +1012,7 @@ namespace ts.refactor.extractSymbol {
changeTracker.insertNodeBefore(context.file, nodeToInsertBefore, newVariable, { suffix: context.newLineCharacter + context.newLineCharacter });

// Consume
changeTracker.replaceNodeWithNodes(context.file, node, [localReference], { nodeSeparator: context.newLineCharacter });
changeTracker.replaceRange(context.file, { pos: node.getStart(), end: node.end }, localReference);
}
else {
const newVariableDeclaration = createVariableDeclaration(localNameText, variableType, initializer);
Expand Down
35 changes: 35 additions & 0 deletions src/services/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1369,4 +1369,39 @@ namespace ts {

return visited;
}

/**
* Sets EmitFlags to suppress leading and trailing trivia on the node.
*/
/* @internal */
export function suppressLeadingAndTrailingTrivia(node: Node) {
Debug.assert(node !== undefined);

suppressLeading(node);
suppressTrailing(node);

function suppressLeading(node: Node) {
addEmitFlags(node, EmitFlags.NoLeadingComments);

const firstChild = forEachChild(node, child => child);
firstChild && suppressLeading(firstChild);
}

function suppressTrailing(node: Node) {
addEmitFlags(node, EmitFlags.NoTrailingComments);

let lastChild: Node = undefined;
forEachChild(
node,
child => (lastChild = child, undefined),
children => {
// As an optimization, jump straight to the end of the list.
if (children.length) {
lastChild = last(children);
}
return undefined;
});
lastChild && suppressTrailing(lastChild);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ==ORIGINAL==

// a
var q = /*b*/ //c
/*d*/ /*[#|*/1 /*e*/ //f
/*g*/ + /*h*/ //i
/*j*/ 2/*|]*/ /*k*/ //l
/*m*/; /*n*/ //o
// ==SCOPE::Extract to constant in enclosing scope==
const newLocal = 1 /*e*/ //f
/*g*/ + /*h*/ //i
/*j*/ 2;

// a
var q = /*b*/ //c
/*d*/ /*RENAME*/newLocal /*k*/ //l
/*m*/; /*n*/ //o
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// ==ORIGINAL==

// a
var q = /*b*/ //c
/*d*/ /*[#|*/1 /*e*/ //f
/*g*/ + /*h*/ //i
/*j*/ 2/*|]*/ /*k*/ //l
/*m*/; /*n*/ //o
// ==SCOPE::Extract to constant in enclosing scope==
const newLocal = 1 /*e*/ //f
/*g*/ + /*h*/ //i
/*j*/ 2;

// a
var q = /*b*/ //c
/*d*/ /*RENAME*/newLocal /*k*/ //l
/*m*/; /*n*/ //o
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
<U2a, U2b>(u2a: U2a, u2b: U2b) => {
function F2<T2a, T2b>(t2a: T2a, t2b: T2b) {
<U3a, U3b>(u3a: U3a, u3b: U3b) => {
/*RENAME*/newFunction<U3a>(u3a);
/*RENAME*/newFunction<U3a>(u3a);
}

function newFunction<U3a>(u3a: U3a) {
Expand All @@ -40,7 +40,7 @@
<U2a, U2b>(u2a: U2a, u2b: U2b) => {
function F2<T2a, T2b>(t2a: T2a, t2b: T2b) {
<U3a, U3b>(u3a: U3a, u3b: U3b) => {
/*RENAME*/newFunction<U2a, T2a, U3a>(t2a, u2a, u3a);
/*RENAME*/newFunction<U2a, T2a, U3a>(t2a, u2a, u3a);
}
}
}
Expand All @@ -60,7 +60,7 @@
<U2a, U2b>(u2a: U2a, u2b: U2b) => {
function F2<T2a, T2b>(t2a: T2a, t2b: T2b) {
<U3a, U3b>(u3a: U3a, u3b: U3b) => {
/*RENAME*/newFunction<U1a, T1a, U2a, T2a, U3a>(t1a, t2a, u1a, u2a, u3a);
/*RENAME*/newFunction<U1a, T1a, U2a, T2a, U3a>(t1a, t2a, u1a, u2a, u3a);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// ==ORIGINAL==

// a
var q = /*b*/ //c
/*d*/ /*[#|*/1 /*e*/ //f
/*g*/ + /*h*/ //i
/*j*/ 2/*|]*/ /*k*/ //l
/*m*/; /*n*/ //o
// ==SCOPE::Extract to function in global scope==

// a
var q = /*b*/ //c
/*d*/ /*RENAME*/newFunction() /*k*/ //l
/*m*/; /*n*/ //o
function newFunction() {
return 1 /*e*/ //f
/*g*/ + /*h*/ //i
/*j*/ 2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// ==ORIGINAL==

// a
var q = /*b*/ //c
/*d*/ /*[#|*/1 /*e*/ //f
/*g*/ + /*h*/ //i
/*j*/ 2/*|]*/ /*k*/ //l
/*m*/; /*n*/ //o
// ==SCOPE::Extract to function in global scope==

// a
var q = /*b*/ //c
/*d*/ /*RENAME*/newFunction() /*k*/ //l
/*m*/; /*n*/ //o
function newFunction() {
return 1 /*e*/ //f
/*g*/ + /*h*/ //i
/*j*/ 2;
}
5 changes: 2 additions & 3 deletions tests/cases/fourslash/extract-method-uniqueName.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ edit.applyRefactor({
actionName: "function_scope_0",
actionDescription: "Extract to function in global scope",
newContent:
`/*RENAME*/newFunction_1();

`// newFunction
/*RENAME*/newFunction_1();
function newFunction_1() {
// newFunction
1 + 1;
}
`
Expand Down