Skip to content

Fix organize imports inserting extra blank lines #41927

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

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
cb05c28
Update package-lock.json
typescript-bot Nov 20, 2020
2f4d4c1
Update package-lock.json
typescript-bot Nov 23, 2020
b94544a
Update package-lock.json
typescript-bot Nov 26, 2020
60d63dd
Update package-lock.json
typescript-bot Nov 27, 2020
3d45ac0
Update package-lock.json
typescript-bot Dec 1, 2020
7dda7e3
Update package-lock.json
typescript-bot Dec 2, 2020
24f0d6b
Update package-lock.json
typescript-bot Dec 3, 2020
a9a8cbb
Update package-lock.json
typescript-bot Dec 4, 2020
9e42a69
Update package-lock.json
typescript-bot Dec 5, 2020
12cc980
Update package-lock.json
typescript-bot Dec 6, 2020
a43ec41
Update package-lock.json
typescript-bot Dec 9, 2020
3a6a36a
Update package-lock.json
typescript-bot Dec 11, 2020
202873a
Update package-lock.json
typescript-bot Dec 13, 2020
78ea60a
Update package-lock.json
typescript-bot Dec 16, 2020
2b0578d
Update package-lock.json
typescript-bot Dec 17, 2020
e7c832a
Update package-lock.json
typescript-bot Dec 19, 2020
62604bc
Update package-lock.json
typescript-bot Dec 21, 2020
4d807de
Update package-lock.json
typescript-bot Dec 23, 2020
dda079d
Update package-lock.json
typescript-bot Dec 24, 2020
9c121aa
Update package-lock.json
typescript-bot Dec 31, 2020
8777dc7
Update package-lock.json
typescript-bot Jan 1, 2021
f1aa0f9
Update package-lock.json
typescript-bot Jan 2, 2021
e4e4cf2
Update package-lock.json
typescript-bot Jan 5, 2021
dd79363
Update package-lock.json
typescript-bot Jan 6, 2021
2e93e31
Merge remote-tracking branch 'upstream/master'
armanio123 Jan 7, 2021
c709f17
More sophisticated check for source position comparability
andrewbranch Apr 8, 2020
67ad6b4
Merge branch 'master' of https://github.com/armanio123/TypeScript
armanio123 Jan 7, 2021
5215977
Fix organize imports by looking at the nodes positions
armanio123 Jan 7, 2021
7cdbdb9
Merge remote-tracking branch 'upstream/master' into FixOrganizeImports
armanio123 Jan 7, 2021
b51345e
Rollback formatting changes
armanio123 Jan 7, 2021
662ee6c
Added tests, fixed organizeImports algorithm
armanio123 Jan 12, 2021
aaf353e
Fix autoformatting again
armanio123 Jan 12, 2021
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
33 changes: 30 additions & 3 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,7 @@ namespace ts {
let containerEnd = -1;
let declarationListContainerEnd = -1;
let currentLineMap: readonly number[] | undefined;
let detachedCommentsInfo: { nodePos: number, detachedCommentEndPos: number}[] | undefined;
let detachedCommentsInfo: { nodePos: number, detachedCommentEndPos: number }[] | undefined;
let hasWrittenComment = false;
let commentsDisabled = !!printerOptions.removeComments;
let lastNode: Node | undefined;
Expand Down Expand Up @@ -4488,7 +4488,7 @@ namespace ts {
// JsxText will be written with its leading whitespace, so don't add more manually.
return 0;
}
else if (!nodeIsSynthesized(previousNode) && !nodeIsSynthesized(nextNode) && previousNode.parent === nextNode.parent) {
else if (siblingNodePositionsAreComparable(previousNode, nextNode)) {
if (preserveSourceNewlines) {
return getEffectiveLines(
includeComments => getLinesBetweenRangeEndAndRangeStart(
Expand All @@ -4509,6 +4509,33 @@ namespace ts {
return format & ListFormat.MultiLine ? 1 : 0;
}

function siblingNodePositionsAreComparable(previousNode: Node, nextNode: Node) {
if (nodeIsSynthesized(previousNode) || nodeIsSynthesized(nextNode) || previousNode.parent !== nextNode.parent) {
return false;
}

if (isImportSpecifier(previousNode)) {
const getNextSpecifier = (node: ImportSpecifier): ImportSpecifier | undefined => {
if (!node.parent) {
return;
}
for (const sibling of node.parent.elements) {
if (node.pos < sibling.pos) {
return sibling;
}
}
};

// Get the next specifier and compare against nextNode. If they are not equal, nodes have been rearranged and positions cannot be compared.
const nextSpecifier = getNextSpecifier(previousNode);
if (nextSpecifier && nextSpecifier !== nextNode) {
return false;
}
}

return true;
}

function getClosingLineTerminatorCount(parentNode: TextRange, children: readonly Node[], format: ListFormat): number {
if (format & ListFormat.PreserveLines || preserveSourceNewlines) {
if (format & ListFormat.PreferNewLine) {
Expand Down Expand Up @@ -4659,7 +4686,7 @@ namespace ts {
const text = isNumericLiteral(textSourceNode) ? textSourceNode.text : getTextOfNode(textSourceNode);
return jsxAttributeEscape ? `"${escapeJsxAttributeString(text)}"` :
neverAsciiEscape || (getEmitFlags(node) & EmitFlags.NoAsciiEscaping) ? `"${escapeString(text)}"` :
`"${escapeNonAsciiString(text)}"`;
`"${escapeNonAsciiString(text)}"`;
}
else {
return getLiteralTextOfNode(textSourceNode, neverAsciiEscape, jsxAttributeEscape);
Expand Down
16 changes: 0 additions & 16 deletions src/compiler/program.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2354,22 +2354,6 @@ namespace ts {
}
}

/** Returns a token if position is in [start-of-leading-trivia, end), includes JSDoc only in JS files */
function getNodeAtPosition(sourceFile: SourceFile, position: number): Node {
let current: Node = sourceFile;
const getContainingChild = (child: Node) => {
if (child.pos <= position && (position < child.end || (position === child.end && (child.kind === SyntaxKind.EndOfFileToken)))) {
return child;
}
};
while (true) {
const child = isJavaScriptFile && hasJSDocNodes(current) && forEach(current.jsDoc, getContainingChild) || forEachChild(current, getContainingChild);
if (!child) {
return current;
}
current = child;
}
}
}

function getLibFileFromReference(ref: FileReference) {
Expand Down
17 changes: 17 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7081,4 +7081,21 @@ namespace ts {
return false;
}
}

/** Returns a token if position is in [start-of-leading-trivia, end), includes JSDoc only in JS files */
export function getNodeAtPosition(sourceFile: SourceFile, position: number): Node {
let current: Node = sourceFile;
const getContainingChild = (child: Node) => {
if (child.pos <= position && (position < child.end || (position === child.end && (child.kind === SyntaxKind.EndOfFileToken)))) {
return child;
}
};
while (true) {
const child = isSourceFileJS(sourceFile) && hasJSDocNodes(current) && forEach(current.jsDoc, getContainingChild) || forEachChild(current, getContainingChild);
if (!child) {
return current;
}
current = child;
}
}
}
32 changes: 32 additions & 0 deletions tests/cases/fourslash/organizeImports1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/// <reference path="fourslash.ts" />

// Regression test for bug #41417

//// import {
//// d, d as D,
//// c,
//// c as C, b,
//// b as B, a
//// } from './foo';
//// import {
//// h, h as H,
//// g,
//// g as G, f,
//// f as F, e
//// } from './foo';
////
//// console.log(a, B, b, c, C, d, D);
//// console.log(e, f, F, g, G, H, h);

verify.organizeImports(
`import {
a, b,
b as B, c,
c as C, d, d as D, e, f,
f as F, g,
g as G, h, h as H
} from './foo';

console.log(a, B, b, c, C, d, D);
console.log(e, f, F, g, G, H, h);`
);
16 changes: 16 additions & 0 deletions tests/cases/fourslash/organizeImports2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path="fourslash.ts" />

// Regression test for bug #41417

//// import {
//// Foo
//// , Bar
//// } from "foo"
////
//// console.log(Foo, Bar);

verify.organizeImports(
`import { Bar, Foo } from "foo";

console.log(Foo, Bar);`
);
18 changes: 18 additions & 0 deletions tests/cases/fourslash/organizeImports3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference path="fourslash.ts" />

// Regression test for bug #41417

//// import {
//// Bar
//// , Foo
//// } from "foo"
////
//// console.log(Foo, Bar);

verify.organizeImports(
`import {
Bar,
Foo
} from "foo";

console.log(Foo, Bar);`);