Skip to content

Commit 662ee6c

Browse files
committed
Added tests, fixed organizeImports algorithm
1 parent b51345e commit 662ee6c

File tree

3 files changed

+58
-19
lines changed

3 files changed

+58
-19
lines changed

src/compiler/emitter.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1245,25 +1245,25 @@ namespace ts {
12451245
if (onEmitNode !== noEmitNotification && (!isEmitNotificationEnabled || isEmitNotificationEnabled(node))) {
12461246
return pipelineEmitWithNotification;
12471247
}
1248-
// falls through
1248+
// falls through
12491249

12501250
case PipelinePhase.Substitution:
12511251
if (substituteNode !== noEmitSubstitution && (lastSubstitution = substituteNode(emitHint, node)) !== node) {
12521252
return pipelineEmitWithSubstitution;
12531253
}
1254-
// falls through
1254+
// falls through
12551255

12561256
case PipelinePhase.Comments:
12571257
if (!commentsDisabled && node.kind !== SyntaxKind.SourceFile) {
12581258
return pipelineEmitWithComments;
12591259
}
1260-
// falls through
1260+
// falls through
12611261

12621262
case PipelinePhase.SourceMaps:
12631263
if (!sourceMapsDisabled && node.kind !== SyntaxKind.SourceFile && !isInJsonFile(node)) {
12641264
return pipelineEmitWithSourceMap;
12651265
}
1266-
// falls through
1266+
// falls through
12671267

12681268
case PipelinePhase.Emit:
12691269
return pipelineEmitWithHint;
@@ -4492,8 +4492,8 @@ namespace ts {
44924492
if (preserveSourceNewlines) {
44934493
return getEffectiveLines(
44944494
includeComments => getLinesBetweenRangeEndAndRangeStart(
4495-
getOriginalNode(previousNode),
4496-
getOriginalNode(nextNode),
4495+
previousNode,
4496+
nextNode,
44974497
currentSourceFile!,
44984498
includeComments));
44994499
}
@@ -4510,25 +4510,30 @@ namespace ts {
45104510
}
45114511

45124512
function siblingNodePositionsAreComparable(previousNode: Node, nextNode: Node) {
4513-
if (previousNode.kind === SyntaxKind.NotEmittedStatement && nextNode.kind === SyntaxKind.NotEmittedStatement) {
4514-
return false;
4515-
}
4516-
if (nodeIsSynthesized(previousNode) || nodeIsSynthesized(nextNode)) {
4513+
if (nodeIsSynthesized(previousNode) || nodeIsSynthesized(nextNode) || previousNode.parent !== nextNode.parent) {
45174514
return false;
45184515
}
45194516

4520-
// Get the position next node and compare against nextNode. If they are not equal, nodes have been rearranged and positions cannot be compared.
4521-
const originalNextNode = getNodeAtPosition(currentSourceFile!, previousNode.end + 1);
4522-
if (originalNextNode.pos !== nextNode.pos) {
4523-
return false;
4524-
}
4517+
if (isImportSpecifier(previousNode)) {
4518+
const getNextSpecifier = (node: ImportSpecifier): ImportSpecifier | undefined => {
4519+
if (!node.parent) {
4520+
return;
4521+
}
4522+
for (const sibling of node.parent.elements) {
4523+
if (node.pos < sibling.pos) {
4524+
return sibling;
4525+
}
4526+
}
4527+
};
45254528

4526-
if (!previousNode.parent || !nextNode.parent) {
4527-
const previousParent = getOriginalNode(previousNode).parent;
4528-
return previousParent && previousParent === getOriginalNode(nextNode).parent;
4529+
// Get the next specifier and compare against nextNode. If they are not equal, nodes have been rearranged and positions cannot be compared.
4530+
const nextSpecifier = getNextSpecifier(previousNode);
4531+
if (nextSpecifier && nextSpecifier !== nextNode) {
4532+
return false;
4533+
}
45294534
}
45304535

4531-
return nextNode.pos >= previousNode.end;
4536+
return true;
45324537
}
45334538

45344539
function getClosingLineTerminatorCount(parentNode: TextRange, children: readonly Node[], format: ListFormat): number {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// Regression test for bug #41417
4+
5+
//// import {
6+
//// Foo
7+
//// , Bar
8+
//// } from "foo"
9+
////
10+
//// console.log(Foo, Bar);
11+
12+
verify.organizeImports(
13+
`import { Bar, Foo } from "foo";
14+
15+
console.log(Foo, Bar);`
16+
);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// Regression test for bug #41417
4+
5+
//// import {
6+
//// Bar
7+
//// , Foo
8+
//// } from "foo"
9+
////
10+
//// console.log(Foo, Bar);
11+
12+
verify.organizeImports(
13+
`import {
14+
Bar,
15+
Foo
16+
} from "foo";
17+
18+
console.log(Foo, Bar);`);

0 commit comments

Comments
 (0)