Skip to content

fix!: deepMerge for arrays, shortcut keycodes returned as array #9047

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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 14, 2025

Conversation

maribethb
Copy link
Contributor

@maribethb maribethb commented May 14, 2025

The basics

The details

Resolves

Fixes #9008

Proposed Changes

Fixes the deepMerge function so that it works correctly for arrays. It still probably does not work correctly for other cases, so I also added a warning to the tsdoc.

Reason for Changes

This was causing the shortcut registry to return the keycodes as an object instead of as an array.

Test Coverage

Added tests for the deepMerge function and for the keycodes property of keyboard shortcuts.

BREAKING CHANGES

  • If you are using the Blockly.utils.object.deepMerge function, it previously incorrectly converted some arrays to objects. You probably shouldn't be using this version anyway. If you need deep merge functionality we'd recommend you use a separate library that is fully functional and has solid tests.
  • If you are getting shortcuts out of the keyboard registry and then re-registering them or reusing the keyCodes property, you can now do so without resorting to workarounds to convert the object back into an array. Your workaround may now fail to work as the underlying data is now correctly an array instead of an object, so remove your workaround.

@maribethb maribethb requested a review from a team as a code owner May 14, 2025 05:27
@maribethb maribethb requested a review from BenHenning May 14, 2025 05:27
@github-actions github-actions bot added breaking change Used to mark a PR or issue that changes our public APIs. PR: fix Fixes a bug labels May 14, 2025
Copy link
Contributor

@BenHenning BenHenning left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @maribethb. Approving to unblock, but I have some thoughts and am happy to take another review pass.

@@ -18,7 +21,9 @@ export function deepMerge(
source: AnyDuringMigration,
Copy link
Contributor

@BenHenning BenHenning May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From a typing perspective (and since this is an API breakage, anyway), would it be better to explicitly type this now?

I'm assuming this can be typed something like so:

export interface MergeableObject<T> {
  [key: string]: MergeableObject<T> | T[] | null;
}

export function deepMerge(target: MergeableObject<unknown>, source: MergeableObject<unknown>): MergeableObject<unknown> {
  for (const x in source) {
    if (source[x] !== null) {
      if (Array.isArray(source[x])) {
        if (!Array.isArray(target[x])) {
          throw new Error(`Expected source and target to both be arrays at key: ${x}.`);
        }
        target[x] = deepMergeArray((target[x] as unknown[] | null) || [], source[x] as unknown[]);
      } else if (typeof source[x] === 'object') {
        if (typeof target[x] !== 'object') {
          throw new Error(`Expected source and target to both be objects at key: ${x}.`);
        }
        target[x] = deepMerge((target[x] as MergeableObject<unknown> | null) || Object.create(null), source[x] as MergeableObject<unknown>);
      } else {
        target[x] = source[x];
      }
    }
  }
  return target;
}

function deepMergeArray(target: unknown[], source: unknown[]): unknown[] {
  for (const x in source) {
    if (source[x] !== null) {
      if (Array.isArray(source[x])) {
        if (!Array.isArray(target[x])) {
          throw new Error(`Expected source and target to both be arrays at key: ${x}.`);
        }
        target[x] = deepMergeArray((target[x] as unknown[] | null) || [], source[x] as unknown[]);
      } else if (typeof source[x] === 'object') {
        if (typeof target[x] !== 'object') {
          throw new Error(`Expected source and target to both be objects at key: ${x}.`);
        }
        target[x] = deepMerge((target[x] as MergeableObject<unknown> | null) || Object.create(null), source[x] as MergeableObject<unknown>);
      } else {
        target[x] = source[x];
      }
    }
  }
  return target;
}

I think there's a way to consolidate these but couldn't quite figure it out.

I'm assuming we probably don't want to put time into typing this, though it did occur to me when drafting the above that there could be some element type inconsistencies.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's worth delaying the release to figure this out. I'd actually just like to remove the function, as actual deep merging is more complicated than we're making it look. I don't think your proposed version works correctly because for example it doesn't work if source[x] is an array but target[x] is undefined. It's not something I want to totally revamp right now, especially since it's only used internally in 2 places, and I sincerely hope that despite us exporting it, nobody else is using it.

Last night I got as far as removing it from themes (which don't even need to be "deep merged" as we're only ever using it to merge one layer of objects that just have strings) but couldn't quickly get rid of it for the shortcut registry so settled for fixing it instead.

@@ -533,4 +533,25 @@ suite('Utils', function () {
assert.equal(Blockly.utils.math.toDegrees(5 * quarter), 360 + 90, '450');
});
});

suite('deepMerge', function () {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do these tests include merging child objects and arrays with the same keys? The actual child merging bit seems missing here unless I'm missing something.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, but to be fair we literally never actually try to do that. We only use this function to create a deep copy of registered shortcuts (so we're only merging with Object.null) and and to copy one set of (non-nested) properties from one theme object to another.

@maribethb maribethb merged commit 205ef6c into google:rc/v12.0.0 May 14, 2025
11 checks passed
@github-actions github-actions bot added breaking change Used to mark a PR or issue that changes our public APIs. PR: fix Fixes a bug and removed PR: fix Fixes a bug breaking change Used to mark a PR or issue that changes our public APIs. labels May 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change Used to mark a PR or issue that changes our public APIs. PR: fix Fixes a bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants