Skip to content

deepMergeObject does not account for undefined property in recursion #51

@benjaminpreiss

Description

@benjaminpreiss

Hi! 👋

Firstly, thanks for your work on this project! 🙂

Today I used patch-package to patch [email protected] 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions