Skip to content

Commit 8ad4aee

Browse files
author
Andy
authored
Make ChangeTracker#newLineCharacter public, to avoid having to pass newLineCharacter around as a parameter (#20574)
* Make ChangeTracker#newLineCharacter public, to avoid having to pass newLineCharacter around as a parameter * Don't require newLineCharacter as input to ChangeTracker methods, and make it private again
1 parent 21ff2cd commit 8ad4aee

27 files changed

+218
-187
lines changed

src/compiler/core.ts

+2
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,8 @@ namespace ts {
288288
return undefined;
289289
}
290290

291+
export function findLast<T, U extends T>(array: ReadonlyArray<T>, predicate: (element: T, index: number) => element is U): U | undefined;
292+
export function findLast<T>(array: ReadonlyArray<T>, predicate: (element: T, index: number) => boolean): T | undefined;
291293
export function findLast<T>(array: ReadonlyArray<T>, predicate: (element: T, index: number) => boolean): T | undefined {
292294
for (let i = array.length - 1; i >= 0; i--) {
293295
const value = array[i];

src/harness/unittests/textChanges.ts

+29-31
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ namespace M
103103
/*body */ createBlock(statements)
104104
);
105105

106-
changeTracker.insertNodeBefore(sourceFile, /*before*/findChild("M2", sourceFile), newFunction, { suffix: newLineCharacter });
106+
changeTracker.insertNodeBefore(sourceFile, /*before*/findChild("M2", sourceFile), newFunction);
107107

108108
// replace statements with return statement
109109
const newStatement = createReturn(
@@ -129,12 +129,11 @@ function bar() {
129129
changeTracker.deleteRange(sourceFile, { pos: text.indexOf("function foo"), end: text.indexOf("function bar") });
130130
});
131131
}
132-
function findVariableStatementContaining(name: string, sourceFile: SourceFile) {
133-
const varDecl = findChild(name, sourceFile);
134-
assert.equal(varDecl.kind, SyntaxKind.VariableDeclaration);
135-
const varStatement = varDecl.parent.parent;
136-
assert.equal(varStatement.kind, SyntaxKind.VariableStatement);
137-
return varStatement;
132+
function findVariableStatementContaining(name: string, sourceFile: SourceFile): VariableStatement {
133+
return cast(findVariableDeclarationContaining(name, sourceFile).parent.parent, isVariableStatement);
134+
}
135+
function findVariableDeclarationContaining(name: string, sourceFile: SourceFile): VariableDeclaration {
136+
return cast(findChild(name, sourceFile), isVariableDeclaration);
138137
}
139138
{
140139
const text = `
@@ -306,11 +305,11 @@ var y; // comment 4
306305
var z = 3; // comment 5
307306
// comment 6
308307
var a = 4; // comment 7`;
309-
runSingleFileTest("insertNodeAt1", /*placeOpenBraceOnNewLineForFunctions*/ true, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
310-
changeTracker.insertNodeAt(sourceFile, text.indexOf("var y"), createTestClass(), { suffix: newLineCharacter });
308+
runSingleFileTest("insertNodeBefore3", /*placeOpenBraceOnNewLineForFunctions*/ true, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
309+
changeTracker.insertNodeBefore(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass());
311310
});
312-
runSingleFileTest("insertNodeAt2", /*placeOpenBraceOnNewLineForFunctions*/ true, text, /*validateNodes*/ false, (sourceFile, changeTracker) => {
313-
changeTracker.insertNodeAt(sourceFile, text.indexOf("; // comment 4"), createTestVariableDeclaration("z1"));
311+
runSingleFileTest("insertNodeAfterVariableDeclaration", /*placeOpenBraceOnNewLineForFunctions*/ true, text, /*validateNodes*/ false, (sourceFile, changeTracker) => {
312+
changeTracker.insertNodeAfter(sourceFile, findVariableDeclarationContaining("y", sourceFile), createTestVariableDeclaration("z1"));
314313
});
315314
}
316315
{
@@ -325,23 +324,22 @@ namespace M {
325324
var a = 4; // comment 7
326325
}`;
327326
runSingleFileTest("insertNodeBefore1", /*placeOpenBraceOnNewLineForFunctions*/ true, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
328-
changeTracker.insertNodeBefore(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass(), { suffix: newLineCharacter });
327+
changeTracker.insertNodeBefore(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass());
329328
});
330329
runSingleFileTest("insertNodeBefore2", /*placeOpenBraceOnNewLineForFunctions*/ true, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
331-
changeTracker.insertNodeBefore(sourceFile, findChild("M", sourceFile), createTestClass(), { suffix: newLineCharacter });
330+
changeTracker.insertNodeBefore(sourceFile, findChild("M", sourceFile), createTestClass());
332331
});
333332
runSingleFileTest("insertNodeAfter1", /*placeOpenBraceOnNewLineForFunctions*/ true, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
334-
changeTracker.insertNodeAfter(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass(), { suffix: newLineCharacter });
333+
changeTracker.insertNodeAfter(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass());
335334
});
336335
runSingleFileTest("insertNodeAfter2", /*placeOpenBraceOnNewLineForFunctions*/ true, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
337-
changeTracker.insertNodeAfter(sourceFile, findChild("M", sourceFile), createTestClass(), { prefix: newLineCharacter });
336+
changeTracker.insertNodeAfter(sourceFile, findChild("M", sourceFile), createTestClass());
338337
});
339338
}
340339

341-
function findOpenBraceForConstructor(sourceFile: SourceFile) {
340+
function findConstructor(sourceFile: SourceFile): ConstructorDeclaration {
342341
const classDecl = <ClassDeclaration>sourceFile.statements[0];
343-
const constructorDecl = forEach(classDecl.members, m => m.kind === SyntaxKind.Constructor && (<ConstructorDeclaration>m).body && <ConstructorDeclaration>m);
344-
return constructorDecl.body.getFirstToken();
342+
return find<ClassElement, ConstructorDeclaration>(classDecl.members, (m): m is ConstructorDeclaration => isConstructorDeclaration(m) && !!m.body)!;
345343
}
346344
function createTestSuperCall() {
347345
const superCall = createCall(
@@ -359,8 +357,8 @@ class A {
359357
}
360358
}
361359
`;
362-
runSingleFileTest("insertNodeAfter3", /*placeOpenBraceOnNewLineForFunctions*/ false, text1, /*validateNodes*/ false, (sourceFile, changeTracker) => {
363-
changeTracker.insertNodeAfter(sourceFile, findOpenBraceForConstructor(sourceFile), createTestSuperCall(), { suffix: newLineCharacter });
360+
runSingleFileTest("insertNodeAtConstructorStart", /*placeOpenBraceOnNewLineForFunctions*/ false, text1, /*validateNodes*/ false, (sourceFile, changeTracker) => {
361+
changeTracker.insertNodeAtConstructorStart(sourceFile, findConstructor(sourceFile), createTestSuperCall());
364362
});
365363
const text2 = `
366364
class A {
@@ -370,7 +368,7 @@ class A {
370368
}
371369
`;
372370
runSingleFileTest("insertNodeAfter4", /*placeOpenBraceOnNewLineForFunctions*/ false, text2, /*validateNodes*/ false, (sourceFile, changeTracker) => {
373-
changeTracker.insertNodeAfter(sourceFile, findVariableStatementContaining("x", sourceFile), createTestSuperCall(), { suffix: newLineCharacter });
371+
changeTracker.insertNodeAfter(sourceFile, findVariableStatementContaining("x", sourceFile), createTestSuperCall());
374372
});
375373
const text3 = `
376374
class A {
@@ -379,8 +377,8 @@ class A {
379377
}
380378
}
381379
`;
382-
runSingleFileTest("insertNodeAfter3-block with newline", /*placeOpenBraceOnNewLineForFunctions*/ false, text3, /*validateNodes*/ false, (sourceFile, changeTracker) => {
383-
changeTracker.insertNodeAfter(sourceFile, findOpenBraceForConstructor(sourceFile), createTestSuperCall(), { suffix: newLineCharacter });
380+
runSingleFileTest("insertNodeAtConstructorStart-block with newline", /*placeOpenBraceOnNewLineForFunctions*/ false, text3, /*validateNodes*/ false, (sourceFile, changeTracker) => {
381+
changeTracker.insertNodeAtConstructorStart(sourceFile, findConstructor(sourceFile), createTestSuperCall());
384382
});
385383
}
386384
{
@@ -638,7 +636,7 @@ class A {
638636
}
639637
const insertAfter = findChild("x", sourceFile);
640638
for (const newNode of newNodes) {
641-
changeTracker.insertNodeAfter(sourceFile, insertAfter, newNode, { suffix: newLineCharacter });
639+
changeTracker.insertNodeAfter(sourceFile, insertAfter, newNode);
642640
}
643641
});
644642
}
@@ -649,7 +647,7 @@ class A {
649647
}
650648
`;
651649
runSingleFileTest("insertNodeAfterInClass1", /*placeOpenBraceOnNewLineForFunctions*/ false, text, /*validateNodes*/ false, (sourceFile, changeTracker) => {
652-
changeTracker.insertNodeAfter(sourceFile, findChild("x", sourceFile), createProperty(undefined, undefined, "a", undefined, createKeywordTypeNode(SyntaxKind.BooleanKeyword), undefined), { suffix: newLineCharacter });
650+
changeTracker.insertNodeAfter(sourceFile, findChild("x", sourceFile), createProperty(undefined, undefined, "a", undefined, createKeywordTypeNode(SyntaxKind.BooleanKeyword), undefined));
653651
});
654652
}
655653
{
@@ -659,7 +657,7 @@ class A {
659657
}
660658
`;
661659
runSingleFileTest("insertNodeAfterInClass2", /*placeOpenBraceOnNewLineForFunctions*/ false, text, /*validateNodes*/ false, (sourceFile, changeTracker) => {
662-
changeTracker.insertNodeAfter(sourceFile, findChild("x", sourceFile), createProperty(undefined, undefined, "a", undefined, createKeywordTypeNode(SyntaxKind.BooleanKeyword), undefined), { suffix: newLineCharacter });
660+
changeTracker.insertNodeAfter(sourceFile, findChild("x", sourceFile), createProperty(undefined, undefined, "a", undefined, createKeywordTypeNode(SyntaxKind.BooleanKeyword), undefined));
663661
});
664662
}
665663
{
@@ -698,7 +696,7 @@ class A {
698696
/*questionToken*/ undefined,
699697
createKeywordTypeNode(SyntaxKind.AnyKeyword),
700698
/*initializer*/ undefined);
701-
changeTracker.insertNodeAfter(sourceFile, findChild("x", sourceFile), newNode, { suffix: newLineCharacter });
699+
changeTracker.insertNodeAfter(sourceFile, findChild("x", sourceFile), newNode);
702700
});
703701
}
704702
{
@@ -716,7 +714,7 @@ class A {
716714
/*questionToken*/ undefined,
717715
createKeywordTypeNode(SyntaxKind.AnyKeyword),
718716
/*initializer*/ undefined);
719-
changeTracker.insertNodeAfter(sourceFile, findChild("x", sourceFile), newNode, { suffix: newLineCharacter });
717+
changeTracker.insertNodeAfter(sourceFile, findChild("x", sourceFile), newNode);
720718
});
721719
}
722720
{
@@ -733,7 +731,7 @@ interface A {
733731
/*questionToken*/ undefined,
734732
createKeywordTypeNode(SyntaxKind.AnyKeyword),
735733
/*initializer*/ undefined);
736-
changeTracker.insertNodeAfter(sourceFile, findChild("x", sourceFile), newNode, { suffix: newLineCharacter });
734+
changeTracker.insertNodeAfter(sourceFile, findChild("x", sourceFile), newNode);
737735
});
738736
}
739737
{
@@ -750,7 +748,7 @@ interface A {
750748
/*questionToken*/ undefined,
751749
createKeywordTypeNode(SyntaxKind.AnyKeyword),
752750
/*initializer*/ undefined);
753-
changeTracker.insertNodeAfter(sourceFile, findChild("x", sourceFile), newNode, { suffix: newLineCharacter });
751+
changeTracker.insertNodeAfter(sourceFile, findChild("x", sourceFile), newNode);
754752
});
755753
}
756754
{
@@ -759,7 +757,7 @@ let x = foo
759757
`;
760758
runSingleFileTest("insertNodeInStatementListAfterNodeWithoutSeparator1", /*placeOpenBraceOnNewLineForFunctions*/ false, text, /*validateNodes*/ false, (sourceFile, changeTracker) => {
761759
const newNode = createStatement(createParen(createLiteral(1)));
762-
changeTracker.insertNodeAfter(sourceFile, findVariableStatementContaining("x", sourceFile), newNode, { suffix: newLineCharacter });
760+
changeTracker.insertNodeAfter(sourceFile, findVariableStatementContaining("x", sourceFile), newNode);
763761
});
764762
}
765763
});

0 commit comments

Comments
 (0)