Skip to content

Commit 0fde07f

Browse files
committed
fix trailing comma in accessor generator
1 parent b4ca23d commit 0fde07f

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/services/refactors/generateGetAccessorAndSetAccessor.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
204204
function insertAccessor(changeTracker: textChanges.ChangeTracker, file: SourceFile, accessor: AccessorDeclaration, declaration: AcceptedDeclaration, container: ContainerDeclaration) {
205205
isParameterPropertyDeclaration(declaration)
206206
? changeTracker.insertNodeAtClassStart(file, <ClassLikeDeclaration>container, accessor)
207-
: changeTracker.insertNodeAfter(file, declaration, accessor);
207+
: isPropertyAssignment(declaration)
208+
? changeTracker.insertNodeAfterComma(file, declaration, accessor)
209+
: changeTracker.insertNodeAfter(file, declaration, accessor);
208210
}
209211

210212
function updateReadonlyPropertyInitializerStatementConstructor(changeTracker: textChanges.ChangeTracker, context: RefactorContext, constructor: ConstructorDeclaration, fieldName: AcceptedNameType, originalName: AcceptedNameType) {

src/services/textChanges.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -320,10 +320,14 @@ namespace ts.textChanges {
320320
return this.replaceRangeWithNodes(sourceFile, getAdjustedRange(sourceFile, startNode, endNode, options), newNodes, options);
321321
}
322322

323+
private nextCommaToken (sourceFile: SourceFile, node: Node): Node | undefined {
324+
const next = findNextToken(node, node.parent, sourceFile);
325+
return next && next.kind === SyntaxKind.CommaToken ? next : undefined;
326+
}
327+
323328
public replacePropertyAssignment(sourceFile: SourceFile, oldNode: PropertyAssignment, newNode: PropertyAssignment) {
324-
return this.replaceNode(sourceFile, oldNode, newNode, {
325-
suffix: "," + this.newLineCharacter
326-
});
329+
const suffix = this.nextCommaToken(sourceFile, oldNode) ? "" : ("," + this.newLineCharacter);
330+
return this.replaceNode(sourceFile, oldNode, newNode, { suffix });
327331
}
328332

329333
private insertNodeAt(sourceFile: SourceFile, pos: number, newNode: Node, options: InsertNodeOptions = {}) {
@@ -465,6 +469,11 @@ namespace ts.textChanges {
465469
}
466470
}
467471

472+
public insertNodeAfterComma(sourceFile: SourceFile, after: Node, newNode: Node): void {
473+
const endPosition = this.insertNodeAfterWorker(sourceFile, this.nextCommaToken(sourceFile, after) || after, newNode);
474+
this.insertNodeAt(sourceFile, endPosition, newNode, this.getInsertNodeAfterOptions(sourceFile, after));
475+
}
476+
468477
public insertNodeAfter(sourceFile: SourceFile, after: Node, newNode: Node): void {
469478
const endPosition = this.insertNodeAfterWorker(sourceFile, after, newNode);
470479
this.insertNodeAt(sourceFile, endPosition, newNode, this.getInsertNodeAfterOptions(sourceFile, after));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// const A = {
4+
//// /*a*/a/*b*/: 1,
5+
//// };
6+
7+
goTo.select("a", "b");
8+
edit.applyRefactor({
9+
refactorName: "Generate 'get' and 'set' accessors",
10+
actionName: "Generate 'get' and 'set' accessors",
11+
actionDescription: "Generate 'get' and 'set' accessors",
12+
newContent: `const A = {
13+
/*RENAME*/_a: 1,
14+
get a() {
15+
return this._a;
16+
},
17+
set a(value) {
18+
this._a = value;
19+
},
20+
};`,
21+
});

0 commit comments

Comments
 (0)