Skip to content

Commit

Permalink
Merge pull request #75900 from Maran23/project-converter-declaring-in…
Browse files Browse the repository at this point in the history
…ferring-types

Fix exported variables declaring or inferring the type were not converted correctly
  • Loading branch information
akien-mga committed Jun 12, 2023
2 parents 9be0aab + 55ba34a commit efea8ae
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions editor/project_converter_3_to_4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ class ProjectConverter3To4::RegExContainer {
RegEx reg_json_parse = RegEx("([\t ]{0,})([^\n]+)parse_json\\(([^\n]+)");
RegEx reg_json_non_new = RegEx("([\t ]{0,})([^\n]+)JSON\\.parse\\(([^\n]+)");
RegEx reg_json_print = RegEx("\\bJSON\\b\\.print\\(");
RegEx reg_export = RegEx("export\\(([a-zA-Z0-9_]+)\\)[ ]+var[ ]+([a-zA-Z0-9_]+)");
RegEx reg_export_simple = RegEx("export\\(([a-zA-Z0-9_]+)\\)[ ]+var[ ]+([a-zA-Z0-9_]+)");
RegEx reg_export_typed = RegEx("export\\(([a-zA-Z0-9_]+)\\)[ ]+var[ ]+([a-zA-Z0-9_]+)[ ]*:[ ]*[a-zA-Z0-9_]+");
RegEx reg_export_inferred_type = RegEx("export\\([a-zA-Z0-9_]+\\)[ ]+var[ ]+([a-zA-Z0-9_]+)[ ]*:[ ]*=");
RegEx reg_export_advanced = RegEx("export\\(([^)^\n]+)\\)[ ]+var[ ]+([a-zA-Z0-9_]+)([^\n]+)");
RegEx reg_setget_setget = RegEx("var[ ]+([a-zA-Z0-9_]+)([^\n]+?)[ \t]*setget[ \t]+([a-zA-Z0-9_]+)[ \t]*,[ \t]*([a-zA-Z0-9_]+)");
RegEx reg_setget_set = RegEx("var[ ]+([a-zA-Z0-9_]+)([^\n]+?)[ \t]*setget[ \t]+([a-zA-Z0-9_]+)[ \t]*[,]*[^\n]*$");
Expand Down Expand Up @@ -902,6 +904,10 @@ bool ProjectConverter3To4::test_conversion(RegExContainer &reg_container) {
valid = valid && test_conversion_gdscript_builtin("export(float) var lifetime = 3.0", "export var lifetime: float = 3.0", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false);
valid = valid && test_conversion_gdscript_builtin("export(String, 'AnonymousPro', 'CourierPrime') var _font_name = 'AnonymousPro'", "export var _font_name = 'AnonymousPro' # (String, 'AnonymousPro', 'CourierPrime')", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false); // TODO, this is only a workaround
valid = valid && test_conversion_gdscript_builtin("export(PackedScene) var mob_scene", "export var mob_scene: PackedScene", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false);
valid = valid && test_conversion_gdscript_builtin("export(float) var lifetime: float = 3.0", "export var lifetime: float = 3.0", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false);
valid = valid && test_conversion_gdscript_builtin("export var lifetime: float = 3.0", "export var lifetime: float = 3.0", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false);
valid = valid && test_conversion_gdscript_builtin("export var lifetime := 3.0", "export var lifetime := 3.0", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false);
valid = valid && test_conversion_gdscript_builtin("export(float) var lifetime := 3.0", "export var lifetime := 3.0", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false);

valid = valid && test_conversion_gdscript_builtin("var d = parse_json(roman(sfs))", "var test_json_conv = JSON.new()\ntest_json_conv.parse(roman(sfs))\nvar d = test_json_conv.get_data()", &ProjectConverter3To4::rename_gdscript_functions, "custom rename", reg_container, false);

Expand Down Expand Up @@ -1631,13 +1637,14 @@ void ProjectConverter3To4::process_gdscript_line(String &line, const RegExContai
line = line.replace("get_node(@", "get_node(");
}

// export(float) var lifetime = 3.0 -> export var lifetime: float = 3.0 GDScript
if (line.contains("export")) {
line = reg_container.reg_export.sub(line, "export var $2: $1");
}

// export(String, 'AnonymousPro', 'CourierPrime') var _font_name = 'AnonymousPro' -> export var _font_name = 'AnonymousPro' #(String, 'AnonymousPro', 'CourierPrime') GDScript
if (line.contains("export")) {
// 1. export(float) var lifetime: float = 3.0 -> export var lifetime: float = 3.0
line = reg_container.reg_export_typed.sub(line, "export var $2: $1");
// 2. export(float) var lifetime := 3.0 -> export var lifetime := 3.0
line = reg_container.reg_export_inferred_type.sub(line, "export var $1 :=");
// 3. export(float) var lifetime = 3.0 -> export var lifetime: float = 3.0 GDScript
line = reg_container.reg_export_simple.sub(line, "export var $2: $1");
// 4. export(String, 'AnonymousPro', 'CourierPrime') var _font_name = 'AnonymousPro' -> export var _font_name = 'AnonymousPro' #(String, 'AnonymousPro', 'CourierPrime') GDScript
line = reg_container.reg_export_advanced.sub(line, "export var $2$3 # ($1)");
}

Expand Down

0 comments on commit efea8ae

Please sign in to comment.