Skip to content

Commit

Permalink
refactor: Remove declare global from internal types (#4583)
Browse files Browse the repository at this point in the history
* refactor: Remove `declare global` from internal types

* refactor: Fix hook types
  • Loading branch information
rschristian authored Nov 30, 2024
1 parent b9ff1b7 commit 42e7b45
Show file tree
Hide file tree
Showing 14 changed files with 249 additions and 226 deletions.
2 changes: 1 addition & 1 deletion hooks/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ export function useId() {
const state = getHookState(currentIndex++, 11);
if (!state._value) {
// Grab either the root node or the nearest async boundary node.
/** @type {import('./internal.d').VNode} */
/** @type {import('./internal').VNode} */
let root = currentComponent._vnode;
while (root !== null && !root._mask && root._parent !== null) {
root = root._parent;
Expand Down
12 changes: 9 additions & 3 deletions hooks/src/internal.d.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import {
Options as PreactOptions,
Component as PreactComponent,
VNode as PreactVNode,
Context as PreactContext,
} from '../../src/internal';
import { Reducer, StateUpdater } from '.';

export { PreactContext };

export interface Options extends globalThis.Options {
export interface Options extends PreactOptions {
/** Attach a hook that is invoked before a vnode is diffed. */
_diff?(vnode: VNode): void;
diffed?(vnode: VNode): void;
Expand All @@ -24,14 +30,14 @@ export interface ComponentHooks {
_pendingEffects: EffectHookState[];
}

export interface Component extends globalThis.Component<any, any> {
export interface Component extends PreactComponent<any, any> {
__hooks?: ComponentHooks;
// Extend to include HookStates
_renderCallbacks?: Array<HookState | (() => void)>;
_hasScuFromHooks?: boolean;
}

export interface VNode extends globalThis.VNode {
export interface VNode extends PreactVNode {
_mask?: [number, number];
_component?: Component; // Override with our specific Component type
}
Expand Down
6 changes: 3 additions & 3 deletions src/clone-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { UNDEFINED } from './constants';
/**
* Clones the given VNode, optionally adding attributes/props and replacing its
* children.
* @param {VNode} vnode The virtual DOM element to clone
* @param {import('./internal').VNode} vnode The virtual DOM element to clone
* @param {object} props Attributes/props to add when cloning
* @param {Array<ComponentChildren>} rest Any additional arguments will be used
* @param {Array<import('./internal').ComponentChildren>} rest Any additional arguments will be used
* as replacement children.
* @returns {VNode}
* @returns {import('./internal').VNode}
*/
export function cloneElement(vnode, props, children) {
let normalizedProps = assign({}, vnode.props),
Expand Down
18 changes: 9 additions & 9 deletions src/component.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export function BaseComponent(props, context) {

/**
* Update component state and schedule a re-render.
* @this {Component}
* @this {import('./internal').Component}
* @param {object | ((s: object, p: object) => object)} update A hash of state
* properties to update with new values or a function that given the current
* state and props returns a new partial state
Expand Down Expand Up @@ -57,7 +57,7 @@ BaseComponent.prototype.setState = function (update, callback) {

/**
* Immediately perform a synchronous re-render of the component
* @this {Component}
* @this {import('./internal').Component}
* @param {() => void} [callback] A function to be called after component is
* re-rendered
*/
Expand Down Expand Up @@ -85,7 +85,7 @@ BaseComponent.prototype.forceUpdate = function (callback) {
BaseComponent.prototype.render = Fragment;

/**
* @param {VNode} vnode
* @param {import('./internal').VNode} vnode
* @param {number | null} [childIndex]
*/
export function getDomSibling(vnode, childIndex) {
Expand Down Expand Up @@ -118,7 +118,7 @@ export function getDomSibling(vnode, childIndex) {

/**
* Trigger in-place re-rendering of a component.
* @param {Component} component The component to rerender
* @param {import('./internal').Component} component The component to rerender
*/
function renderComponent(component) {
let oldVNode = component._vnode,
Expand Down Expand Up @@ -155,7 +155,7 @@ function renderComponent(component) {
}

/**
* @param {VNode} vnode
* @param {import('./internal').VNode} vnode
*/
function updateParentDomPointers(vnode) {
if ((vnode = vnode._parent) != null && vnode._component != null) {
Expand All @@ -174,7 +174,7 @@ function updateParentDomPointers(vnode) {

/**
* The render queue
* @type {Array<Component>}
* @type {Array<import('./internal').Component>}
*/
let rerenderQueue = [];

Expand All @@ -196,7 +196,7 @@ const defer =

/**
* Enqueue a rerender of a component
* @param {Component} c The component to rerender
* @param {import('./internal').Component} c The component to rerender
*/
export function enqueueRender(c) {
if (
Expand All @@ -212,8 +212,8 @@ export function enqueueRender(c) {
}

/**
* @param {Component} a
* @param {Component} b
* @param {import('./internal').Component} a
* @param {import('./internal').Component} b
*/
const depthSort = (a, b) => a._vnode._depth - b._vnode._depth;

Expand Down
6 changes: 3 additions & 3 deletions src/create-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ export function createContext(defaultValue, contextId) {
const context = {
_id: contextId,
_defaultValue: defaultValue,
/** @type {FunctionComponent} */
/** @type {import('./internal').FunctionComponent} */
Consumer(props, contextValue) {
// return props.children(
// context[contextId] ? context[contextId].props.value : defaultValue
// );
return props.children(contextValue);
},
/** @type {FunctionComponent} */
/** @type {import('./internal').FunctionComponent} */
Provider(props) {
if (!this.getChildContext) {
/** @type {Set<Component> | null} */
/** @type {Set<import('./internal').Component> | null} */
let subs = new Set();
let ctx = {};
ctx[contextId] = this;
Expand Down
12 changes: 6 additions & 6 deletions src/create-element.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ let vnodeId = 0;

/**
* Create an virtual node (used for JSX)
* @param {VNode["type"]} type The node name or Component constructor for this
* @param {import('./internal').VNode["type"]} type The node name or Component constructor for this
* virtual node
* @param {object | null | undefined} [props] The properties of the virtual node
* @param {Array<import('.').ComponentChildren>} [children] The children of the
* virtual node
* @returns {VNode}
* @returns {import('./internal').VNode}
*/
export function createElement(type, props, children) {
let normalizedProps = {},
Expand Down Expand Up @@ -44,20 +44,20 @@ export function createElement(type, props, children) {

/**
* Create a VNode (used internally by Preact)
* @param {VNode["type"]} type The node name or Component
* @param {import('./internal').VNode["type"]} type The node name or Component
* Constructor for this virtual node
* @param {object | string | number | null} props The properties of this virtual node.
* If this virtual node represents a text node, this is the text of the node (string or number).
* @param {string | number | null} key The key for this virtual node, used when
* diffing it against its children
* @param {VNode["ref"]} ref The ref property that will
* @param {import('./internal').VNode["ref"]} ref The ref property that will
* receive a reference to its created child
* @returns {VNode}
* @returns {import('./internal').VNode}
*/
export function createVNode(type, props, key, ref, original) {
// V8 seems to be better at detecting type shapes if the object is allocated from the same call site
// Do not inline into createElement and coerceToVNode!
/** @type {VNode} */
/** @type {import('./internal').VNode} */
const vnode = {
type,
props,
Expand Down
10 changes: 5 additions & 5 deletions src/diff/catch-error.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
/**
* Find the closest error boundary to a thrown error and call it
* @param {object} error The thrown value
* @param {VNode} vnode The vnode that threw the error that was caught (except
* @param {import('../internal').VNode} vnode The vnode that threw the error that was caught (except
* for unmounting when this parameter is the highest parent that was being
* unmounted)
* @param {VNode} [oldVNode]
* @param {ErrorInfo} [errorInfo]
* @param {import('../internal').VNode} [oldVNode]
* @param {import('../internal').ErrorInfo} [errorInfo]
*/
export function _catchError(error, vnode, oldVNode, errorInfo) {
/** @type {Component} */
/** @type {import('../internal').Component} */
let component,
/** @type {ComponentType} */
/** @type {import('../internal').ComponentType} */
ctor,
/** @type {boolean} */
handled;
Expand Down
7 changes: 7 additions & 0 deletions src/diff/children.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@ import {
import { isArray } from '../util';
import { getDomSibling } from '../component';

/**
* @typedef {import('../internal').ComponentChildren} ComponentChildren
* @typedef {import('../internal').Component} Component
* @typedef {import('../internal').PreactElement} PreactElement
* @typedef {import('../internal').VNode} VNode
*/

/**
* Diff the children of a virtual node
* @param {PreactElement} parentDom The DOM element whose children are being
Expand Down
12 changes: 12 additions & 0 deletions src/diff/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@ import { setProperty } from './props';
import { assign, isArray, removeNode, slice } from '../util';
import options from '../options';

/**
* @typedef {import('../internal').ComponentChildren} ComponentChildren
* @typedef {import('../internal').Component} Component
* @typedef {import('../internal').PreactElement} PreactElement
* @typedef {import('../internal').VNode} VNode
*/

/**
* @template {any} T
* @typedef {import('../internal').Ref<T>} Ref<T>
*/

/**
* Diff two virtual nodes and apply proper changes to the DOM
* @param {PreactElement} parentDom The parent of the DOM element
Expand Down
4 changes: 2 additions & 2 deletions src/diff/props.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ let eventClock = 0;

/**
* Set a property value on a DOM node
* @param {PreactElement} dom The DOM node to modify
* @param {import('../internal').PreactElement} dom The DOM node to modify
* @param {string} name The name of the property to set
* @param {*} value The value to set the property to
* @param {*} oldValue The old value the property had
Expand Down Expand Up @@ -152,7 +152,7 @@ export function setProperty(dom, name, value, oldValue, namespace) {
function createEventProxy(useCapture) {
/**
* Proxy an event to hooked event handlers
* @param {PreactEvent} e The event object from the browser
* @param {import('../internal').PreactEvent} e The event object from the browser
* @private
*/
return function (e) {
Expand Down
Loading

0 comments on commit 42e7b45

Please sign in to comment.