Skip to content

Commit

Permalink
tsc: type all of src/preact/*.js (#37322)
Browse files Browse the repository at this point in the history
* tsc: typecheck src/preact

* rebase fixes

* More ryan comments + eslint doin work

* opt-error typing

* dont even need the AmpElement

* rm shadow-embed.ts

* update TODO with PR

* remove unecessary cast
  • Loading branch information
samouri committed Jan 13, 2022
1 parent c04895b commit cd80895
Show file tree
Hide file tree
Showing 17 changed files with 347 additions and 250 deletions.
40 changes: 11 additions & 29 deletions src/base-element.d.ts
@@ -1,4 +1,9 @@
// See src/base-element.js for method documentation.

type Layout_Enum = import('#core/dom/layout').Layout_Enum;
type LayoutSize = import('#core/dom/layout/rect').LayoutSizeDef;
type LayoutRect = import('#core/dom/layout/rect').LayoutRectDef;

declare namespace AMP {
class BaseElement {
static R1(): boolean;
Expand All @@ -23,18 +28,18 @@ declare namespace AMP {
getDefaultActionAlias(): string | undefined;
getLayoutPriority(): number;
updateLayoutPriority(pri: number): void;
getLayout(): Layout;
getLayout(): Layout_Enum;
getLayoutBox(): LayoutRect;
getLayoutSize(): LayoutSize;
getWin(): Window;
getAmpDoc(): any;
getVsync(): any;
getConsentPolicy(): string | undefined;
isLayoutSupported(layout: Layout): boolean;
isLayoutSupported(layout: Layout_Enum): boolean;
isAlwaysFixed(): boolean;
upgradeCallback(): null | BaseElement | Promise<BaseElement>;
buildCallback(): void | Promise<void>;
preconnectCallback(onLayout?: boolean);
preconnectCallback(onLayout?: boolean): void;
attachedCallback(): void;
detachedCallback(): void;
setAsContainer(scroller?: Element): void;
Expand All @@ -56,14 +61,14 @@ declare namespace AMP {
unlayoutOnPause(): boolean;
reconstructWhenReparented(): boolean;
loadPromise<T>(element: T): Promise<T>;
registerAction(alias: string, handler: any, minTrust: any);
registerDefaultAction(handler: any, alias: string, minTrust: any);
registerAction(alias: string, handler: any, minTrust: any): void;
registerDefaultAction(handler: any, alias: string, minTrust: any): void;
executeAction(invocation: any, deferred?: boolean): any;
forwardEvents(events: string | string[], element: Element): any;
getPlaceholder(): Element;
togglePlaceholder(state: boolean): void;
getFallback(): Element | undefined;
toggleFallback(state: boolean);
toggleFallback(state: boolean): void;
toggleLoading(state: boolean, force?: boolean): void;
getOverflowElement(): Element | undefined;
renderStarted(): void;
Expand Down Expand Up @@ -107,29 +112,6 @@ declare namespace AMP {
) => void;
}

declare type Layout =
| 'nodisplay'
| 'fixed'
| 'fixed-height'
| 'responsive'
| 'container'
| 'fill'
| 'flex-item'
| 'fluid'
| 'intrinsic';

declare type LayoutSize = {height: number; width: number};
declare type LayoutRect = {
top: number;
bottom: number;
left: number;
right: number;
width: number;
height: number;
x: number;
y: number;
};

type Mutations = {
[key: string]: null | boolean | string | number | Array<any> | Object;
};
6 changes: 3 additions & 3 deletions src/core/context/index.js
Expand Up @@ -37,7 +37,7 @@ export function unassignSlot(node, slot) {
* Sets (or unsets) the direct parent. If the parent is set, the node will no
* longer try to discover itself.
*
* @param {Node} node
* @param {Node|ShadowRoot} node
* @param {?Node} parent
*/
export function setParent(node, parent) {
Expand Down Expand Up @@ -87,7 +87,7 @@ export function rediscoverChildren(node) {
*
* @param {Node} node The target node.
* @param {IContextProp<T, ?>} prop
* @param {function(T):void} setter
* @param {function(*):void} setter
* @param {T} value
* @template T
*/
Expand All @@ -101,7 +101,7 @@ export function setProp(node, prop, setter, value) {
*
* @param {Node} node The target node.
* @param {IContextProp<T, ?>} prop
* @param {function(T):void} setter
* @param {function(*):void} setter
* @template T
*/
export function removeProp(node, prop, setter) {
Expand Down
2 changes: 1 addition & 1 deletion src/core/context/prop.js
Expand Up @@ -16,7 +16,7 @@ const EMPTY_DEPS = [];
* type?: Object,
* deps?: IContextProp<DEP, *>[],
* recursive?: boolean | (function(T[]):boolean),
* compute?: function(Node, T[], ...DEP):(T | undefined),
* compute?: function(Node, T[], ...T):(T | undefined),
* defaultValue?: T,
* }} opt_spec
* @return {IContextProp<T, DEP>}
Expand Down
22 changes: 14 additions & 8 deletions src/core/context/types.d.ts
@@ -1,4 +1,4 @@
import {ContextNode} from './node'
import {ContextNode} from './node';
export interface IContextProp<T, DEP> {
/**
* A globally unique key. Extensions must use a fully qualified name such
Expand All @@ -7,10 +7,10 @@ export interface IContextProp<T, DEP> {
key: string;

/**
* An optional type object that can be used for a using system. E.g.
* A type object that can be used for a using system. E.g.
* this could be a Preact's Context object.
*/
type?: Object;
type: import('preact').Context<T>;

/**
* An array of dependencies that are required for the `compute` callback.
Expand Down Expand Up @@ -44,7 +44,13 @@ export interface IContextProp<T, DEP> {
* 3. If it's a recursive property, the parent value.
* 4. If `deps` are specified - the dep values.
*/
compute: (node: Node, inputs: T[], ...deps: DEP[]) => (T | undefined);
compute(node: Node, inputs: T[], ...deps: T[]): T | undefined;
compute(
node: Node,
inputs: T[],
parentValue: T,
...deps: DEP[]
): T | undefined;

/**
* The default value of a recursive property.
Expand All @@ -67,17 +73,17 @@ export type PendingEnumValue = number;

/** The structure for a property's computed values and subscribers. */
export interface IContextPropUsed<T, DEP> {
prop: IContextProp<T, DEP>
prop: IContextProp<T, DEP>;
subscribers: ((value: T) => void)[];
value: T;
pending: PendingEnumValue;
counter: number;
depValues: DEP[];
parentValue: T;
parentContextNode: null | import('./node').ContextNode<?>;
ping: (refreshParent: boolean) => void
pingDep: ((dep: DEP) => void)[]
pingParent: null | ((parentValue: T) => void)
ping: (refreshParent: boolean) => void;
pingDep: ((dep: DEP) => void)[];
pingParent: null | ((parentValue: T) => void);
}
declare global {
interface Node {
Expand Down
3 changes: 3 additions & 0 deletions src/core/dom/amp-globals.d.ts
@@ -1,3 +1,5 @@
import { ReadyState_Enum } from "#core/constants/ready-state";

export {};

declare global {
Expand All @@ -19,6 +21,7 @@ declare global {
// expectations rather than the catch-all AMP-specific `AmpElement` class. This
// is already done with the `IPausable` interface.
interface AmpElement extends HTMLElement, IPausable {
readyState: ReadyState_Enum;
sizerElement?: HTMLElement;

getPlaceholder: () => null | Element;
Expand Down
8 changes: 4 additions & 4 deletions src/core/dom/query.js
Expand Up @@ -26,12 +26,12 @@ function assertIsName(name) {
* This method isn't required for modern builds, can be removed.
* TODO(#37136): This will fail if `root` is a `ShadowRoot`.
*
* @param {HTMLElement|ShadowRoot} root
* @param {Element|ShadowRoot} root
* @param {string} selector
* @return {NodeList}
*/
function scopedQuerySelectionFallback(root, selector) {
const {classList} = /** @type {HTMLElement} */ (root);
const {classList} = /** @type {Element} */ (root);

const unique = 'i-amphtml-scoped';
classList.add(unique);
Expand All @@ -44,7 +44,7 @@ function scopedQuerySelectionFallback(root, selector) {
/**
* Finds the first element that matches `selector`, scoped inside `root`.
* Note: in IE, this causes a quick mutation of the element's class list.
* @param {HTMLElement|ShadowRoot} root
* @param {Element|ShadowRoot} root
* @param {string} selector
* @return {?HTMLElement}
*
Expand Down Expand Up @@ -263,7 +263,7 @@ export function childNodes(parent, callback) {

/**
* Finds the first child element that has the specified attribute.
* @param {HTMLElement|ShadowRoot} parent
* @param {Element|ShadowRoot} parent
* @param {string} attr
* @return {?Element}
*/
Expand Down

0 comments on commit cd80895

Please sign in to comment.