-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add refactor of convert private field to getter and setter
- Loading branch information
Showing
4 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* @internal */ | ||
namespace ts.refactor.ConvertGetterAndSetter { | ||
const actionName = "Convert to getter and setter"; | ||
const description = Diagnostics.Convert_to_getter_and_setter.message; | ||
registerRefactor(actionName, { getEditsForAction, getAvailableActions }); | ||
|
||
function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined { | ||
const { file, startPosition } = context; | ||
|
||
const fieldInfo = getConvertibleFieldAtPosition(file, startPosition); | ||
if (!fieldInfo) return undefined; | ||
|
||
return [ | ||
{ | ||
name: actionName, | ||
description, | ||
actions: [ | ||
{ | ||
description, | ||
name: actionName | ||
} | ||
] | ||
} | ||
]; | ||
|
||
} | ||
|
||
function getEditsForAction(context: RefactorContext, _actionName: string): RefactorEditInfo | undefined { | ||
const { file, startPosition } = context; | ||
Debug.assertEqual(actionName, _actionName); | ||
|
||
const fieldInfo = getConvertibleFieldAtPosition(file, startPosition); | ||
if (!fieldInfo) return undefined; | ||
|
||
const { fieldName, name, type, propertyDeclaration } = fieldInfo; | ||
const changeTracker = textChanges.ChangeTracker.fromContext(context); | ||
|
||
const getter = createGetter(fieldName, name, type); | ||
const setter = createSetter(fieldName, name, type); | ||
|
||
changeTracker.insertNodeAfter(file, propertyDeclaration, getter); | ||
changeTracker.insertNodeAfter(file, propertyDeclaration, setter); | ||
|
||
return { | ||
edits: changeTracker.getChanges(), | ||
renameFilename: undefined, | ||
renameLocation: undefined, | ||
}; | ||
} | ||
|
||
interface Info { fieldName: string; name: string; type: TypeNode; propertyDeclaration: PropertyDeclaration; } | ||
function getConvertibleFieldAtPosition(file: SourceFile, startPosition: number): Info | undefined { | ||
const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false); | ||
const propertyDeclaration = findAncestor(node.parent, isPropertyDeclaration); | ||
if (!(propertyDeclaration && propertyDeclaration.name.getText().charAt(0) === "_") && hasModifier(propertyDeclaration, ModifierFlags.Private)) return undefined; | ||
|
||
return { | ||
fieldName: propertyDeclaration.name.getText(), | ||
name: propertyDeclaration.name.getText().substring(1), | ||
type: propertyDeclaration.type, | ||
propertyDeclaration | ||
}; | ||
} | ||
|
||
function createGetter (fieldName: string, name: string, type: TypeNode) { | ||
return createGetAccessor( | ||
/*decorators*/ undefined, | ||
[createToken(SyntaxKind.PublicKeyword)], | ||
name, | ||
/*parameters*/ undefined, | ||
type, | ||
createBlock([ | ||
createReturn( | ||
createPropertyAccess( | ||
createThis(), | ||
fieldName | ||
) | ||
) | ||
], /*multiLine*/ true) | ||
); | ||
} | ||
|
||
function createSetter (fieldName: string, name: string, type: TypeNode) { | ||
return createSetAccessor( | ||
/*decorators*/ undefined, | ||
[createToken(SyntaxKind.PublicKeyword)], | ||
name, | ||
[createParameter( | ||
/*decorators*/ undefined, | ||
/*modifies*/ undefined, | ||
/*dotDotDotToken*/ undefined, | ||
createIdentifier("value"), | ||
/*questionToken*/ undefined, | ||
type | ||
)], | ||
createBlock([ | ||
createStatement( | ||
createAssignment( | ||
createPropertyAccess( | ||
createThis(), | ||
fieldName | ||
), | ||
createIdentifier("value") | ||
) | ||
) | ||
], /*multiLine*/ true) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/// <reference path='fourslash.ts' /> | ||
|
||
//// class A { | ||
//// /*a*/private _a: string;/*b*/ | ||
//// } | ||
|
||
goTo.select("a", "b"); | ||
edit.applyRefactor({ | ||
refactorName: "Convert to getter and setter", | ||
actionName: "Convert to getter and setter", | ||
actionDescription: "Convert to getter and setter", | ||
newContent: `class A { | ||
private _a: string; | ||
public get a(): string { | ||
return this._a; | ||
} | ||
public set a(value: string) { | ||
this._a = value; | ||
} | ||
}`, | ||
}); |