Skip to content

Commit 08f6bf6

Browse files
committed
Just Use Prettier™
1 parent ad17e0d commit 08f6bf6

File tree

9 files changed

+157
-138
lines changed

9 files changed

+157
-138
lines changed

modules/Broadcast.js

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
1-
import React from "react"
2-
import PropTypes from "prop-types"
3-
import invariant from "invariant"
1+
import React from "react";
2+
import PropTypes from "prop-types";
3+
import invariant from "invariant";
44

55
function createBroadcast(initialValue) {
6-
let currentValue = initialValue
7-
let subscribers = []
6+
let currentValue = initialValue;
7+
let subscribers = [];
88

9-
const getValue = () => currentValue
9+
const getValue = () => currentValue;
1010

1111
const publish = state => {
12-
currentValue = state
13-
subscribers.forEach(s => s(currentValue))
14-
}
12+
currentValue = state;
13+
subscribers.forEach(s => s(currentValue));
14+
};
1515

1616
const subscribe = subscriber => {
17-
subscribers.push(subscriber)
17+
subscribers.push(subscriber);
1818

1919
return () => {
20-
subscribers = subscribers.filter(s => s !== subscriber)
21-
}
22-
}
20+
subscribers = subscribers.filter(s => s !== subscriber);
21+
};
22+
};
2323

2424
return {
2525
getValue,
2626
publish,
2727
subscribe
28-
}
28+
};
2929
}
3030

3131
/**
@@ -43,42 +43,45 @@ class Broadcast extends React.Component {
4343
children: PropTypes.node.isRequired,
4444
compareValues: PropTypes.func,
4545
value: PropTypes.any
46-
}
46+
};
4747

4848
static defaultProps = {
4949
compareValues: (prevValue, nextValue) => prevValue === nextValue
50-
}
50+
};
5151

5252
static contextTypes = {
5353
broadcasts: PropTypes.object
54-
}
54+
};
5555

5656
static childContextTypes = {
5757
broadcasts: PropTypes.object.isRequired
58-
}
58+
};
5959

60-
broadcast = createBroadcast(this.props.value)
60+
broadcast = createBroadcast(this.props.value);
6161

6262
getChildContext() {
6363
return {
6464
broadcasts: {
6565
...this.context.broadcasts,
6666
[this.props.channel]: this.broadcast
6767
}
68-
}
68+
};
6969
}
7070

7171
componentWillReceiveProps(nextProps) {
72-
invariant(this.props.channel === nextProps.channel, "You cannot change <Broadcast channel>")
72+
invariant(
73+
this.props.channel === nextProps.channel,
74+
"You cannot change <Broadcast channel>"
75+
);
7376

7477
if (!this.props.compareValues(this.props.value, nextProps.value)) {
75-
this.broadcast.publish(nextProps.value)
78+
this.broadcast.publish(nextProps.value);
7679
}
7780
}
7881

7982
render() {
80-
return React.Children.only(this.props.children)
83+
return React.Children.only(this.props.children);
8184
}
8285
}
8386

84-
export default Broadcast
87+
export default Broadcast;

modules/Subscriber.js

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import React from "react"
2-
import PropTypes from "prop-types"
3-
import invariant from "invariant"
1+
import React from "react";
2+
import PropTypes from "prop-types";
3+
import invariant from "invariant";
44

55
/**
66
* A <Subscriber> pulls the value for a channel off of context.broadcasts
@@ -11,62 +11,62 @@ class Subscriber extends React.Component {
1111
channel: PropTypes.string.isRequired,
1212
children: PropTypes.func,
1313
quiet: PropTypes.bool
14-
}
14+
};
1515

1616
static defaultProps = {
1717
quiet: false
18-
}
18+
};
1919

2020
static contextTypes = {
2121
broadcasts: PropTypes.object
22-
}
22+
};
2323

2424
state = {
2525
value: undefined
26-
}
26+
};
2727

2828
getBroadcast() {
29-
const broadcasts = this.context.broadcasts || {}
30-
const broadcast = broadcasts[this.props.channel]
29+
const broadcasts = this.context.broadcasts || {};
30+
const broadcast = broadcasts[this.props.channel];
3131

3232
invariant(
3333
this.props.quiet || broadcast,
3434
'<Subscriber channel="%s"> must be rendered in the context of a <Broadcast channel="%s">',
3535
this.props.channel,
3636
this.props.channel
37-
)
37+
);
3838

39-
return broadcast
39+
return broadcast;
4040
}
4141

4242
componentWillMount() {
43-
const broadcast = this.getBroadcast()
43+
const broadcast = this.getBroadcast();
4444

4545
if (broadcast) {
4646
this.setState({
4747
value: broadcast.getValue()
48-
})
48+
});
4949
}
5050
}
5151

5252
componentDidMount() {
53-
const broadcast = this.getBroadcast()
53+
const broadcast = this.getBroadcast();
5454

5555
if (broadcast) {
5656
this.unsubscribe = broadcast.subscribe(value => {
57-
this.setState({ value })
58-
})
57+
this.setState({ value });
58+
});
5959
}
6060
}
6161

6262
componentWillUnmount() {
63-
if (this.unsubscribe) this.unsubscribe()
63+
if (this.unsubscribe) this.unsubscribe();
6464
}
6565

6666
render() {
67-
const { children } = this.props
68-
return children ? children(this.state.value) : null
67+
const { children } = this.props;
68+
return children ? children(this.state.value) : null;
6969
}
7070
}
7171

72-
export default Subscriber
72+
export default Subscriber;

modules/__tests__/Subscriber-test.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
1-
import React from "react"
2-
import ReactDOMServer from "react-dom/server"
3-
import Subscriber from "../Subscriber"
1+
import React from "react";
2+
import ReactDOMServer from "react-dom/server";
3+
import Subscriber from "../Subscriber";
44

55
describe("A <Subscriber>", () => {
66
it("throws an invariant when it is not rendered in the context of a <Broadcast>", () => {
77
expect(() => {
8-
ReactDOMServer.renderToStaticMarkup(<Subscriber channel="cupcakes" />)
9-
}).toThrow()
10-
})
8+
ReactDOMServer.renderToStaticMarkup(<Subscriber channel="cupcakes" />);
9+
}).toThrow();
10+
});
1111

1212
describe("with quiet=true", () => {
1313
it("does not throw when it is not rendered in the context of a <Broadcast>", () => {
1414
expect(() => {
15-
ReactDOMServer.renderToStaticMarkup(<Subscriber quiet channel="cupcakes" />)
16-
}).not.toThrow()
17-
})
18-
})
19-
})
15+
ReactDOMServer.renderToStaticMarkup(
16+
<Subscriber quiet channel="cupcakes" />
17+
);
18+
}).not.toThrow();
19+
});
20+
});
21+
});

modules/__tests__/integration-test.js

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,75 @@
1-
import React from "react"
2-
import ReactDOM from "react-dom"
3-
import { EventEmitter } from "events"
4-
import { Broadcast, Subscriber } from "../index"
1+
import React from "react";
2+
import ReactDOM from "react-dom";
3+
import { EventEmitter } from "events";
4+
import { Broadcast, Subscriber } from "../index";
55

66
it("works", done => {
7-
const steps = []
8-
const execNextStep = () => steps.shift()()
9-
const div = document.createElement("div")
7+
const steps = [];
8+
const execNextStep = () => steps.shift()();
9+
const div = document.createElement("div");
1010

1111
// so we can trigger rerenders in ComponentWithStateForDescendants
12-
const emitter = new EventEmitter()
12+
const emitter = new EventEmitter();
1313

1414
// A component has some state it wants to make available to descendants
1515
// 1. We create our Broadcast and Subscriber components
1616
const CheeseBroadcast = ({ cheese, children }) => (
1717
<Broadcast channel="cheese" value={cheese} children={children} />
18-
)
18+
);
1919

20-
const CheeseSubscriber = ({ children }) => <Subscriber channel="cheese" children={children} />
20+
const CheeseSubscriber = ({ children }) => (
21+
<Subscriber channel="cheese" children={children} />
22+
);
2123

2224
class ComponentWithStateForDescendants extends React.Component {
2325
constructor() {
24-
super()
25-
this.state = { cheese: "cheddar" }
26+
super();
27+
this.state = { cheese: "cheddar" };
2628
emitter.on("CHEESE", cheese => {
27-
this.setState({ cheese })
28-
})
29+
this.setState({ cheese });
30+
});
2931
}
3032

31-
componentDidMount = execNextStep
33+
componentDidMount = execNextStep;
3234

33-
componentDidUpdate = execNextStep
35+
componentDidUpdate = execNextStep;
3436

3537
render() {
3638
// 2. render the Broadcast in the component w/ state and pass
3739
// it the value we want accessible through context as a prop
3840
// by the same name as when the Broadcast was created
39-
return <CheeseBroadcast cheese={this.state.cheese}>{this.props.children}</CheeseBroadcast>
41+
return (
42+
<CheeseBroadcast cheese={this.state.cheese}>
43+
{this.props.children}
44+
</CheeseBroadcast>
45+
);
4046
}
4147
}
4248

43-
let actualCheese = null
49+
let actualCheese = null;
4450

4551
steps.push(
4652
() => {
47-
expect(actualCheese).toBe("cheddar")
48-
emitter.emit("CHEESE", "gouda")
53+
expect(actualCheese).toBe("cheddar");
54+
emitter.emit("CHEESE", "gouda");
4955
},
5056
() => {
51-
expect(actualCheese).toBe("gouda")
52-
done()
57+
expect(actualCheese).toBe("gouda");
58+
done();
5359
}
54-
)
60+
);
5561

5662
// 3. Render a <Subscriber> that calls back when the Broadcast
5763
// gets a new value in its prop
5864
ReactDOM.render(
5965
<ComponentWithStateForDescendants>
6066
<CheeseSubscriber>
6167
{cheese => {
62-
actualCheese = cheese
63-
return null
68+
actualCheese = cheese;
69+
return null;
6470
}}
6571
</CheeseSubscriber>
6672
</ComponentWithStateForDescendants>,
6773
div
68-
)
69-
})
74+
);
75+
});

modules/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
export Broadcast from "./Broadcast"
2-
export Subscriber from "./Subscriber"
1+
export Broadcast from "./Broadcast";
2+
export Subscriber from "./Subscriber";

package.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,5 @@
7070
"subscriber",
7171
"subscription",
7272
"subscriptions"
73-
],
74-
"prettier": {
75-
"printWidth": 100,
76-
"semi": false
77-
}
73+
]
7874
}

0 commit comments

Comments
 (0)