This commit is contained in:
Andrew Branch
2020-02-07 11:41:49 -08:00
parent c689617410
commit 6e272f3939
15 changed files with 40 additions and 33 deletions
+10 -4
View File
@@ -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.
@@ -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);
@@ -1175,6 +1180,7 @@ namespace ts {
const substitute = lastSubstitution;
lastNode = savedLastNode;
lastSubstitution = savedLastSubstitution;
preserveNewlines = savedPreserveNewlines;
return substitute || node;
}
@@ -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)) {
@@ -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;
}
@@ -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;
}
@@ -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;
}
+5
View File
@@ -3559,6 +3559,11 @@ namespace ts {
return node;
}
export function ignoreSourceNewlines<T extends Node>(node: T): T {
getOrCreateEmitNode(node).flags |= EmitFlags.IgnoreSourceNewlines;
return node;
}
/**
* Gets the constant value to emit for an expression.
*/
+1
View File
@@ -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.
}
export interface EmitHelper {
+6 -5
View File
@@ -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[] {
@@ -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;
}
@@ -991,7 +992,7 @@ 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:
@@ -999,7 +1000,7 @@ namespace ts.formatting {
// 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:
+2 -2
View File
@@ -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);
changes.replaceNode(file, selection, createTypeReferenceNode(name, typeParameters.map(id => createTypeReferenceNode(id.name, /* typeArguments */ undefined))));
}
@@ -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))));
}
+1
View File
@@ -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.
*/
+1
View File
@@ -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.
*/
@@ -35,7 +35,8 @@ var y = {
"typeof":
};
var x = (_a = {
a: a, : .b,
a: a,
: .b,
a: a
},
_a["ss"] = ,
@@ -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;
@@ -42,5 +42,6 @@ var C2 = /** @class */ (function () {
return C2;
}());
var b = {
x: function () { }, 1: // error
x: function () { },
1: // error
};
@@ -3,5 +3,4 @@ var v = { a
return;
//// [parserErrorRecovery_ObjectLiteral2.js]
var v = { a: a,
"return": };
var v = { a: a, "return": };
@@ -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);`
});
@@ -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 { }
`,
},
});
@@ -22,6 +22,5 @@ verify.codeFix({
export function f() { }
export function g() { }
export function h() { }
export class C {
}`,
export class C { }`,
});
@@ -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 };`,
});