Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
♻️ Fixit: standalone assert helpers w/ format messages (#32788)
* support formattrings in asserts * Add tests for pure-assert * Import as pure asserts * Update date.js to use format args * rename pureAssertion to assertion * Rename `pure-assert.js` to `assert.js` * Fix test file * Fix copyright year Co-authored-by: Caroline Liu <10456171+caroqliu@users.noreply.github.com> * fix assertion * Lint fixes Co-authored-by: Caroline Liu <10456171+caroqliu@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
255 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
/** | ||
* Copyright 2021 The AMP HTML Authors. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS-IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** @fileoverview Dependency-free assertion helpers for use in Preact. */ | ||
|
||
/** | ||
* Triple zero width space. | ||
* | ||
* This is added to user error messages, so that we can later identify | ||
* them, when the only thing that we have is the message. This is the | ||
* case in many browsers when the global exception handler is invoked. | ||
* | ||
* @const {string} | ||
*/ | ||
export const USER_ERROR_SENTINEL = '\u200B\u200B\u200B'; | ||
|
||
/** | ||
* User error class for use in Preact. Use of sentinel string instead of a | ||
* boolean to check user errors because errors could be rethrown by some native | ||
* code as a new error, and only a message would survive. Mirrors errors | ||
* produced by `user().error()` in src/log.js. | ||
* @final | ||
* @public | ||
*/ | ||
export class UserError extends Error { | ||
/** | ||
* Builds the error, adding the user sentinel if not present. | ||
* @param {string} message | ||
*/ | ||
constructor(message) { | ||
if (!message) { | ||
message = USER_ERROR_SENTINEL; | ||
} else if (message.indexOf(USER_ERROR_SENTINEL) == -1) { | ||
message += USER_ERROR_SENTINEL; | ||
} | ||
|
||
super(message); | ||
} | ||
} | ||
|
||
/** | ||
* Throws a provided error if the second argument isn't trueish. | ||
* @param {Object} errorCls | ||
* @param {T} shouldBeTruthy | ||
* @param {string} opt_message | ||
* @param {...*} var_args Arguments substituted into %s in the message | ||
* @return {T} | ||
* @throws {Error} when shouldBeTruthy is not truthy. | ||
*/ | ||
function assertion(errorCls, shouldBeTruthy, opt_message, var_args) { | ||
if (shouldBeTruthy) { | ||
return shouldBeTruthy; | ||
} | ||
|
||
// Substitute provided values into format string in message | ||
const message = Array.prototype.slice | ||
// Skip the first 3 arguments to isolate format params | ||
.call(arguments, 3) | ||
.reduce( | ||
(msg, subValue) => msg.replace('%s', subValue), | ||
opt_message || 'Assertion failed' | ||
); | ||
|
||
throw new errorCls(message); | ||
} | ||
|
||
/** | ||
* Throws a user error if the first argument isn't trueish. Mirrors userAssert | ||
* in src/log.js. | ||
* @param {T} shouldBeTruthy | ||
* @param {string} opt_message | ||
* @param {*=} opt_1 Optional argument (var arg as individual params for better | ||
* @param {*=} opt_2 Optional argument inlining) | ||
* @param {*=} opt_3 Optional argument | ||
* @param {*=} opt_4 Optional argument | ||
* @param {*=} opt_5 Optional argument | ||
* @param {*=} opt_6 Optional argument | ||
* @param {*=} opt_7 Optional argument | ||
* @param {*=} opt_8 Optional argument | ||
* @param {*=} opt_9 Optional argument | ||
* @return {T} | ||
* @throws {UserError} when shouldBeTruthy is not truthy. | ||
* @closurePrimitive {asserts.truthy} | ||
*/ | ||
export function pureUserAssert( | ||
shouldBeTruthy, | ||
opt_message, | ||
opt_1, | ||
opt_2, | ||
opt_3, | ||
opt_4, | ||
opt_5, | ||
opt_6, | ||
opt_7, | ||
opt_8, | ||
opt_9 | ||
) { | ||
return assertion( | ||
UserError, | ||
shouldBeTruthy, | ||
opt_message, | ||
opt_1, | ||
opt_2, | ||
opt_3, | ||
opt_4, | ||
opt_5, | ||
opt_6, | ||
opt_7, | ||
opt_8, | ||
opt_9 | ||
); | ||
} | ||
|
||
/** | ||
* Throws an error if the first argument isn't trueish. Mirrors devAssert in | ||
* src/log.js. | ||
* @param {T} shouldBeTruthy | ||
* @param {string} opt_message | ||
* @param {*=} opt_1 Optional argument (var arg as individual params for better | ||
* @param {*=} opt_2 Optional argument inlining) | ||
* @param {*=} opt_3 Optional argument | ||
* @param {*=} opt_4 Optional argument | ||
* @param {*=} opt_5 Optional argument | ||
* @param {*=} opt_6 Optional argument | ||
* @param {*=} opt_7 Optional argument | ||
* @param {*=} opt_8 Optional argument | ||
* @param {*=} opt_9 Optional argument | ||
* @return {T} | ||
* @throws {Error} when shouldBeTruthy is not truthy. | ||
* @closurePrimitive {asserts.truthy} | ||
*/ | ||
export function pureDevAssert( | ||
shouldBeTruthy, | ||
opt_message, | ||
opt_1, | ||
opt_2, | ||
opt_3, | ||
opt_4, | ||
opt_5, | ||
opt_6, | ||
opt_7, | ||
opt_8, | ||
opt_9 | ||
) { | ||
return assertion( | ||
Error, | ||
shouldBeTruthy, | ||
opt_message, | ||
opt_1, | ||
opt_2, | ||
opt_3, | ||
opt_4, | ||
opt_5, | ||
opt_6, | ||
opt_7, | ||
opt_8, | ||
opt_9 | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.