Skip to content

Commit 7aee3a1

Browse files
authored
Merge pull request #18831 from amcasey/AdditionSpecialCase
Eliminate special case for extracting from a binary operator chain
2 parents f71930c + 4167624 commit 7aee3a1

File tree

4 files changed

+17
-114
lines changed

4 files changed

+17
-114
lines changed

src/harness/unittests/extractFunctions.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,16 +109,6 @@ namespace ts {
109109
return C.foo();|]
110110
}
111111
}
112-
}`);
113-
testExtractFunction("extractFunction8",
114-
`namespace A {
115-
let x = 1;
116-
namespace B {
117-
function a() {
118-
let a1 = 1;
119-
return 1 + [#|a1 + x|] + 100;
120-
}
121-
}
122112
}`);
123113
testExtractFunction("extractFunction9",
124114
`namespace A {

src/harness/unittests/extractRanges.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -152,18 +152,6 @@ namespace ts {
152152
}
153153
}
154154
`);
155-
testExtractRange(`
156-
function f() {
157-
return [$|1 + [#|2 + 3|]|];
158-
}
159-
}
160-
`);
161-
testExtractRange(`
162-
function f() {
163-
return [$|1 + 2 + [#|3 + 4|]|];
164-
}
165-
}
166-
`);
167155
});
168156

169157
testExtractRangeFailed("extractRangeFailed1",
@@ -311,7 +299,18 @@ switch (x) {
311299
testExtractRangeFailed("extractRangeFailed9",
312300
`var x = ([#||]1 + 2);`,
313301
[
314-
"Cannot extract empty range."
302+
refactor.extractSymbol.Messages.CannotExtractEmpty.message
303+
]);
304+
305+
testExtractRangeFailed("extractRangeFailed10",
306+
`
307+
function f() {
308+
return 1 + [#|2 + 3|];
309+
}
310+
}
311+
`,
312+
[
313+
refactor.extractSymbol.Messages.CannotExtractRange.message
315314
]);
316315

317316
testExtractRangeFailed("extract-method-not-for-token-expression-statement", `[#|a|]`, [refactor.extractSymbol.Messages.CannotExtractIdentifier.message]);

src/services/refactors/extractSymbol.ts

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,9 @@ namespace ts.refactor.extractSymbol {
201201

202202
// Walk up starting from the the start position until we find a non-SourceFile node that subsumes the selected span.
203203
// This may fail (e.g. you select two statements in the root of a source file)
204-
let start = getParentNodeInSpan(getTokenAtPosition(sourceFile, span.start, /*includeJsDocComment*/ false), sourceFile, span);
204+
const start = getParentNodeInSpan(getTokenAtPosition(sourceFile, span.start, /*includeJsDocComment*/ false), sourceFile, span);
205205
// Do the same for the ending position
206-
let end = getParentNodeInSpan(findTokenOnLeftOfPosition(sourceFile, textSpanEnd(span)), sourceFile, span);
206+
const end = getParentNodeInSpan(findTokenOnLeftOfPosition(sourceFile, textSpanEnd(span)), sourceFile, span);
207207

208208
const declarations: Symbol[] = [];
209209

@@ -217,31 +217,10 @@ namespace ts.refactor.extractSymbol {
217217
}
218218

219219
if (start.parent !== end.parent) {
220-
// handle cases like 1 + [2 + 3] + 4
221-
// user selection is marked with [].
222-
// in this case 2 + 3 does not belong to the same tree node
223-
// instead the shape of the tree looks like this:
224-
// +
225-
// / \
226-
// + 4
227-
// / \
228-
// + 3
229-
// / \
230-
// 1 2
231-
// in this case there is no such one node that covers ends of selection and is located inside the selection
232-
// to handle this we check if both start and end of the selection belong to some binary operation
233-
// and start node is parented by the parent of the end node
234-
// if this is the case - expand the selection to the entire parent of end node (in this case it will be [1 + 2 + 3] + 4)
235-
const startParent = skipParentheses(start.parent);
236-
const endParent = skipParentheses(end.parent);
237-
if (isBinaryExpression(startParent) && isBinaryExpression(endParent) && isNodeDescendantOf(startParent, endParent)) {
238-
start = end = endParent;
239-
}
240-
else {
241-
// start and end nodes belong to different subtrees
242-
return { errors: [createFileDiagnostic(sourceFile, span.start, length, Messages.CannotExtractRange)] };
243-
}
220+
// start and end nodes belong to different subtrees
221+
return { errors: [createFileDiagnostic(sourceFile, span.start, length, Messages.CannotExtractRange)] };
244222
}
223+
245224
if (start !== end) {
246225
// start and end should be statements and parent should be either block or a source file
247226
if (!isBlockLike(start.parent)) {

tests/baselines/reference/extractFunction/extractFunction8.ts

Lines changed: 0 additions & 65 deletions
This file was deleted.

0 commit comments

Comments
 (0)