Skip to content
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
Progress
  • Loading branch information
andrewbranch committed Feb 7, 2020
commit 6e272f3939fa0eda39a9aef9441b1732d1ff9d26
14 changes: 10 additions & 4 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,7 @@ namespace ts {
let tempFlags: TempFlags; // TempFlags for the current name generation scope.
let reservedNamesStack: Map<true>[]; // Stack of TempFlags reserved in enclosing name generation scopes.
let reservedNames: Map<true>; // TempFlags to reserve in nested name generation scopes.
let preserveNewlines = printerOptions.preserveNewlines; // Can be overridden inside nodes with the `IgnoreSourceNewlines` emit flag.

let writer: EmitTextWriter;
let ownWriter: EmitTextWriter; // Reusable `EmitTextWriter` for basic printing.
Expand Down Expand Up @@ -1164,8 +1165,12 @@ namespace ts {
function pipelineEmit(emitHint: EmitHint, node: Node) {
const savedLastNode = lastNode;
const savedLastSubstitution = lastSubstitution;
const savedPreserveNewlines = preserveNewlines;
lastNode = node;
lastSubstitution = undefined;
if (preserveNewlines && !!(getEmitFlags(node) & EmitFlags.IgnoreSourceNewlines)) {
preserveNewlines = false;
}

const pipelinePhase = getPipelinePhase(PipelinePhase.Notification, emitHint, node);
pipelinePhase(emitHint, node);
Expand All @@ -1175,6 +1180,7 @@ namespace ts {
const substitute = lastSubstitution;
lastNode = savedLastNode;
lastSubstitution = savedLastSubstitution;
preserveNewlines = savedPreserveNewlines;

return substitute || node;
}
Expand Down Expand Up @@ -3991,7 +3997,7 @@ namespace ts {

if (isEmpty) {
// Write a line terminator if the parent node was multi-line
if (format & ListFormat.MultiLine) {
if (format & ListFormat.MultiLine && !(preserveNewlines && rangeIsOnSingleLine(parentNode, currentSourceFile!))) {
writeLine();
}
else if (format & ListFormat.SpaceBetweenBraces && !(format & ListFormat.NoSpaceIfEmpty)) {
Expand Down Expand Up @@ -4262,7 +4268,7 @@ namespace ts {
}

function getLeadingLineTerminatorCount(parentNode: TextRange, children: NodeArray<Node>, format: ListFormat): number {
if (format & ListFormat.PreserveLines || printerOptions.preserveNewlines) {
if (format & ListFormat.PreserveLines || preserveNewlines) {
if (format & ListFormat.PreferNewLine) {
return 1;
}
Expand All @@ -4283,7 +4289,7 @@ namespace ts {
}

function getSeparatingLineTerminatorCount(previousNode: Node | undefined, nextNode: Node, format: ListFormat): number {
if (format & ListFormat.PreserveLines || printerOptions.preserveNewlines) {
if (format & ListFormat.PreserveLines || preserveNewlines) {
if (previousNode === undefined || nextNode === undefined) {
return 0;
}
Expand All @@ -4302,7 +4308,7 @@ namespace ts {
}

function getClosingLineTerminatorCount(parentNode: TextRange, children: NodeArray<Node>, format: ListFormat): number {
if (format & ListFormat.PreserveLines || printerOptions.preserveNewlines) {
if (format & ListFormat.PreserveLines || preserveNewlines) {
if (format & ListFormat.PreferNewLine) {
return 1;
}
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/factoryPublic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3559,6 +3559,11 @@ namespace ts {
return node;
}

export function ignoreSourceNewlines<T extends Node>(node: T): T {
Comment thread
andrewbranch marked this conversation as resolved.
getOrCreateEmitNode(node).flags |= EmitFlags.IgnoreSourceNewlines;
return node;
}

/**
* Gets the constant value to emit for an expression.
*/
Expand Down
1 change: 1 addition & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5780,6 +5780,7 @@ namespace ts {
NoAsciiEscaping = 1 << 24, // When synthesizing nodes that lack an original node or textSourceNode, we want to write the text on the node with ASCII escaping substitutions.
/*@internal*/ TypeScriptClassWrapper = 1 << 25, // The node is an IIFE class wrapper created by the ts transform.
/*@internal*/ NeverApplyImportHelper = 1 << 26, // Indicates the node should never be wrapped with an import star helper (because, for example, it imports tslib itself)
/*@internal*/ IgnoreSourceNewlines = 1 << 27, // Overrides `printerOptions.preserveNewlines` to print this node (and all descendants) with default whitespace.
Comment thread
andrewbranch marked this conversation as resolved.
Outdated
}

export interface EmitHelper {
Expand Down
11 changes: 6 additions & 5 deletions src/services/formatting/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace ts.formatting {
* Formatter calls this function when rule adds or deletes new lines from the text
* so indentation scope can adjust values of indentation and delta.
*/
recomputeIndentation(lineAddedByFormatting: boolean): void;
recomputeIndentation(lineAddedByFormatting: boolean, parent: Node): void;
}

export function formatOnEnter(position: number, sourceFile: SourceFile, formatContext: FormatContext): TextChange[] {
Expand Down Expand Up @@ -565,8 +565,9 @@ namespace ts.formatting {
!suppressDelta && shouldAddDelta(line, kind, container) ? indentation + getDelta(container) : indentation,
getIndentation: () => indentation,
getDelta,
recomputeIndentation: lineAdded => {
if (node.parent && SmartIndenter.shouldIndentChildNode(options, node.parent, node, sourceFile)) {
recomputeIndentation: (lineAdded, parentIn) => {
const parent = node.parent || parentIn;
if (node !== parent && SmartIndenter.shouldIndentChildNode(options, parent, node, sourceFile)) {
indentation += lineAdded ? options.indentSize! : -options.indentSize!;
delta = SmartIndenter.shouldIndentChildNode(options, node) ? options.indentSize! : 0;
}
Expand Down Expand Up @@ -991,15 +992,15 @@ namespace ts.formatting {
// Handle the case where the next line is moved to be the end of this line.
// In this case we don't indent the next line in the next pass.
if (currentParent.getStart(sourceFile) === currentItem.pos) {
dynamicIndentation.recomputeIndentation(/*lineAddedByFormatting*/ false);
dynamicIndentation.recomputeIndentation(/*lineAddedByFormatting*/ false, currentParent);
}
break;
case LineAction.LineAdded:
// Handle the case where token2 is moved to the new line.
// In this case we indent token2 in the next pass but we set
// sameLineIndent flag to notify the indenter that the indentation is within the line.
if (currentParent.getStart(sourceFile) === currentItem.pos) {
dynamicIndentation.recomputeIndentation(/*lineAddedByFormatting*/ true);
dynamicIndentation.recomputeIndentation(/*lineAddedByFormatting*/ true, currentParent);
}
break;
default:
Expand Down
4 changes: 2 additions & 2 deletions src/services/refactors/extractType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ namespace ts.refactor {
typeParameters.map(id => updateTypeParameterDeclaration(id, id.name, id.constraint, /* defaultType */ undefined)),
selection
);
changes.insertNodeBefore(file, firstStatement, newTypeNode, /* blankLineBetween */ true);
changes.insertNodeBefore(file, firstStatement, ignoreSourceNewlines(newTypeNode), /* blankLineBetween */ true);
Comment thread
andrewbranch marked this conversation as resolved.
changes.replaceNode(file, selection, createTypeReferenceNode(name, typeParameters.map(id => createTypeReferenceNode(id.name, /* typeArguments */ undefined))));
}

Expand All @@ -174,7 +174,7 @@ namespace ts.refactor {
/* heritageClauses */ undefined,
typeElements
);
changes.insertNodeBefore(file, firstStatement, newTypeNode, /* blankLineBetween */ true);
changes.insertNodeBefore(file, firstStatement, ignoreSourceNewlines(newTypeNode), /* blankLineBetween */ true);
changes.replaceNode(file, selection, createTypeReferenceNode(name, typeParameters.map(id => createTypeReferenceNode(id.name, /* typeArguments */ undefined))));
}

Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4359,6 +4359,7 @@ declare namespace ts {
function setSyntheticTrailingComments<T extends Node>(node: T, comments: SynthesizedComment[] | undefined): T;
function addSyntheticTrailingComment<T extends Node>(node: T, kind: SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia, text: string, hasTrailingNewLine?: boolean): T;
function moveSyntheticComments<T extends Node>(node: T, original: Node): T;
function ignoreSourceNewlines<T extends Node>(node: T): T;
Comment thread
andrewbranch marked this conversation as resolved.
Outdated
/**
* Gets the constant value to emit for an expression.
*/
Expand Down
1 change: 1 addition & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4359,6 +4359,7 @@ declare namespace ts {
function setSyntheticTrailingComments<T extends Node>(node: T, comments: SynthesizedComment[] | undefined): T;
function addSyntheticTrailingComment<T extends Node>(node: T, kind: SyntaxKind.SingleLineCommentTrivia | SyntaxKind.MultiLineCommentTrivia, text: string, hasTrailingNewLine?: boolean): T;
function moveSyntheticComments<T extends Node>(node: T, original: Node): T;
function ignoreSourceNewlines<T extends Node>(node: T): T;
/**
* Gets the constant value to emit for an expression.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ 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,7 +25,8 @@ 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,5 +42,6 @@ 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,5 +3,4 @@ var v = { a
return;

//// [parserErrorRecovery_ObjectLiteral2.js]
var v = { a: a,
"return": };
var v = { a: a, "return": };
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ edit.applyRefactor({
actionDescription: "Extract to constant in enclosing scope",
newContent:
`declare function fWithThis(fn: (this: { a: string }, a: string) => string): void;
const newLocal = function(this: {
a: string;
}, a: string): string { return this.a; };
const newLocal = function(this: { a: string; }, a: string): string { return this.a; };
fWithThis(/*RENAME*/newLocal);`
});
13 changes: 4 additions & 9 deletions tests/cases/fourslash/moveToNewFile_declarationKinds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,11 @@ type U = T; type V = I;`,
"/x.ts":
`export const x = 0;
export function f() { }
export class C {
}
export enum E {
}
export namespace N {
export const x = 0;
}
export class C { }
export enum E { }
export namespace N { export const x = 0; }
export type T = number;
export interface I {
}
export interface I { }
`,
},
});
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,5 @@ verify.codeFix({
export function f() { }
export function g() { }
export function h() { }
export class C {
}`,
export class C { }`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ verify.codeFix({
`var C = {};
console.log(C);
export async function* f(p) { p; }
const _C = class C extends D {
m() { }
};
const _C = class C extends D { m() { } };
export { _C as C };`,
});