Skip to content

Commit

Permalink
Apply prettier formatting to js files
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinhagemeister committed Oct 29, 2019
1 parent 2f6e847 commit 4f52598
Show file tree
Hide file tree
Showing 130 changed files with 6,898 additions and 4,145 deletions.
10 changes: 6 additions & 4 deletions compat/server.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
/* eslint-disable */
var renderToString
var renderToString;
try {
renderToString = dep(require('preact-render-to-string'));
} catch (e) {
throw new Error(
'You seem to be missing the "preact-render-to-string" dependency.\n' +
'You can add this by using "npm install --save preact-render-to-string@next".'
)
'You can add this by using "npm install --save preact-render-to-string@next".'
);
}

function dep(obj) { return obj['default'] || obj; }
function dep(obj) {
return obj['default'] || obj;
}

module.exports = {
renderToString: renderToString,
Expand Down
71 changes: 53 additions & 18 deletions compat/src/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as _hooks from '../../hooks';
import * as preact from '../../src';
import { JSXInternal } from '../../src/jsx'
import { JSXInternal } from '../../src/jsx';
import * as _Suspense from './suspense';

// export default React;
export = React;
export as namespace React;
declare namespace React {
// Export JSX
export import JSX = JSXInternal
export import JSX = JSXInternal;

// Hooks
export import CreateHandle = _hooks.CreateHandle;
Expand Down Expand Up @@ -44,38 +44,73 @@ declare namespace React {
// Compat
export const version: string;

export function createPortal(vnode: preact.VNode, container: Element): preact.VNode<any>;

export function render(vnode: preact.VNode<any>, parent: Element, callback?: () => void): Component | null;

export function hydrate(vnode: preact.VNode<any>, parent: Element, callback?: () => void): Component | null;

export function unmountComponentAtNode(container: Element | Document | ShadowRoot | DocumentFragment): boolean;

export function createFactory(type: preact.VNode<any>["type"]): (props?: any, ...children: preact.ComponentChildren[]) => preact.VNode<any>;
export function createPortal(
vnode: preact.VNode,
container: Element
): preact.VNode<any>;

export function render(
vnode: preact.VNode<any>,
parent: Element,
callback?: () => void
): Component | null;

export function hydrate(
vnode: preact.VNode<any>,
parent: Element,
callback?: () => void
): Component | null;

export function unmountComponentAtNode(
container: Element | Document | ShadowRoot | DocumentFragment
): boolean;

export function createFactory(
type: preact.VNode<any>['type']
): (
props?: any,
...children: preact.ComponentChildren[]
) => preact.VNode<any>;
export function isValidElement(element: any): boolean;
export function findDOMNode(component: preact.Component): Element | null;

export abstract class PureComponent<P = {}, S = {}> extends preact.Component<P, S> {
export abstract class PureComponent<P = {}, S = {}> extends preact.Component<
P,
S
> {
isPureReactComponent: boolean;
}

export function memo<P = {}>(component: preact.FunctionalComponent<P>, comparer?: (prev: P, next: P) => boolean): preact.FunctionComponent<P>;
export function memo<P = {}>(
component: preact.FunctionalComponent<P>,
comparer?: (prev: P, next: P) => boolean
): preact.FunctionComponent<P>;

export interface ForwardFn<P = {}, T = any> {
(props: P, ref: Ref<T>): preact.ComponentChild;
displayName?: string;
}

export function forwardRef<R, P = {}>(fn: ForwardFn<P, R>): preact.FunctionalComponent<P>;
export function forwardRef<R, P = {}>(
fn: ForwardFn<P, R>
): preact.FunctionalComponent<P>;

export function unstable_batchedUpdates(callback: (arg?: any) => void, arg?: any): void;
export function unstable_batchedUpdates(
callback: (arg?: any) => void,
arg?: any
): void;

export const Children: {
map<T extends preact.ComponentChild, R>(children: T | T[], fn: (child: T, i: number, array: T[]) => R): R[];
forEach<T extends preact.ComponentChild>(children: T | T[], fn: (child: T, i: number, array: T[]) => void): void;
map<T extends preact.ComponentChild, R>(
children: T | T[],
fn: (child: T, i: number, array: T[]) => R
): R[];
forEach<T extends preact.ComponentChild>(
children: T | T[],
fn: (child: T, i: number, array: T[]) => void
): void;
count: (children: preact.ComponentChildren) => number;
only: (children: preact.ComponentChildren) => preact.ComponentChild;
toArray: (children: preact.ComponentChildren) => preact.VNode<{}>[];
}
};
}
115 changes: 83 additions & 32 deletions compat/src/index.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
import { hydrate, render as preactRender, cloneElement as preactCloneElement, createRef, h, Component, options, toChildArray, createContext, Fragment, _unmount } from 'preact';
import { useState, useReducer, useEffect, useLayoutEffect, useRef, useImperativeHandle, useMemo, useCallback, useContext, useDebugValue } from 'preact/hooks';
import {
hydrate,
render as preactRender,
cloneElement as preactCloneElement,
createRef,
h,
Component,
options,
toChildArray,
createContext,
Fragment,
_unmount
} from 'preact';
import {
useState,
useReducer,
useEffect,
useLayoutEffect,
useRef,
useImperativeHandle,
useMemo,
useCallback,
useContext,
useDebugValue
} from 'preact/hooks';
import { Suspense, lazy } from './suspense';
import { assign, removeNode } from '../../src/util';

const version = '16.8.0'; // trick libraries to think we are react

/* istanbul ignore next */
const REACT_ELEMENT_TYPE = (typeof Symbol!=='undefined' && Symbol.for && Symbol.for('react.element')) || 0xeac7;
const REACT_ELEMENT_TYPE =
(typeof Symbol !== 'undefined' &&
Symbol.for &&
Symbol.for('react.element')) ||
0xeac7;

const CAMEL_PROPS = /^(?:accent|alignment|arabic|baseline|cap|clip|color|fill|flood|font|glyph|horiz|marker|overline|paint|stop|strikethrough|stroke|text|underline|unicode|units|v|vector|vert|word|writing|x)[A-Z]/;

let oldEventHook = options.event;
options.event = e => {
if (oldEventHook) e = oldEventHook(e);
e.persist = () => {};
return e.nativeEvent = e;
return (e.nativeEvent = e);
};

/**
Expand All @@ -36,7 +63,9 @@ function handleElementVNode(vnode, props) {
if (shouldSanitize) {
attrs = vnode.props = {};
for (i in props) {
attrs[CAMEL_PROPS.test(i) ? i.replace(/([A-Z0-9])/, '-$1').toLowerCase() : i] = props[i];
attrs[
CAMEL_PROPS.test(i) ? i.replace(/([A-Z0-9])/, '-$1').toLowerCase() : i
] = props[i];
}
}
}
Expand All @@ -51,14 +80,14 @@ function handleElementVNode(vnode, props) {
function render(vnode, parent, callback) {
// React destroys any existing DOM nodes, see #1727
// ...but only on the first render, see #1828
if (parent._children==null) {
if (parent._children == null) {
while (parent.firstChild) {
removeNode(parent.firstChild);
}
}

preactRender(vnode, parent);
if (typeof callback==='function') callback();
if (typeof callback === 'function') callback();

return vnode ? vnode._component : null;
}
Expand Down Expand Up @@ -106,8 +135,7 @@ function Portal(props) {
// Render our wrapping element into temp.
preactRender(wrap, container, _this._temp);
_this._children = this._temp._children;
}
else {
} else {
// When we have mounted and the vnode is present it means the
// props have changed or a parent is triggering a rerender.
// This implies we only need to call render. But we need to keep
Expand Down Expand Up @@ -158,7 +186,8 @@ let Children = {
},
only(children) {
children = toChildArray(children);
if (children.length!==1) throw new Error('Children.only() expects only one child.');
if (children.length !== 1)
throw new Error('Children.only() expects only one child.');
return children[0];
},
toArray: toChildArray
Expand All @@ -174,18 +203,19 @@ let Children = {
function createElement(...args) {
let vnode = h(...args);

let type = vnode.type, props = vnode.props;
if (typeof type!='function') {
let type = vnode.type,
props = vnode.props;
if (typeof type != 'function') {
if (props.defaultValue) {
if (!props.value && props.value!==0) {
if (!props.value && props.value !== 0) {
props.value = props.defaultValue;
}
delete props.defaultValue;
}

if (Array.isArray(props.value) && props.multiple && type==='select') {
toChildArray(props.children).forEach((child) => {
if (props.value.indexOf(child.props.value)!=-1) {
if (Array.isArray(props.value) && props.multiple && type === 'select') {
toChildArray(props.children).forEach(child => {
if (props.value.indexOf(child.props.value) != -1) {
child.props.selected = true;
}
});
Expand Down Expand Up @@ -227,15 +257,15 @@ function cloneElement(element) {
* @returns {boolean}
*/
function isValidElement(element) {
return !!element && element.$$typeof===REACT_ELEMENT_TYPE;
return !!element && element.$$typeof === REACT_ELEMENT_TYPE;
}

/**
* Normalize event handlers like react does. Most famously it uses `onChange` for any input element.
* @param {import('./internal').VNode} vnode The vnode to normalize events on
*/
function applyEventNormalization({ type, props }) {
if (!props || typeof type!='string') return;
if (!props || typeof type != 'string') return;
let newProps = {};

for (let i in props) {
Expand All @@ -244,7 +274,6 @@ function applyEventNormalization({ type, props }) {
delete props[i];
}
newProps[i.toLowerCase()] = i;

}
if (newProps.ondoubleclick) {
props.ondblclick = props[newProps.ondoubleclick];
Expand All @@ -255,7 +284,11 @@ function applyEventNormalization({ type, props }) {
delete props[newProps.onbeforeinput];
}
// for *textual inputs* (incl textarea), normalize `onChange` -> `onInput`:
if (newProps.onchange && (type==='textarea' || (type.toLowerCase()==='input' && !/^fil|che|ra/i.test(props.type)))) {
if (
newProps.onchange &&
(type === 'textarea' ||
(type.toLowerCase() === 'input' && !/^fil|che|ra/i.test(props.type)))
) {
let normalized = newProps.oninput || 'oninput';
if (!props[normalized]) {
props[normalized] = props[newProps.onchange];
Expand Down Expand Up @@ -292,7 +325,9 @@ function applyClassName(vnode) {

let classNameDescriptor = {
configurable: true,
get() { return this.class; }
get() {
return this.class;
}
};

/**
Expand All @@ -303,7 +338,7 @@ let classNameDescriptor = {
*/
function shallowDiffers(a, b) {
for (let i in a) if (i !== '__source' && !(i in b)) return true;
for (let i in b) if (i !== '__source' && a[i]!==b[i]) return true;
for (let i in b) if (i !== '__source' && a[i] !== b[i]) return true;
return false;
}

Expand All @@ -313,7 +348,11 @@ function shallowDiffers(a, b) {
* @returns {import('./internal').PreactElement | null}
*/
function findDOMNode(component) {
return component && (component.base || component.nodeType === 1 && component) || null;
return (
(component &&
(component.base || (component.nodeType === 1 && component))) ||
null
);
}

/**
Expand All @@ -327,7 +366,9 @@ class PureComponent extends Component {
}

shouldComponentUpdate(props, state) {
return shallowDiffers(this.props, props) || shallowDiffers(this.state, state);
return (
shallowDiffers(this.props, props) || shallowDiffers(this.state, state)
);
}
}

Expand All @@ -344,13 +385,15 @@ Component.prototype.isReactComponent = {};
function memo(c, comparer) {
function shouldUpdate(nextProps) {
let ref = this.props.ref;
let updateRef = ref==nextProps.ref;
let updateRef = ref == nextProps.ref;
if (!updateRef && ref) {
ref.call ? ref(null) : (ref.current = null);
}
return (!comparer
? shallowDiffers(this.props, nextProps)
: !comparer(this.props, nextProps)) || !updateRef;
return (
(!comparer
? shallowDiffers(this.props, nextProps)
: !comparer(this.props, nextProps)) || !updateRef
);
}

function Memoed(props) {
Expand Down Expand Up @@ -384,15 +427,19 @@ function forwardRef(fn) {

// Patch in `UNSAFE_*` lifecycle hooks
function setSafeDescriptor(proto, key) {
if (proto['UNSAFE_'+key] && !proto[key]) {
if (proto['UNSAFE_' + key] && !proto[key]) {
Object.defineProperty(proto, key, {
configurable: false,
get() { return this['UNSAFE_' + key]; },
get() {
return this['UNSAFE_' + key];
},
// This `set` is only used if a user sets a lifecycle like cWU
// after setting a lifecycle like UNSAFE_cWU. I doubt anyone
// actually does this in practice so not testing it
/* istanbul ignore next */
set(v) { this['UNSAFE_' + key] = v; }
set(v) {
this['UNSAFE_' + key] = v;
}
});
}
}
Expand All @@ -411,7 +458,11 @@ options.vnode = vnode => {
// We can't just patch the base component class, because components that use
// inheritance and are transpiled down to ES5 will overwrite our patched
// getters and setters. See #1941
if (typeof type === 'function' && !type._patchedLifecycles && type.prototype) {
if (
typeof type === 'function' &&
!type._patchedLifecycles &&
type.prototype
) {
setSafeDescriptor(type.prototype, 'componentWillMount');
setSafeDescriptor(type.prototype, 'componentWillReceiveProps');
setSafeDescriptor(type.prototype, 'componentWillUpdate');
Expand Down
Loading

0 comments on commit 4f52598

Please sign in to comment.