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 types in 3p code and fix unknown types in main AMP JS binary. #3552

Merged
merged 1 commit into from
Jun 12, 2016
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions 3p/3p.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export function register(id, draw) {

/**
* Execute the 3p integration with the given id.
* @param {id} id
* @param {string} id
* @param {!Window} win
* @param {!Object} data
*/
Expand Down Expand Up @@ -83,7 +83,7 @@ export function writeScript(win, url, opt_cb) {
* Asynchronously load the given script URL.
* @param {!Window} win
* @param {string} url
* @param {function()=} cb
* @param {function()} cb
*/
export function loadScript(win, url, cb) {
const s = win.document.createElement('script');
Expand Down
6 changes: 3 additions & 3 deletions 3p/environment.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function instrumentSrcdoc(parent, iframe) {
/**
* Instrument added nodes if they are instrumentable iframes.
* @param {!Window} win
* @param {!Array<!Node>} addedNodes
* @param {!Array<!Node>|NodeList<!Node>|NodeList<!Element>|null} addedNodes
Copy link
Member

Choose a reason for hiding this comment

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

can we use IArrayLike here instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Probably could.

*/
function maybeInstrumentsNodes(win, addedNodes) {
for (let n = 0; n < addedNodes.length; n++) {
Expand Down Expand Up @@ -281,8 +281,8 @@ export function becomeVisible() {

/**
* Calculates the minimum time that a timeout should have right now.
* @param {number} time
* @return {number}
* @param {number|undefined} time
* @return {number|undefined}
*/
function minTime(time) {
if (!inViewport) {
Expand Down
6 changes: 3 additions & 3 deletions 3p/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ function updateVisibilityState(global) {

/**
* Registers a callback for communicating when a resize request succeeds.
* @param {function(number)} observerCallback
* @param {function(number, number)} observerCallback
* @returns {!function()} A function which removes the event listener that
* observes for resize status messages.
*/
Expand All @@ -332,7 +332,7 @@ function onResizeSuccess(observerCallback) {

/**
* Registers a callback for communicating when a resize request is denied.
* @param {function(number)} observerCallback
* @param {function(number, number)} observerCallback
* @returns {!function()} A function which removes the event listener that
* observes for resize status messages.
*/
Expand Down Expand Up @@ -469,7 +469,7 @@ export function parseFragment(fragment) {
if (json.indexOf('{%22') == 0) {
json = decodeURIComponent(json);
}
return json ? JSON.parse(json) : {};
return /** @type {!JSONType} */ (json ? JSON.parse(json) : {});
}

/**
Expand Down
3 changes: 2 additions & 1 deletion 3p/messaging.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ function startListening(win) {
return;
}
// Parse JSON only once per message.
const data = JSON.parse(event.data.substr(4));
const data = /** @type {!Object} */ (
JSON.parse(event.data.substr(4)));
if (data.sentinel != win.context.amp3pSentinel) {
return;
}
Expand Down
54 changes: 53 additions & 1 deletion build-system/amp.extern.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ process.end.NODE_ENV;

// Exposed to ads.
window.context = {};
window.context.amp3pSentinel;

// Exposed to custom ad iframes.
window.draw3p = function() {};
/* @type {!Function} */
window.draw3p;

// AMP's globals
window.AMP_TEST;
Expand All @@ -39,3 +41,53 @@ window.AMP.BaseTemplate;
// Externed explicitly because this private property is read across
// binaries.
Element.implementation_ = {};

/** @typedef {number} */
var time;

/**
* This type signifies a callback that can be called to remove the listener.
* @typedef {function()}
*/
var UnlistenDef;


/**
* Just an element, but used with AMP custom elements..
* @typedef {!Element}
*/
var AmpElement;

// Temp until we figure out forward declarations
/** @constructor */
var AccessService = function() {};
/** @constructor */
var UserNotificationManager = function() {};
/** @constructor */
var Cid = function() {};
/** @constructor */
var Activity = function() {};



// data
var data;
data.tweetid;
data.requestedHeight;
data.requestedWidth;
data.pageHidden;
data.changes;
data._context;
data.inViewport;


// 3p code
var twttr;
twttr.events;
twttr.events.bind;
twttr.widgets;
twttr.widgets.createTweet;

var FB;
FB.init;

10 changes: 9 additions & 1 deletion build-system/tasks/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,10 @@ function compile(entryModuleFilename, outputDir,
// Transpile from ES6 to ES5.
language_in: 'ECMASCRIPT6',
language_out: 'ECMASCRIPT5',
externs: 'build-system/amp.extern.js',
externs: [
'build-system/amp.extern.js',
'third_party/closure-compiler/externs/intersection_observer.js',
],
js_module_root: [
'node_modules/',
'build/patched-module/',
Expand All @@ -197,6 +200,11 @@ function compile(entryModuleFilename, outputDir,
source_map_location_mapping:
'|' + sourceMapBase,
warning_level: 'DEFAULT',
hide_warnings_for: [
'ads/', // TODO(@cramforce): Remove when we are better at typing.
'node_modules/',
'build/patched-module/',
],
}
};

Expand Down
2 changes: 1 addition & 1 deletion builtins/amp-img.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class AmpImg extends BaseElement {

this.element.appendChild(this.img_);

/** @private @const {!Srcset} */
/** @private @const {!../src/srcset.Srcset} */
this.srcset_ = srcsetFromElement(this.element);
}

Expand Down
2 changes: 1 addition & 1 deletion extensions/amp-sticky-ad/0.1/amp-sticky-ad.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class AmpStickyAd extends AMP.BaseElement {

/**
* On viewport scroll, check requirements for amp-stick-ad to display.
* @const @private {!Unlisten}
* @const @private {!UnlistenDef}
*/
this.scrollUnlisten_ =
this.viewport_.onScroll(() => this.displayAfterScroll_());
Expand Down
10 changes: 9 additions & 1 deletion gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,10 +292,17 @@ function checkTypes() {
buildAlp({
minify: true,
checkTypes: true,
preventRemoveAndMakeDir: true,
});
buildExperiments({
minify: true,
checkTypes: true,
preventRemoveAndMakeDir: true,
});
// These are not turned on on Travis.
if (argv.more) {
compile(false, true, true, /* check types */ true);
compile(false, true, /* opt_preventRemoveAndMakeDir*/ true,
/* check types */ true);
}
}

Expand Down Expand Up @@ -612,6 +619,7 @@ function buildExperiments(options) {
includePolyfills: true,
minifiedName: minifiedName,
preventRemoveAndMakeDir: options.preventRemoveAndMakeDir,
checkTypes: options.checkTypes,
});
});
}
Expand Down
18 changes: 9 additions & 9 deletions src/base-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,13 @@ export class BaseElement {
/** @package {boolean} */
this.inViewport_ = false;

/** @private {!Object<string, function(!ActionInvocation)>} */
/** @private {!Object<string, function(!./service/action-impl.ActionInvocation)>} */
this.actionMap_ = this.getWin().Object.create(null);

/** @protected {!Preconnect} */
/** @protected {!./preconnect.Preconnect} */
this.preconnect = preconnectFor(this.getWin());

/** @private {!Resources} */
/** @private {!./service/resources-impl.Resources} */
this.resources_ = resourcesFor(this.getWin());
}

Expand All @@ -160,7 +160,7 @@ export class BaseElement {
return this.element.ownerDocument.defaultView;
}

/** @protected @return {!Vsync} */
/** @protected @return {!./service/vsync-impl.Vsync} */
getVsync() {
return vsyncFor(this.getWin());
}
Expand Down Expand Up @@ -402,15 +402,15 @@ export class BaseElement {
/**
* Instructs the element that its activation is requested based on some
* user event. Intended to be implemented by actual components.
* @param {!ActionInvocation} unusedInvocation
* @param {!./service/action-impl.ActionInvocation} unusedInvocation
*/
activate(unusedInvocation) {
}

/**
* Registers the action handler for the method with the specified name.
* @param {string} method
* @param {function(!ActionInvocation)} handler
* @param {function(!./service/action-impl.ActionInvocation)} handler
* @protected
*/
registerAction(method, handler) {
Expand All @@ -421,7 +421,7 @@ export class BaseElement {
* Requests the element to execute the specified method. If method must have
* been previously registered using {@link registerAction}, otherwise an
* error is thrown.
* @param {!ActionInvocation} invocation The invocation data.
* @param {!./service/action-impl.ActionInvocation} invocation The invocation data.
* @param {boolean} unusedDeferred Whether the invocation has had to wait any time
* for the element to be resolved, upgraded and built.
* @final
Expand Down Expand Up @@ -560,15 +560,15 @@ export class BaseElement {

/**
* Returns the viewport within which the element operates.
* @return {!Viewport}
* @return {!./service/viewport-impl.Viewport}
*/
getViewport() {
return viewportFor(this.getWin());
}

/**
* Returns a previously measured layout box of the element.
* @return {!LayoutRect}
* @return {!./layout-rect.LayoutRectDef}
*/
getIntersectionElementLayoutBox() {
return this.resources_.getResourceForElement(this.element).getLayoutBox();
Expand Down
28 changes: 14 additions & 14 deletions src/custom-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ class AmpElement {
*
* @param {!Window} win The window in which to register the elements.
* @param {string} name Name of the custom element
* @param {function(new:BaseElement, !Element)} opt_implementationClass For
* @param {function(new:./base-element.BaseElement, !Element)} opt_implementationClass For
* testing only.
* @return {!AmpElement.prototype}
* @return {!Object} Prototype of element.
*/
export function createAmpElementProto(win, name, opt_implementationClass) {
/**
Expand All @@ -316,7 +316,7 @@ export function createAmpElementProto(win, name, opt_implementationClass) {
this.readyState = 'loading';
this.everAttached = false;

/** @private @const {!Resources} */
/** @private @const {!./service/resources-impl.Resources} */
this.resources_ = resourcesFor(win);

/** @private {!Layout} */
Expand All @@ -334,10 +334,10 @@ export function createAmpElementProto(win, name, opt_implementationClass) {
/** @private {string|null|undefined} */
this.mediaQuery_ = undefined;

/** @private {!SizeList|null|undefined} */
/** @private {!./size-list.SizeList|null|undefined} */
this.sizeList_ = undefined;

/** @private {!SizeList|null|undefined} */
/** @private {!./size-list.SizeList|null|undefined} */
this.heightsList_ = undefined;

/**
Expand Down Expand Up @@ -365,14 +365,14 @@ export function createAmpElementProto(win, name, opt_implementationClass) {
// `opt_implementationClass` is only used for tests.
const Ctor = opt_implementationClass || knownElements[name];

/** @private {!BaseElement} */
/** @private {!./base-element.BaseElement} */
this.implementation_ = new Ctor(this);
this.implementation_.createdCallback();

/**
* Action queue is initially created and kept around until the element
* is ready to send actions directly to the implementation.
* @private {?Array<!ActionInvocation>}
* @private {?Array<!./service/action-impl.ActionInvocation>}
*/
this.actionQueue_ = [];

Expand Down Expand Up @@ -401,7 +401,7 @@ export function createAmpElementProto(win, name, opt_implementationClass) {
* Upgrades the element to the provided new implementation. If element
* has already been attached, it's layout validation and attachment flows
* are repeated for the new implementation.
* @param {function(new:BaseElement, !Element)} newImplClass
* @param {function(new:./base-element.BaseElement, !Element)} newImplClass
* @final @package @this {!Element}
*/
ElementProto.upgrade = function(newImplClass) {
Expand Down Expand Up @@ -508,7 +508,7 @@ export function createAmpElementProto(win, name, opt_implementationClass) {
};

/**
* @return {!Vsync}
* @return {!./service/vsync-impl.Vsync}
* @private @this {!Element}
*/
ElementProto.getVsync_ = function() {
Expand All @@ -527,7 +527,7 @@ export function createAmpElementProto(win, name, opt_implementationClass) {
/**
* Updates the layout box of the element.
* See {@link BaseElement.getLayoutWidth} for details.
* @param {!LayoutRect} layoutBox
* @param {!./layout-rect.LayoutRectDef} layoutBox
* @this {!Element}
*/
ElementProto.updateLayoutBox = function(layoutBox) {
Expand Down Expand Up @@ -733,7 +733,7 @@ export function createAmpElementProto(win, name, opt_implementationClass) {
};

/**
* @return {!LayoutRect}
* @return {!./layout-rect.LayoutRectDef}
* @final @this {!Element}
*/
ElementProto.getLayoutBox = function() {
Expand Down Expand Up @@ -907,7 +907,7 @@ export function createAmpElementProto(win, name, opt_implementationClass) {
* built, the action is dispatched to the implementation right away.
* Otherwise the invocation is enqueued until the implementation is ready
* to receive actions.
* @param {!ActionInvocation} invocation
* @param {!./service/action-impl.ActionInvocation} invocation
* @final @this {!Element}
*/
ElementProto.enqueAction = function(invocation) {
Expand Down Expand Up @@ -940,7 +940,7 @@ export function createAmpElementProto(win, name, opt_implementationClass) {

/**
* Executes the action immediately. All errors are consumed and reported.
* @param {!ActionInvocation} invocation
* @param {!./service/action-impl.ActionInvocation} invocation
* @param {boolean} deferred
* @final
* @private @this {!Element}
Expand Down Expand Up @@ -1219,7 +1219,7 @@ export function createAmpElementProto(win, name, opt_implementationClass) {
* Registers a new custom element with its implementation class.
* @param {!Window} win The window in which to register the elements.
* @param {string} name Name of the custom element
* @param {function(new:BaseElement, !Element)} implementationClass
* @param {function(new:./base-element.BaseElement, !Element)} implementationClass
*/
export function registerElement(win, name, implementationClass) {
knownElements[name] = implementationClass;
Expand Down
Loading