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

Already on GitHub? Sign in to your account

fix: Fix mutators when compiled with renames. #5644

Merged
merged 3 commits into from
Oct 27, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Comment on lines +257 to +258
Copy link
Contributor

Choose a reason for hiding this comment

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

Ought these not to be @param {function|undefined}? Though I guess as a test function it's not too important, since the test will fail if the wrong types are supplied.

* @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