mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-11 10:00:13 -06:00
Merge pull request #24162 from Kingwl/fix-generate-accessor-starting-underscore
fix generate accessor if starting with underscore
This commit is contained in:
commit
13ac88647f
@ -17,6 +17,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
|
||||
fieldName: AcceptedNameType;
|
||||
accessorName: AcceptedNameType;
|
||||
originalName: AcceptedNameType;
|
||||
renameAccessor: boolean;
|
||||
}
|
||||
|
||||
function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined {
|
||||
@ -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);
|
||||
@ -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 };
|
||||
}
|
||||
|
||||
@ -110,6 +113,10 @@ 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);
|
||||
@ -117,8 +124,10 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
|
||||
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),
|
||||
@ -128,6 +137,7 @@ namespace ts.refactor.generateGetAccessorAndSetAccessor {
|
||||
declaration,
|
||||
fieldName,
|
||||
accessorName,
|
||||
renameAccessor: startWithUnderscore
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -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");
|
||||
@ -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;
|
||||
}
|
||||
}`,
|
||||
});
|
||||
@ -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 {
|
||||
|
||||
@ -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,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,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,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;
|
||||
}
|
||||
}`,
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user