Skip to content

Commit

Permalink
♻️ Clean up extern typedefs (ampproject#34345)
Browse files Browse the repository at this point in the history
* Un-extern AssertionFunction typedef

* Un-extern Timestamp

* Move UnlistenDef to function.extern.js

* %s/Timestamp/TimestampDef/ for linter

* Fix typo

* Fix log type decl
  • Loading branch information
rcebulko authored and rochapablo committed Aug 30, 2021
1 parent d8fa756 commit 5e33b51
Show file tree
Hide file tree
Showing 10 changed files with 50 additions and 92 deletions.
20 changes: 13 additions & 7 deletions src/core/assert/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ import {remove} from '../types/array';
* `dev` or `user`. It is also used by the Log class for its assertions.
*/

/**
* A base assertion function, provided to various assertion helpers.
* @typedef {function(?, string=, ...*):?|function(?, !Array<*>)}
*/
export let AssertionFunctionDef;

/**
* Throws an error if the second argument isn't trueish.
*
Expand Down Expand Up @@ -94,7 +100,7 @@ export function assert(
* Otherwise creates a sprintf syntax string containing the optional message or the
* default. The `subject` of the assertion is added at the end.
*
* @param {!AssertionFunction} assertFn underlying assertion function to call
* @param {!AssertionFunctionDef} assertFn underlying assertion function to call
* @param {T} subject
* @param {*} shouldBeTruthy
* @param {string} defaultMessage
Expand Down Expand Up @@ -127,7 +133,7 @@ function assertType_(
*
* For more details see `assert`.
*
* @param {!AssertionFunction} assertFn underlying assertion function to call
* @param {!AssertionFunctionDef} assertFn underlying assertion function to call
* @param {*} shouldBeElement
* @param {!Array<*>|string=} opt_message The assertion message
* @return {!Element} The value of shouldBeTrueish.
Expand All @@ -152,7 +158,7 @@ export function assertElement(assertFn, shouldBeElement, opt_message) {
*
* For more details see `assert`.
*
* @param {!AssertionFunction} assertFn underlying assertion function to call
* @param {!AssertionFunctionDef} assertFn underlying assertion function to call
* @param {*} shouldBeString
* @param {!Array<*>|string=} opt_message The assertion message
* @return {string} The string value. Can be an empty string.
Expand All @@ -177,7 +183,7 @@ export function assertString(assertFn, shouldBeString, opt_message) {
*
* For more details see `assert`.
*
* @param {!AssertionFunction} assertFn underlying assertion function to call
* @param {!AssertionFunctionDef} assertFn underlying assertion function to call
* @param {*} shouldBeNumber
* @param {!Array<*>|string=} opt_message The assertion message
* @return {number} The number value. The allowed values include `0`
Expand All @@ -203,7 +209,7 @@ export function assertNumber(assertFn, shouldBeNumber, opt_message) {
*
* For more details see `assert`.
*
* @param {!AssertionFunction} assertFn underlying assertion function to call
* @param {!AssertionFunctionDef} assertFn underlying assertion function to call
* @param {*} shouldBeArray
* @param {!Array<*>|string=} opt_message The assertion message
* @return {!Array} The array value
Expand All @@ -227,7 +233,7 @@ export function assertArray(assertFn, shouldBeArray, opt_message) {
*
* For more details see `assert`.
*
* @param {!AssertionFunction} assertFn underlying assertion function to call
* @param {!AssertionFunctionDef} assertFn underlying assertion function to call
* @param {*} shouldBeBoolean
* @param {!Array<*>|string=} opt_message The assertion message
* @return {boolean} The boolean value.
Expand All @@ -250,7 +256,7 @@ export function assertBoolean(assertFn, shouldBeBoolean, opt_message) {
* Asserts and returns the enum value. If the enum doesn't contain such a
* value, the error is thrown.
*
* @param {!AssertionFunction} assertFn underlying assertion function to call
* @param {!AssertionFunctionDef} assertFn underlying assertion function to call
* @param {!Object<T>} enumObj
* @param {*} shouldBeEnum
* @param {string=} opt_enumName
Expand Down
12 changes: 6 additions & 6 deletions src/core/assert/dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ export function devAssertElement(shouldBeElement, opt_message) {
devAssertDceCheck();

return assertions.assertElement(
/** @type {!AssertionFunction} */ (devAssert),
/** @type {!assertions.AssertionFunctionDef} */ (devAssert),
shouldBeElement,
opt_message
);
Expand All @@ -131,7 +131,7 @@ export function devAssertString(shouldBeString, opt_message) {
devAssertDceCheck();

return assertions.assertString(
/** @type {!AssertionFunction} */ (devAssert),
/** @type {!assertions.AssertionFunctionDef} */ (devAssert),
shouldBeString,
opt_message
);
Expand All @@ -157,7 +157,7 @@ export function devAssertNumber(shouldBeNumber, opt_message) {
devAssertDceCheck();

return assertions.assertNumber(
/** @type {!AssertionFunction} */ (devAssert),
/** @type {!assertions.AssertionFunctionDef} */ (devAssert),
shouldBeNumber,
opt_message
);
Expand All @@ -182,7 +182,7 @@ export function devAssertArray(shouldBeArray, opt_message) {
devAssertDceCheck();

return assertions.assertArray(
/** @type {!AssertionFunction} */ (devAssert),
/** @type {!assertions.AssertionFunctionDef} */ (devAssert),
shouldBeArray,
opt_message
);
Expand All @@ -206,7 +206,7 @@ export function devAssertBoolean(shouldBeBoolean, opt_message) {
devAssertDceCheck();

return assertions.assertBoolean(
/** @type {!AssertionFunction} */ (devAssert),
/** @type {!assertions.AssertionFunctionDef} */ (devAssert),
shouldBeBoolean,
opt_message
);
Expand All @@ -230,7 +230,7 @@ export function devAssertEnumValue(enumObj, shouldBeEnum, opt_enumName) {
devAssertDceCheck();

return assertions.assertEnumValue(
/** @type {!AssertionFunction} */ (devAssert),
/** @type {!assertions.AssertionFunctionDef} */ (devAssert),
enumObj,
shouldBeEnum,
opt_enumName
Expand Down
12 changes: 6 additions & 6 deletions src/core/assert/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export function userAssert(
*/
export function userAssertElement(shouldBeElement, opt_message) {
return assertions.assertElement(
/** @type {!AssertionFunction} */ (userAssert),
/** @type {!assertions.AssertionFunctionDef} */ (userAssert),
shouldBeElement,
opt_message
);
Expand All @@ -118,7 +118,7 @@ export function userAssertElement(shouldBeElement, opt_message) {
*/
export function userAssertString(shouldBeString, opt_message) {
return assertions.assertString(
/** @type {!AssertionFunction} */ (userAssert),
/** @type {!assertions.AssertionFunctionDef} */ (userAssert),
shouldBeString,
opt_message
);
Expand All @@ -139,7 +139,7 @@ export function userAssertString(shouldBeString, opt_message) {
*/
export function userAssertNumber(shouldBeNumber, opt_message) {
return assertions.assertNumber(
/** @type {!AssertionFunction} */ (userAssert),
/** @type {!assertions.AssertionFunctionDef} */ (userAssert),
shouldBeNumber,
opt_message
);
Expand All @@ -159,7 +159,7 @@ export function userAssertNumber(shouldBeNumber, opt_message) {
*/
export function userAssertArray(shouldBeArray, opt_message) {
return assertions.assertArray(
/** @type {!AssertionFunction} */ (userAssert),
/** @type {!assertions.AssertionFunctionDef} */ (userAssert),
shouldBeArray,
opt_message
);
Expand All @@ -178,7 +178,7 @@ export function userAssertArray(shouldBeArray, opt_message) {
*/
export function userAssertBoolean(shouldBeBoolean, opt_message) {
return assertions.assertBoolean(
/** @type {!AssertionFunction} */ (userAssert),
/** @type {!assertions.AssertionFunctionDef} */ (userAssert),
shouldBeBoolean,
opt_message
);
Expand All @@ -197,7 +197,7 @@ export function userAssertBoolean(shouldBeBoolean, opt_message) {
*/
export function userAssertEnumValue(enumObj, shouldBeEnum, opt_enumName) {
return assertions.assertEnumValue(
/** @type {!AssertionFunction} */ (userAssert),
/** @type {!assertions.AssertionFunctionDef} */ (userAssert),
enumObj,
shouldBeEnum,
opt_enumName
Expand Down
13 changes: 8 additions & 5 deletions src/core/data-structures/signals.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import {Deferred} from './promise';
import {map} from '../types/object';

// typedef imports
import {TimestampDef} from '../types/date';

/**
* This object tracts signals and allows blocking until a signal has been
* received.
Expand All @@ -29,7 +32,7 @@ export class Signals {
/**
* A mapping from a signal name to the signal response: either time or
* an error.
* @private @const {!Object<string, (Timestamp|!Error)>}
* @private @const {!Object<string, (!TimestampDef|!Error)>}
*/
this.map_ = map();

Expand All @@ -38,7 +41,7 @@ export class Signals {
* Only allocated when promise has been requested.
* @private {?Object<string, {
* promise: !Promise,
* resolve: (function(Timestamp)|undefined),
* resolve: (function(!TimestampDef)|undefined),
* reject: (function(!Error)|undefined)
* }>}
*/
Expand All @@ -60,7 +63,7 @@ export class Signals {
* Returns the promise that's resolved when the signal is triggered. The
* resolved value is the time of the signal.
* @param {string} name
* @return {!Promise<Timestamp>}
* @return {!Promise<!TimestampDef>}
*/
whenSignal(name) {
let promiseStruct = this.promiseMap_ && this.promiseMap_[name];
Expand Down Expand Up @@ -89,9 +92,9 @@ export class Signals {
/**
* Triggers the signal with the specified name on the element. The time is
* optional; if not provided, the current time is used. The associated
* promise is resolved with the resulting Timestamp.
* promise is resolved with the resulting TimestampDef.
* @param {string} name
* @param {Timestamp=} opt_time
* @param {!TimestampDef=} opt_time
*/
signal(name, opt_time) {
if (this.map_[name] != null) {
Expand Down
37 changes: 0 additions & 37 deletions src/core/shame.extern.js

This file was deleted.

21 changes: 0 additions & 21 deletions src/core/types/date.extern.js

This file was deleted.

14 changes: 11 additions & 3 deletions src/core/types/date.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@
* limitations under the License.
*/

/** @fileoverview helpers for dealing with dates and times. */

/**
* A timestamp, such as that produced by `Date.now()`.
* @typedef {number}
*/
export let TimestampDef;

/**
* Parses the date using the `Date.parse()` rules. Additionally supports the
* keyword "now" that indicates the "current date/time". Returns either a
* valid epoch value or null.
*
* @param {?string|undefined} s
* @return {?Timestamp}
* @return {?TimestampDef}
*/
export function parseDate(s) {
if (!s) {
Expand All @@ -34,9 +42,9 @@ export function parseDate(s) {
}

/**
* Converts various date formats into a Timestamp.
* Converts various date formats into a TimestampDef.
* @param {!Date|number|string} value
* @return {?Timestamp}
* @return {?TimestampDef}
*/
export function getDate(value) {
if (!value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@
*/

/**
* @fileoverview Type externs for assertions.
* @fileoverview Global type definitions related to functions.
* @externs
*/

/**
* @typedef {function(?, string=, ...*):?|function(?, !Array<*>)}
* This type signifies a callback that can be called to remove a listener.
* @typedef {function()}
*/
let AssertionFunction;
let UnlistenDef;
4 changes: 1 addition & 3 deletions src/core/types/function.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
* limitations under the License.
*/

/**
* @fileoverview Helpers to wrap functions.
*/
/** @fileoverview Helpers to wrap functions. */

/**
* Creates a function that is evaluated only once and returns the cached result
Expand Down
2 changes: 1 addition & 1 deletion src/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ export class Log {
// migrated to an AMP-independent form for use in core. This binding allows
// Log assertion helpers to maintain message-extraction capabilities until
// that logic can be moved to core.
this.boundAssertFn_ = /** @type {!AssertionFunction} */ (
this.boundAssertFn_ = /** @type {!assertions.AssertionFunctionDef} */ (
this.assert.bind(this)
);
}
Expand Down

0 comments on commit 5e33b51

Please sign in to comment.