8000 Preserve newlines from original source when printing nodes from TextChanges by andrewbranch · Pull Request #36688 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content

Preserve newlines from original source when printing nodes from TextChanges #36688

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

Merged
merged 28 commits into from
Mar 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2b9e4aa
Allow emitter to write multiple newlines in node lists
andrewbranch Feb 6, 2020
c689617
Progress
andrewbranch Feb 7, 2020
6e272f3
Progress
andrewbranch Feb 7, 2020
839fb8e
Fix recomputeIndentation
andrewbranch Feb 7, 2020
0ea8d79
Add tests, fix leading line terminator count
andrewbranch Feb 7, 2020
6ff9c2b
Do a bit less work when `preserveNewlines` is off
andrewbranch Feb 10, 2020
c91efd4
Fix accidental find/replace rename
andrewbranch Feb 11, 2020
e3ef427
Restore some monomorphism
andrewbranch Feb 19, 2020
e535e27
Fix single line writer
andrewbranch Feb 19, 2020
21b0cb8
Fix other writers
andrewbranch Feb 19, 2020
b6cf73d
Merge branch 'master' into bug/27294
andrewbranch Feb 19, 2020
0c536c5
Revert "Fix other writers"
andrewbranch Feb 19, 2020
91eaf80
Revert "Fix single line writer"
andrewbranch Feb 19, 2020
3be2c86
Revert "Restore some monomorphism"
andrewbranch Feb 19, 2020
8ed98bc
Add equal position optimization to getLinesBetweenRangeEndAndRangeStart
andrewbranch Feb 19, 2020
d9c80fd
Add one more test
andrewbranch Feb 19, 2020
af188d4
Actually save the test file
andrewbranch Feb 19, 2020
19a9728
Rename preserveNewlines to preserveSourceNewlines
andrewbranch Feb 28, 2020
131f2bb
Make ignoreSourceNewlines internal
andrewbranch Feb 28, 2020
34147a5
Optimize lines-between functions
andrewbranch Mar 2, 2020
cf96308
Merge branch 'master' into bug/27294
andrewbranch Mar 2, 2020
075c782
Add comment;
andrewbranch Mar 2, 2020
563d223
Fix trailing line terminator count bug for function parameters
andrewbranch Mar 13, 2020
8b61d66
Preserve newlines around parenthesized expressions
andrewbranch Mar 14, 2020
e7c2b28
Merge branch 'master' into bug/27294
andrewbranch Mar 14, 2020
e99d833
Back to speculative microoptimizations, yay
andrewbranch Mar 16, 2020
ac768d0
Don’t call getEffectiveLines during tsc emit at all
andrewbranch Mar 16, 2020
6daa27e
Merge branch 'master' into bug/27294
andrewbranch Mar 16, 2020
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
Prev Previous commit
Next Next commit
Preserve newlines around parenthesized expressions
  • Loading branch information
andrewbranch committed Mar 14, 2020
commit 8b61d665178149d25d81adbe018e2a084a22741c
31 changes: 24 additions & 7 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2360,7 +2360,16 @@ namespace ts {

function emitParenthesizedExpression(node: ParenthesizedExpression) {
const openParenPos = emitTokenWithComment(SyntaxKind.OpenParenToken, node.pos, writePunctuation, node);
const leadingNewlines = getLeadingLineTerminatorCount(node, node.expression, ListFormat.None);
if (leadingNewlines) {
writeLinesAndIndent(leadingNewlines, /*writeLinesIfNotIndenting*/ false);
}
emitExpression(node.expression);
const trailingNewlines = getClosingLineTerminatorCount(node, node.expression, ListFormat.None);
if (trailingNewlines) {
writeLine(trailingNewlines);
}
decreaseIndentIf(leadingNewlines);
emitTokenWithComment(SyntaxKind.CloseParenToken, node.expression ? node.expression.end : openParenPos, writePunctuation, node);
}

Expand Down Expand Up @@ -4259,7 +4268,7 @@ namespace ts {
// previous indent values to be considered at a time. This also allows caller to just
// call this once, passing in all their appropriate indent values, instead of needing
// to call this helper function multiple times.
function decreaseIndentIf(value1: boolean | number, value2: boolean | number) {
function decreaseIndentIf(value1: boolean | number | undefined, value2?: boolean | number) {
if (value1) {
decreaseIndent();
}
Expand All @@ -4268,17 +4277,21 @@ namespace ts {
}
}

function getLeadingLineTerminatorCount(parentNode: TextRange, children: NodeArray<Node>, format: ListFormat): number {
function getLeadingLineTerminatorCount(parentNode: TextRange, children: NodeArray<Node> | Node, format: ListFormat): number {
if (format & ListFormat.PreserveLines || preserveSourceNewlines) {
if (format & ListFormat.PreferNewLine) {
return 1;
}

const firstChild = children[0];
const firstChild = isArray(children) ? children[0] : children;
if (firstChild === undefined) {
return rangeIsOnSingleLine(parentNode, currentSourceFile!) ? 0 : 1;
}
else if (!positionIsSynthesized(parentNode.pos) && !nodeIsSynthesized(firstChild) && firstChild.parent === parentNode) {
if (firstChild.kind === SyntaxKind.JsxText) {
// JsxText will be written with its leading whitespace, so don't add more manually.
return 0;
}
if (!positionIsSynthesized(parentNode.pos) && !nodeIsSynthesized(firstChild) && firstChild.parent === parentNode) {
if (preserveSourceNewlines) {
return getEffectiveLines(
includeComments => getLinesBetweenPositionAndPrecedingNonWhitespaceCharacter(
Expand All @@ -4288,7 +4301,7 @@ namespace ts {
}
return rangeStartPositionsAreOnSameLine(parentNode, firstChild, currentSourceFile!) ? 0 : 1;
}
else if (synthesizedNodeStartsOnNewLine(firstChild, format)) {
if (synthesizedNodeStartsOnNewLine(firstChild, format)) {
return 1;
}
}
Expand All @@ -4300,6 +4313,10 @@ namespace ts {
if (previousNode === undefined || nextNode === undefined) {
return 0;
}
if (nextNode.kind === SyntaxKind.JsxText) {
// 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) {
return getEffectiveLines(
includeComments => getLinesBetweenRangeEndAndRangeStart(
Expand All @@ -4318,13 +4335,13 @@ namespace ts {
return format & ListFormat.MultiLine ? 1 : 0;
}

function getClosingLineTerminatorCount(parentNode: TextRange, children: NodeArray<Node>, format: ListFormat): number {
function getClosingLineTerminatorCount(parentNode: TextRange, children: NodeArray<Node> | Node, format: ListFormat): number {
if (format & ListFormat.PreserveLines || preserveSourceNewlines) {
if (format & ListFormat.PreferNewLine) {
return 1;
}

const lastChild = lastOrUndefined(children);
const lastChild = isArray(children) ? lastOrUndefined(children) : children;
if (lastChild === undefined) {
return rangeIsOnSingleLine(parentNode, currentSourceFile!) ? 0 : 1;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/cases/fourslash/textChangesPreserveNewlines7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ verify.moveToNewFile({
</div>
</div>
);
}`
}
`
}
});
0