Repository: seaofvoices/darklua
Pull request #151 (2023-11-06 05:24:13)

Fix bundled code bug where the modules identifier is undeclared
The generated code when bundling is broken and the modules identifier is not declared when running through the load function.

- [x] add entry to the changelog


github-actions[bot]: Coverage after merging <b>fix-bundle-module-local-bug</b> into <b>main</b> will be<table><tbody><tr><th>87.12%</th></tr></tbody></table>

<details><summary>Coverage Report for Changed Files</summary><table><tbody><tr><th>File</th><th>Stmts</th><th>Branches</th><th>Funcs</th><th>Lines</th><th>Uncovered Lines</th></tr><tr><td colspan='6'><b>src/rules/bundle/path_require_mode</b></td></tr><tr><td>&nbsp; &nbsp;<a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs'>module_definitions.rs</a></td><td><b>80.48%</b></td><td>100%</td><td><b>76%</b></td><td><b>81.03%</b></td><td><a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L105-L109'>105&ndash;109</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L114'>114</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L133'>133</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L17'>17</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L277-L278'>277&ndash;278</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L296-L297'>296&ndash;297</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L317'>317</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L321'>321</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L333'>333</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L337-L341'>337&ndash;341</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L343'>343</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L346-L348'>346&ndash;348</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L350'>350</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L353-L370'>353&ndash;370</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L373-L375'>373&ndash;375</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L377'>377</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L420-L422'>420&ndash;422</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L47-L50'>47&ndash;50</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L53-L56'>53&ndash;56</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L72'>72</a>, <a href='https://github.com/seaofvoices/darklua/blob/b4f8487c7c921258a16cab35e334d7467f69a44d/src/rules/bundle/path_require_mode/module_definitions.rs#L78-L79'>78&ndash;79</a></td></tr></tbody></table></details>
github-actions[bot]: Coverage after merging <b>fix-bundle-module-local-bug</b> into <b>main</b> will be<table><tbody><tr><th>86.98%</th></tr></tbody></table>

<details><summary>Coverage Report for Changed Files</summary><table><tbody><tr><th>File</th><th>Stmts</th><th>Branches</th><th>Funcs</th><th>Lines</th><th>Uncovered Lines</th></tr><tr><td colspan='6'><b>src/rules/bundle/path_require_mode</b></td></tr><tr><td>&nbsp; &nbsp;<a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs'>module_definitions.rs</a></td><td><b>80.48%</b></td><td>100%</td><td><b>76%</b></td><td><b>81.03%</b></td><td><a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L105-L109'>105&ndash;109</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L114'>114</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L133'>133</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L17'>17</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L277-L278'>277&ndash;278</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L296-L297'>296&ndash;297</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L317'>317</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L321'>321</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L333'>333</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L337-L341'>337&ndash;341</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L343'>343</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L346-L348'>346&ndash;348</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L350'>350</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L353-L370'>353&ndash;370</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L373-L375'>373&ndash;375</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L377'>377</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L420-L422'>420&ndash;422</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L47-L50'>47&ndash;50</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L53-L56'>53&ndash;56</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L72'>72</a>, <a href='https://github.com/seaofvoices/darklua/blob/920568ddb339103e2fd7172061d49b87ad66bca4/src/rules/bundle/path_require_mode/module_definitions.rs#L78-L79'>78&ndash;79</a></td></tr></tbody></table></details>

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 8cf6015..c2ccfc9 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,6 +2,8 @@
 
 ## Unreleased
 
+* fix undeclared modules variable when bundling ([#151](https://github.com/seaofvoices/darklua/pull/151))
+
 ## 0.11.2
 
 * fix bundling to handle modules with early return calls. This change also makes the bundled code preserve the module require ordering ([#147](https://github.com/seaofvoices/darklua/pull/147))
diff --git a/src/rules/bundle/path_require_mode/module_definitions.rs b/src/rules/bundle/path_require_mode/module_definitions.rs
index 7563120..34913fd 100644
--- a/src/rules/bundle/path_require_mode/module_definitions.rs
+++ b/src/rules/bundle/path_require_mode/module_definitions.rs
@@ -167,7 +167,11 @@ impl BuildModuleDefinitions {
         let modules_table = self.build_modules_table();
         block.insert_statement(
             0,
-            LocalAssignStatement::from_variable(self.modules_identifier).with_value(modules_table),
+            AssignStatement::from_variable(modules_identifier, modules_table),
+        );
+        block.insert_statement(
+            0,
+            LocalAssignStatement::from_variable(self.modules_identifier),
         );
     }
 


Test patch:
diff --git a/tests/snapshots/bundle__without_rules__bundle_without_rules_override_require.snap b/tests/snapshots/bundle__without_rules__bundle_without_rules_override_require.snap
index e573041..e623ee2 100644
--- a/tests/snapshots/bundle__without_rules__bundle_without_rules_override_require.snap
+++ b/tests/snapshots/bundle__without_rules__bundle_without_rules_override_require.snap
@@ -2,7 +2,9 @@
 source: tests/bundle.rs
 expression: main
 ---
-local __DARKLUA_BUNDLE_MODULES = {
+local __DARKLUA_BUNDLE_MODULES
+
+__DARKLUA_BUNDLE_MODULES = {
     cache = {},
     load = function(m)
         if not __DARKLUA_BUNDLE_MODULES.cache[m] then
diff --git a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_json_file_with_object.snap b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_json_file_with_object.snap
index 971e112..7f61985 100644
--- a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_json_file_with_object.snap
+++ b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_json_file_with_object.snap
@@ -2,7 +2,9 @@
 source: tests/bundle.rs
 expression: main
 ---
-local __DARKLUA_BUNDLE_MODULES = {
+local __DARKLUA_BUNDLE_MODULES
+
+__DARKLUA_BUNDLE_MODULES = {
     cache = {},
     load = function(m)
         if not __DARKLUA_BUNDLE_MODULES.cache[m] then
diff --git a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file.snap b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file.snap
index f099323..8269707 100644
--- a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file.snap
+++ b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file.snap
@@ -2,7 +2,9 @@
 source: tests/bundle.rs
 expression: main
 ---
-local __DARKLUA_BUNDLE_MODULES = {
+local __DARKLUA_BUNDLE_MODULES
+
+__DARKLUA_BUNDLE_MODULES = {
     cache = {},
     load = function(m)
         if not __DARKLUA_BUNDLE_MODULES.cache[m] then
diff --git a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_after_declaration.snap b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_after_declaration.snap
index 384a3b2..a757233 100644
--- a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_after_declaration.snap
+++ b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_after_declaration.snap
@@ -2,7 +2,9 @@
 source: tests/bundle.rs
 expression: main
 ---
-local __DARKLUA_BUNDLE_MODULES = {
+local __DARKLUA_BUNDLE_MODULES
+
+__DARKLUA_BUNDLE_MODULES = {
     cache = {},
     load = function(m)
         if not __DARKLUA_BUNDLE_MODULES.cache[m] then
diff --git a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_nested.snap b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_nested.snap
index 61d196c..1b812f4 100644
--- a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_nested.snap
+++ b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_nested.snap
@@ -2,7 +2,9 @@
 source: tests/bundle.rs
 expression: main
 ---
-local __DARKLUA_BUNDLE_MODULES = {
+local __DARKLUA_BUNDLE_MODULES
+
+__DARKLUA_BUNDLE_MODULES = {
     cache = {},
     load = function(m)
         if not __DARKLUA_BUNDLE_MODULES.cache[m] then
diff --git a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_twice.snap b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_twice.snap
index 76f5cac..5d051a4 100644
--- a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_twice.snap
+++ b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_twice.snap
@@ -2,7 +2,9 @@
 source: tests/bundle.rs
 expression: main
 ---
-local __DARKLUA_BUNDLE_MODULES = {
+local __DARKLUA_BUNDLE_MODULES
+
+__DARKLUA_BUNDLE_MODULES = {
     cache = {},
     load = function(m)
         if not __DARKLUA_BUNDLE_MODULES.cache[m] then
diff --git a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_twice_with_different_paths.snap b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_twice_with_different_paths.snap
index 76f5cac..5d051a4 100644
--- a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_twice_with_different_paths.snap
+++ b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_twice_with_different_paths.snap
@@ -2,7 +2,9 @@
 source: tests/bundle.rs
 expression: main
 ---
-local __DARKLUA_BUNDLE_MODULES = {
+local __DARKLUA_BUNDLE_MODULES
+
+__DARKLUA_BUNDLE_MODULES = {
     cache = {},
     load = function(m)
         if not __DARKLUA_BUNDLE_MODULES.cache[m] then
diff --git a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_with_field_expression.snap b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_with_field_expression.snap
index d499f6f..abaa8af 100644
--- a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_with_field_expression.snap
+++ b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_with_field_expression.snap
@@ -2,7 +2,9 @@
 source: tests/bundle.rs
 expression: main
 ---
-local __DARKLUA_BUNDLE_MODULES = {
+local __DARKLUA_BUNDLE_MODULES
+
+__DARKLUA_BUNDLE_MODULES = {
     cache = {},
     load = function(m)
         if not __DARKLUA_BUNDLE_MODULES.cache[m] then
diff --git a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_with_statement.snap b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_with_statement.snap
index 0e99f3c..4defd34 100644
--- a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_with_statement.snap
+++ b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_lua_file_with_statement.snap
@@ -2,7 +2,9 @@
 source: tests/bundle.rs
 expression: main
 ---
-local __DARKLUA_BUNDLE_MODULES = {
+local __DARKLUA_BUNDLE_MODULES
+
+__DARKLUA_BUNDLE_MODULES = {
     cache = {},
     load = function(m)
         if not __DARKLUA_BUNDLE_MODULES.cache[m] then
diff --git a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_small_bundle_case.snap b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_small_bundle_case.snap
index dd0fbc9..07dc2f6 100644
--- a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_small_bundle_case.snap
+++ b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_small_bundle_case.snap
@@ -2,7 +2,7 @@
 source: tests/bundle.rs
 expression: main
 ---
-local __DARKLUA_BUNDLE_MODULES={cache={}, load=function(m)if not __DARKLUA_BUNDLE_MODULES.cache[m]then __DARKLUA_BUNDLE_MODULES.cache[m]={c=__DARKLUA_BUNDLE_MODULES[m]()}end return __DARKLUA_BUNDLE_MODULES.cache[m].c end}do function __DARKLUA_BUNDLE_MODULES.a()local function initialize()
+local __DARKLUA_BUNDLE_MODULES __DARKLUA_BUNDLE_MODULES={cache={}, load=function(m)if not __DARKLUA_BUNDLE_MODULES.cache[m]then __DARKLUA_BUNDLE_MODULES.cache[m]={c=__DARKLUA_BUNDLE_MODULES[m]()}end return __DARKLUA_BUNDLE_MODULES.cache[m].c end}do function __DARKLUA_BUNDLE_MODULES.a()local function initialize()
 end
 
 return initialize
diff --git a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_toml_with_object.snap b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_toml_with_object.snap
index 59d30a5..0db517d 100644
--- a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_toml_with_object.snap
+++ b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_toml_with_object.snap
@@ -2,7 +2,9 @@
 source: tests/bundle.rs
 expression: main
 ---
-local __DARKLUA_BUNDLE_MODULES = {
+local __DARKLUA_BUNDLE_MODULES
+
+__DARKLUA_BUNDLE_MODULES = {
     cache = {},
     load = function(m)
         if not __DARKLUA_BUNDLE_MODULES.cache[m] then
diff --git a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_txt_file.snap b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_txt_file.snap
index 9647b16..d081ab0 100644
--- a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_txt_file.snap
+++ b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_txt_file.snap
@@ -2,7 +2,9 @@
 source: tests/bundle.rs
 expression: main
 ---
-local __DARKLUA_BUNDLE_MODULES = {
+local __DARKLUA_BUNDLE_MODULES
+
+__DARKLUA_BUNDLE_MODULES = {
     cache = {},
     load = function(m)
         if not __DARKLUA_BUNDLE_MODULES.cache[m] then
diff --git a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_yaml_with_array.snap b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_yaml_with_array.snap
index 26ea5fb..18768d9 100644
--- a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_yaml_with_array.snap
+++ b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_yaml_with_array.snap
@@ -2,7 +2,9 @@
 source: tests/bundle.rs
 expression: main
 ---
-local __DARKLUA_BUNDLE_MODULES = {
+local __DARKLUA_BUNDLE_MODULES
+
+__DARKLUA_BUNDLE_MODULES = {
     cache = {},
     load = function(m)
         if not __DARKLUA_BUNDLE_MODULES.cache[m] then
diff --git a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_yml_with_object.snap b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_yml_with_object.snap
index 559de97..3e3c936 100644
--- a/tests/snapshots/bundle__without_rules__bundle_without_rules_require_yml_with_object.snap
+++ b/tests/snapshots/bundle__without_rules__bundle_without_rules_require_yml_with_object.snap
@@ -2,7 +2,9 @@
 source: tests/bundle.rs
 expression: main
 ---
-local __DARKLUA_BUNDLE_MODULES = {
+local __DARKLUA_BUNDLE_MODULES
+
+__DARKLUA_BUNDLE_MODULES = {
     cache = {},
     load = function(m)
         if not __DARKLUA_BUNDLE_MODULES.cache[m] then