Skip to content

Commit b7f725d

Browse files
committed
fix generate accessor if starting with underscore
1 parent b4ca23d commit b7f725d

7 files changed

+46
-59
lines changed

src/services/refactors/generateGetAccessorAndSetAccessor.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
1717
fieldName: AcceptedNameType;
1818
accessorName: AcceptedNameType;
1919
originalName: AcceptedNameType;
20+
renameAccessor: boolean;
2021
}
2122

2223
function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined {
@@ -43,7 +44,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
4344

4445
const isJS = isSourceFileJavaScript(file);
4546
const changeTracker = textChanges.ChangeTracker.fromContext(context);
46-
const { isStatic, isReadonly, fieldName, accessorName, originalName, type, container, declaration } = fieldInfo;
47+
const { isStatic, isReadonly, fieldName, accessorName, originalName, type, container, declaration, renameAccessor } = fieldInfo;
4748

4849
suppressLeadingAndTrailingTrivia(fieldName);
4950
suppressLeadingAndTrailingTrivia(declaration);
@@ -80,8 +81,10 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
8081

8182
const edits = changeTracker.getChanges();
8283
const renameFilename = file.fileName;
83-
const renameLocationOffset = isIdentifier(fieldName) ? 0 : -1;
84-
const renameLocation = renameLocationOffset + getRenameLocation(edits, renameFilename, fieldName.text, /*preferLastLocation*/ isParameter(declaration));
84+
85+
const nameNeedRename = renameAccessor ? accessorName : fieldName;
86+
const renameLocationOffset = isIdentifier(nameNeedRename) ? 0 : -1;
87+
const renameLocation = renameLocationOffset + getRenameLocation(edits, renameFilename, nameNeedRename.text, /*preferLastLocation*/ isParameter(declaration));
8588
return { renameFilename, renameLocation, edits };
8689
}
8790

@@ -110,15 +113,21 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
110113
return modifiers && createNodeArray(modifiers);
111114
}
112115

116+
function startsWithUnderscore(name: string): boolean {
117+
return name.charCodeAt(0) === CharacterCodes._;
118+
}
119+
113120
function getConvertibleFieldAtPosition(file: SourceFile, startPosition: number): Info | undefined {
114121
const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false);
115122
const declaration = findAncestor(node.parent, isAcceptedDeclaration);
116123
// make sure declaration have AccessibilityModifier or Static Modifier or Readonly Modifier
117124
const meaning = ModifierFlags.AccessibilityModifier | ModifierFlags.Static | ModifierFlags.Readonly;
118125
if (!declaration || !isConvertableName(declaration.name) || (getModifierFlags(declaration) | meaning) !== meaning) return undefined;
119126

120-
const fieldName = createPropertyName(getUniqueName(`_${declaration.name.text}`, file.text), declaration.name);
121-
const accessorName = createPropertyName(declaration.name.text, declaration.name);
127+
const name = declaration.name.text;
128+
const startWithUnderscore = startsWithUnderscore(name);
129+
const fieldName = createPropertyName(startWithUnderscore ? name : getUniqueName(`_${name}`, file.text), declaration.name);
130+
const accessorName = createPropertyName(startWithUnderscore ? getUniqueName(name.substring(1), file.text) : name, declaration.name);
122131
return {
123132
isStatic: hasStaticModifier(declaration),
124133
isReadonly: hasReadonlyModifier(declaration),
@@ -128,6 +137,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
128137
declaration,
129138
fieldName,
130139
accessorName,
140+
renameAccessor: startWithUnderscore
131141
};
132142
}
133143

tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess15.ts

+6-29
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
//// class A {
44
//// /*a*/public _a: number = 1;/*b*/
5-
//// /*c*/public a: string = "foo";/*d*/
5+
//// public a: string = "foo";
66
//// }
77

88
goTo.select("a", "b");
@@ -11,36 +11,13 @@ edit.applyRefactor({
1111
actionName: "Generate 'get' and 'set' accessors",
1212
actionDescription: "Generate 'get' and 'set' accessors",
1313
newContent: `class A {
14-
private /*RENAME*/__a: number = 1;
15-
public get _a(): number {
16-
return this.__a;
14+
private _a: number = 1;
15+
public get /*RENAME*/a_1(): number {
16+
return this._a;
1717
}
18-
public set _a(value: number) {
19-
this.__a = value;
18+
public set a_1(value: number) {
19+
this._a = value;
2020
}
2121
public a: string = "foo";
2222
}`,
2323
});
24-
25-
goTo.select("c", "d");
26-
edit.applyRefactor({
27-
refactorName: "Generate 'get' and 'set' accessors",
28-
actionName: "Generate 'get' and 'set' accessors",
29-
actionDescription: "Generate 'get' and 'set' accessors",
30-
newContent: `class A {
31-
private __a: number = 1;
32-
public get _a(): number {
33-
return this.__a;
34-
}
35-
public set _a(value: number) {
36-
this.__a = value;
37-
}
38-
private /*RENAME*/_a_1: string = "foo";
39-
public get a(): string {
40-
return this._a_1;
41-
}
42-
public set a(value: string) {
43-
this._a_1 = value;
44-
}
45-
}`,
46-
});

tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess23.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ edit.applyRefactor({
2828
actionName: "Generate 'get' and 'set' accessors",
2929
actionDescription: "Generate 'get' and 'set' accessors",
3030
newContent: `class A {
31-
private /*RENAME*/__a: number = 1;
32-
public get _a(): number {
33-
return this.__a;
31+
private _a: number = 1;
32+
public get /*RENAME*/a_2(): number {
33+
return this._a;
3434
}
35-
public set _a(value: number) {
36-
this.__a = value;
35+
public set a_2(value: number) {
36+
this._a = value;
3737
}
3838
private _a_1: string = "foo";
3939
public get a(): string {

tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess4.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ edit.applyRefactor({
1010
actionName: "Generate 'get' and 'set' accessors",
1111
actionDescription: "Generate 'get' and 'set' accessors",
1212
newContent: `class A {
13-
private /*RENAME*/__a: string;
14-
public get _a(): string {
15-
return this.__a;
13+
private _a: string;
14+
public get /*RENAME*/a_1(): string {
15+
return this._a;
1616
}
17-
public set _a(value: string) {
18-
this.__a = value;
17+
public set a_1(value: string) {
18+
this._a = value;
1919
}
2020
}`,
2121
});

tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess5.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ edit.applyRefactor({
1010
actionName: "Generate 'get' and 'set' accessors",
1111
actionDescription: "Generate 'get' and 'set' accessors",
1212
newContent: `class A {
13-
private /*RENAME*/__a: string;
14-
public get _a(): string {
15-
return this.__a;
13+
private _a: string;
14+
public get /*RENAME*/a_1(): string {
15+
return this._a;
1616
}
17-
public set _a(value: string) {
18-
this.__a = value;
17+
public set a_1(value: string) {
18+
this._a = value;
1919
}
2020
}`,
2121
});

tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess6.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ edit.applyRefactor({
1010
actionName: "Generate 'get' and 'set' accessors",
1111
actionDescription: "Generate 'get' and 'set' accessors",
1212
newContent: `class A {
13-
private /*RENAME*/__a: string;
14-
public get _a(): string {
15-
return this.__a;
13+
private _a: string;
14+
public get /*RENAME*/a_1(): string {
15+
return this._a;
1616
}
17-
public set _a(value: string) {
18-
this.__a = value;
17+
public set a_1(value: string) {
18+
this._a = value;
1919
}
2020
}`,
2121
});

tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess7.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ edit.applyRefactor({
1010
actionName: "Generate 'get' and 'set' accessors",
1111
actionDescription: "Generate 'get' and 'set' accessors",
1212
newContent: `class A {
13-
private /*RENAME*/__a: string;
14-
protected get _a(): string {
15-
return this.__a;
13+
private _a: string;
14+
protected get /*RENAME*/a_1(): string {
15+
return this._a;
1616
}
17-
protected set _a(value: string) {
18-
this.__a = value;
17+
protected set a_1(value: string) {
18+
this._a = value;
1919
}
2020
}`,
2121
});

0 commit comments

Comments
 (0)