Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

deepMergeObject does not account for undefined property in recursion #51

Open
benjaminpreiss opened this issue Apr 26, 2023 · 0 comments

Comments

@benjaminpreiss
Copy link

Hi! 馃憢

Firstly, thanks for your work on this project! 馃檪

Today I used patch-package to patch magicast@0.2.4 for the project I'm working on.

When running

const requiredImports = `
import { browser } from "$app/environment"
import { initRootLayoutLoadWrapper } from "@inlang/sdk-js/adapter-sveltekit/shared"
import { initLocalStorageDetector, navigatorDetector } from "@inlang/sdk-js/detectors/client"
import { localStorageKey } from "@inlang/sdk-js/adapter-sveltekit/client/reactive"
`

const code = ''

const ast = parseModule(code)
const importsAst = parseModule(requiredImports)
deepMergeObject(ast, importsAst)

I get an error within deepMergeObject.
That's because the recursion happens to step into an undefined property.

Here is the diff that solved my problem:

diff --git a/node_modules/magicast/dist/helpers.cjs b/node_modules/magicast/dist/helpers.cjs
index 7813fd4..16d215a 100644
--- a/node_modules/magicast/dist/helpers.cjs
+++ b/node_modules/magicast/dist/helpers.cjs
@@ -8,7 +8,8 @@ require('@babel/parser');
 function deepMergeObject(magicast, object) {
   if (typeof object === "object") {
     for (const key in object) {
-      if (typeof object[key] === "object") {
+      if (typeof object[key] === "object" && magicast[key] !== undefined) {
+        // It could happen that magicast[key] is not defined
         deepMergeObject(magicast[key], object[key]);
       } else {
         magicast[key] = object[key];
diff --git a/node_modules/magicast/dist/helpers.mjs b/node_modules/magicast/dist/helpers.mjs
index 12c3234..fcb273c 100644
--- a/node_modules/magicast/dist/helpers.mjs
+++ b/node_modules/magicast/dist/helpers.mjs
@@ -6,7 +6,9 @@ import '@babel/parser';
 function deepMergeObject(magicast, object) {
   if (typeof object === "object") {
     for (const key in object) {
-      if (typeof object[key] === "object") {
+      if (typeof object[key] === "object" && magicast[key] !== undefined) {
+        // It could happen that magicast[key] is not defined
+        if(!magicast[key]) magicast[key] = {}
         deepMergeObject(magicast[key], object[key]);
       } else {
         magicast[key] = object[key];

This issue body was partially generated by patch-package.

@benjaminpreiss benjaminpreiss changed the title deepMergeObject does not account for undefined object deepMergeObject does not account for undefined property in recursion Apr 26, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant