Skip to content

Commit 2c083bd

Browse files
committed
Fix extra lines on organize imports command
1 parent a9a8cbb commit 2c083bd

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

src/compiler/emitter.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,7 @@ namespace ts {
879879
let containerEnd = -1;
880880
let declarationListContainerEnd = -1;
881881
let currentLineMap: readonly number[] | undefined;
882-
let detachedCommentsInfo: { nodePos: number, detachedCommentEndPos: number}[] | undefined;
882+
let detachedCommentsInfo: { nodePos: number, detachedCommentEndPos: number }[] | undefined;
883883
let hasWrittenComment = false;
884884
let commentsDisabled = !!printerOptions.removeComments;
885885
let lastNode: Node | undefined;
@@ -902,6 +902,11 @@ namespace ts {
902902
bundleFileInfo
903903
};
904904

905+
function getPreserveSourceNewlines(node: Node) {
906+
// Ignore preserveSourceNewlines if it has enabled the flag.
907+
return preserveSourceNewlines && !(getEmitFlags(node) & EmitFlags.IgnoreSourceNewlines);
908+
}
909+
905910
function printNode(hint: EmitHint, node: Node, sourceFile: SourceFile): string {
906911
switch (hint) {
907912
case EmitHint.SourceFile:
@@ -1205,25 +1210,25 @@ namespace ts {
12051210
if (onEmitNode !== noEmitNotification && (!isEmitNotificationEnabled || isEmitNotificationEnabled(node))) {
12061211
return pipelineEmitWithNotification;
12071212
}
1208-
// falls through
1213+
// falls through
12091214

12101215
case PipelinePhase.Substitution:
12111216
if (substituteNode !== noEmitSubstitution && (lastSubstitution = substituteNode(emitHint, node)) !== node) {
12121217
return pipelineEmitWithSubstitution;
12131218
}
1214-
// falls through
1219+
// falls through
12151220

12161221
case PipelinePhase.Comments:
12171222
if (!commentsDisabled && node.kind !== SyntaxKind.SourceFile) {
12181223
return pipelineEmitWithComments;
12191224
}
1220-
// falls through
1225+
// falls through
12211226

12221227
case PipelinePhase.SourceMaps:
12231228
if (!sourceMapsDisabled && node.kind !== SyntaxKind.SourceFile && !isInJsonFile(node)) {
12241229
return pipelineEmitWithSourceMap;
12251230
}
1226-
// falls through
1231+
// falls through
12271232

12281233
case PipelinePhase.Emit:
12291234
return pipelineEmitWithHint;
@@ -4421,7 +4426,7 @@ namespace ts {
44214426
return 0;
44224427
}
44234428
else if (!nodeIsSynthesized(previousNode) && !nodeIsSynthesized(nextNode) && previousNode.parent === nextNode.parent) {
4424-
if (preserveSourceNewlines) {
4429+
if (getPreserveSourceNewlines(nextNode)) {
44254430
return getEffectiveLines(
44264431
includeComments => getLinesBetweenRangeEndAndRangeStart(
44274432
previousNode,
@@ -4591,7 +4596,7 @@ namespace ts {
45914596
const text = isNumericLiteral(textSourceNode) ? textSourceNode.text : getTextOfNode(textSourceNode);
45924597
return jsxAttributeEscape ? `"${escapeJsxAttributeString(text)}"` :
45934598
neverAsciiEscape || (getEmitFlags(node) & EmitFlags.NoAsciiEscaping) ? `"${escapeString(text)}"` :
4594-
`"${escapeNonAsciiString(text)}"`;
4599+
`"${escapeNonAsciiString(text)}"`;
45954600
}
45964601
else {
45974602
return getLiteralTextOfNode(textSourceNode, neverAsciiEscape, jsxAttributeEscape);

src/services/organizeImports.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -395,14 +395,21 @@ namespace ts.OrganizeImports {
395395
factory.updateImportClause(importDeclaration.importClause!, importDeclaration.importClause!.isTypeOnly, name, namedBindings), // TODO: GH#18217
396396
importDeclaration.moduleSpecifier);
397397
}
398-
398+
399399
function sortSpecifiers<T extends ImportOrExportSpecifier>(specifiers: readonly T[]) {
400400
return stableSort(specifiers, compareImportOrExportSpecifiers);
401401
}
402402

403403
export function compareImportOrExportSpecifiers<T extends ImportOrExportSpecifier>(s1: T, s2: T) {
404-
return compareIdentifiers(s1.propertyName || s1.name, s2.propertyName || s2.name)
404+
const comparison = compareIdentifiers(s1.propertyName || s1.name, s2.propertyName || s2.name)
405405
|| compareIdentifiers(s1.name, s2.name);
406+
407+
if (comparison === Comparison.LessThan) {
408+
// Ignore new lines if the node is going to get rearranged.
409+
ignoreSourceNewlines(s1);
410+
}
411+
412+
return comparison;
406413
}
407414

408415
/* internal */ // Exported for testing
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
// Regression test for bug #41417
4+
5+
/// import {
6+
/// a, b as B,
7+
/// c,
8+
/// d as D,
9+
/// c as C, b, d
10+
/// } from './foo';
11+
/// console.log(a, B, b, c, C, d, D);
12+
13+
verify.organizeImports(
14+
`import {
15+
a, b as B,
16+
b, c,
17+
c as C, d as D,
18+
d
19+
} from './foo';
20+
console.log(a, B, b, c, C, d, D);`
21+
);

0 commit comments

Comments
 (0)