Skip to content

Commit

Permalink
fix: Fix mutators when compiled with renames. (google#5644)
Browse files Browse the repository at this point in the history
Previous code not compatible with advanced compilation since 'mutationToDom' and other function names were hardcoded in strings.
  • Loading branch information
NeilFraser authored Oct 27, 2021
1 parent 0ae10fe commit 36fe365
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions core/extensions.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@ const checkNoMutatorProperties = function(mutationName, block) {
*/
const checkXmlHooks = function(object, errorPrefix) {
return checkHasFunctionPair(
object, 'mutationToDom', 'domToMutation', errorPrefix);
object.mutationToDom, object.domToMutation,
errorPrefix + ' mutationToDom/domToMutation');
};

/**
Expand All @@ -232,7 +233,8 @@ const checkXmlHooks = function(object, errorPrefix) {
*/
const checkJsonHooks = function(object, errorPrefix) {
return checkHasFunctionPair(
object, 'saveExtraState', 'loadExtraState', errorPrefix);
object.saveExtraState, object.loadExtraState,
errorPrefix + ' saveExtraState/loadExtraState');
};

/**
Expand All @@ -245,38 +247,31 @@ const checkJsonHooks = function(object, errorPrefix) {
* not actually a function.
*/
const checkMutatorDialog = function(object, errorPrefix) {
return checkHasFunctionPair(object, 'compose', 'decompose', errorPrefix);
return checkHasFunctionPair(
object.compose, object.decompose, errorPrefix + ' compose/decompose');
};

/**
* Checks that the given object has both or neither of the given functions, and
* that they are indeed functions.
* @param {!Object} object The object to check.
* @param {string} name1 The name of the first function in the pair.
* @param {string} name2 The name of the second function in the pair.
* Checks that both or neither of the given functions exist and that they are
* indeed functions.
* @param {*} func1 The first function in the pair.
* @param {*} func2 The second function in the pair.
* @param {string} errorPrefix The string to prepend to any error message.
* @return {boolean} True if the object has both functions. False if it has
* @return {boolean} True if the object has both functions. False if it has
* neither function.
* @throws {Error} If the object has only one of the functions, or either is
* not actually a function.
*/
const checkHasFunctionPair = function(object, name1, name2, errorPrefix) {
const has1 = object[name1] !== undefined;
const has2 = object[name2] !== undefined;

if (has1 && has2) {
if (typeof object[name1] !== 'function') {
throw Error(errorPrefix + name1 + ' must be a function.');
} else if (typeof object[name2] !== 'function') {
throw Error(errorPrefix + name2 + ' must be a function.');
const checkHasFunctionPair = function(func1, func2, errorPrefix) {
if (func1 && func2) {
if (typeof func1 !== 'function' || typeof func2 !== 'function') {
throw Error(errorPrefix + ' must be a function');
}
return true;
} else if (!has1 && !has2) {
} else if (!func1 && !func2) {
return false;
}
throw Error(
errorPrefix + 'Must have both or neither of "' + name1 + '" and "' +
name2 + '"');
throw Error(errorPrefix + 'Must have both or neither functions');
};

/**
Expand Down

0 comments on commit 36fe365

Please sign in to comment.