Skip to content

Commit

Permalink
Enable no-use-before-define rule (#13606)
Browse files Browse the repository at this point in the history
  • Loading branch information
gaearon committed Sep 10, 2018
1 parent 3ab394e commit cb2cdc9
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 79 deletions.
132 changes: 66 additions & 66 deletions src/ReactShallowRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,72 @@ if (__DEV__) {
Object.freeze(emptyObject);
}

class Updater {
constructor(renderer) {
this._renderer = renderer;
this._callbacks = [];
}

_enqueueCallback(callback, publicInstance) {
if (typeof callback === 'function' && publicInstance) {
this._callbacks.push({
callback,
publicInstance,
});
}
}

_invokeCallbacks() {
const callbacks = this._callbacks;
this._callbacks = [];

callbacks.forEach(({callback, publicInstance}) => {
callback.call(publicInstance);
});
}

isMounted(publicInstance) {
return !!this._renderer._element;
}

enqueueForceUpdate(publicInstance, callback, callerName) {
this._enqueueCallback(callback, publicInstance);
this._renderer._forcedUpdate = true;
this._renderer.render(this._renderer._element, this._renderer._context);
}

enqueueReplaceState(publicInstance, completeState, callback, callerName) {
this._enqueueCallback(callback, publicInstance);
this._renderer._newState = completeState;
this._renderer.render(this._renderer._element, this._renderer._context);
}

enqueueSetState(publicInstance, partialState, callback, callerName) {
this._enqueueCallback(callback, publicInstance);
const currentState = this._renderer._newState || publicInstance.state;

if (typeof partialState === 'function') {
partialState = partialState.call(
publicInstance,
currentState,
publicInstance.props,
);
}

// Null and undefined are treated as no-ops.
if (partialState === null || partialState === undefined) {
return;
}

this._renderer._newState = {
...currentState,
...partialState,
};

this._renderer.render(this._renderer._element, this._renderer._context);
}
}

class ReactShallowRenderer {
static createRenderer = function() {
return new ReactShallowRenderer();
Expand Down Expand Up @@ -263,72 +329,6 @@ class ReactShallowRenderer {
}
}

class Updater {
constructor(renderer) {
this._renderer = renderer;
this._callbacks = [];
}

_enqueueCallback(callback, publicInstance) {
if (typeof callback === 'function' && publicInstance) {
this._callbacks.push({
callback,
publicInstance,
});
}
}

_invokeCallbacks() {
const callbacks = this._callbacks;
this._callbacks = [];

callbacks.forEach(({callback, publicInstance}) => {
callback.call(publicInstance);
});
}

isMounted(publicInstance) {
return !!this._renderer._element;
}

enqueueForceUpdate(publicInstance, callback, callerName) {
this._enqueueCallback(callback, publicInstance);
this._renderer._forcedUpdate = true;
this._renderer.render(this._renderer._element, this._renderer._context);
}

enqueueReplaceState(publicInstance, completeState, callback, callerName) {
this._enqueueCallback(callback, publicInstance);
this._renderer._newState = completeState;
this._renderer.render(this._renderer._element, this._renderer._context);
}

enqueueSetState(publicInstance, partialState, callback, callerName) {
this._enqueueCallback(callback, publicInstance);
const currentState = this._renderer._newState || publicInstance.state;

if (typeof partialState === 'function') {
partialState = partialState.call(
publicInstance,
currentState,
publicInstance.props,
);
}

// Null and undefined are treated as no-ops.
if (partialState === null || partialState === undefined) {
return;
}

this._renderer._newState = {
...currentState,
...partialState,
};

this._renderer.render(this._renderer._element, this._renderer._context);
}
}

let currentlyValidatingElement = null;

function getDisplayName(element) {
Expand Down
2 changes: 2 additions & 0 deletions src/ReactTestHostConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import warning from 'shared/warning';
import * as TestRendererScheduling from './ReactTestRendererScheduling';

/* eslint-disable no-use-before-define */
export type Type = string;
export type Props = Object;
export type Container = {|
Expand All @@ -35,6 +36,7 @@ export type UpdatePayload = Object;
export type ChildSet = void; // Unused
export type TimeoutHandle = TimeoutID;
export type NoTimeout = -1;
/* eslint-enable no-use-before-define */

export * from 'shared/HostConfigWithNoPersistence';
export * from 'shared/HostConfigWithNoHydration';
Expand Down
28 changes: 15 additions & 13 deletions src/ReactTestRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import ReactVersion from 'shared/ReactVersion';
import * as ReactTestHostConfig from './ReactTestHostConfig';
import * as TestRendererScheduling from './ReactTestRendererScheduling';

/* eslint-disable no-use-before-define */
type TestRendererOptions = {
createNodeMock: (element: React$Element<any>) => any,
unstable_isAsync: boolean,
Expand All @@ -57,6 +58,7 @@ type FindOptions = $Shape<{
}>;

export type Predicate = (node: ReactTestInstance) => ?boolean;
/* eslint-enable no-use-before-define */

const defaultTestOptions = {
createNodeMock: function() {
Expand Down Expand Up @@ -209,19 +211,6 @@ function toTree(node: ?Fiber) {
}
}

const fiberToWrapper = new WeakMap();
function wrapFiber(fiber: Fiber): ReactTestInstance {
let wrapper = fiberToWrapper.get(fiber);
if (wrapper === undefined && fiber.alternate !== null) {
wrapper = fiberToWrapper.get(fiber.alternate);
}
if (wrapper === undefined) {
wrapper = new ReactTestInstance(fiber);
fiberToWrapper.set(fiber, wrapper);
}
return wrapper;
}

const validWrapperTypes = new Set([
FunctionalComponent,
FunctionalComponentLazy,
Expand Down Expand Up @@ -543,6 +532,19 @@ const ReactTestRendererFiber = {
unstable_setNowImplementation: TestRendererScheduling.setNowImplementation,
};

const fiberToWrapper = new WeakMap();
function wrapFiber(fiber: Fiber): ReactTestInstance {
let wrapper = fiberToWrapper.get(fiber);
if (wrapper === undefined && fiber.alternate !== null) {
wrapper = fiberToWrapper.get(fiber.alternate);
}
if (wrapper === undefined) {
wrapper = new ReactTestInstance(fiber);
fiberToWrapper.set(fiber, wrapper);
}
return wrapper;
}

// Enable ReactTestRenderer to be used to test DevTools integration.
TestRenderer.injectIntoDevTools({
findFiberByHostInstance: (() => {
Expand Down

0 comments on commit cb2cdc9

Please sign in to comment.