Passed
Push — master ( 6ceee8...d4902a )
by Tristan
20:51 queued 08:57
created

resources/assets/js/components/WithPropsChecker.tsx   A

Complexity

Total Complexity 5
Complexity/F 1.25

Size

Lines of Code 50
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 5
mnd 1
bc 1
fnc 4
bpm 0.25
cpm 1.25
noi 2

1 Function

Rating   Name   Duplication   Size   Complexity  
B WithPropsChecker.tsx ➔ withPropsChecker 0 41 5
1
import React from "react";
2
3
interface Props<P> {
4
  [key: string]: any;
0 ignored issues
show
introduced by
Unexpected any. Specify a different type.
Loading history...
5
}
6
7
/**
8
 * This is a HOC (Higher Order Component).
9
 * It can be used to log which properties of a component changed when it re-rerenders.
10
 * Use it by wrapping the exported component function, similarly to connect() or injectIntl().
11
 * For Debugging use only.
12
 * @param WrappedComponent
13
 */
14
export default function withPropsChecker<P>(
15
  WrappedComponent: React.ComponentType<P>,
16
): React.ComponentClass<Props<P>> {
17
  return class PropsChecker extends React.Component<Props<P>> {
18
    // eslint-disable-next-line camelcase
19
    public UNSAFE_componentWillReceiveProps(nextProps: Props<P>): void {
20
      if (process.env.NODE_ENV === "development") {
21
        this.logChanges(nextProps);
22
      }
23
    }
24
25
    private logChanges(nextProps: Props<P>): void {
26
      Object.keys(nextProps)
27
        // eslint-disable-next-line react/destructuring-assignment
28
        .filter((key): boolean => nextProps[key] !== this.props[key])
29
        .forEach((key): void => {
30
          // eslint-disable-next-line no-console
31
          console.log(
32
            WrappedComponent.name,
33
            "changed property:",
34
            key,
35
            "from",
36
            // eslint-disable-next-line react/destructuring-assignment
37
            this.props[key],
38
            "to",
39
            nextProps[key],
40
          );
41
        });
42
    }
43
44
    public render(): React.ReactElement {
45
      // @ts-ignore
0 ignored issues
show
introduced by
Do not use "// @ts-ignore" comments because they suppress compilation errors.
Loading history...
46
      return <WrappedComponent {...this.props} />;
47
    }
48
  };
49
}
50