Skip to content

fix generate accessor if starting with underscore #24162

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/services/refactors/generateGetAccessorAndSetAccessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
fieldName: AcceptedNameType;
accessorName: AcceptedNameType;
originalName: AcceptedNameType;
renameAccessor: boolean;
}

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

const isJS = isSourceFileJavaScript(file);
const changeTracker = textChanges.ChangeTracker.fromContext(context);
const { isStatic, isReadonly, fieldName, accessorName, originalName, type, container, declaration } = fieldInfo;
const { isStatic, isReadonly, fieldName, accessorName, originalName, type, container, declaration, renameAccessor } = fieldInfo;

suppressLeadingAndTrailingTrivia(fieldName);
suppressLeadingAndTrailingTrivia(declaration);
Expand Down Expand Up @@ -80,8 +81,10 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {

const edits = changeTracker.getChanges();
const renameFilename = file.fileName;
const renameLocationOffset = isIdentifier(fieldName) ? 0 : -1;
const renameLocation = renameLocationOffset + getRenameLocation(edits, renameFilename, fieldName.text, /*preferLastLocation*/ isParameter(declaration));

const nameNeedRename = renameAccessor ? accessorName : fieldName;
const renameLocationOffset = isIdentifier(nameNeedRename) ? 0 : -1;
const renameLocation = renameLocationOffset + getRenameLocation(edits, renameFilename, nameNeedRename.text, /*preferLastLocation*/ isParameter(declaration));
return { renameFilename, renameLocation, edits };
}

Expand Down Expand Up @@ -110,15 +113,21 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
return modifiers && createNodeArray(modifiers);
}

function startsWithUnderscore(name: string): boolean {
return name.charCodeAt(0) === CharacterCodes._;
}

function getConvertibleFieldAtPosition(file: SourceFile, startPosition: number): Info | undefined {
const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false);
const declaration = findAncestor(node.parent, isAcceptedDeclaration);
// make sure declaration have AccessibilityModifier or Static Modifier or Readonly Modifier
const meaning = ModifierFlags.AccessibilityModifier | ModifierFlags.Static | ModifierFlags.Readonly;
if (!declaration || !isConvertableName(declaration.name) || (getModifierFlags(declaration) | meaning) !== meaning) return undefined;

const fieldName = createPropertyName(getUniqueName(`_${declaration.name.text}`, file.text), declaration.name);
const accessorName = createPropertyName(declaration.name.text, declaration.name);
const name = declaration.name.text;
const startWithUnderscore = startsWithUnderscore(name);
const fieldName = createPropertyName(startWithUnderscore ? name : getUniqueName(`_${name}`, file.text), declaration.name);
const accessorName = createPropertyName(startWithUnderscore ? getUniqueName(name.substring(1), file.text) : name, declaration.name);
return {
isStatic: hasStaticModifier(declaration),
isReadonly: hasReadonlyModifier(declaration),
Expand All @@ -128,6 +137,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
declaration,
fieldName,
accessorName,
renameAccessor: startWithUnderscore
};
}

Expand Down
35 changes: 6 additions & 29 deletions tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess15.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

//// class A {
//// /*a*/public _a: number = 1;/*b*/
//// /*c*/public a: string = "foo";/*d*/
//// public a: string = "foo";
//// }

goTo.select("a", "b");
Expand All @@ -11,36 +11,13 @@ edit.applyRefactor({
actionName: "Generate 'get' and 'set' accessors",
actionDescription: "Generate 'get' and 'set' accessors",
newContent: `class A {
private /*RENAME*/__a: number = 1;
public get _a(): number {
return this.__a;
private _a: number = 1;
public get /*RENAME*/a_1(): number {
return this._a;
}
public set _a(value: number) {
this.__a = value;
public set a_1(value: number) {
this._a = value;
}
public a: string = "foo";
}`,
});

goTo.select("c", "d");
edit.applyRefactor({
refactorName: "Generate 'get' and 'set' accessors",
actionName: "Generate 'get' and 'set' accessors",
actionDescription: "Generate 'get' and 'set' accessors",
newContent: `class A {
private __a: number = 1;
public get _a(): number {
return this.__a;
}
public set _a(value: number) {
this.__a = value;
}
private /*RENAME*/_a_1: string = "foo";
public get a(): string {
return this._a_1;
}
public set a(value: string) {
this._a_1 = value;
}
}`,
});
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ edit.applyRefactor({
actionName: "Generate 'get' and 'set' accessors",
actionDescription: "Generate 'get' and 'set' accessors",
newContent: `class A {
private /*RENAME*/__a: number = 1;
public get _a(): number {
return this.__a;
private _a: number = 1;
public get /*RENAME*/a_2(): number {
return this._a;
}
public set _a(value: number) {
this.__a = value;
public set a_2(value: number) {
this._a = value;
}
private _a_1: string = "foo";
public get a(): string {
Expand Down
10 changes: 5 additions & 5 deletions tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ edit.applyRefactor({
actionName: "Generate 'get' and 'set' accessors",
actionDescription: "Generate 'get' and 'set' accessors",
newContent: `class A {
private /*RENAME*/__a: string;
public get _a(): string {
return this.__a;
private _a: string;
public get /*RENAME*/a_1(): string {
return this._a;
}
public set _a(value: string) {
this.__a = value;
public set a_1(value: string) {
this._a = value;
}
}`,
});
10 changes: 5 additions & 5 deletions tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess5.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ edit.applyRefactor({
actionName: "Generate 'get' and 'set' accessors",
actionDescription: "Generate 'get' and 'set' accessors",
newContent: `class A {
private /*RENAME*/__a: string;
public get _a(): string {
return this.__a;
private _a: string;
public get /*RENAME*/a_1(): string {
return this._a;
}
public set _a(value: string) {
this.__a = value;
public set a_1(value: string) {
this._a = value;
}
}`,
});
10 changes: 5 additions & 5 deletions tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess6.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ edit.applyRefactor({
actionName: "Generate 'get' and 'set' accessors",
actionDescription: "Generate 'get' and 'set' accessors",
newContent: `class A {
private /*RENAME*/__a: string;
public get _a(): string {
return this.__a;
private _a: string;
public get /*RENAME*/a_1(): string {
return this._a;
}
public set _a(value: string) {
this.__a = value;
public set a_1(value: string) {
this._a = value;
}
}`,
});
10 changes: 5 additions & 5 deletions tests/cases/fourslash/refactorConvertToGetAccessAndSetAccess7.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ edit.applyRefactor({
actionName: "Generate 'get' and 'set' accessors",
actionDescription: "Generate 'get' and 'set' accessors",
newContent: `class A {
private /*RENAME*/__a: string;
protected get _a(): string {
return this.__a;
private _a: string;
protected get /*RENAME*/a_1(): string {
return this._a;
}
protected set _a(value: string) {
this.__a = value;
protected set a_1(value: string) {
this._a = value;
}
}`,
});