A practical implementation of the code refactoring utility, safely renaming a function while ignoring occurrences in comments and strings.
ID: 1402
using uCalcSoftware;
var uc = new uCalc();
// Simulate user inputs for the tool
var findText = "GetUserData";
var replaceText = "FetchUserProfile";
var sourceCode = """
// Deprecated: Use FetchUserProfile instead of GetUserData
function GetUserData(id) {
print("Calling GetUserData is not recommended.");
return http.get("/users/" + id);
}
var user = GetUserData(123);
""";
using (var refactorTool = new uCalc.Transformer()) {
// 1. Define rules to ignore comments. These have the highest precedence.
refactorTool.SkipOver("// {text}");
refactorTool.SkipOver("/* {text} */");
// 2. Define the replacement rule. QuoteSensitive is true by default, protecting strings.
var rule = refactorTool.FromTo(findText, replaceText);
// 3. Run the transformation and print the result.
Console.WriteLine(refactorTool.Transform(sourceCode));
}
// Deprecated: Use FetchUserProfile instead of GetUserData
function FetchUserProfile(id) {
print("Calling GetUserData is not recommended.");
return http.get("/users/" + id);
}
var user = FetchUserProfile(123);
using uCalcSoftware; var uc = new uCalc(); // Simulate user inputs for the tool var findText = "GetUserData"; var replaceText = "FetchUserProfile"; var sourceCode = """ // Deprecated: Use FetchUserProfile instead of GetUserData function GetUserData(id) { print("Calling GetUserData is not recommended."); return http.get("/users/" + id); } var user = GetUserData(123); """; using (var refactorTool = new uCalc.Transformer()) { // 1. Define rules to ignore comments. These have the highest precedence. refactorTool.SkipOver("// {text}"); refactorTool.SkipOver("/* {text} */"); // 2. Define the replacement rule. QuoteSensitive is true by default, protecting strings. var rule = refactorTool.FromTo(findText, replaceText); // 3. Run the transformation and print the result. Console.WriteLine(refactorTool.Transform(sourceCode)); }
#include
#include "uCalc.h"
using namespace std;
using namespace uCalcSoftware;
int main() {
uCalc uc;
// Simulate user inputs for the tool
auto findText = "GetUserData";
auto replaceText = "FetchUserProfile";
auto sourceCode = R"(
// Deprecated: Use FetchUserProfile instead of GetUserData
function GetUserData(id) {
print("Calling GetUserData is not recommended.");
return http.get("/users/" + id);
}
var user = GetUserData(123);
)";
{
uCalc::Transformer refactorTool;
refactorTool.Owned(); // Causes refactorTool to be released when it goes out of scope
// 1. Define rules to ignore comments. These have the highest precedence.
refactorTool.SkipOver("// {text}");
refactorTool.SkipOver("/* {text} */");
// 2. Define the replacement rule. QuoteSensitive is true by default, protecting strings.
auto rule = refactorTool.FromTo(findText, replaceText);
// 3. Run the transformation and print the result.
cout << refactorTool.Transform(sourceCode) << endl;
}
}
// Deprecated: Use FetchUserProfile instead of GetUserData
function FetchUserProfile(id) {
print("Calling GetUserData is not recommended.");
return http.get("/users/" + id);
}
var user = FetchUserProfile(123);
#include <iostream> #include "uCalc.h" using namespace std; using namespace uCalcSoftware; int main() { uCalc uc; // Simulate user inputs for the tool auto findText = "GetUserData"; auto replaceText = "FetchUserProfile"; auto sourceCode = R"( // Deprecated: Use FetchUserProfile instead of GetUserData function GetUserData(id) { print("Calling GetUserData is not recommended."); return http.get("/users/" + id); } var user = GetUserData(123); )"; { uCalc::Transformer refactorTool; refactorTool.Owned(); // Causes refactorTool to be released when it goes out of scope // 1. Define rules to ignore comments. These have the highest precedence. refactorTool.SkipOver("// {text}"); refactorTool.SkipOver("/* {text} */"); // 2. Define the replacement rule. QuoteSensitive is true by default, protecting strings. auto rule = refactorTool.FromTo(findText, replaceText); // 3. Run the transformation and print the result. cout << refactorTool.Transform(sourceCode) << endl; } }
Imports System
Imports uCalcSoftware
Public Module Program
Public Sub Main()
Dim uc As New uCalc()
'// Simulate user inputs for the tool
Dim findText = "GetUserData"
Dim replaceText = "FetchUserProfile"
Dim sourceCode = "
// Deprecated: Use FetchUserProfile instead of GetUserData
function GetUserData(id) {
print(""Calling GetUserData is not recommended."");
return http.get(""/users/"" + id);
}
var user = GetUserData(123);
"
Using refactorTool As New uCalc.Transformer()
'// 1. Define rules to ignore comments. These have the highest precedence.
refactorTool.SkipOver("// {text}")
refactorTool.SkipOver("/* {text} */")
'// 2. Define the replacement rule. QuoteSensitive is true by default, protecting strings.
Dim rule = refactorTool.FromTo(findText, replaceText)
'// 3. Run the transformation and print the result.
Console.WriteLine(refactorTool.Transform(sourceCode))
End Using
End Sub
End Module
// Deprecated: Use FetchUserProfile instead of GetUserData
function FetchUserProfile(id) {
print("Calling GetUserData is not recommended.");
return http.get("/users/" + id);
}
var user = FetchUserProfile(123);
Imports System Imports uCalcSoftware Public Module Program Public Sub Main() Dim uc As New uCalc() '// Simulate user inputs for the tool Dim findText = "GetUserData" Dim replaceText = "FetchUserProfile" Dim sourceCode = " // Deprecated: Use FetchUserProfile instead of GetUserData function GetUserData(id) { print(""Calling GetUserData is not recommended.""); return http.get(""/users/"" + id); } var user = GetUserData(123); " Using refactorTool As New uCalc.Transformer() '// 1. Define rules to ignore comments. These have the highest precedence. refactorTool.SkipOver("// {text}") refactorTool.SkipOver("/* {text} */") '// 2. Define the replacement rule. QuoteSensitive is true by default, protecting strings. Dim rule = refactorTool.FromTo(findText, replaceText) '// 3. Run the transformation and print the result. Console.WriteLine(refactorTool.Transform(sourceCode)) End Using End Sub End Module