Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit c632a1c

Browse filesBrowse files
committed
add refactor of convert private field to getter and setter
1 parent e8fb587 commit c632a1c
Copy full SHA for c632a1c

4 files changed

+135Lines changed: 135 additions & 0 deletions

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎src/compiler/diagnosticMessages.json‎

Copy file name to clipboardExpand all lines: src/compiler/diagnosticMessages.json
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3965,5 +3965,9 @@
39653965
"Convert to ES6 module": {
39663966
"category": "Message",
39673967
"code": 95017
3968+
},
3969+
"Convert to getter and setter": {
3970+
"category": "Message",
3971+
"code": 95018
39683972
}
39693973
}
Collapse file
+109Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/* @internal */
2+
namespace ts.refactor.ConvertGetterAndSetter {
3+
const actionName = "Convert to getter and setter";
4+
const description = Diagnostics.Convert_to_getter_and_setter.message;
5+
registerRefactor(actionName, { getEditsForAction, getAvailableActions });
6+
7+
function getAvailableActions(context: RefactorContext): ApplicableRefactorInfo[] | undefined {
8+
const { file, startPosition } = context;
9+
10+
const fieldInfo = getConvertibleFieldAtPosition(file, startPosition);
11+
if (!fieldInfo) return undefined;
12+
13+
return [
14+
{
15+
name: actionName,
16+
description,
17+
actions: [
18+
{
19+
description,
20+
name: actionName
21+
}
22+
]
23+
}
24+
];
25+
26+
}
27+
28+
function getEditsForAction(context: RefactorContext, _actionName: string): RefactorEditInfo | undefined {
29+
const { file, startPosition } = context;
30+
Debug.assertEqual(actionName, _actionName);
31+
32+
const fieldInfo = getConvertibleFieldAtPosition(file, startPosition);
33+
if (!fieldInfo) return undefined;
34+
35+
const { fieldName, name, type, propertyDeclaration } = fieldInfo;
36+
const changeTracker = textChanges.ChangeTracker.fromContext(context);
37+
38+
const getter = createGetter(fieldName, name, type);
39+
const setter = createSetter(fieldName, name, type);
40+
41+
changeTracker.insertNodeAfter(file, propertyDeclaration, getter);
42+
changeTracker.insertNodeAfter(file, propertyDeclaration, setter);
43+
44+
return {
45+
edits: changeTracker.getChanges(),
46+
renameFilename: undefined,
47+
renameLocation: undefined,
48+
};
49+
}
50+
51+
interface Info { fieldName: string; name: string; type: TypeNode; propertyDeclaration: PropertyDeclaration; }
52+
function getConvertibleFieldAtPosition(file: SourceFile, startPosition: number): Info | undefined {
53+
const node = getTokenAtPosition(file, startPosition, /*includeJsDocComment*/ false);
54+
const propertyDeclaration = findAncestor(node.parent, isPropertyDeclaration);
55+
if (!(propertyDeclaration && propertyDeclaration.name.getText().charAt(0) === "_" && hasModifier(propertyDeclaration, ModifierFlags.Private))) return undefined;
56+
57+
return {
58+
fieldName: propertyDeclaration.name.getText(),
59+
name: propertyDeclaration.name.getText().substring(1),
60+
type: propertyDeclaration.type,
61+
propertyDeclaration
62+
};
63+
}
64+
65+
function createGetter (fieldName: string, name: string, type: TypeNode) {
66+
return createGetAccessor(
67+
/*decorators*/ undefined,
68+
[createToken(SyntaxKind.PublicKeyword)],
69+
name,
70+
/*parameters*/ undefined,
71+
type,
72+
createBlock([
73+
createReturn(
74+
createPropertyAccess(
75+
createThis(),
76+
fieldName
77+
)
78+
)
79+
], /*multiLine*/ true)
80+
);
81+
}
82+
83+
function createSetter (fieldName: string, name: string, type: TypeNode) {
84+
return createSetAccessor(
85+
/*decorators*/ undefined,
86+
[createToken(SyntaxKind.PublicKeyword)],
87+
name,
88+
[createParameter(
89+
/*decorators*/ undefined,
90+
/*modifies*/ undefined,
91+
/*dotDotDotToken*/ undefined,
92+
createIdentifier("value"),
93+
/*questionToken*/ undefined,
94+
type
95+
)],
96+
createBlock([
97+
createStatement(
98+
createAssignment(
99+
createPropertyAccess(
100+
createThis(),
101+
fieldName
102+
),
103+
createIdentifier("value")
104+
)
105+
)
106+
], /*multiLine*/ true)
107+
);
108+
}
109+
}
Collapse file

‎src/services/refactors/refactors.ts‎

Copy file name to clipboardExpand all lines: src/services/refactors/refactors.ts
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,4 @@
44
/// <reference path="extractSymbol.ts" />
55
/// <reference path="installTypesForPackage.ts" />
66
/// <reference path="useDefaultImport.ts" />
7+
/// <reference path="ConvertToGetterAndSetter.ts" />
Collapse file
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// class A {
4+
//// /*a*/private _a: string;/*b*/
5+
//// }
6+
7+
goTo.select("a", "b");
8+
edit.applyRefactor({
9+
refactorName: "Convert to getter and setter",
10+
actionName: "Convert to getter and setter",
11+
actionDescription: "Convert to getter and setter",
12+
newContent: `class A {
13+
private _a: string;
14+
public get a(): string {
15+
return this._a;
16+
}
17+
public set a(value: string) {
18+
this._a = value;
19+
}
20+
}`,
21+
});

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.