Skip to content

Commit 147de76

Browse files
committed
put prefix/suffix in user provided options
1 parent 2ae7b10 commit 147de76

File tree

5 files changed

+39
-75
lines changed

5 files changed

+39
-75
lines changed

src/harness/unittests/textChanges.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ namespace M
130130
/*body */ createBlock(statements)
131131
);
132132

133-
changeTracker.insertNodeBefore(sourceFile, /*before*/findChild("M2", sourceFile), newFunction, { insertTrailingNewLine: true });
133+
changeTracker.insertNodeBefore(sourceFile, /*before*/findChild("M2", sourceFile), newFunction, { suffix: newLineCharacter });
134134

135135
// replace statements with return statement
136136
const newStatement = createReturn(
@@ -139,7 +139,7 @@ namespace M
139139
/*typeArguments*/ undefined,
140140
/*argumentsArray*/ emptyArray
141141
));
142-
changeTracker.replaceNodeRange(sourceFile, statements[0], lastOrUndefined(statements), newStatement, { insertTrailingNewLine: true });
142+
changeTracker.replaceNodeRange(sourceFile, statements[0], lastOrUndefined(statements), newStatement, { suffix: newLineCharacter });
143143
});
144144
}
145145
{
@@ -255,10 +255,10 @@ var z = 3; // comment 5
255255
// comment 6
256256
var a = 4; // comment 7`;
257257
runSingleFileTest("replaceRange", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
258-
changeTracker.replaceRange(sourceFile, { pos: text.indexOf("var y"), end: text.indexOf("var a") }, createTestClass(), { insertTrailingNewLine: true });
258+
changeTracker.replaceRange(sourceFile, { pos: text.indexOf("var y"), end: text.indexOf("var a") }, createTestClass(), { suffix: newLineCharacter });
259259
});
260260
runSingleFileTest("replaceRangeWithForcedIndentation", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
261-
changeTracker.replaceRange(sourceFile, { pos: text.indexOf("var y"), end: text.indexOf("var a") }, createTestClass(), { insertTrailingNewLine: true, indentation: 8, delta: 0 });
261+
changeTracker.replaceRange(sourceFile, { pos: text.indexOf("var y"), end: text.indexOf("var a") }, createTestClass(), { suffix: newLineCharacter, indentation: 8, delta: 0 });
262262
});
263263

264264
runSingleFileTest("replaceRangeNoLineBreakBefore", setNewLineForOpenBraceInFunctions, `const x = 1, y = "2";`, /*validateNodes*/ false, (sourceFile, changeTracker) => {
@@ -287,13 +287,13 @@ var z = 3; // comment 5
287287
// comment 6
288288
var a = 4; // comment 7`;
289289
runSingleFileTest("replaceNode1", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
290-
changeTracker.replaceNode(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass(), { insertTrailingNewLine: true });
290+
changeTracker.replaceNode(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass(), { suffix: newLineCharacter });
291291
});
292292
runSingleFileTest("replaceNode2", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
293-
changeTracker.replaceNode(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass(), { useNonAdjustedStartPosition: true, insertTrailingNewLine: true, insertLeadingNewLine: true });
293+
changeTracker.replaceNode(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass(), { useNonAdjustedStartPosition: true, suffix: newLineCharacter, prefix: newLineCharacter });
294294
});
295295
runSingleFileTest("replaceNode3", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
296-
changeTracker.replaceNode(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass(), { useNonAdjustedEndPosition: true, insertTrailingNewLine: true });
296+
changeTracker.replaceNode(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass(), { useNonAdjustedEndPosition: true, suffix: newLineCharacter });
297297
});
298298
runSingleFileTest("replaceNode4", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
299299
changeTracker.replaceNode(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass(), { useNonAdjustedStartPosition: true, useNonAdjustedEndPosition: true });
@@ -312,13 +312,13 @@ var z = 3; // comment 5
312312
// comment 6
313313
var a = 4; // comment 7`;
314314
runSingleFileTest("replaceNodeRange1", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
315-
changeTracker.replaceNodeRange(sourceFile, findVariableStatementContaining("y", sourceFile), findVariableStatementContaining("z", sourceFile), createTestClass(), { insertTrailingNewLine: true });
315+
changeTracker.replaceNodeRange(sourceFile, findVariableStatementContaining("y", sourceFile), findVariableStatementContaining("z", sourceFile), createTestClass(), { suffix: newLineCharacter });
316316
});
317317
runSingleFileTest("replaceNodeRange2", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
318-
changeTracker.replaceNodeRange(sourceFile, findVariableStatementContaining("y", sourceFile), findVariableStatementContaining("z", sourceFile), createTestClass(), { useNonAdjustedStartPosition: true, insertTrailingNewLine: true, insertLeadingNewLine: true });
318+
changeTracker.replaceNodeRange(sourceFile, findVariableStatementContaining("y", sourceFile), findVariableStatementContaining("z", sourceFile), createTestClass(), { useNonAdjustedStartPosition: true, suffix: newLineCharacter, prefix: newLineCharacter });
319319
});
320320
runSingleFileTest("replaceNodeRange3", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
321-
changeTracker.replaceNodeRange(sourceFile, findVariableStatementContaining("y", sourceFile), findVariableStatementContaining("z", sourceFile), createTestClass(), { useNonAdjustedEndPosition: true, insertTrailingNewLine: true });
321+
changeTracker.replaceNodeRange(sourceFile, findVariableStatementContaining("y", sourceFile), findVariableStatementContaining("z", sourceFile), createTestClass(), { useNonAdjustedEndPosition: true, suffix: newLineCharacter });
322322
});
323323
runSingleFileTest("replaceNodeRange4", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
324324
changeTracker.replaceNodeRange(sourceFile, findVariableStatementContaining("y", sourceFile), findVariableStatementContaining("z", sourceFile), createTestClass(), { useNonAdjustedStartPosition: true, useNonAdjustedEndPosition: true });
@@ -334,7 +334,7 @@ var z = 3; // comment 5
334334
// comment 6
335335
var a = 4; // comment 7`;
336336
runSingleFileTest("insertNodeAt1", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
337-
changeTracker.insertNodeAt(sourceFile, text.indexOf("var y"), createTestClass(), { insertTrailingNewLine: true });
337+
changeTracker.insertNodeAt(sourceFile, text.indexOf("var y"), createTestClass(), { suffix: newLineCharacter });
338338
});
339339
runSingleFileTest("insertNodeAt2", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ false, (sourceFile, changeTracker) => {
340340
changeTracker.insertNodeAt(sourceFile, text.indexOf("; // comment 4"), createTestVariableDeclaration("z1"));
@@ -352,16 +352,16 @@ namespace M {
352352
var a = 4; // comment 7
353353
}`;
354354
runSingleFileTest("insertNodeBefore1", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
355-
changeTracker.insertNodeBefore(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass(), { insertTrailingNewLine: true });
355+
changeTracker.insertNodeBefore(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass(), { suffix: newLineCharacter });
356356
});
357357
runSingleFileTest("insertNodeBefore2", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
358-
changeTracker.insertNodeBefore(sourceFile, findChild("M", sourceFile), createTestClass(), { insertTrailingNewLine: true });
358+
changeTracker.insertNodeBefore(sourceFile, findChild("M", sourceFile), createTestClass(), { suffix: newLineCharacter });
359359
});
360360
runSingleFileTest("insertNodeAfter1", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
361-
changeTracker.insertNodeAfter(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass(), { insertTrailingNewLine: true });
361+
changeTracker.insertNodeAfter(sourceFile, findVariableStatementContaining("y", sourceFile), createTestClass(), { suffix: newLineCharacter });
362362
});
363363
runSingleFileTest("insertNodeAfter2", setNewLineForOpenBraceInFunctions, text, /*validateNodes*/ true, (sourceFile, changeTracker) => {
364-
changeTracker.insertNodeAfter(sourceFile, findChild("M", sourceFile), createTestClass(), { insertLeadingNewLine: true });
364+
changeTracker.insertNodeAfter(sourceFile, findChild("M", sourceFile), createTestClass(), { prefix: newLineCharacter });
365365
});
366366
}
367367
{
@@ -385,7 +385,7 @@ class A {
385385
}
386386
`;
387387
runSingleFileTest("insertNodeAfter3", noop, text1, /*validateNodes*/ false, (sourceFile, changeTracker) => {
388-
changeTracker.insertNodeAfter(sourceFile, findOpenBraceForConstructor(sourceFile), createTestSuperCall(), { insertTrailingNewLine: true });
388+
changeTracker.insertNodeAfter(sourceFile, findOpenBraceForConstructor(sourceFile), createTestSuperCall(), { suffix: newLineCharacter });
389389
});
390390
const text2 = `
391391
class A {
@@ -395,7 +395,7 @@ class A {
395395
}
396396
`;
397397
runSingleFileTest("insertNodeAfter4", noop, text2, /*validateNodes*/ false, (sourceFile, changeTracker) => {
398-
changeTracker.insertNodeAfter(sourceFile, findVariableStatementContaining("x", sourceFile), createTestSuperCall(), { insertTrailingNewLine: true });
398+
changeTracker.insertNodeAfter(sourceFile, findVariableStatementContaining("x", sourceFile), createTestSuperCall(), { suffix: newLineCharacter });
399399
});
400400
const text3 = `
401401
class A {
@@ -405,7 +405,7 @@ class A {
405405
}
406406
`;
407407
runSingleFileTest("insertNodeAfter3-block with newline", noop, text3, /*validateNodes*/ false, (sourceFile, changeTracker) => {
408-
changeTracker.insertNodeAfter(sourceFile, findOpenBraceForConstructor(sourceFile), createTestSuperCall(), { insertTrailingNewLine: true });
408+
changeTracker.insertNodeAfter(sourceFile, findOpenBraceForConstructor(sourceFile), createTestSuperCall(), { suffix: newLineCharacter });
409409
});
410410
}
411411
{

src/services/codefixes/fixClassSuperMustPrecedeThisAccess.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ namespace ts.codefix {
2727
}
2828
}
2929
const changeTracker = textChanges.ChangeTracker.fromCodeFixContext(context);
30-
changeTracker.insertNodeAfter(sourceFile, getOpenBrace(<ConstructorDeclaration>constructor, sourceFile), superCall, { insertTrailingNewLine: true });
30+
changeTracker.insertNodeAfter(sourceFile, getOpenBrace(<ConstructorDeclaration>constructor, sourceFile), superCall, { suffix: context.newLineCharacter });
3131
changeTracker.deleteNode(sourceFile, superCall);
3232

3333
return [{

src/services/codefixes/fixConstructorForDerivedNeedSuperCall.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ts.codefix {
1212

1313
const changeTracker = textChanges.ChangeTracker.fromCodeFixContext(context);
1414
const superCall = createStatement(createCall(createSuper(), /*typeArguments*/ undefined, /*argumentsArray*/ emptyArray));
15-
changeTracker.insertNodeAfter(sourceFile, getOpenBrace(<ConstructorDeclaration>token.parent, sourceFile), superCall, { insertTrailingNewLine: true });
15+
changeTracker.insertNodeAfter(sourceFile, getOpenBrace(<ConstructorDeclaration>token.parent, sourceFile), superCall, { suffix: context.newLineCharacter });
1616

1717
return [{
1818
description: getLocaleSpecificMessage(Diagnostics.Add_missing_super_call),

src/services/codefixes/importFixes.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,10 +403,10 @@ namespace ts.codefix {
403403
: createImportClause(/*name*/ undefined, createNamedImports([createImportSpecifier(/*propertyName*/ undefined, createIdentifier(name))]));
404404
const importDecl = createImportDeclaration(/*decorators*/ undefined, /*modifiers*/ undefined, importClause, createLiteral(moduleSpecifierWithoutQuotes));
405405
if (!lastImportDeclaration) {
406-
changeTracker.insertNodeAt(sourceFile, 0, importDecl, { insertTrailingNewLine: true });
406+
changeTracker.insertNodeAt(sourceFile, 0, importDecl, { suffix: `${context.newLineCharacter}${context.newLineCharacter}` });
407407
}
408408
else {
409-
changeTracker.insertNodeAfter(sourceFile, lastImportDeclaration, importDecl, { insertTrailingNewLine: true });
409+
changeTracker.insertNodeAfter(sourceFile, lastImportDeclaration, importDecl, { suffix: context.newLineCharacter });
410410
}
411411
// const importStatementText = isDefault
412412
// ? `import ${name} from "${moduleSpecifierWithoutQuotes}"`

src/services/textChanges.ts

Lines changed: 17 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ namespace ts.textChanges {
6565

6666
export interface InsertNodeOptions {
6767
/**
68-
* Set this value to true to make sure that node text of newly inserted node ends with new line
68+
* Text to be inserted before the new node
6969
*/
70-
insertTrailingNewLine?: boolean;
70+
prefix?: string;
7171
/**
72-
* Set this value to true to make sure that node text of newly inserted node starts with new line
72+
* Text to be inserted after the new node
7373
*/
74-
insertLeadingNewLine?: boolean;
74+
suffix?: string;
7575
/**
7676
* Text of inserted node will be formatted with this indentation, otherwise indentation will be inferred from the old node
7777
*/
@@ -89,13 +89,13 @@ namespace ts.textChanges {
8989
readonly range: TextRange;
9090
readonly useIndentationFromFile?: boolean;
9191
readonly node?: Node;
92-
readonly separatorBefore?: Token<SyntaxKind.CommaToken | SyntaxKind.SemicolonToken>;
93-
readonly separatorAfter?: Token<SyntaxKind.CommaToken | SyntaxKind.SemicolonToken>,
94-
readonly prefix?: string;
95-
readonly suffix?: string;
9692
readonly options?: ChangeNodeOptions;
9793
}
9894

95+
export function getSeparatorCharacter(separator: Token<SyntaxKind.CommaToken | SyntaxKind.SemicolonToken>) {
96+
return tokenToString(separator.kind);
97+
}
98+
9999
export function getAdjustedStartPosition(sourceFile: SourceFile, node: Node, options: ConfigurableStart, position: Position) {
100100
if (options.useNonAdjustedStartPosition) {
101101
return node.getFullStart();
@@ -277,9 +277,7 @@ namespace ts.textChanges {
277277
range: { pos: endPosition, end: endPosition },
278278
node: newNode,
279279
useIndentationFromFile: true,
280-
separatorBefore: createToken(SyntaxKind.CommaToken),
281-
prefix: " ",
282-
options: {}
280+
options: { prefix: ", " }
283281
});
284282
}
285283
else if (index !== containingList.length - 1) {
@@ -323,10 +321,10 @@ namespace ts.textChanges {
323321
range: { pos: startPos, end: containingList[index + 1].getStart(sourceFile) },
324322
node: newNode,
325323
useIndentationFromFile: true,
326-
prefix,
327-
separatorAfter: createToken(<SyntaxKind.CommaToken | SyntaxKind.SemicolonToken>nextToken.kind),
328-
suffix: sourceFile.text.substring(nextToken.end, containingList[index + 1].getStart(sourceFile)),
329-
options: {}
324+
options: {
325+
prefix,
326+
suffix: `${tokenToString(nextToken.kind)}${sourceFile.text.substring(nextToken.end, containingList[index + 1].getStart(sourceFile))}`
327+
}
330328
});
331329
}
332330
}
@@ -350,8 +348,7 @@ namespace ts.textChanges {
350348
sourceFile,
351349
range,
352350
node: newNode,
353-
options: { insertLeadingNewLine: true, indentation },
354-
separatorBefore
351+
options: { prefix: `${getSeparatorCharacter(separatorBefore)}${this.newLineCharacter}`, indentation },
355352
});
356353
}
357354
else {
@@ -360,9 +357,7 @@ namespace ts.textChanges {
360357
sourceFile,
361358
range,
362359
node: newNode,
363-
options: {},
364-
prefix: " ", // ensure that new item is separate from previous item by one whitespace
365-
separatorBefore
360+
options: { prefix: `${getSeparatorCharacter(separatorBefore)} ` }, // ensure that new item is separate from previous item by one whitespace
366361
})
367362
}
368363
}
@@ -422,7 +417,7 @@ namespace ts.textChanges {
422417
change.options.indentation !== undefined
423418
? change.options.indentation
424419
: change.useIndentationFromFile
425-
? formatting.SmartIndenter.getIndentation(change.range.pos, sourceFile, formatOptions, posStartsLine || change.options.insertLeadingNewLine)
420+
? formatting.SmartIndenter.getIndentation(change.range.pos, sourceFile, formatOptions, posStartsLine || (change.options.prefix == this.newLineCharacter))
426421
: 0;
427422
const delta =
428423
change.options.delta !== undefined
@@ -435,38 +430,7 @@ namespace ts.textChanges {
435430
// strip initial indentation (spaces or tabs) if text will be inserted in the middle of the line
436431
// however keep indentation if it is was forced
437432
text = posStartsLine || change.options.indentation !== undefined ? text : text.replace(/^\s+/, "");
438-
439-
let prefix = "";
440-
if (change.prefix) {
441-
prefix = change.prefix;
442-
}
443-
else {
444-
if (change.separatorBefore) {
445-
prefix += tokenToString(change.separatorBefore.kind);
446-
}
447-
if (options.insertLeadingNewLine) {
448-
prefix += this.newLineCharacter;
449-
}
450-
}
451-
if (prefix) {
452-
text = prefix + text;
453-
}
454-
let suffix = "";
455-
if (change.suffix) {
456-
suffix = change.suffix;
457-
}
458-
else {
459-
if (change.separatorAfter) {
460-
suffix += tokenToString(change.separatorAfter.kind);
461-
}
462-
if (options.insertTrailingNewLine) {
463-
suffix = suffix + this.newLineCharacter;
464-
}
465-
}
466-
if (suffix) {
467-
text += suffix;
468-
}
469-
return text;
433+
return (options.prefix || "") + text + (options.suffix || "");
470434
}
471435

472436
private static normalize(changes: Change[]) {

0 commit comments

Comments
 (0)