Skip to content
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

Fix preserveSourceNewlines sibling node comparison (fixes extra newlines in organize imports) #42630

Merged
merged 40 commits into from
Feb 26, 2021
Merged
Show file tree
Hide file tree
Changes from 35 commits
Commits
Show all changes
40 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
c40b579
Make sibling node comparison work for all lists
andrewbranch Jan 12, 2021
07930c2
Don’t run siblingNodePositionsAreComparable at all unless `preserveSo…
andrewbranch Feb 3, 2021
a368a48
Merge branch 'master' into FixOrganizeImports
andrewbranch Feb 3, 2021
de698f9
Move getNodeAtPosition back
andrewbranch Feb 3, 2021
77b1902
Optimize
andrewbranch Feb 5, 2021
bcbec24
Use node array index check instead of tree walk
andrewbranch Feb 9, 2021
af14aef
Revert unneeded change
andrewbranch Feb 9, 2021
a0bf137
Merge branch 'master' into FixOrganizeImports
andrewbranch Feb 26, 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
43 changes: 32 additions & 11 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -920,7 +920,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 @@ -4490,15 +4490,15 @@ 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) {
if (preserveSourceNewlines) {
return getEffectiveLines(
includeComments => getLinesBetweenRangeEndAndRangeStart(
previousNode,
nextNode,
currentSourceFile!,
includeComments));
}
else if (preserveSourceNewlines && siblingNodePositionsAreComparable(previousNode, nextNode)) {
return getEffectiveLines(
includeComments => getLinesBetweenRangeEndAndRangeStart(
previousNode,
nextNode,
currentSourceFile!,
includeComments));
}
else if (!preserveSourceNewlines && !nodeIsSynthesized(previousNode) && !nodeIsSynthesized(nextNode)) {
return rangeEndIsOnSameLineAsRangeStart(previousNode, nextNode, currentSourceFile!) ? 0 : 1;
}
else if (synthesizedNodeStartsOnNewLine(previousNode, format) || synthesizedNodeStartsOnNewLine(nextNode, format)) {
Expand All @@ -4511,6 +4511,27 @@ 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 (nextNode.pos < previousNode.end) {
return false;
}

if (!previousNode.parent || !nextNode.parent) {
const previousParent = getOriginalNode(previousNode).parent;
return previousParent && previousParent === getOriginalNode(nextNode).parent;
}

if (!nodeIsFirstNodeAtOrAfterPosition(currentSourceFile!, getOriginalNode(nextNode), previousNode.end)) {
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 @@ -4661,7 +4682,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 @@ -2355,22 +2355,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
50 changes: 50 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7119,6 +7119,56 @@ namespace ts {
}
}

/** 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;
}
}

export function nodeIsFirstNodeAtOrAfterPosition(sourceFile: SourceFile, node: Node, position: number): boolean {
if (node.pos === position) return true;
if (node.pos < position) return false;
let current: Node = sourceFile;
let next: Node | undefined;
const getContainingChild = (child: Node) => {
if (child.pos <= position && (position < child.end || (position === child.end && (child.kind === SyntaxKind.EndOfFileToken)))) {
return child;
}
else if (!next && child.pos > position) {
next = child;
}
};
while (true) {
const child = isSourceFileJS(sourceFile) && hasJSDocNodes(current) && forEach(current.jsDoc, getContainingChild) || forEachChild(current, getContainingChild);
andrewbranch marked this conversation as resolved.
Show resolved Hide resolved
if (child === node || next === node) {
return true;
}
if (!child) {
if (next) {
// 'position' fell between two nodes (e.g., the comma between
// two ImportSpecifiers). Instead of stopping at the parent node,
// shift forward to the next node and continue searching there.
position = next.pos;
next = undefined;
continue;
}
return false;
}
current = child;
}
}

export function containsIgnoredPath(path: string) {
return some(ignoredPaths, p => stringContains(path, p));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ var y = {
"typeof":
};
var x = (_a = {
a: a,
: .b,
a: a, : .b,
a: a
},
_a["ss"] = ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ var n;
(function (n) {
var z = 10000;
n.y = {
m: m,
: .x // error
m: m, : .x // error
};
})(n || (n = {}));
m.y.x;
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,5 @@ var C2 = /** @class */ (function () {
return C2;
}());
var b = {
x: function () { },
1: // error
x: function () { }, 1: // error
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ var v = { a
return;

//// [parserErrorRecovery_ObjectLiteral2.js]
var v = { a: a, "return": };
var v = { a: a,
"return": };
Copy link
Member Author

Choose a reason for hiding this comment

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

These JS baseline changes are reverting back to their original state pre-#36688. That PR was never really meant to change JS emit, but the couple changes that did occur seemed ok so we went with it. Since siblingNodePositionsAreComparable might do a little more work than what’s currently happening in master, I reverted the codepath that non-codefix/refactor emit takes back to the way it originally was.

Copy link
Member Author

Choose a reason for hiding this comment

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

siblingNodePositionsAreComparable is now quite cheap, but I think it still makes sense to keep it out of JS emit for now.

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);`);